aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkinobu Mita <akinobu.mita@gmail.com>2013-08-03 05:52:07 -0400
committerNitin Garg <nitin.garg@freescale.com>2014-04-16 09:06:06 -0400
commitddad6d9e0eaa33d29d94b4222187b69891530c41 (patch)
tree9c2d3c58ab768500a053ec3a45d3f524be4c3058
parent6f7163826661bfe6975be70241bcb2a54aedc1a7 (diff)
mtd: tests: introduce helper functions
This introduces the helper functions which can be used by several mtd/tests modules. The following three functions are used all over the test modules. - mtdtest_erase_eraseblock() - mtdtest_scan_for_bad_eraseblocks() - mtdtest_erase_good_eraseblocks() The following are wrapper functions for mtd_read() and mtd_write() which can simplify the return value check. - mtdtest_read() - mtdtest_write() All helpers are put into a single .c file and it will be linked to every test module later. The code will actually be copied to every test module, but it is fine for our small test infrastructure. [dwmw2: merge later 'return -EIO when mtdtest_read() failed' fix] Signed-off-by: Akinobu 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: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com> Signed-off-by: Huang Shijie <b32955@freescale.com>
-rw-r--r--drivers/mtd/tests/mtd_test.c110
-rw-r--r--drivers/mtd/tests/mtd_test.h11
2 files changed, 121 insertions, 0 deletions
diff --git a/drivers/mtd/tests/mtd_test.c b/drivers/mtd/tests/mtd_test.c
new file mode 100644
index 000000000000..bda8c4de3433
--- /dev/null
+++ b/drivers/mtd/tests/mtd_test.c
@@ -0,0 +1,110 @@
1#define pr_fmt(fmt) "mtd_test: " fmt
2
3#include <linux/init.h>
4#include <linux/module.h>
5#include <linux/sched.h>
6#include <linux/printk.h>
7
8#include "mtd_test.h"
9
10int mtdtest_erase_eraseblock(struct mtd_info *mtd, unsigned int ebnum)
11{
12 int err;
13 struct erase_info ei;
14 loff_t addr = ebnum * mtd->erasesize;
15
16 memset(&ei, 0, sizeof(struct erase_info));
17 ei.mtd = mtd;
18 ei.addr = addr;
19 ei.len = mtd->erasesize;
20
21 err = mtd_erase(mtd, &ei);
22 if (err) {
23 pr_info("error %d while erasing EB %d\n", err, ebnum);
24 return err;
25 }
26
27 if (ei.state == MTD_ERASE_FAILED) {
28 pr_info("some erase error occurred at EB %d\n", ebnum);
29 return -EIO;
30 }
31 return 0;
32}
33
34static int is_block_bad(struct mtd_info *mtd, unsigned int ebnum)
35{
36 int ret;
37 loff_t addr = ebnum * mtd->erasesize;
38
39 ret = mtd_block_isbad(mtd, addr);
40 if (ret)
41 pr_info("block %d is bad\n", ebnum);
42
43 return ret;
44}
45
46int mtdtest_scan_for_bad_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
47 unsigned int eb, int ebcnt)
48{
49 int i, bad = 0;
50
51 if (!mtd_can_have_bb(mtd))
52 return 0;
53
54 pr_info("scanning for bad eraseblocks\n");
55 for (i = 0; i < ebcnt; ++i) {
56 bbt[i] = is_block_bad(mtd, eb + i) ? 1 : 0;
57 if (bbt[i])
58 bad += 1;
59 cond_resched();
60 }
61 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
62
63 return 0;
64}
65
66int mtdtest_erase_good_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
67 unsigned int eb, int ebcnt)
68{
69 int err;
70 unsigned int i;
71
72 for (i = 0; i < ebcnt; ++i) {
73 if (bbt[i])
74 continue;
75 err = mtdtest_erase_eraseblock(mtd, eb + i);
76 if (err)
77 return err;
78 cond_resched();
79 }
80
81 return 0;
82}
83
84int mtdtest_read(struct mtd_info *mtd, loff_t addr, size_t size, void *buf)
85{
86 size_t read;
87 int err;
88
89 err = mtd_read(mtd, addr, size, &read, buf);
90 /* Ignore corrected ECC errors */
91 if (mtd_is_bitflip(err))
92 err = 0;
93 if (!err && read != size)
94 err = -EIO;
95
96 return err;
97}
98
99int mtdtest_write(struct mtd_info *mtd, loff_t addr, size_t size,
100 const void *buf)
101{
102 size_t written;
103 int err;
104
105 err = mtd_write(mtd, addr, size, &written, buf);
106 if (!err && written != size)
107 err = -EIO;
108
109 return err;
110}
diff --git a/drivers/mtd/tests/mtd_test.h b/drivers/mtd/tests/mtd_test.h
new file mode 100644
index 000000000000..f437c776c54f
--- /dev/null
+++ b/drivers/mtd/tests/mtd_test.h
@@ -0,0 +1,11 @@
1#include <linux/mtd/mtd.h>
2
3int mtdtest_erase_eraseblock(struct mtd_info *mtd, unsigned int ebnum);
4int mtdtest_scan_for_bad_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
5 unsigned int eb, int ebcnt);
6int mtdtest_erase_good_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
7 unsigned int eb, int ebcnt);
8
9int mtdtest_read(struct mtd_info *mtd, loff_t addr, size_t size, void *buf);
10int mtdtest_write(struct mtd_info *mtd, loff_t addr, size_t size,
11 const void *buf);