0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/init.h>
0014 #include <linux/module.h>
0015 #include <linux/string.h>
0016 #include <linux/kernel.h>
0017 #include <linux/crc32.h>
0018
0019 #include <crypto/internal/hash.h>
0020
0021 #include <asm/pstate.h>
0022 #include <asm/elf.h>
0023
0024 #include "opcodes.h"
0025
0026
0027
0028
0029
0030
0031 static int crc32c_sparc64_setkey(struct crypto_shash *hash, const u8 *key,
0032 unsigned int keylen)
0033 {
0034 u32 *mctx = crypto_shash_ctx(hash);
0035
0036 if (keylen != sizeof(u32))
0037 return -EINVAL;
0038 *mctx = le32_to_cpup((__le32 *)key);
0039 return 0;
0040 }
0041
0042 static int crc32c_sparc64_init(struct shash_desc *desc)
0043 {
0044 u32 *mctx = crypto_shash_ctx(desc->tfm);
0045 u32 *crcp = shash_desc_ctx(desc);
0046
0047 *crcp = *mctx;
0048
0049 return 0;
0050 }
0051
0052 extern void crc32c_sparc64(u32 *crcp, const u64 *data, unsigned int len);
0053
0054 static void crc32c_compute(u32 *crcp, const u64 *data, unsigned int len)
0055 {
0056 unsigned int asm_len;
0057
0058 asm_len = len & ~7U;
0059 if (asm_len) {
0060 crc32c_sparc64(crcp, data, asm_len);
0061 data += asm_len / 8;
0062 len -= asm_len;
0063 }
0064 if (len)
0065 *crcp = __crc32c_le(*crcp, (const unsigned char *) data, len);
0066 }
0067
0068 static int crc32c_sparc64_update(struct shash_desc *desc, const u8 *data,
0069 unsigned int len)
0070 {
0071 u32 *crcp = shash_desc_ctx(desc);
0072
0073 crc32c_compute(crcp, (const u64 *) data, len);
0074
0075 return 0;
0076 }
0077
0078 static int __crc32c_sparc64_finup(u32 *crcp, const u8 *data, unsigned int len,
0079 u8 *out)
0080 {
0081 u32 tmp = *crcp;
0082
0083 crc32c_compute(&tmp, (const u64 *) data, len);
0084
0085 *(__le32 *) out = ~cpu_to_le32(tmp);
0086 return 0;
0087 }
0088
0089 static int crc32c_sparc64_finup(struct shash_desc *desc, const u8 *data,
0090 unsigned int len, u8 *out)
0091 {
0092 return __crc32c_sparc64_finup(shash_desc_ctx(desc), data, len, out);
0093 }
0094
0095 static int crc32c_sparc64_final(struct shash_desc *desc, u8 *out)
0096 {
0097 u32 *crcp = shash_desc_ctx(desc);
0098
0099 *(__le32 *) out = ~cpu_to_le32p(crcp);
0100 return 0;
0101 }
0102
0103 static int crc32c_sparc64_digest(struct shash_desc *desc, const u8 *data,
0104 unsigned int len, u8 *out)
0105 {
0106 return __crc32c_sparc64_finup(crypto_shash_ctx(desc->tfm), data, len,
0107 out);
0108 }
0109
0110 static int crc32c_sparc64_cra_init(struct crypto_tfm *tfm)
0111 {
0112 u32 *key = crypto_tfm_ctx(tfm);
0113
0114 *key = ~0;
0115
0116 return 0;
0117 }
0118
0119 #define CHKSUM_BLOCK_SIZE 1
0120 #define CHKSUM_DIGEST_SIZE 4
0121
0122 static struct shash_alg alg = {
0123 .setkey = crc32c_sparc64_setkey,
0124 .init = crc32c_sparc64_init,
0125 .update = crc32c_sparc64_update,
0126 .final = crc32c_sparc64_final,
0127 .finup = crc32c_sparc64_finup,
0128 .digest = crc32c_sparc64_digest,
0129 .descsize = sizeof(u32),
0130 .digestsize = CHKSUM_DIGEST_SIZE,
0131 .base = {
0132 .cra_name = "crc32c",
0133 .cra_driver_name = "crc32c-sparc64",
0134 .cra_priority = SPARC_CR_OPCODE_PRIORITY,
0135 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
0136 .cra_blocksize = CHKSUM_BLOCK_SIZE,
0137 .cra_ctxsize = sizeof(u32),
0138 .cra_alignmask = 7,
0139 .cra_module = THIS_MODULE,
0140 .cra_init = crc32c_sparc64_cra_init,
0141 }
0142 };
0143
0144 static bool __init sparc64_has_crc32c_opcode(void)
0145 {
0146 unsigned long cfr;
0147
0148 if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
0149 return false;
0150
0151 __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
0152 if (!(cfr & CFR_CRC32C))
0153 return false;
0154
0155 return true;
0156 }
0157
0158 static int __init crc32c_sparc64_mod_init(void)
0159 {
0160 if (sparc64_has_crc32c_opcode()) {
0161 pr_info("Using sparc64 crc32c opcode optimized CRC32C implementation\n");
0162 return crypto_register_shash(&alg);
0163 }
0164 pr_info("sparc64 crc32c opcode not available.\n");
0165 return -ENODEV;
0166 }
0167
0168 static void __exit crc32c_sparc64_mod_fini(void)
0169 {
0170 crypto_unregister_shash(&alg);
0171 }
0172
0173 module_init(crc32c_sparc64_mod_init);
0174 module_exit(crc32c_sparc64_mod_fini);
0175
0176 MODULE_LICENSE("GPL");
0177 MODULE_DESCRIPTION("CRC32c (Castagnoli), sparc64 crc32c opcode accelerated");
0178
0179 MODULE_ALIAS_CRYPTO("crc32c");
0180
0181 #include "crop_devid.c"