1 /* Glue code for CRC32C optimized for sparc64 crypto opcodes. 2 * 3 * This is based largely upon arch/x86/crypto/crc32c-intel.c 4 * 5 * Copyright (C) 2008 Intel Corporation 6 * Authors: Austin Zhang <austin_zhang@linux.intel.com> 7 * Kent Liu <kent.liu@intel.com> 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/string.h> 15 #include <linux/kernel.h> 16 #include <linux/crc32.h> 17 18 #include <crypto/internal/hash.h> 19 20 #include <asm/pstate.h> 21 #include <asm/elf.h> 22 23 #include "opcodes.h" 24 25 /* 26 * Setting the seed allows arbitrary accumulators and flexible XOR policy 27 * If your algorithm starts with ~0, then XOR with ~0 before you set 28 * the seed. 29 */ 30 static int crc32c_sparc64_setkey(struct crypto_shash *hash, const u8 *key, 31 unsigned int keylen) 32 { 33 u32 *mctx = crypto_shash_ctx(hash); 34 35 if (keylen != sizeof(u32)) { 36 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN); 37 return -EINVAL; 38 } 39 *(__le32 *)mctx = le32_to_cpup((__le32 *)key); 40 return 0; 41 } 42 43 static int crc32c_sparc64_init(struct shash_desc *desc) 44 { 45 u32 *mctx = crypto_shash_ctx(desc->tfm); 46 u32 *crcp = shash_desc_ctx(desc); 47 48 *crcp = *mctx; 49 50 return 0; 51 } 52 53 extern void crc32c_sparc64(u32 *crcp, const u64 *data, unsigned int len); 54 55 static void crc32c_compute(u32 *crcp, const u64 *data, unsigned int len) 56 { 57 unsigned int asm_len; 58 59 asm_len = len & ~7U; 60 if (asm_len) { 61 crc32c_sparc64(crcp, data, asm_len); 62 data += asm_len / 8; 63 len -= asm_len; 64 } 65 if (len) 66 *crcp = __crc32c_le(*crcp, (const unsigned char *) data, len); 67 } 68 69 static int crc32c_sparc64_update(struct shash_desc *desc, const u8 *data, 70 unsigned int len) 71 { 72 u32 *crcp = shash_desc_ctx(desc); 73 74 crc32c_compute(crcp, (const u64 *) data, len); 75 76 return 0; 77 } 78 79 static int __crc32c_sparc64_finup(u32 *crcp, const u8 *data, unsigned int len, 80 u8 *out) 81 { 82 u32 tmp = *crcp; 83 84 crc32c_compute(&tmp, (const u64 *) data, len); 85 86 *(__le32 *) out = ~cpu_to_le32(tmp); 87 return 0; 88 } 89 90 static int crc32c_sparc64_finup(struct shash_desc *desc, const u8 *data, 91 unsigned int len, u8 *out) 92 { 93 return __crc32c_sparc64_finup(shash_desc_ctx(desc), data, len, out); 94 } 95 96 static int crc32c_sparc64_final(struct shash_desc *desc, u8 *out) 97 { 98 u32 *crcp = shash_desc_ctx(desc); 99 100 *(__le32 *) out = ~cpu_to_le32p(crcp); 101 return 0; 102 } 103 104 static int crc32c_sparc64_digest(struct shash_desc *desc, const u8 *data, 105 unsigned int len, u8 *out) 106 { 107 return __crc32c_sparc64_finup(crypto_shash_ctx(desc->tfm), data, len, 108 out); 109 } 110 111 static int crc32c_sparc64_cra_init(struct crypto_tfm *tfm) 112 { 113 u32 *key = crypto_tfm_ctx(tfm); 114 115 *key = ~0; 116 117 return 0; 118 } 119 120 #define CHKSUM_BLOCK_SIZE 1 121 #define CHKSUM_DIGEST_SIZE 4 122 123 static struct shash_alg alg = { 124 .setkey = crc32c_sparc64_setkey, 125 .init = crc32c_sparc64_init, 126 .update = crc32c_sparc64_update, 127 .final = crc32c_sparc64_final, 128 .finup = crc32c_sparc64_finup, 129 .digest = crc32c_sparc64_digest, 130 .descsize = sizeof(u32), 131 .digestsize = CHKSUM_DIGEST_SIZE, 132 .base = { 133 .cra_name = "crc32c", 134 .cra_driver_name = "crc32c-sparc64", 135 .cra_priority = SPARC_CR_OPCODE_PRIORITY, 136 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 137 .cra_blocksize = CHKSUM_BLOCK_SIZE, 138 .cra_ctxsize = sizeof(u32), 139 .cra_alignmask = 7, 140 .cra_module = THIS_MODULE, 141 .cra_init = crc32c_sparc64_cra_init, 142 } 143 }; 144 145 static bool __init sparc64_has_crc32c_opcode(void) 146 { 147 unsigned long cfr; 148 149 if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO)) 150 return false; 151 152 __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr)); 153 if (!(cfr & CFR_CRC32C)) 154 return false; 155 156 return true; 157 } 158 159 static int __init crc32c_sparc64_mod_init(void) 160 { 161 if (sparc64_has_crc32c_opcode()) { 162 pr_info("Using sparc64 crc32c opcode optimized CRC32C implementation\n"); 163 return crypto_register_shash(&alg); 164 } 165 pr_info("sparc64 crc32c opcode not available.\n"); 166 return -ENODEV; 167 } 168 169 static void __exit crc32c_sparc64_mod_fini(void) 170 { 171 crypto_unregister_shash(&alg); 172 } 173 174 module_init(crc32c_sparc64_mod_init); 175 module_exit(crc32c_sparc64_mod_fini); 176 177 MODULE_LICENSE("GPL"); 178 MODULE_DESCRIPTION("CRC32c (Castagnoli), sparc64 crc32c opcode accelerated"); 179 180 MODULE_ALIAS_CRYPTO("crc32c"); 181 182 #include "crop_devid.c" 183