^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Accelerated CRC-T10DIF using arm64 NEON and Crypto Extensions instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/crc-t10dif.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <crypto/internal/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/neon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/simd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define CRC_T10DIF_PMULL_CHUNK_SIZE 16U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) asmlinkage u16 crc_t10dif_pmull_p8(u16 init_crc, const u8 *buf, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) asmlinkage u16 crc_t10dif_pmull_p64(u16 init_crc, const u8 *buf, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int crct10dif_init(struct shash_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u16 *crc = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^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) static int crct10dif_update_pmull_p8(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u16 *crc = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && crypto_simd_usable()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int chunk = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (chunk > SZ_4K + CRC_T10DIF_PMULL_CHUNK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) chunk = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) kernel_neon_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *crc = crc_t10dif_pmull_p8(*crc, data, chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) kernel_neon_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) data += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) length -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) } while (length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *crc = crc_t10dif_generic(*crc, data, length);
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int crct10dif_update_pmull_p64(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u16 *crc = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && crypto_simd_usable()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int chunk = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (chunk > SZ_4K + CRC_T10DIF_PMULL_CHUNK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) chunk = SZ_4K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kernel_neon_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *crc = crc_t10dif_pmull_p64(*crc, data, chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kernel_neon_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) data += chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) length -= chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) } while (length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *crc = crc_t10dif_generic(*crc, data, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int crct10dif_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) u16 *crc = shash_desc_ctx(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *(u16 *)out = *crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static struct shash_alg crc_t10dif_alg[] = {{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .digestsize = CRC_T10DIF_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .init = crct10dif_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .update = crct10dif_update_pmull_p8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .final = crct10dif_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .descsize = CRC_T10DIF_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .base.cra_name = "crct10dif",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .base.cra_driver_name = "crct10dif-arm64-neon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .base.cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .digestsize = CRC_T10DIF_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .init = crct10dif_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .update = crct10dif_update_pmull_p64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .final = crct10dif_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .descsize = CRC_T10DIF_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .base.cra_name = "crct10dif",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .base.cra_driver_name = "crct10dif-arm64-ce",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .base.cra_priority = 200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int __init crc_t10dif_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (cpu_have_named_feature(PMULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return crypto_register_shashes(crc_t10dif_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ARRAY_SIZE(crc_t10dif_alg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* only register the first array element */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return crypto_register_shash(crc_t10dif_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void __exit crc_t10dif_mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (cpu_have_named_feature(PMULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) crypto_unregister_shashes(crc_t10dif_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ARRAY_SIZE(crc_t10dif_alg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) crypto_unregister_shash(crc_t10dif_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) module_cpu_feature_match(ASIMD, crc_t10dif_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) module_exit(crc_t10dif_mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) MODULE_ALIAS_CRYPTO("crct10dif");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) MODULE_ALIAS_CRYPTO("crct10dif-arm64-ce");