Whamcloud - gitweb
LU-3570 libcfs: accelerate crc32c with pclmulqdq
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-crypto-crc32pclmul.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see http://www.gnu.org/licenses
17  *
18  * Please  visit http://www.xyratex.com/contact if you need additional
19  * information or have any questions.
20  *
21  * GPL HEADER END
22  */
23
24 /*
25  * Copyright 2012 Xyratex Technology Limited
26  *
27  * Wrappers for kernel crypto shash api to pclmulqdq crc32 imlementation.
28  *
29  * Author:     Alexander Boyko <Alexander_Boyko@xyratex.com>
30  */
31 #include <linux/crc32.h>
32 #include <crypto/internal/hash.h>
33 #include <linux/crc32.h>
34 #include <asm/cpufeature.h>
35 #include <asm/i387.h>
36 #include <libcfs/libcfs.h>
37
38 #define CHKSUM_BLOCK_SIZE       1
39 #define CHKSUM_DIGEST_SIZE      4
40
41 #define PCLMUL_MIN_LEN          64L     /* minimum size of buffer
42                                          * for crc32_pclmul_le_16 */
43 #define SCALE_F                 16L     /* size of xmm register */
44 #define SCALE_F_MASK            (SCALE_F - 1)
45
46 u32 crc32_pclmul_le_16(unsigned char const *buffer, size_t len, u32 crc32);
47
48 static u32 __attribute__((pure))
49         crc32_pclmul_le(u32 crc, unsigned char const *p, size_t len)
50 {
51         unsigned int iquotient;
52         unsigned int iremainder;
53         unsigned int prealign;
54
55         if (len < PCLMUL_MIN_LEN + SCALE_F_MASK)
56                 return crc32_le(crc, p, len);
57
58         if ((long)p & SCALE_F_MASK) {
59                 /* align p to 16 byte */
60                 prealign = SCALE_F - ((long)p & SCALE_F_MASK);
61
62                 crc = crc32_le(crc, p, prealign);
63                 len -= prealign;
64                 p = (unsigned char *)(((unsigned long)p + SCALE_F_MASK) &
65                                      ~SCALE_F_MASK);
66         }
67         iquotient = len & (~SCALE_F_MASK);
68         iremainder = len & SCALE_F_MASK;
69
70         kernel_fpu_begin();
71         crc = crc32_pclmul_le_16(p, iquotient, crc);
72         kernel_fpu_end();
73
74         if (iremainder)
75                 crc = crc32_le(crc, p + iquotient, iremainder);
76
77         return crc;
78 }
79
80 static int crc32_pclmul_cra_init(struct crypto_tfm *tfm)
81 {
82         u32 *key = crypto_tfm_ctx(tfm);
83
84         *key = 0;
85
86         return 0;
87 }
88
89 /*
90  * Setting the seed allows arbitrary accumulators and flexible XOR policy
91  * If your algorithm starts with ~0, then XOR with ~0 before you set
92  * the seed.
93  */
94 static int crc32_pclmul_setkey(struct crypto_shash *hash, const u8 *key,
95                         unsigned int keylen)
96 {
97         u32 *mctx = crypto_shash_ctx(hash);
98
99         if (keylen != sizeof(u32)) {
100                 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
101                 return -EINVAL;
102         }
103         *mctx = le32_to_cpup((__le32 *)key);
104         return 0;
105 }
106
107 static int crc32_pclmul_init(struct shash_desc *desc)
108 {
109         u32 *mctx = crypto_shash_ctx(desc->tfm);
110         u32 *crcp = shash_desc_ctx(desc);
111
112         *crcp = *mctx;
113
114         return 0;
115 }
116
117 static int crc32_pclmul_update(struct shash_desc *desc, const u8 *data,
118                                unsigned int len)
119 {
120         u32 *crcp = shash_desc_ctx(desc);
121
122         *crcp = crc32_pclmul_le(*crcp, data, len);
123         return 0;
124 }
125
126 /* No final XOR 0xFFFFFFFF, like crc32_le */
127 static int __crc32_pclmul_finup(u32 *crcp, const u8 *data, unsigned int len,
128                                 u8 *out)
129 {
130         *(__le32 *)out = cpu_to_le32(crc32_pclmul_le(*crcp, data, len));
131         return 0;
132 }
133
134 static int crc32_pclmul_finup(struct shash_desc *desc, const u8 *data,
135                               unsigned int len, u8 *out)
136 {
137         return __crc32_pclmul_finup(shash_desc_ctx(desc), data, len, out);
138 }
139
140 static int crc32_pclmul_final(struct shash_desc *desc, u8 *out)
141 {
142         u32 *crcp = shash_desc_ctx(desc);
143
144         *(__le32 *)out = cpu_to_le32p(crcp);
145         return 0;
146 }
147
148 static int crc32_pclmul_digest(struct shash_desc *desc, const u8 *data,
149                                unsigned int len, u8 *out)
150 {
151         return __crc32_pclmul_finup(crypto_shash_ctx(desc->tfm), data, len,
152                                     out);
153 }
154
155 static struct shash_alg alg = {
156         .setkey         = crc32_pclmul_setkey,
157         .init           = crc32_pclmul_init,
158         .update         = crc32_pclmul_update,
159         .final          = crc32_pclmul_final,
160         .finup          = crc32_pclmul_finup,
161         .digest         = crc32_pclmul_digest,
162         .descsize       = sizeof(u32),
163         .digestsize     = CHKSUM_DIGEST_SIZE,
164         .base           = {
165                         .cra_name               = "crc32",
166                         .cra_driver_name        = "crc32-pclmul",
167                         .cra_priority           = 200,
168                         .cra_blocksize          = CHKSUM_BLOCK_SIZE,
169                         .cra_ctxsize            = sizeof(u32),
170                         .cra_module             = THIS_MODULE,
171                         .cra_init               = crc32_pclmul_cra_init,
172         }
173 };
174
175 #ifndef X86_FEATURE_PCLMULQDQ
176 #define X86_FEATURE_PCLMULQDQ   (4*32+1)        /* PCLMULQDQ instruction */
177 #endif
178
179 int cfs_crypto_crc32_pclmul_register(void)
180 {
181         if (!boot_cpu_has(X86_FEATURE_PCLMULQDQ)) {
182                 CDEBUG(D_INFO, "PCLMULQDQ-NI instructions are not "
183                        "detected.\n");
184                 return -ENODEV;
185         }
186         return crypto_register_shash(&alg);
187 }
188
189 void cfs_crypto_crc32_pclmul_unregister(void)
190 {
191         crypto_unregister_shash(&alg);
192 }