1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Calculate a CRC T10-DIF with vpmsum acceleration
5 * Copyright 2017, Daniel Axtens, IBM Corporation.
6 * [based on crc32c-vpmsum_glue.c]
9 #include <linux/crc-t10dif.h>
10 #include <crypto/internal/hash.h>
11 #include <crypto/internal/simd.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/cpufeature.h>
18 #include <asm/switch_to.h>
21 #define VMX_ALIGN_MASK (VMX_ALIGN-1)
23 #define VECTOR_BREAKPOINT 64
25 u32
__crct10dif_vpmsum(u32 crc
, unsigned char const *p
, size_t len
);
27 static u16
crct10dif_vpmsum(u16 crci
, unsigned char const *p
, size_t len
)
29 unsigned int prealign
;
33 if (len
< (VECTOR_BREAKPOINT
+ VMX_ALIGN
) || !crypto_simd_usable())
34 return crc_t10dif_generic(crc
, p
, len
);
36 if ((unsigned long)p
& VMX_ALIGN_MASK
) {
37 prealign
= VMX_ALIGN
- ((unsigned long)p
& VMX_ALIGN_MASK
);
38 crc
= crc_t10dif_generic(crc
, p
, prealign
);
43 if (len
& ~VMX_ALIGN_MASK
) {
47 enable_kernel_altivec();
48 crc
= __crct10dif_vpmsum(crc
, p
, len
& ~VMX_ALIGN_MASK
);
49 disable_kernel_altivec();
55 tail
= len
& VMX_ALIGN_MASK
;
57 p
+= len
& ~VMX_ALIGN_MASK
;
58 crc
= crc_t10dif_generic(crc
, p
, tail
);
64 static int crct10dif_vpmsum_init(struct shash_desc
*desc
)
66 u16
*crc
= shash_desc_ctx(desc
);
72 static int crct10dif_vpmsum_update(struct shash_desc
*desc
, const u8
*data
,
75 u16
*crc
= shash_desc_ctx(desc
);
77 *crc
= crct10dif_vpmsum(*crc
, data
, length
);
83 static int crct10dif_vpmsum_final(struct shash_desc
*desc
, u8
*out
)
85 u16
*crcp
= shash_desc_ctx(desc
);
91 static struct shash_alg alg
= {
92 .init
= crct10dif_vpmsum_init
,
93 .update
= crct10dif_vpmsum_update
,
94 .final
= crct10dif_vpmsum_final
,
95 .descsize
= CRC_T10DIF_DIGEST_SIZE
,
96 .digestsize
= CRC_T10DIF_DIGEST_SIZE
,
98 .cra_name
= "crct10dif",
99 .cra_driver_name
= "crct10dif-vpmsum",
101 .cra_blocksize
= CRC_T10DIF_BLOCK_SIZE
,
102 .cra_module
= THIS_MODULE
,
106 static int __init
crct10dif_vpmsum_mod_init(void)
108 if (!cpu_has_feature(CPU_FTR_ARCH_207S
))
111 return crypto_register_shash(&alg
);
114 static void __exit
crct10dif_vpmsum_mod_fini(void)
116 crypto_unregister_shash(&alg
);
119 module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO
, crct10dif_vpmsum_mod_init
);
120 module_exit(crct10dif_vpmsum_mod_fini
);
122 MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
123 MODULE_DESCRIPTION("CRCT10DIF using vector polynomial multiply-sum instructions");
124 MODULE_LICENSE("GPL");
125 MODULE_ALIAS_CRYPTO("crct10dif");
126 MODULE_ALIAS_CRYPTO("crct10dif-vpmsum");