2 * Calculate a CRC T10-DIF with vpmsum acceleration
4 * Copyright 2017, Daniel Axtens, IBM Corporation.
5 * [based on crc32c-vpmsum_glue.c]
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
13 #include <linux/crc-t10dif.h>
14 #include <crypto/internal/hash.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/string.h>
18 #include <linux/kernel.h>
19 #include <linux/cpufeature.h>
20 #include <asm/switch_to.h>
23 #define VMX_ALIGN_MASK (VMX_ALIGN-1)
25 #define VECTOR_BREAKPOINT 64
27 u32
__crct10dif_vpmsum(u32 crc
, unsigned char const *p
, size_t len
);
29 static u16
crct10dif_vpmsum(u16 crci
, unsigned char const *p
, size_t len
)
31 unsigned int prealign
;
35 if (len
< (VECTOR_BREAKPOINT
+ VMX_ALIGN
) || in_interrupt())
36 return crc_t10dif_generic(crc
, p
, len
);
38 if ((unsigned long)p
& VMX_ALIGN_MASK
) {
39 prealign
= VMX_ALIGN
- ((unsigned long)p
& VMX_ALIGN_MASK
);
40 crc
= crc_t10dif_generic(crc
, p
, prealign
);
45 if (len
& ~VMX_ALIGN_MASK
) {
49 enable_kernel_altivec();
50 crc
= __crct10dif_vpmsum(crc
, p
, len
& ~VMX_ALIGN_MASK
);
51 disable_kernel_altivec();
57 tail
= len
& VMX_ALIGN_MASK
;
59 p
+= len
& ~VMX_ALIGN_MASK
;
60 crc
= crc_t10dif_generic(crc
, p
, tail
);
66 static int crct10dif_vpmsum_init(struct shash_desc
*desc
)
68 u16
*crc
= shash_desc_ctx(desc
);
74 static int crct10dif_vpmsum_update(struct shash_desc
*desc
, const u8
*data
,
77 u16
*crc
= shash_desc_ctx(desc
);
79 *crc
= crct10dif_vpmsum(*crc
, data
, length
);
85 static int crct10dif_vpmsum_final(struct shash_desc
*desc
, u8
*out
)
87 u16
*crcp
= shash_desc_ctx(desc
);
93 static struct shash_alg alg
= {
94 .init
= crct10dif_vpmsum_init
,
95 .update
= crct10dif_vpmsum_update
,
96 .final
= crct10dif_vpmsum_final
,
97 .descsize
= CRC_T10DIF_DIGEST_SIZE
,
98 .digestsize
= CRC_T10DIF_DIGEST_SIZE
,
100 .cra_name
= "crct10dif",
101 .cra_driver_name
= "crct10dif-vpmsum",
103 .cra_blocksize
= CRC_T10DIF_BLOCK_SIZE
,
104 .cra_module
= THIS_MODULE
,
108 static int __init
crct10dif_vpmsum_mod_init(void)
110 if (!cpu_has_feature(CPU_FTR_ARCH_207S
))
113 return crypto_register_shash(&alg
);
116 static void __exit
crct10dif_vpmsum_mod_fini(void)
118 crypto_unregister_shash(&alg
);
121 module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO
, crct10dif_vpmsum_mod_init
);
122 module_exit(crct10dif_vpmsum_mod_fini
);
124 MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
125 MODULE_DESCRIPTION("CRCT10DIF using vector polynomial multiply-sum instructions");
126 MODULE_LICENSE("GPL");
127 MODULE_ALIAS_CRYPTO("crct10dif");
128 MODULE_ALIAS_CRYPTO("crct10dif-vpmsum");