0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <linux/types.h>
0026 #include <linux/module.h>
0027 #include <linux/crc-t10dif.h>
0028 #include <crypto/internal/hash.h>
0029 #include <crypto/internal/simd.h>
0030 #include <linux/init.h>
0031 #include <linux/string.h>
0032 #include <linux/kernel.h>
0033 #include <asm/cpufeatures.h>
0034 #include <asm/cpu_device_id.h>
0035 #include <asm/simd.h>
0036
0037 asmlinkage u16 crc_t10dif_pcl(u16 init_crc, const u8 *buf, size_t len);
0038
0039 struct chksum_desc_ctx {
0040 __u16 crc;
0041 };
0042
0043 static int chksum_init(struct shash_desc *desc)
0044 {
0045 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0046
0047 ctx->crc = 0;
0048
0049 return 0;
0050 }
0051
0052 static int chksum_update(struct shash_desc *desc, const u8 *data,
0053 unsigned int length)
0054 {
0055 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0056
0057 if (length >= 16 && crypto_simd_usable()) {
0058 kernel_fpu_begin();
0059 ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
0060 kernel_fpu_end();
0061 } else
0062 ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
0063 return 0;
0064 }
0065
0066 static int chksum_final(struct shash_desc *desc, u8 *out)
0067 {
0068 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0069
0070 *(__u16 *)out = ctx->crc;
0071 return 0;
0072 }
0073
0074 static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
0075 {
0076 if (len >= 16 && crypto_simd_usable()) {
0077 kernel_fpu_begin();
0078 *(__u16 *)out = crc_t10dif_pcl(crc, data, len);
0079 kernel_fpu_end();
0080 } else
0081 *(__u16 *)out = crc_t10dif_generic(crc, data, len);
0082 return 0;
0083 }
0084
0085 static int chksum_finup(struct shash_desc *desc, const u8 *data,
0086 unsigned int len, u8 *out)
0087 {
0088 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
0089
0090 return __chksum_finup(ctx->crc, data, len, out);
0091 }
0092
0093 static int chksum_digest(struct shash_desc *desc, const u8 *data,
0094 unsigned int length, u8 *out)
0095 {
0096 return __chksum_finup(0, data, length, out);
0097 }
0098
0099 static struct shash_alg alg = {
0100 .digestsize = CRC_T10DIF_DIGEST_SIZE,
0101 .init = chksum_init,
0102 .update = chksum_update,
0103 .final = chksum_final,
0104 .finup = chksum_finup,
0105 .digest = chksum_digest,
0106 .descsize = sizeof(struct chksum_desc_ctx),
0107 .base = {
0108 .cra_name = "crct10dif",
0109 .cra_driver_name = "crct10dif-pclmul",
0110 .cra_priority = 200,
0111 .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
0112 .cra_module = THIS_MODULE,
0113 }
0114 };
0115
0116 static const struct x86_cpu_id crct10dif_cpu_id[] = {
0117 X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
0118 {}
0119 };
0120 MODULE_DEVICE_TABLE(x86cpu, crct10dif_cpu_id);
0121
0122 static int __init crct10dif_intel_mod_init(void)
0123 {
0124 if (!x86_match_cpu(crct10dif_cpu_id))
0125 return -ENODEV;
0126
0127 return crypto_register_shash(&alg);
0128 }
0129
0130 static void __exit crct10dif_intel_mod_fini(void)
0131 {
0132 crypto_unregister_shash(&alg);
0133 }
0134
0135 module_init(crct10dif_intel_mod_init);
0136 module_exit(crct10dif_intel_mod_fini);
0137
0138 MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
0139 MODULE_DESCRIPTION("T10 DIF CRC calculation accelerated with PCLMULQDQ.");
0140 MODULE_LICENSE("GPL");
0141
0142 MODULE_ALIAS_CRYPTO("crct10dif");
0143 MODULE_ALIAS_CRYPTO("crct10dif-pclmul");