1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/types.h>
26#include <linux/module.h>
27#include <linux/crc-t10dif.h>
28#include <crypto/internal/hash.h>
29#include <crypto/internal/simd.h>
30#include <linux/init.h>
31#include <linux/string.h>
32#include <linux/kernel.h>
33#include <asm/cpufeatures.h>
34#include <asm/cpu_device_id.h>
35#include <asm/simd.h>
36
37asmlinkage u16 crc_t10dif_pcl(u16 init_crc, const u8 *buf, size_t len);
38
39struct chksum_desc_ctx {
40 __u16 crc;
41};
42
43static int chksum_init(struct shash_desc *desc)
44{
45 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
46
47 ctx->crc = 0;
48
49 return 0;
50}
51
52static int chksum_update(struct shash_desc *desc, const u8 *data,
53 unsigned int length)
54{
55 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
56
57 if (length >= 16 && crypto_simd_usable()) {
58 kernel_fpu_begin();
59 ctx->crc = crc_t10dif_pcl(ctx->crc, data, length);
60 kernel_fpu_end();
61 } else
62 ctx->crc = crc_t10dif_generic(ctx->crc, data, length);
63 return 0;
64}
65
66static int chksum_final(struct shash_desc *desc, u8 *out)
67{
68 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
69
70 *(__u16 *)out = ctx->crc;
71 return 0;
72}
73
74static int __chksum_finup(__u16 crc, const u8 *data, unsigned int len, u8 *out)
75{
76 if (len >= 16 && crypto_simd_usable()) {
77 kernel_fpu_begin();
78 *(__u16 *)out = crc_t10dif_pcl(crc, data, len);
79 kernel_fpu_end();
80 } else
81 *(__u16 *)out = crc_t10dif_generic(crc, data, len);
82 return 0;
83}
84
85static int chksum_finup(struct shash_desc *desc, const u8 *data,
86 unsigned int len, u8 *out)
87{
88 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
89
90 return __chksum_finup(ctx->crc, data, len, out);
91}
92
93static int chksum_digest(struct shash_desc *desc, const u8 *data,
94 unsigned int length, u8 *out)
95{
96 return __chksum_finup(0, data, length, out);
97}
98
99static struct shash_alg alg = {
100 .digestsize = CRC_T10DIF_DIGEST_SIZE,
101 .init = chksum_init,
102 .update = chksum_update,
103 .final = chksum_final,
104 .finup = chksum_finup,
105 .digest = chksum_digest,
106 .descsize = sizeof(struct chksum_desc_ctx),
107 .base = {
108 .cra_name = "crct10dif",
109 .cra_driver_name = "crct10dif-pclmul",
110 .cra_priority = 200,
111 .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
112 .cra_module = THIS_MODULE,
113 }
114};
115
116static const struct x86_cpu_id crct10dif_cpu_id[] = {
117 X86_MATCH_FEATURE(X86_FEATURE_PCLMULQDQ, NULL),
118 {}
119};
120MODULE_DEVICE_TABLE(x86cpu, crct10dif_cpu_id);
121
122static int __init crct10dif_intel_mod_init(void)
123{
124 if (!x86_match_cpu(crct10dif_cpu_id))
125 return -ENODEV;
126
127 return crypto_register_shash(&alg);
128}
129
130static void __exit crct10dif_intel_mod_fini(void)
131{
132 crypto_unregister_shash(&alg);
133}
134
135module_init(crct10dif_intel_mod_init);
136module_exit(crct10dif_intel_mod_fini);
137
138MODULE_AUTHOR("Tim Chen <tim.c.chen@linux.intel.com>");
139MODULE_DESCRIPTION("T10 DIF CRC calculation accelerated with PCLMULQDQ.");
140MODULE_LICENSE("GPL");
141
142MODULE_ALIAS_CRYPTO("crct10dif");
143MODULE_ALIAS_CRYPTO("crct10dif-pclmul");
144