2 * Accelerated CRC-T10DIF using arm64 NEON and Crypto Extensions instructions
4 * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/cpufeature.h>
12 #include <linux/crc-t10dif.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
18 #include <crypto/internal/hash.h>
23 #define CRC_T10DIF_PMULL_CHUNK_SIZE 16U
25 asmlinkage u16 crc_t10dif_pmull_p8(u16 init_crc, const u8 *buf, size_t len);
26 asmlinkage u16 crc_t10dif_pmull_p64(u16 init_crc, const u8 *buf, size_t len);
28 static int crct10dif_init(struct shash_desc *desc)
30 u16 *crc = shash_desc_ctx(desc);
36 static int crct10dif_update_pmull_p8(struct shash_desc *desc, const u8 *data,
39 u16 *crc = shash_desc_ctx(desc);
41 if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && may_use_simd()) {
43 *crc = crc_t10dif_pmull_p8(*crc, data, length);
46 *crc = crc_t10dif_generic(*crc, data, length);
52 static int crct10dif_update_pmull_p64(struct shash_desc *desc, const u8 *data,
55 u16 *crc = shash_desc_ctx(desc);
57 if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && may_use_simd()) {
59 *crc = crc_t10dif_pmull_p64(*crc, data, length);
62 *crc = crc_t10dif_generic(*crc, data, length);
68 static int crct10dif_final(struct shash_desc *desc, u8 *out)
70 u16 *crc = shash_desc_ctx(desc);
76 static struct shash_alg crc_t10dif_alg[] = {{
77 .digestsize = CRC_T10DIF_DIGEST_SIZE,
78 .init = crct10dif_init,
79 .update = crct10dif_update_pmull_p8,
80 .final = crct10dif_final,
81 .descsize = CRC_T10DIF_DIGEST_SIZE,
83 .base.cra_name = "crct10dif",
84 .base.cra_driver_name = "crct10dif-arm64-neon",
85 .base.cra_priority = 100,
86 .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
87 .base.cra_module = THIS_MODULE,
89 .digestsize = CRC_T10DIF_DIGEST_SIZE,
90 .init = crct10dif_init,
91 .update = crct10dif_update_pmull_p64,
92 .final = crct10dif_final,
93 .descsize = CRC_T10DIF_DIGEST_SIZE,
95 .base.cra_name = "crct10dif",
96 .base.cra_driver_name = "crct10dif-arm64-ce",
97 .base.cra_priority = 200,
98 .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
99 .base.cra_module = THIS_MODULE,
102 static int __init crc_t10dif_mod_init(void)
104 if (elf_hwcap & HWCAP_PMULL)
105 return crypto_register_shashes(crc_t10dif_alg,
106 ARRAY_SIZE(crc_t10dif_alg));
108 /* only register the first array element */
109 return crypto_register_shash(crc_t10dif_alg);
112 static void __exit crc_t10dif_mod_exit(void)
114 if (elf_hwcap & HWCAP_PMULL)
115 crypto_unregister_shashes(crc_t10dif_alg,
116 ARRAY_SIZE(crc_t10dif_alg));
118 crypto_unregister_shash(crc_t10dif_alg);
121 module_cpu_feature_match(ASIMD, crc_t10dif_mod_init);
122 module_exit(crc_t10dif_mod_exit);
124 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
125 MODULE_LICENSE("GPL v2");
126 MODULE_ALIAS_CRYPTO("crct10dif");
127 MODULE_ALIAS_CRYPTO("crct10dif-arm64-ce");