blob: 617bcfc1b0804e6e543c14b7da08ac4f54028801 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Accelerated CRC-T10DIF using arm64 NEON and Crypto Extensions instructions
3 *
4 * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/cpufeature.h>
12#include <linux/crc-t10dif.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/string.h>
17
18#include <crypto/internal/hash.h>
19
20#include <asm/neon.h>
21#include <asm/simd.h>
22
23#define CRC_T10DIF_PMULL_CHUNK_SIZE 16U
24
25asmlinkage u16 crc_t10dif_pmull(u16 init_crc, const u8 buf[], u64 len);
26
27static int crct10dif_init(struct shash_desc *desc)
28{
29 u16 *crc = shash_desc_ctx(desc);
30
31 *crc = 0;
32 return 0;
33}
34
35static int crct10dif_update(struct shash_desc *desc, const u8 *data,
36 unsigned int length)
37{
38 u16 *crc = shash_desc_ctx(desc);
39
40 if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && may_use_simd()) {
41 kernel_neon_begin();
42 *crc = crc_t10dif_pmull(*crc, data, length);
43 kernel_neon_end();
44 } else {
45 *crc = crc_t10dif_generic(*crc, data, length);
46 }
47
48 return 0;
49}
50
51static int crct10dif_final(struct shash_desc *desc, u8 *out)
52{
53 u16 *crc = shash_desc_ctx(desc);
54
55 *(u16 *)out = *crc;
56 return 0;
57}
58
59static struct shash_alg crc_t10dif_alg = {
60 .digestsize = CRC_T10DIF_DIGEST_SIZE,
61 .init = crct10dif_init,
62 .update = crct10dif_update,
63 .final = crct10dif_final,
64 .descsize = CRC_T10DIF_DIGEST_SIZE,
65
66 .base.cra_name = "crct10dif",
67 .base.cra_driver_name = "crct10dif-arm64-ce",
68 .base.cra_priority = 200,
69 .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
70 .base.cra_module = THIS_MODULE,
71};
72
73static int __init crc_t10dif_mod_init(void)
74{
75 return crypto_register_shash(&crc_t10dif_alg);
76}
77
78static void __exit crc_t10dif_mod_exit(void)
79{
80 crypto_unregister_shash(&crc_t10dif_alg);
81}
82
83module_cpu_feature_match(PMULL, crc_t10dif_mod_init);
84module_exit(crc_t10dif_mod_exit);
85
86MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
87MODULE_LICENSE("GPL v2");