mtd_nandecctest.c 7.9 KB

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