Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mtd / tests / mtd_nandecctest.c
blobc4f271314f5264acf7f6c0bfa1475f00619e0074
1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/list.h>
7 #include <linux/random.h>
8 #include <linux/string.h>
9 #include <linux/bitops.h>
10 #include <linux/slab.h>
11 #include <linux/mtd/nand-ecc-sw-hamming.h>
13 #include "mtd_test.h"
16 * Test the implementation for software ECC
18 * No actual MTD device is needed, So we don't need to warry about losing
19 * important data by human error.
21 * This covers possible patterns of corruption which can be reliably corrected
22 * or detected.
25 #if IS_ENABLED(CONFIG_MTD_RAW_NAND)
27 struct nand_ecc_test {
28 const char *name;
29 void (*prepare)(void *, void *, void *, void *, const size_t);
30 int (*verify)(void *, void *, void *, const size_t);
34 * The reason for this __change_bit_le() instead of __change_bit() is to inject
35 * bit error properly within the region which is not a multiple of
36 * sizeof(unsigned long) on big-endian systems
38 #ifdef __LITTLE_ENDIAN
39 #define __change_bit_le(nr, addr) __change_bit(nr, addr)
40 #elif defined(__BIG_ENDIAN)
41 #define __change_bit_le(nr, addr) \
42 __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
43 #else
44 #error "Unknown byte order"
45 #endif
47 static void single_bit_error_data(void *error_data, void *correct_data,
48 size_t size)
50 unsigned int offset = prandom_u32() % (size * BITS_PER_BYTE);
52 memcpy(error_data, correct_data, size);
53 __change_bit_le(offset, error_data);
56 static void double_bit_error_data(void *error_data, void *correct_data,
57 size_t size)
59 unsigned int offset[2];
61 offset[0] = prandom_u32() % (size * BITS_PER_BYTE);
62 do {
63 offset[1] = prandom_u32() % (size * BITS_PER_BYTE);
64 } while (offset[0] == offset[1]);
66 memcpy(error_data, correct_data, size);
68 __change_bit_le(offset[0], error_data);
69 __change_bit_le(offset[1], error_data);
72 static unsigned int random_ecc_bit(size_t size)
74 unsigned int offset = prandom_u32() % (3 * BITS_PER_BYTE);
76 if (size == 256) {
78 * Don't inject a bit error into the insignificant bits (16th
79 * and 17th bit) in ECC code for 256 byte data block
81 while (offset == 16 || offset == 17)
82 offset = prandom_u32() % (3 * BITS_PER_BYTE);
85 return offset;
88 static void single_bit_error_ecc(void *error_ecc, void *correct_ecc,
89 size_t size)
91 unsigned int offset = random_ecc_bit(size);
93 memcpy(error_ecc, correct_ecc, 3);
94 __change_bit_le(offset, error_ecc);
97 static void double_bit_error_ecc(void *error_ecc, void *correct_ecc,
98 size_t size)
100 unsigned int offset[2];
102 offset[0] = random_ecc_bit(size);
103 do {
104 offset[1] = random_ecc_bit(size);
105 } while (offset[0] == offset[1]);
107 memcpy(error_ecc, correct_ecc, 3);
108 __change_bit_le(offset[0], error_ecc);
109 __change_bit_le(offset[1], error_ecc);
112 static void no_bit_error(void *error_data, void *error_ecc,
113 void *correct_data, void *correct_ecc, const size_t size)
115 memcpy(error_data, correct_data, size);
116 memcpy(error_ecc, correct_ecc, 3);
119 static int no_bit_error_verify(void *error_data, void *error_ecc,
120 void *correct_data, const size_t size)
122 bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
123 unsigned char calc_ecc[3];
124 int ret;
126 ecc_sw_hamming_calculate(error_data, size, calc_ecc, sm_order);
127 ret = ecc_sw_hamming_correct(error_data, error_ecc, calc_ecc, size,
128 sm_order);
129 if (ret == 0 && !memcmp(correct_data, error_data, size))
130 return 0;
132 return -EINVAL;
135 static void single_bit_error_in_data(void *error_data, void *error_ecc,
136 void *correct_data, void *correct_ecc, const size_t size)
138 single_bit_error_data(error_data, correct_data, size);
139 memcpy(error_ecc, correct_ecc, 3);
142 static void single_bit_error_in_ecc(void *error_data, void *error_ecc,
143 void *correct_data, void *correct_ecc, const size_t size)
145 memcpy(error_data, correct_data, size);
146 single_bit_error_ecc(error_ecc, correct_ecc, size);
149 static int single_bit_error_correct(void *error_data, void *error_ecc,
150 void *correct_data, const size_t size)
152 bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
153 unsigned char calc_ecc[3];
154 int ret;
156 ecc_sw_hamming_calculate(error_data, size, calc_ecc, sm_order);
157 ret = ecc_sw_hamming_correct(error_data, error_ecc, calc_ecc, size,
158 sm_order);
159 if (ret == 1 && !memcmp(correct_data, error_data, size))
160 return 0;
162 return -EINVAL;
165 static void double_bit_error_in_data(void *error_data, void *error_ecc,
166 void *correct_data, void *correct_ecc, const size_t size)
168 double_bit_error_data(error_data, correct_data, size);
169 memcpy(error_ecc, correct_ecc, 3);
172 static void single_bit_error_in_data_and_ecc(void *error_data, void *error_ecc,
173 void *correct_data, void *correct_ecc, const size_t size)
175 single_bit_error_data(error_data, correct_data, size);
176 single_bit_error_ecc(error_ecc, correct_ecc, size);
179 static void double_bit_error_in_ecc(void *error_data, void *error_ecc,
180 void *correct_data, void *correct_ecc, const size_t size)
182 memcpy(error_data, correct_data, size);
183 double_bit_error_ecc(error_ecc, correct_ecc, size);
186 static int double_bit_error_detect(void *error_data, void *error_ecc,
187 void *correct_data, const size_t size)
189 bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
190 unsigned char calc_ecc[3];
191 int ret;
193 ecc_sw_hamming_calculate(error_data, size, calc_ecc, sm_order);
194 ret = ecc_sw_hamming_correct(error_data, error_ecc, calc_ecc, size,
195 sm_order);
197 return (ret == -EBADMSG) ? 0 : -EINVAL;
200 static const struct nand_ecc_test nand_ecc_test[] = {
202 .name = "no-bit-error",
203 .prepare = no_bit_error,
204 .verify = no_bit_error_verify,
207 .name = "single-bit-error-in-data-correct",
208 .prepare = single_bit_error_in_data,
209 .verify = single_bit_error_correct,
212 .name = "single-bit-error-in-ecc-correct",
213 .prepare = single_bit_error_in_ecc,
214 .verify = single_bit_error_correct,
217 .name = "double-bit-error-in-data-detect",
218 .prepare = double_bit_error_in_data,
219 .verify = double_bit_error_detect,
222 .name = "single-bit-error-in-data-and-ecc-detect",
223 .prepare = single_bit_error_in_data_and_ecc,
224 .verify = double_bit_error_detect,
227 .name = "double-bit-error-in-ecc-detect",
228 .prepare = double_bit_error_in_ecc,
229 .verify = double_bit_error_detect,
233 static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
234 void *correct_ecc, const size_t size)
236 pr_info("hexdump of error data:\n");
237 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
238 error_data, size, false);
239 print_hex_dump(KERN_INFO, "hexdump of error ecc: ",
240 DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false);
242 pr_info("hexdump of correct data:\n");
243 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
244 correct_data, size, false);
245 print_hex_dump(KERN_INFO, "hexdump of correct ecc: ",
246 DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
249 static int nand_ecc_test_run(const size_t size)
251 bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
252 int i;
253 int err = 0;
254 void *error_data;
255 void *error_ecc;
256 void *correct_data;
257 void *correct_ecc;
259 error_data = kmalloc(size, GFP_KERNEL);
260 error_ecc = kmalloc(3, GFP_KERNEL);
261 correct_data = kmalloc(size, GFP_KERNEL);
262 correct_ecc = kmalloc(3, GFP_KERNEL);
264 if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
265 err = -ENOMEM;
266 goto error;
269 prandom_bytes(correct_data, size);
270 ecc_sw_hamming_calculate(correct_data, size, correct_ecc, sm_order);
271 for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) {
272 nand_ecc_test[i].prepare(error_data, error_ecc,
273 correct_data, correct_ecc, size);
274 err = nand_ecc_test[i].verify(error_data, error_ecc,
275 correct_data, size);
277 if (err) {
278 pr_err("not ok - %s-%zd\n",
279 nand_ecc_test[i].name, size);
280 dump_data_ecc(error_data, error_ecc,
281 correct_data, correct_ecc, size);
282 break;
284 pr_info("ok - %s-%zd\n",
285 nand_ecc_test[i].name, size);
287 err = mtdtest_relax();
288 if (err)
289 break;
291 error:
292 kfree(error_data);
293 kfree(error_ecc);
294 kfree(correct_data);
295 kfree(correct_ecc);
297 return err;
300 #else
302 static int nand_ecc_test_run(const size_t size)
304 return 0;
307 #endif
309 static int __init ecc_test_init(void)
311 int err;
313 err = nand_ecc_test_run(256);
314 if (err)
315 return err;
317 return nand_ecc_test_run(512);
320 static void __exit ecc_test_exit(void)
324 module_init(ecc_test_init);
325 module_exit(ecc_test_exit);
327 MODULE_DESCRIPTION("NAND ECC function test module");
328 MODULE_AUTHOR("Akinobu Mita");
329 MODULE_LICENSE("GPL");