| 1 | /* |
| 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 | * |
| 9 | * Copyright (C) 2008 Intel Corporation |
| 10 | * Authors: Austin Zhang <austin_zhang@linux.intel.com> |
| 11 | * Kent Liu <kent.liu@intel.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * 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. |
| 25 | * |
| 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> |
| 34 | |
| 35 | #define CHKSUM_BLOCK_SIZE 1 |
| 36 | #define CHKSUM_DIGEST_SIZE 4 |
| 37 | |
| 38 | #define SCALE_F sizeof(unsigned long) |
| 39 | |
| 40 | #ifdef CONFIG_X86_64 |
| 41 | #define REX_PRE "0x48, " |
| 42 | #else |
| 43 | #define REX_PRE |
| 44 | #endif |
| 45 | |
| 46 | static u32 crc32c_intel_le_hw_byte(u32 crc, unsigned char const *data, size_t length) |
| 47 | { |
| 48 | while (length--) { |
| 49 | __asm__ __volatile__( |
| 50 | ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1" |
| 51 | :"=S"(crc) |
| 52 | :"0"(crc), "c"(*data) |
| 53 | ); |
| 54 | data++; |
| 55 | } |
| 56 | |
| 57 | return crc; |
| 58 | } |
| 59 | |
| 60 | static u32 __pure crc32c_intel_le_hw(u32 crc, unsigned char const *p, size_t len) |
| 61 | { |
| 62 | unsigned int iquotient = len / SCALE_F; |
| 63 | unsigned int iremainder = len % SCALE_F; |
| 64 | unsigned long *ptmp = (unsigned long *)p; |
| 65 | |
| 66 | while (iquotient--) { |
| 67 | __asm__ __volatile__( |
| 68 | ".byte 0xf2, " REX_PRE "0xf, 0x38, 0xf1, 0xf1;" |
| 69 | :"=S"(crc) |
| 70 | :"0"(crc), "c"(*ptmp) |
| 71 | ); |
| 72 | ptmp++; |
| 73 | } |
| 74 | |
| 75 | if (iremainder) |
| 76 | crc = crc32c_intel_le_hw_byte(crc, (unsigned char *)ptmp, |
| 77 | iremainder); |
| 78 | |
| 79 | return crc; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Setting the seed allows arbitrary accumulators and flexible XOR policy |
| 84 | * If your algorithm starts with ~0, then XOR with ~0 before you set |
| 85 | * the seed. |
| 86 | */ |
| 87 | static int crc32c_intel_setkey(struct crypto_ahash *hash, const u8 *key, |
| 88 | unsigned int keylen) |
| 89 | { |
| 90 | u32 *mctx = crypto_ahash_ctx(hash); |
| 91 | |
| 92 | if (keylen != sizeof(u32)) { |
| 93 | crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | *mctx = le32_to_cpup((__le32 *)key); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int crc32c_intel_init(struct ahash_request *req) |
| 101 | { |
| 102 | u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
| 103 | u32 *crcp = ahash_request_ctx(req); |
| 104 | |
| 105 | *crcp = *mctx; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int crc32c_intel_update(struct ahash_request *req) |
| 111 | { |
| 112 | struct crypto_hash_walk walk; |
| 113 | u32 *crcp = ahash_request_ctx(req); |
| 114 | u32 crc = *crcp; |
| 115 | int nbytes; |
| 116 | |
| 117 | for (nbytes = crypto_hash_walk_first(req, &walk); nbytes; |
| 118 | nbytes = crypto_hash_walk_done(&walk, 0)) |
| 119 | crc = crc32c_intel_le_hw(crc, walk.data, nbytes); |
| 120 | |
| 121 | *crcp = crc; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int crc32c_intel_final(struct ahash_request *req) |
| 126 | { |
| 127 | u32 *crcp = ahash_request_ctx(req); |
| 128 | |
| 129 | *(__le32 *)req->result = ~cpu_to_le32p(crcp); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int crc32c_intel_digest(struct ahash_request *req) |
| 134 | { |
| 135 | struct crypto_hash_walk walk; |
| 136 | u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
| 137 | u32 crc = *mctx; |
| 138 | int nbytes; |
| 139 | |
| 140 | for (nbytes = crypto_hash_walk_first(req, &walk); nbytes; |
| 141 | nbytes = crypto_hash_walk_done(&walk, 0)) |
| 142 | crc = crc32c_intel_le_hw(crc, walk.data, nbytes); |
| 143 | |
| 144 | *(__le32 *)req->result = ~cpu_to_le32(crc); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int crc32c_intel_cra_init(struct crypto_tfm *tfm) |
| 149 | { |
| 150 | u32 *key = crypto_tfm_ctx(tfm); |
| 151 | |
| 152 | *key = ~0; |
| 153 | |
| 154 | tfm->crt_ahash.reqsize = sizeof(u32); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static struct crypto_alg alg = { |
| 160 | .cra_name = "crc32c", |
| 161 | .cra_driver_name = "crc32c-intel", |
| 162 | .cra_priority = 200, |
| 163 | .cra_flags = CRYPTO_ALG_TYPE_AHASH, |
| 164 | .cra_blocksize = CHKSUM_BLOCK_SIZE, |
| 165 | .cra_alignmask = 3, |
| 166 | .cra_ctxsize = sizeof(u32), |
| 167 | .cra_module = THIS_MODULE, |
| 168 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
| 169 | .cra_init = crc32c_intel_cra_init, |
| 170 | .cra_type = &crypto_ahash_type, |
| 171 | .cra_u = { |
| 172 | .ahash = { |
| 173 | .digestsize = CHKSUM_DIGEST_SIZE, |
| 174 | .setkey = crc32c_intel_setkey, |
| 175 | .init = crc32c_intel_init, |
| 176 | .update = crc32c_intel_update, |
| 177 | .final = crc32c_intel_final, |
| 178 | .digest = crc32c_intel_digest, |
| 179 | } |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | |
| 184 | static int __init crc32c_intel_mod_init(void) |
| 185 | { |
| 186 | if (cpu_has_xmm4_2) |
| 187 | return crypto_register_alg(&alg); |
| 188 | else |
| 189 | return -ENODEV; |
| 190 | } |
| 191 | |
| 192 | static void __exit crc32c_intel_mod_fini(void) |
| 193 | { |
| 194 | crypto_unregister_alg(&alg); |
| 195 | } |
| 196 | |
| 197 | module_init(crc32c_intel_mod_init); |
| 198 | module_exit(crc32c_intel_mod_fini); |
| 199 | |
| 200 | MODULE_AUTHOR("Austin Zhang <austin.zhang@intel.com>, Kent Liu <kent.liu@intel.com>"); |
| 201 | MODULE_DESCRIPTION("CRC32c (Castagnoli) optimization using Intel Hardware."); |
| 202 | MODULE_LICENSE("GPL"); |
| 203 | |
| 204 | MODULE_ALIAS("crc32c"); |
| 205 | MODULE_ALIAS("crc32c-intel"); |