Commit 66b28183 authored by Akinobu Mita's avatar Akinobu Mita Committed by David Woodhouse

mtd: mtd_pagetest: use mtd_test helpers

Use mtdtest_write(), mtdtest_read(), mtdtest_erase_eraseblock(),
mtdtest_scan_for_bad_eraseblocks(), and mtdtest_erase_good_eraseblocks()
in mtd_test helpers.

[dwmw2: merge later 'remove always true condition' fix]
Signed-off-by: default avatarAkinobu Mita <akinobu.mita@gmail.com>
Cc: Brian Norris <computersforpeace@gmail.com>
Cc: Vikram Narayanan <vikram186@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: default avatarArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 4bf527aa
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/random.h> #include <linux/random.h>
#include "mtd_test.h"
static int dev = -EINVAL; static int dev = -EINVAL;
module_param(dev, int, S_IRUGO); module_param(dev, int, S_IRUGO);
MODULE_PARM_DESC(dev, "MTD device number to use"); MODULE_PARM_DESC(dev, "MTD device number to use");
...@@ -48,42 +50,15 @@ static int pgcnt; ...@@ -48,42 +50,15 @@ static int pgcnt;
static int errcnt; static int errcnt;
static struct rnd_state rnd_state; static struct rnd_state rnd_state;
static int erase_eraseblock(int ebnum)
{
int err;
struct erase_info ei;
loff_t addr = ebnum * mtd->erasesize;
memset(&ei, 0, sizeof(struct erase_info));
ei.mtd = mtd;
ei.addr = addr;
ei.len = mtd->erasesize;
err = mtd_erase(mtd, &ei);
if (err) {
pr_err("error %d while erasing EB %d\n", err, ebnum);
return err;
}
if (ei.state == MTD_ERASE_FAILED) {
pr_err("some erase error occurred at EB %d\n",
ebnum);
return -EIO;
}
return 0;
}
static int write_eraseblock(int ebnum) static int write_eraseblock(int ebnum)
{ {
int err = 0; int err;
size_t written;
loff_t addr = ebnum * mtd->erasesize; loff_t addr = ebnum * mtd->erasesize;
prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize); prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
cond_resched(); cond_resched();
err = mtd_write(mtd, addr, mtd->erasesize, &written, writebuf); err = mtdtest_write(mtd, addr, mtd->erasesize, writebuf);
if (err || written != mtd->erasesize) if (err)
pr_err("error: write failed at %#llx\n", pr_err("error: write failed at %#llx\n",
(long long)addr); (long long)addr);
...@@ -93,7 +68,6 @@ static int write_eraseblock(int ebnum) ...@@ -93,7 +68,6 @@ static int write_eraseblock(int ebnum)
static int verify_eraseblock(int ebnum) static int verify_eraseblock(int ebnum)
{ {
uint32_t j; uint32_t j;
size_t read;
int err = 0, i; int err = 0, i;
loff_t addr0, addrn; loff_t addr0, addrn;
loff_t addr = ebnum * mtd->erasesize; loff_t addr = ebnum * mtd->erasesize;
...@@ -109,27 +83,21 @@ static int verify_eraseblock(int ebnum) ...@@ -109,27 +83,21 @@ static int verify_eraseblock(int ebnum)
prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize); prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) { for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
/* Do a read to set the internal dataRAMs to different data */ /* Do a read to set the internal dataRAMs to different data */
err = mtd_read(mtd, addr0, bufsize, &read, twopages); err = mtdtest_read(mtd, addr0, bufsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != bufsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err; return err;
} }
err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages); err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != bufsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)(addrn - bufsize)); (long long)(addrn - bufsize));
return err; return err;
} }
memset(twopages, 0, bufsize); memset(twopages, 0, bufsize);
err = mtd_read(mtd, addr, bufsize, &read, twopages); err = mtdtest_read(mtd, addr, bufsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != bufsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
break; break;
...@@ -145,27 +113,21 @@ static int verify_eraseblock(int ebnum) ...@@ -145,27 +113,21 @@ static int verify_eraseblock(int ebnum)
struct rnd_state old_state = rnd_state; struct rnd_state old_state = rnd_state;
/* Do a read to set the internal dataRAMs to different data */ /* Do a read to set the internal dataRAMs to different data */
err = mtd_read(mtd, addr0, bufsize, &read, twopages); err = mtdtest_read(mtd, addr0, bufsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != bufsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err; return err;
} }
err = mtd_read(mtd, addrn - bufsize, bufsize, &read, twopages); err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != bufsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)(addrn - bufsize)); (long long)(addrn - bufsize));
return err; return err;
} }
memset(twopages, 0, bufsize); memset(twopages, 0, bufsize);
err = mtd_read(mtd, addr, bufsize, &read, twopages); err = mtdtest_read(mtd, addr, bufsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != bufsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
return err; return err;
...@@ -184,7 +146,6 @@ static int verify_eraseblock(int ebnum) ...@@ -184,7 +146,6 @@ static int verify_eraseblock(int ebnum)
static int crosstest(void) static int crosstest(void)
{ {
size_t read;
int err = 0, i; int err = 0, i;
loff_t addr, addr0, addrn; loff_t addr, addr0, addrn;
unsigned char *pp1, *pp2, *pp3, *pp4; unsigned char *pp1, *pp2, *pp3, *pp4;
...@@ -208,10 +169,8 @@ static int crosstest(void) ...@@ -208,10 +169,8 @@ static int crosstest(void)
/* Read 2nd-to-last page to pp1 */ /* Read 2nd-to-last page to pp1 */
addr = addrn - pgsize - pgsize; addr = addrn - pgsize - pgsize;
err = mtd_read(mtd, addr, pgsize, &read, pp1); err = mtdtest_read(mtd, addr, pgsize, pp1);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
kfree(pp1); kfree(pp1);
...@@ -220,10 +179,8 @@ static int crosstest(void) ...@@ -220,10 +179,8 @@ static int crosstest(void)
/* Read 3rd-to-last page to pp1 */ /* Read 3rd-to-last page to pp1 */
addr = addrn - pgsize - pgsize - pgsize; addr = addrn - pgsize - pgsize - pgsize;
err = mtd_read(mtd, addr, pgsize, &read, pp1); err = mtdtest_read(mtd, addr, pgsize, pp1);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
kfree(pp1); kfree(pp1);
...@@ -233,10 +190,8 @@ static int crosstest(void) ...@@ -233,10 +190,8 @@ static int crosstest(void)
/* Read first page to pp2 */ /* Read first page to pp2 */
addr = addr0; addr = addr0;
pr_info("reading page at %#llx\n", (long long)addr); pr_info("reading page at %#llx\n", (long long)addr);
err = mtd_read(mtd, addr, pgsize, &read, pp2); err = mtdtest_read(mtd, addr, pgsize, pp2);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
kfree(pp1); kfree(pp1);
...@@ -246,10 +201,8 @@ static int crosstest(void) ...@@ -246,10 +201,8 @@ static int crosstest(void)
/* Read last page to pp3 */ /* Read last page to pp3 */
addr = addrn - pgsize; addr = addrn - pgsize;
pr_info("reading page at %#llx\n", (long long)addr); pr_info("reading page at %#llx\n", (long long)addr);
err = mtd_read(mtd, addr, pgsize, &read, pp3); err = mtdtest_read(mtd, addr, pgsize, pp3);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
kfree(pp1); kfree(pp1);
...@@ -259,10 +212,8 @@ static int crosstest(void) ...@@ -259,10 +212,8 @@ static int crosstest(void)
/* Read first page again to pp4 */ /* Read first page again to pp4 */
addr = addr0; addr = addr0;
pr_info("reading page at %#llx\n", (long long)addr); pr_info("reading page at %#llx\n", (long long)addr);
err = mtd_read(mtd, addr, pgsize, &read, pp4); err = mtdtest_read(mtd, addr, pgsize, pp4);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr); (long long)addr);
kfree(pp1); kfree(pp1);
...@@ -283,7 +234,6 @@ static int crosstest(void) ...@@ -283,7 +234,6 @@ static int crosstest(void)
static int erasecrosstest(void) static int erasecrosstest(void)
{ {
size_t read, written;
int err = 0, i, ebnum, ebnum2; int err = 0, i, ebnum, ebnum2;
loff_t addr0; loff_t addr0;
char *readbuf = twopages; char *readbuf = twopages;
...@@ -302,29 +252,27 @@ static int erasecrosstest(void) ...@@ -302,29 +252,27 @@ static int erasecrosstest(void)
ebnum2 -= 1; ebnum2 -= 1;
pr_info("erasing block %d\n", ebnum); pr_info("erasing block %d\n", ebnum);
err = erase_eraseblock(ebnum); err = mtdtest_erase_eraseblock(mtd, ebnum);
if (err) if (err)
return err; return err;
pr_info("writing 1st page of block %d\n", ebnum); pr_info("writing 1st page of block %d\n", ebnum);
prandom_bytes_state(&rnd_state, writebuf, pgsize); prandom_bytes_state(&rnd_state, writebuf, pgsize);
strcpy(writebuf, "There is no data like this!"); strcpy(writebuf, "There is no data like this!");
err = mtd_write(mtd, addr0, pgsize, &written, writebuf); err = mtdtest_write(mtd, addr0, pgsize, writebuf);
if (err || written != pgsize) { if (err) {
pr_info("error: write failed at %#llx\n", pr_info("error: write failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err ? err : -1; return err;
} }
pr_info("reading 1st page of block %d\n", ebnum); pr_info("reading 1st page of block %d\n", ebnum);
memset(readbuf, 0, pgsize); memset(readbuf, 0, pgsize);
err = mtd_read(mtd, addr0, pgsize, &read, readbuf); err = mtdtest_read(mtd, addr0, pgsize, readbuf);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err ? err : -1; return err;
} }
pr_info("verifying 1st page of block %d\n", ebnum); pr_info("verifying 1st page of block %d\n", ebnum);
...@@ -335,34 +283,32 @@ static int erasecrosstest(void) ...@@ -335,34 +283,32 @@ static int erasecrosstest(void)
} }
pr_info("erasing block %d\n", ebnum); pr_info("erasing block %d\n", ebnum);
err = erase_eraseblock(ebnum); err = mtdtest_erase_eraseblock(mtd, ebnum);
if (err) if (err)
return err; return err;
pr_info("writing 1st page of block %d\n", ebnum); pr_info("writing 1st page of block %d\n", ebnum);
prandom_bytes_state(&rnd_state, writebuf, pgsize); prandom_bytes_state(&rnd_state, writebuf, pgsize);
strcpy(writebuf, "There is no data like this!"); strcpy(writebuf, "There is no data like this!");
err = mtd_write(mtd, addr0, pgsize, &written, writebuf); err = mtdtest_write(mtd, addr0, pgsize, writebuf);
if (err || written != pgsize) { if (err) {
pr_err("error: write failed at %#llx\n", pr_err("error: write failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err ? err : -1; return err;
} }
pr_info("erasing block %d\n", ebnum2); pr_info("erasing block %d\n", ebnum2);
err = erase_eraseblock(ebnum2); err = mtdtest_erase_eraseblock(mtd, ebnum2);
if (err) if (err)
return err; return err;
pr_info("reading 1st page of block %d\n", ebnum); pr_info("reading 1st page of block %d\n", ebnum);
memset(readbuf, 0, pgsize); memset(readbuf, 0, pgsize);
err = mtd_read(mtd, addr0, pgsize, &read, readbuf); err = mtdtest_read(mtd, addr0, pgsize, readbuf);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err ? err : -1; return err;
} }
pr_info("verifying 1st page of block %d\n", ebnum); pr_info("verifying 1st page of block %d\n", ebnum);
...@@ -379,7 +325,6 @@ static int erasecrosstest(void) ...@@ -379,7 +325,6 @@ static int erasecrosstest(void)
static int erasetest(void) static int erasetest(void)
{ {
size_t read, written;
int err = 0, i, ebnum, ok = 1; int err = 0, i, ebnum, ok = 1;
loff_t addr0; loff_t addr0;
...@@ -393,32 +338,30 @@ static int erasetest(void) ...@@ -393,32 +338,30 @@ static int erasetest(void)
} }
pr_info("erasing block %d\n", ebnum); pr_info("erasing block %d\n", ebnum);
err = erase_eraseblock(ebnum); err = mtdtest_erase_eraseblock(mtd, ebnum);
if (err) if (err)
return err; return err;
pr_info("writing 1st page of block %d\n", ebnum); pr_info("writing 1st page of block %d\n", ebnum);
prandom_bytes_state(&rnd_state, writebuf, pgsize); prandom_bytes_state(&rnd_state, writebuf, pgsize);
err = mtd_write(mtd, addr0, pgsize, &written, writebuf); err = mtdtest_write(mtd, addr0, pgsize, writebuf);
if (err || written != pgsize) { if (err) {
pr_err("error: write failed at %#llx\n", pr_err("error: write failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err ? err : -1; return err;
} }
pr_info("erasing block %d\n", ebnum); pr_info("erasing block %d\n", ebnum);
err = erase_eraseblock(ebnum); err = mtdtest_erase_eraseblock(mtd, ebnum);
if (err) if (err)
return err; return err;
pr_info("reading 1st page of block %d\n", ebnum); pr_info("reading 1st page of block %d\n", ebnum);
err = mtd_read(mtd, addr0, pgsize, &read, twopages); err = mtdtest_read(mtd, addr0, pgsize, twopages);
if (mtd_is_bitflip(err)) if (err) {
err = 0;
if (err || read != pgsize) {
pr_err("error: read failed at %#llx\n", pr_err("error: read failed at %#llx\n",
(long long)addr0); (long long)addr0);
return err ? err : -1; return err;
} }
pr_info("verifying 1st page of block %d is all 0xff\n", pr_info("verifying 1st page of block %d is all 0xff\n",
...@@ -438,36 +381,6 @@ static int erasetest(void) ...@@ -438,36 +381,6 @@ static int erasetest(void)
return err; return err;
} }
static int is_block_bad(int ebnum)
{
loff_t addr = ebnum * mtd->erasesize;
int ret;
ret = mtd_block_isbad(mtd, addr);
if (ret)
pr_info("block %d is bad\n", ebnum);
return ret;
}
static int scan_for_bad_eraseblocks(void)
{
int i, bad = 0;
bbt = kzalloc(ebcnt, GFP_KERNEL);
if (!bbt)
return -ENOMEM;
pr_info("scanning for bad eraseblocks\n");
for (i = 0; i < ebcnt; ++i) {
bbt[i] = is_block_bad(i) ? 1 : 0;
if (bbt[i])
bad += 1;
cond_resched();
}
pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
return 0;
}
static int __init mtd_pagetest_init(void) static int __init mtd_pagetest_init(void)
{ {
int err = 0; int err = 0;
...@@ -521,21 +434,19 @@ static int __init mtd_pagetest_init(void) ...@@ -521,21 +434,19 @@ static int __init mtd_pagetest_init(void)
if (!boundary) if (!boundary)
goto out; goto out;
err = scan_for_bad_eraseblocks(); bbt = kzalloc(ebcnt, GFP_KERNEL);
if (!bbt)
goto out;
err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
if (err) if (err)
goto out; goto out;
/* Erase all eraseblocks */ /* Erase all eraseblocks */
pr_info("erasing whole device\n"); pr_info("erasing whole device\n");
for (i = 0; i < ebcnt; ++i) { err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
if (bbt[i])
continue;
err = erase_eraseblock(i);
if (err) if (err)
goto out; goto out;
cond_resched(); pr_info("erased %u eraseblocks\n", ebcnt);
}
pr_info("erased %u eraseblocks\n", i);
/* Write all eraseblocks */ /* Write all eraseblocks */
prandom_seed_state(&rnd_state, 1); prandom_seed_state(&rnd_state, 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment