1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/list.h>
6 #include <linux/random.h>
7 #include <linux/string.h>
8 #include <linux/bitops.h>
9 #include <linux/slab.h>
10 #include <linux/mtd/nand_ecc.h>
13 * Test the implementation for software ECC
15 * No actual MTD device is needed, So we don't need to warry about losing
16 * important data by human error.
18 * This covers possible patterns of corruption which can be reliably corrected
22 #if IS_ENABLED(CONFIG_MTD_NAND)
24 struct nand_ecc_test
{
26 void (*prepare
)(void *, void *, void *, void *, const size_t);
27 int (*verify
)(void *, void *, void *, const size_t);
31 * The reason for this __change_bit_le() instead of __change_bit() is to inject
32 * bit error properly within the region which is not a multiple of
33 * sizeof(unsigned long) on big-endian systems
35 #ifdef __LITTLE_ENDIAN
36 #define __change_bit_le(nr, addr) __change_bit(nr, addr)
37 #elif defined(__BIG_ENDIAN)
38 #define __change_bit_le(nr, addr) \
39 __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
41 #error "Unknown byte order"
44 static void single_bit_error_data(void *error_data
, void *correct_data
,
47 unsigned int offset
= prandom_u32() % (size
* BITS_PER_BYTE
);
49 memcpy(error_data
, correct_data
, size
);
50 __change_bit_le(offset
, error_data
);
53 static void double_bit_error_data(void *error_data
, void *correct_data
,
56 unsigned int offset
[2];
58 offset
[0] = prandom_u32() % (size
* BITS_PER_BYTE
);
60 offset
[1] = prandom_u32() % (size
* BITS_PER_BYTE
);
61 } while (offset
[0] == offset
[1]);
63 memcpy(error_data
, correct_data
, size
);
65 __change_bit_le(offset
[0], error_data
);
66 __change_bit_le(offset
[1], error_data
);
69 static unsigned int random_ecc_bit(size_t size
)
71 unsigned int offset
= prandom_u32() % (3 * BITS_PER_BYTE
);
75 * Don't inject a bit error into the insignificant bits (16th
76 * and 17th bit) in ECC code for 256 byte data block
78 while (offset
== 16 || offset
== 17)
79 offset
= prandom_u32() % (3 * BITS_PER_BYTE
);
85 static void single_bit_error_ecc(void *error_ecc
, void *correct_ecc
,
88 unsigned int offset
= random_ecc_bit(size
);
90 memcpy(error_ecc
, correct_ecc
, 3);
91 __change_bit_le(offset
, error_ecc
);
94 static void double_bit_error_ecc(void *error_ecc
, void *correct_ecc
,
97 unsigned int offset
[2];
99 offset
[0] = random_ecc_bit(size
);
101 offset
[1] = random_ecc_bit(size
);
102 } while (offset
[0] == offset
[1]);
104 memcpy(error_ecc
, correct_ecc
, 3);
105 __change_bit_le(offset
[0], error_ecc
);
106 __change_bit_le(offset
[1], error_ecc
);
109 static void no_bit_error(void *error_data
, void *error_ecc
,
110 void *correct_data
, void *correct_ecc
, const size_t size
)
112 memcpy(error_data
, correct_data
, size
);
113 memcpy(error_ecc
, correct_ecc
, 3);
116 static int no_bit_error_verify(void *error_data
, void *error_ecc
,
117 void *correct_data
, const size_t size
)
119 unsigned char calc_ecc
[3];
122 __nand_calculate_ecc(error_data
, size
, calc_ecc
);
123 ret
= __nand_correct_data(error_data
, error_ecc
, calc_ecc
, size
);
124 if (ret
== 0 && !memcmp(correct_data
, error_data
, size
))
130 static void single_bit_error_in_data(void *error_data
, void *error_ecc
,
131 void *correct_data
, void *correct_ecc
, const size_t size
)
133 single_bit_error_data(error_data
, correct_data
, size
);
134 memcpy(error_ecc
, correct_ecc
, 3);
137 static void single_bit_error_in_ecc(void *error_data
, void *error_ecc
,
138 void *correct_data
, void *correct_ecc
, const size_t size
)
140 memcpy(error_data
, correct_data
, size
);
141 single_bit_error_ecc(error_ecc
, correct_ecc
, size
);
144 static int single_bit_error_correct(void *error_data
, void *error_ecc
,
145 void *correct_data
, const size_t size
)
147 unsigned char calc_ecc
[3];
150 __nand_calculate_ecc(error_data
, size
, calc_ecc
);
151 ret
= __nand_correct_data(error_data
, error_ecc
, calc_ecc
, size
);
152 if (ret
== 1 && !memcmp(correct_data
, error_data
, size
))
158 static void double_bit_error_in_data(void *error_data
, void *error_ecc
,
159 void *correct_data
, void *correct_ecc
, const size_t size
)
161 double_bit_error_data(error_data
, correct_data
, size
);
162 memcpy(error_ecc
, correct_ecc
, 3);
165 static void single_bit_error_in_data_and_ecc(void *error_data
, void *error_ecc
,
166 void *correct_data
, void *correct_ecc
, const size_t size
)
168 single_bit_error_data(error_data
, correct_data
, size
);
169 single_bit_error_ecc(error_ecc
, correct_ecc
, size
);
172 static void double_bit_error_in_ecc(void *error_data
, void *error_ecc
,
173 void *correct_data
, void *correct_ecc
, const size_t size
)
175 memcpy(error_data
, correct_data
, size
);
176 double_bit_error_ecc(error_ecc
, correct_ecc
, size
);
179 static int double_bit_error_detect(void *error_data
, void *error_ecc
,
180 void *correct_data
, const size_t size
)
182 unsigned char calc_ecc
[3];
185 __nand_calculate_ecc(error_data
, size
, calc_ecc
);
186 ret
= __nand_correct_data(error_data
, error_ecc
, calc_ecc
, size
);
188 return (ret
== -1) ? 0 : -EINVAL
;
191 static const struct nand_ecc_test nand_ecc_test
[] = {
193 .name
= "no-bit-error",
194 .prepare
= no_bit_error
,
195 .verify
= no_bit_error_verify
,
198 .name
= "single-bit-error-in-data-correct",
199 .prepare
= single_bit_error_in_data
,
200 .verify
= single_bit_error_correct
,
203 .name
= "single-bit-error-in-ecc-correct",
204 .prepare
= single_bit_error_in_ecc
,
205 .verify
= single_bit_error_correct
,
208 .name
= "double-bit-error-in-data-detect",
209 .prepare
= double_bit_error_in_data
,
210 .verify
= double_bit_error_detect
,
213 .name
= "single-bit-error-in-data-and-ecc-detect",
214 .prepare
= single_bit_error_in_data_and_ecc
,
215 .verify
= double_bit_error_detect
,
218 .name
= "double-bit-error-in-ecc-detect",
219 .prepare
= double_bit_error_in_ecc
,
220 .verify
= double_bit_error_detect
,
224 static void dump_data_ecc(void *error_data
, void *error_ecc
, void *correct_data
,
225 void *correct_ecc
, const size_t size
)
227 pr_info("hexdump of error data:\n");
228 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 16, 4,
229 error_data
, size
, false);
230 print_hex_dump(KERN_INFO
, "hexdump of error ecc: ",
231 DUMP_PREFIX_NONE
, 16, 1, error_ecc
, 3, false);
233 pr_info("hexdump of correct data:\n");
234 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_OFFSET
, 16, 4,
235 correct_data
, size
, false);
236 print_hex_dump(KERN_INFO
, "hexdump of correct ecc: ",
237 DUMP_PREFIX_NONE
, 16, 1, correct_ecc
, 3, false);
240 static int nand_ecc_test_run(const size_t size
)
249 error_data
= kmalloc(size
, GFP_KERNEL
);
250 error_ecc
= kmalloc(3, GFP_KERNEL
);
251 correct_data
= kmalloc(size
, GFP_KERNEL
);
252 correct_ecc
= kmalloc(3, GFP_KERNEL
);
254 if (!error_data
|| !error_ecc
|| !correct_data
|| !correct_ecc
) {
259 prandom_bytes(correct_data
, size
);
260 __nand_calculate_ecc(correct_data
, size
, correct_ecc
);
262 for (i
= 0; i
< ARRAY_SIZE(nand_ecc_test
); i
++) {
263 nand_ecc_test
[i
].prepare(error_data
, error_ecc
,
264 correct_data
, correct_ecc
, size
);
265 err
= nand_ecc_test
[i
].verify(error_data
, error_ecc
,
269 pr_err("not ok - %s-%zd\n",
270 nand_ecc_test
[i
].name
, size
);
271 dump_data_ecc(error_data
, error_ecc
,
272 correct_data
, correct_ecc
, size
);
275 pr_info("ok - %s-%zd\n",
276 nand_ecc_test
[i
].name
, size
);
289 static int nand_ecc_test_run(const size_t size
)
296 static int __init
ecc_test_init(void)
300 err
= nand_ecc_test_run(256);
304 return nand_ecc_test_run(512);
307 static void __exit
ecc_test_exit(void)
311 module_init(ecc_test_init
);
312 module_exit(ecc_test_exit
);
314 MODULE_DESCRIPTION("NAND ECC function test module");
315 MODULE_AUTHOR("Akinobu Mita");
316 MODULE_LICENSE("GPL");