mtd_nandecctest.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #if defined(CONFIG_MTD_NAND) || defined(CONFIG_MTD_NAND_MODULE)
  10. /*
  11. * The reason for this __change_bit_le() instead of __change_bit() is to inject
  12. * bit error properly within the region which is not a multiple of
  13. * sizeof(unsigned long) on big-endian systems
  14. */
  15. #ifdef __LITTLE_ENDIAN
  16. #define __change_bit_le(nr, addr) __change_bit(nr, addr)
  17. #elif defined(__BIG_ENDIAN)
  18. #define __change_bit_le(nr, addr) \
  19. __change_bit((nr) ^ ((BITS_PER_LONG - 1) & ~0x7), addr)
  20. #else
  21. #error "Unknown byte order"
  22. #endif
  23. static void inject_single_bit_error(void *data, size_t size)
  24. {
  25. unsigned int offset = random32() % (size * BITS_PER_BYTE);
  26. __change_bit_le(offset, data);
  27. }
  28. static void dump_data_ecc(void *error_data, void *error_ecc, void *correct_data,
  29. void *correct_ecc, const size_t size)
  30. {
  31. pr_info("hexdump of error data:\n");
  32. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  33. error_data, size, false);
  34. print_hex_dump(KERN_INFO, "hexdump of error ecc: ",
  35. DUMP_PREFIX_NONE, 16, 1, error_ecc, 3, false);
  36. pr_info("hexdump of correct data:\n");
  37. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 4,
  38. correct_data, size, false);
  39. print_hex_dump(KERN_INFO, "hexdump of correct ecc: ",
  40. DUMP_PREFIX_NONE, 16, 1, correct_ecc, 3, false);
  41. }
  42. static int nand_ecc_test(const size_t size)
  43. {
  44. int err = 0;
  45. void *error_data;
  46. void *error_ecc;
  47. void *correct_data;
  48. void *correct_ecc;
  49. char testname[30];
  50. error_data = kmalloc(size, GFP_KERNEL);
  51. error_ecc = kmalloc(3, GFP_KERNEL);
  52. correct_data = kmalloc(size, GFP_KERNEL);
  53. correct_ecc = kmalloc(3, GFP_KERNEL);
  54. if (!error_data || !error_ecc || !correct_data || !correct_ecc) {
  55. err = -ENOMEM;
  56. goto error;
  57. }
  58. sprintf(testname, "nand-ecc-%zu", size);
  59. get_random_bytes(correct_data, size);
  60. memcpy(error_data, correct_data, size);
  61. inject_single_bit_error(error_data, size);
  62. __nand_calculate_ecc(correct_data, size, correct_ecc);
  63. __nand_calculate_ecc(error_data, size, error_ecc);
  64. __nand_correct_data(error_data, correct_ecc, error_ecc, size);
  65. if (memcmp(correct_data, error_data, size)) {
  66. pr_err("mtd_nandecctest: not ok - %s\n", testname);
  67. dump_data_ecc(error_data, error_ecc, correct_data, correct_ecc,
  68. size);
  69. err = -EINVAL;
  70. goto error;
  71. }
  72. pr_info("mtd_nandecctest: ok - %s\n", testname);
  73. error:
  74. kfree(error_data);
  75. kfree(error_ecc);
  76. kfree(correct_data);
  77. kfree(correct_ecc);
  78. return err;
  79. }
  80. #else
  81. static int nand_ecc_test(const size_t size)
  82. {
  83. return 0;
  84. }
  85. #endif
  86. static int __init ecc_test_init(void)
  87. {
  88. int err;
  89. err = nand_ecc_test(256);
  90. if (err)
  91. return err;
  92. return nand_ecc_test(512);
  93. }
  94. static void __exit ecc_test_exit(void)
  95. {
  96. }
  97. module_init(ecc_test_init);
  98. module_exit(ecc_test_exit);
  99. MODULE_DESCRIPTION("NAND ECC function test module");
  100. MODULE_AUTHOR("Akinobu Mita");
  101. MODULE_LICENSE("GPL");