mtd_nandecctest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/list.h>
  4. #include <linux/random.h>
  5. #include <linux/string.h>
  6. #include <linux/bitops.h>
  7. #include <linux/slab.h>
  8. #include <linux/mtd/nand_ecc.h>
  9. /*
  10. * Test the implementation for software ECC
  11. *
  12. * No actual MTD device is needed, So we don't need to warry about losing
  13. * important data by human error.
  14. *
  15. * This covers possible patterns of corruption which can be reliably corrected
  16. * or detected.
  17. */
  18. #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)
  19. struct nand_ecc_test {
  20. const char *name;
  21. void (*prepare)(void *, void *, void *, void *, const size_t);
  22. int (*verify)(void *, void *, void *, const size_t);
  23. };
  24. /*
  25. * The reason for this __change_bit_le() instead of __change_bit() is to inject
  26. * bit error properly within the region which is not a multiple of
  27. * sizeof(unsigned long) on big-endian systems
  28. */
  29. #ifdef __LITTLE_ENDIAN
  30. #define __change_bit_le(nr, addr) __change_bit(nr, addr)
  31. #elif defined(__BIG_ENDIAN)
  32. #define __change_bit_le(nr, addr) \
  33. __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
  34. #else
  35. #error "Unknown byte order"
  36. #endif
  37. static void single_bit_error_data(void *error_data, void *correct_data,
  38. size_t size)
  39. {
  40. unsigned int offset = random32() % (size * BITS_PER_BYTE);
  41. memcpy(error_data, correct_data, size);
  42. __change_bit_le(offset, error_data);
  43. }
  44. static void single_bit_error_in_data(void *error_data, void *error_ecc,
  45. void *correct_data, void *correct_ecc, const size_t size)
  46. {
  47. single_bit_error_data(error_data, correct_data, size);
  48. memcpy(error_ecc, correct_ecc, 3);
  49. }
  50. static int single_bit_error_correct(void *error_data, void *error_ecc,
  51. void *correct_data, const size_t size)
  52. {
  53. unsigned char calc_ecc[3];
  54. int ret;
  55. __nand_calculate_ecc(error_data, size, calc_ecc);
  56. ret = __nand_correct_data(error_data, error_ecc, calc_ecc, size);
  57. if (ret == 1 && !memcmp(correct_data, error_data, size))
  58. return 0;
  59. return -EINVAL;
  60. }
  61. static const struct nand_ecc_test nand_ecc_test[] = {
  62. {
  63. .name = "single-bit-error-in-data-correct",
  64. .prepare = single_bit_error_in_data,
  65. .verify = single_bit_error_correct,
  66. },
  67. };
  68. static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
  69. void *correct_ecc, const size_t size)
  70. {
  71. pr_info("hexdump of error data:\n");
  72. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  73. error_data, size, false);
  74. print_hex_dump(KERN_INFO, "hexdump of error ecc: ",
  75. DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false);
  76. pr_info("hexdump of correct data:\n");
  77. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  78. correct_data, size, false);
  79. print_hex_dump(KERN_INFO, "hexdump of correct ecc: ",
  80. DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
  81. }
  82. static int nand_ecc_test_run(const size_t size)
  83. {
  84. int i;
  85. int err = 0;
  86. void *error_data;
  87. void *error_ecc;
  88. void *correct_data;
  89. void *correct_ecc;
  90. error_data = kmalloc(size, GFP_KERNEL);
  91. error_ecc = kmalloc(3, GFP_KERNEL);
  92. correct_data = kmalloc(size, GFP_KERNEL);
  93. correct_ecc = kmalloc(3, GFP_KERNEL);
  94. if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
  95. err = -ENOMEM;
  96. goto error;
  97. }
  98. get_random_bytes(correct_data, size);
  99. __nand_calculate_ecc(correct_data, size, correct_ecc);
  100. for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) {
  101. nand_ecc_test[i].prepare(error_data, error_ecc,
  102. correct_data, correct_ecc, size);
  103. err = nand_ecc_test[i].verify(error_data, error_ecc,
  104. correct_data, size);
  105. if (err) {
  106. pr_err("mtd_nandecctest: not ok - %s-%zd\n",
  107. nand_ecc_test[i].name, size);
  108. dump_data_ecc(error_data, error_ecc,
  109. correct_data, correct_ecc, size);
  110. break;
  111. }
  112. pr_info("mtd_nandecctest: ok - %s-%zd\n",
  113. nand_ecc_test[i].name, size);
  114. }
  115. error:
  116. kfree(error_data);
  117. kfree(error_ecc);
  118. kfree(correct_data);
  119. kfree(correct_ecc);
  120. return err;
  121. }
  122. #else
  123. static int nand_ecc_test_run(const size_t size)
  124. {
  125. return 0;
  126. }
  127. #endif
  128. static int __init ecc_test_init(void)
  129. {
  130. int err;
  131. err = nand_ecc_test_run(256);
  132. if (err)
  133. return err;
  134. return nand_ecc_test_run(512);
  135. }
  136. static void __exit ecc_test_exit(void)
  137. {
  138. }
  139. module_init(ecc_test_init);
  140. module_exit(ecc_test_exit);
  141. MODULE_DESCRIPTION("NAND ECC function test module");
  142. MODULE_AUTHOR("Akinobu Mita");
  143. MODULE_LICENSE("GPL");