1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Glue code for CRC32C optimized for sparc64 crypto opcodes.
4 * This is based largely upon arch/x86/crypto/crc32c-intel.c
6 * Copyright (C) 2008 Intel Corporation
7 * Authors: Austin Zhang <austin_zhang@linux.intel.com>
8 * Kent Liu <kent.liu@intel.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/crc32.h>
19 #include <crypto/internal/hash.h>
21 #include <asm/pstate.h>
27 * Setting the seed allows arbitrary accumulators and flexible XOR policy
28 * If your algorithm starts with ~0, then XOR with ~0 before you set
31 static int crc32c_sparc64_setkey(struct crypto_shash
*hash
, const u8
*key
,
34 u32
*mctx
= crypto_shash_ctx(hash
);
36 if (keylen
!= sizeof(u32
)) {
37 crypto_shash_set_flags(hash
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
40 *(__le32
*)mctx
= le32_to_cpup((__le32
*)key
);
44 static int crc32c_sparc64_init(struct shash_desc
*desc
)
46 u32
*mctx
= crypto_shash_ctx(desc
->tfm
);
47 u32
*crcp
= shash_desc_ctx(desc
);
54 extern void crc32c_sparc64(u32
*crcp
, const u64
*data
, unsigned int len
);
56 static void crc32c_compute(u32
*crcp
, const u64
*data
, unsigned int len
)
62 crc32c_sparc64(crcp
, data
, asm_len
);
67 *crcp
= __crc32c_le(*crcp
, (const unsigned char *) data
, len
);
70 static int crc32c_sparc64_update(struct shash_desc
*desc
, const u8
*data
,
73 u32
*crcp
= shash_desc_ctx(desc
);
75 crc32c_compute(crcp
, (const u64
*) data
, len
);
80 static int __crc32c_sparc64_finup(u32
*crcp
, const u8
*data
, unsigned int len
,
85 crc32c_compute(&tmp
, (const u64
*) data
, len
);
87 *(__le32
*) out
= ~cpu_to_le32(tmp
);
91 static int crc32c_sparc64_finup(struct shash_desc
*desc
, const u8
*data
,
92 unsigned int len
, u8
*out
)
94 return __crc32c_sparc64_finup(shash_desc_ctx(desc
), data
, len
, out
);
97 static int crc32c_sparc64_final(struct shash_desc
*desc
, u8
*out
)
99 u32
*crcp
= shash_desc_ctx(desc
);
101 *(__le32
*) out
= ~cpu_to_le32p(crcp
);
105 static int crc32c_sparc64_digest(struct shash_desc
*desc
, const u8
*data
,
106 unsigned int len
, u8
*out
)
108 return __crc32c_sparc64_finup(crypto_shash_ctx(desc
->tfm
), data
, len
,
112 static int crc32c_sparc64_cra_init(struct crypto_tfm
*tfm
)
114 u32
*key
= crypto_tfm_ctx(tfm
);
121 #define CHKSUM_BLOCK_SIZE 1
122 #define CHKSUM_DIGEST_SIZE 4
124 static struct shash_alg alg
= {
125 .setkey
= crc32c_sparc64_setkey
,
126 .init
= crc32c_sparc64_init
,
127 .update
= crc32c_sparc64_update
,
128 .final
= crc32c_sparc64_final
,
129 .finup
= crc32c_sparc64_finup
,
130 .digest
= crc32c_sparc64_digest
,
131 .descsize
= sizeof(u32
),
132 .digestsize
= CHKSUM_DIGEST_SIZE
,
134 .cra_name
= "crc32c",
135 .cra_driver_name
= "crc32c-sparc64",
136 .cra_priority
= SPARC_CR_OPCODE_PRIORITY
,
137 .cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
138 .cra_blocksize
= CHKSUM_BLOCK_SIZE
,
139 .cra_ctxsize
= sizeof(u32
),
141 .cra_module
= THIS_MODULE
,
142 .cra_init
= crc32c_sparc64_cra_init
,
146 static bool __init
sparc64_has_crc32c_opcode(void)
150 if (!(sparc64_elf_hwcap
& HWCAP_SPARC_CRYPTO
))
153 __asm__
__volatile__("rd %%asr26, %0" : "=r" (cfr
));
154 if (!(cfr
& CFR_CRC32C
))
160 static int __init
crc32c_sparc64_mod_init(void)
162 if (sparc64_has_crc32c_opcode()) {
163 pr_info("Using sparc64 crc32c opcode optimized CRC32C implementation\n");
164 return crypto_register_shash(&alg
);
166 pr_info("sparc64 crc32c opcode not available.\n");
170 static void __exit
crc32c_sparc64_mod_fini(void)
172 crypto_unregister_shash(&alg
);
175 module_init(crc32c_sparc64_mod_init
);
176 module_exit(crc32c_sparc64_mod_fini
);
178 MODULE_LICENSE("GPL");
179 MODULE_DESCRIPTION("CRC32c (Castagnoli), sparc64 crc32c opcode accelerated");
181 MODULE_ALIAS_CRYPTO("crc32c");
183 #include "crop_devid.c"