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