blob: 493f959261f7274eaf69729a7add9a2664d475fd [file] [log] [blame]
Austin Zhang8cb51ba2008-08-07 09:57:03 +08001/*
2 * Using hardware provided CRC32 instruction to accelerate the CRC32 disposal.
3 * CRC32C polynomial:0x1EDC6F41(BE)/0x82F63B78(LE)
4 * CRC32 is a new instruction in Intel SSE4.2, the reference can be found at:
5 * http://www.intel.com/products/processor/manuals/
6 * Intel(R) 64 and IA-32 Architectures Software Developer's Manual
7 * Volume 2A: Instruction Set Reference, A-M
8 *
Kent Liu1c06da82008-10-31 16:52:58 +08009 * Copyright (C) 2008 Intel Corporation
10 * Authors: Austin Zhang <austin_zhang@linux.intel.com>
11 * Kent Liu <kent.liu@intel.com>
Austin Zhang8cb51ba2008-08-07 09:57:03 +080012 *
13 * This program is free software; you can redistribute it and/or modify it
Kent Liu1c06da82008-10-31 16:52:58 +080014 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Austin Zhang8cb51ba2008-08-07 09:57:03 +080025 *
26 */
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/string.h>
30#include <linux/kernel.h>
31#include <crypto/internal/hash.h>
32
33#include <asm/cpufeature.h>
Andi Kleen3bd391f2012-01-26 00:09:06 +010034#include <asm/cpu_device_id.h>
Austin Zhang8cb51ba2008-08-07 09:57:03 +080035
36#define CHKSUM_BLOCK_SIZE 1
37#define CHKSUM_DIGEST_SIZE 4
38
39#define SCALE_F sizeof(unsigned long)
40
41#ifdef CONFIG_X86_64
42#define REX_PRE "0x48, "
43#else
44#define REX_PRE
45#endif
46
47static u32 crc32c_intel_le_hw_byte(u32 crc, unsigned char const *data, size_t length)
48{
49 while (length--) {
50 __asm__ __volatile__(
51 ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
52 :"=S"(crc)
53 :"0"(crc), "c"(*data)
54 );
55 data++;
56 }
57
58 return crc;
59}
60
61static u32 __pure crc32c_intel_le_hw(u32 crc, unsigned char const *p, size_t len)
62{
63 unsigned int iquotient = len / SCALE_F;
64 unsigned int iremainder = len % SCALE_F;
65 unsigned long *ptmp = (unsigned long *)p;
66
67 while (iquotient--) {
68 __asm__ __volatile__(
69 ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;"
70 :"=S"(crc)
71 :"0"(crc), "c"(*ptmp)
72 );
73 ptmp++;
74 }
75
76 if (iremainder)
77 crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp,
78 iremainder);
79
80 return crc;
81}
82
83/*
84 * Setting the seed allows arbitrary accumulators and flexible XOR policy
85 * If your algorithm starts with ~0, then XOR with ~0 before you set
86 * the seed.
87 */
Herbert Xub7e8bda2008-11-06 16:56:41 +080088static int crc32c_intel_setkey(struct crypto_shash *hash, const u8 *key,
Austin Zhang8cb51ba2008-08-07 09:57:03 +080089 unsigned int keylen)
90{
Herbert Xub7e8bda2008-11-06 16:56:41 +080091 u32 *mctx = crypto_shash_ctx(hash);
Austin Zhang8cb51ba2008-08-07 09:57:03 +080092
93 if (keylen != sizeof(u32)) {
Herbert Xub7e8bda2008-11-06 16:56:41 +080094 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
Austin Zhang8cb51ba2008-08-07 09:57:03 +080095 return -EINVAL;
96 }
97 *mctx = le32_to_cpup((__le32 *)key);
98 return 0;
99}
100
Herbert Xub7e8bda2008-11-06 16:56:41 +0800101static int crc32c_intel_init(struct shash_desc *desc)
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800102{
Herbert Xub7e8bda2008-11-06 16:56:41 +0800103 u32 *mctx = crypto_shash_ctx(desc->tfm);
104 u32 *crcp = shash_desc_ctx(desc);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800105
106 *crcp = *mctx;
107
108 return 0;
109}
110
Herbert Xub7e8bda2008-11-06 16:56:41 +0800111static int crc32c_intel_update(struct shash_desc *desc, const u8 *data,
112 unsigned int len)
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800113{
Herbert Xub7e8bda2008-11-06 16:56:41 +0800114 u32 *crcp = shash_desc_ctx(desc);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800115
Herbert Xub7e8bda2008-11-06 16:56:41 +0800116 *crcp = crc32c_intel_le_hw(*crcp, data, len);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800117 return 0;
118}
119
Herbert Xub7e8bda2008-11-06 16:56:41 +0800120static int __crc32c_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
121 u8 *out)
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800122{
Herbert Xub7e8bda2008-11-06 16:56:41 +0800123 *(__le32 *)out = ~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800124 return 0;
125}
126
Herbert Xub7e8bda2008-11-06 16:56:41 +0800127static int crc32c_intel_finup(struct shash_desc *desc, const u8 *data,
128 unsigned int len, u8 *out)
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800129{
Herbert Xub7e8bda2008-11-06 16:56:41 +0800130 return __crc32c_intel_finup(shash_desc_ctx(desc), data, len, out);
131}
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800132
Herbert Xub7e8bda2008-11-06 16:56:41 +0800133static int crc32c_intel_final(struct shash_desc *desc, u8 *out)
134{
135 u32 *crcp = shash_desc_ctx(desc);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800136
Herbert Xub7e8bda2008-11-06 16:56:41 +0800137 *(__le32 *)out = ~cpu_to_le32p(crcp);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800138 return 0;
139}
140
Herbert Xub7e8bda2008-11-06 16:56:41 +0800141static int crc32c_intel_digest(struct shash_desc *desc, const u8 *data,
142 unsigned int len, u8 *out)
143{
144 return __crc32c_intel_finup(crypto_shash_ctx(desc->tfm), data, len,
145 out);
146}
147
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800148static int crc32c_intel_cra_init(struct crypto_tfm *tfm)
149{
150 u32 *key = crypto_tfm_ctx(tfm);
151
152 *key = ~0;
153
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800154 return 0;
155}
156
Herbert Xub7e8bda2008-11-06 16:56:41 +0800157static struct shash_alg alg = {
158 .setkey = crc32c_intel_setkey,
159 .init = crc32c_intel_init,
160 .update = crc32c_intel_update,
161 .final = crc32c_intel_final,
162 .finup = crc32c_intel_finup,
163 .digest = crc32c_intel_digest,
164 .descsize = sizeof(u32),
165 .digestsize = CHKSUM_DIGEST_SIZE,
166 .base = {
167 .cra_name = "crc32c",
168 .cra_driver_name = "crc32c-intel",
169 .cra_priority = 200,
170 .cra_blocksize = CHKSUM_BLOCK_SIZE,
171 .cra_ctxsize = sizeof(u32),
172 .cra_module = THIS_MODULE,
173 .cra_init = crc32c_intel_cra_init,
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800174 }
175};
176
Andi Kleen3bd391f2012-01-26 00:09:06 +0100177static const struct x86_cpu_id crc32c_cpu_id[] = {
178 X86_FEATURE_MATCH(X86_FEATURE_XMM4_2),
179 {}
180};
181MODULE_DEVICE_TABLE(x86cpu, crc32c_cpu_id);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800182
183static int __init crc32c_intel_mod_init(void)
184{
Andi Kleen3bd391f2012-01-26 00:09:06 +0100185 if (!x86_match_cpu(crc32c_cpu_id))
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800186 return -ENODEV;
Andi Kleen3bd391f2012-01-26 00:09:06 +0100187 return crypto_register_shash(&alg);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800188}
189
190static void __exit crc32c_intel_mod_fini(void)
191{
Herbert Xub7e8bda2008-11-06 16:56:41 +0800192 crypto_unregister_shash(&alg);
Austin Zhang8cb51ba2008-08-07 09:57:03 +0800193}
194
195module_init(crc32c_intel_mod_init);
196module_exit(crc32c_intel_mod_fini);
197
198MODULE_AUTHOR("Austin Zhang <austin.zhang@intel.com>, Kent Liu <kent.liu@intel.com>");
199MODULE_DESCRIPTION("CRC32c (Castagnoli) optimization using Intel Hardware.");
200MODULE_LICENSE("GPL");
201
202MODULE_ALIAS("crc32c");
203MODULE_ALIAS("crc32c-intel");