1 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
2 
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>
11 
12 #include "mtd_test.h"
13 
14 /*
15  * Test the implementation for software ECC
16  *
17  * No actual MTD device is needed, So we don't need to warry about losing
18  * important data by human error.
19  *
20  * This covers possible patterns of corruption which can be reliably corrected
21  * or detected.
22  */
23 
24 #if IS_ENABLED(CONFIG_MTD_NAND)
25 
26 struct nand_ecc_test {
27 	const char *name;
28 	void (*prepare)(void *, void *, void *, void *, const size_t);
29 	int (*verify)(void *, void *, void *, const size_t);
30 };
31 
32 /*
33  * The reason for this __change_bit_le() instead of __change_bit() is to inject
34  * bit error properly within the region which is not a multiple of
35  * sizeof(unsigned long) on big-endian systems
36  */
37 #ifdef __LITTLE_ENDIAN
38 #define __change_bit_le(nr, addr) __change_bit(nr, addr)
39 #elif defined(__BIG_ENDIAN)
40 #define __change_bit_le(nr, addr) \
41 		__change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
42 #else
43 #error "Unknown byte order"
44 #endif
45 
46 static void single_bit_error_data(void *error_data, void *correct_data,
47 				size_t size)
48 {
49 	unsigned int offset = prandom_u32() % (size * BITS_PER_BYTE);
50 
51 	memcpy(error_data, correct_data, size);
52 	__change_bit_le(offset, error_data);
53 }
54 
55 static void double_bit_error_data(void *error_data, void *correct_data,
56 				size_t size)
57 {
58 	unsigned int offset[2];
59 
60 	offset[0] = prandom_u32() % (size * BITS_PER_BYTE);
61 	do {
62 		offset[1] = prandom_u32() % (size * BITS_PER_BYTE);
63 	} while (offset[0] == offset[1]);
64 
65 	memcpy(error_data, correct_data, size);
66 
67 	__change_bit_le(offset[0], error_data);
68 	__change_bit_le(offset[1], error_data);
69 }
70 
71 static unsigned int random_ecc_bit(size_t size)
72 {
73 	unsigned int offset = prandom_u32() % (3 * BITS_PER_BYTE);
74 
75 	if (size == 256) {
76 		/*
77 		 * Don't inject a bit error into the insignificant bits (16th
78 		 * and 17th bit) in ECC code for 256 byte data block
79 		 */
80 		while (offset == 16 || offset == 17)
81 			offset = prandom_u32() % (3 * BITS_PER_BYTE);
82 	}
83 
84 	return offset;
85 }
86 
87 static void single_bit_error_ecc(void *error_ecc, void *correct_ecc,
88 				size_t size)
89 {
90 	unsigned int offset = random_ecc_bit(size);
91 
92 	memcpy(error_ecc, correct_ecc, 3);
93 	__change_bit_le(offset, error_ecc);
94 }
95 
96 static void double_bit_error_ecc(void *error_ecc, void *correct_ecc,
97 				size_t size)
98 {
99 	unsigned int offset[2];
100 
101 	offset[0] = random_ecc_bit(size);
102 	do {
103 		offset[1] = random_ecc_bit(size);
104 	} while (offset[0] == offset[1]);
105 
106 	memcpy(error_ecc, correct_ecc, 3);
107 	__change_bit_le(offset[0], error_ecc);
108 	__change_bit_le(offset[1], error_ecc);
109 }
110 
111 static void no_bit_error(void *error_data, void *error_ecc,
112 		void *correct_data, void *correct_ecc, const size_t size)
113 {
114 	memcpy(error_data, correct_data, size);
115 	memcpy(error_ecc, correct_ecc, 3);
116 }
117 
118 static int no_bit_error_verify(void *error_data, void *error_ecc,
119 				void *correct_data, const size_t size)
120 {
121 	unsigned char calc_ecc[3];
122 	int ret;
123 
124 	__nand_calculate_ecc(error_data, size, calc_ecc,
125 			     IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
126 	ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size,
127 				  IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
128 	if (ret == 0 && !memcmp(correct_data, error_data, size))
129 		return 0;
130 
131 	return -EINVAL;
132 }
133 
134 static void single_bit_error_in_data(void *error_data, void *error_ecc,
135 		void *correct_data, void *correct_ecc, const size_t size)
136 {
137 	single_bit_error_data(error_data, correct_data, size);
138 	memcpy(error_ecc, correct_ecc, 3);
139 }
140 
141 static void single_bit_error_in_ecc(void *error_data, void *error_ecc,
142 		void *correct_data, void *correct_ecc, const size_t size)
143 {
144 	memcpy(error_data, correct_data, size);
145 	single_bit_error_ecc(error_ecc, correct_ecc, size);
146 }
147 
148 static int single_bit_error_correct(void *error_data, void *error_ecc,
149 				void *correct_data, const size_t size)
150 {
151 	unsigned char calc_ecc[3];
152 	int ret;
153 
154 	__nand_calculate_ecc(error_data, size, calc_ecc,
155 			     IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
156 	ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size,
157 				  IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
158 	if (ret == 1 && !memcmp(correct_data, error_data, size))
159 		return 0;
160 
161 	return -EINVAL;
162 }
163 
164 static void double_bit_error_in_data(void *error_data, void *error_ecc,
165 		void *correct_data, void *correct_ecc, const size_t size)
166 {
167 	double_bit_error_data(error_data, correct_data, size);
168 	memcpy(error_ecc, correct_ecc, 3);
169 }
170 
171 static void single_bit_error_in_data_and_ecc(void *error_data, void *error_ecc,
172 		void *correct_data, void *correct_ecc, const size_t size)
173 {
174 	single_bit_error_data(error_data, correct_data, size);
175 	single_bit_error_ecc(error_ecc, correct_ecc, size);
176 }
177 
178 static void double_bit_error_in_ecc(void *error_data, void *error_ecc,
179 		void *correct_data, void *correct_ecc, const size_t size)
180 {
181 	memcpy(error_data, correct_data, size);
182 	double_bit_error_ecc(error_ecc, correct_ecc, size);
183 }
184 
185 static int double_bit_error_detect(void *error_data, void *error_ecc,
186 				void *correct_data, const size_t size)
187 {
188 	unsigned char calc_ecc[3];
189 	int ret;
190 
191 	__nand_calculate_ecc(error_data, size, calc_ecc,
192 			     IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
193 	ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size,
194 				  IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
195 
196 	return (ret == -EBADMSG) ? 0 : -EINVAL;
197 }
198 
199 static const struct nand_ecc_test nand_ecc_test[] = {
200 	{
201 		.name = "no-bit-error",
202 		.prepare = no_bit_error,
203 		.verify = no_bit_error_verify,
204 	},
205 	{
206 		.name = "single-bit-error-in-data-correct",
207 		.prepare = single_bit_error_in_data,
208 		.verify = single_bit_error_correct,
209 	},
210 	{
211 		.name = "single-bit-error-in-ecc-correct",
212 		.prepare = single_bit_error_in_ecc,
213 		.verify = single_bit_error_correct,
214 	},
215 	{
216 		.name = "double-bit-error-in-data-detect",
217 		.prepare = double_bit_error_in_data,
218 		.verify = double_bit_error_detect,
219 	},
220 	{
221 		.name = "single-bit-error-in-data-and-ecc-detect",
222 		.prepare = single_bit_error_in_data_and_ecc,
223 		.verify = double_bit_error_detect,
224 	},
225 	{
226 		.name = "double-bit-error-in-ecc-detect",
227 		.prepare = double_bit_error_in_ecc,
228 		.verify = double_bit_error_detect,
229 	},
230 };
231 
232 static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
233 			void *correct_ecc, const size_t size)
234 {
235 	pr_info("hexdump of error data:\n");
236 	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
237 			error_data, size, false);
238 	print_hex_dump(KERN_INFO, "hexdump of error ecc: ",
239 			DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false);
240 
241 	pr_info("hexdump of correct data:\n");
242 	print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
243 			correct_data, size, false);
244 	print_hex_dump(KERN_INFO, "hexdump of correct ecc: ",
245 			DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
246 }
247 
248 static int nand_ecc_test_run(const size_t size)
249 {
250 	int i;
251 	int err = 0;
252 	void *error_data;
253 	void *error_ecc;
254 	void *correct_data;
255 	void *correct_ecc;
256 
257 	error_data = kmalloc(size, GFP_KERNEL);
258 	error_ecc = kmalloc(3, GFP_KERNEL);
259 	correct_data = kmalloc(size, GFP_KERNEL);
260 	correct_ecc = kmalloc(3, GFP_KERNEL);
261 
262 	if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
263 		err = -ENOMEM;
264 		goto error;
265 	}
266 
267 	prandom_bytes(correct_data, size);
268 	__nand_calculate_ecc(correct_data, size, correct_ecc,
269 			     IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC));
270 
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);
276 
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;
283 		}
284 		pr_info("ok - %s-%zd\n",
285 			nand_ecc_test[i].name, size);
286 
287 		err = mtdtest_relax();
288 		if (err)
289 			break;
290 	}
291 error:
292 	kfree(error_data);
293 	kfree(error_ecc);
294 	kfree(correct_data);
295 	kfree(correct_ecc);
296 
297 	return err;
298 }
299 
300 #else
301 
302 static int nand_ecc_test_run(const size_t size)
303 {
304 	return 0;
305 }
306 
307 #endif
308 
309 static int __init ecc_test_init(void)
310 {
311 	int err;
312 
313 	err = nand_ecc_test_run(256);
314 	if (err)
315 		return err;
316 
317 	return nand_ecc_test_run(512);
318 }
319 
320 static void __exit ecc_test_exit(void)
321 {
322 }
323 
324 module_init(ecc_test_init);
325 module_exit(ecc_test_exit);
326 
327 MODULE_DESCRIPTION("NAND ECC function test module");
328 MODULE_AUTHOR("Akinobu Mita");
329 MODULE_LICENSE("GPL");
330