blob: 02ea277863d15001be3c919aa17c7f2ccd73aa1a [file] [log] [blame]
Daniel Axtensb01df1c2017-03-15 23:37:36 +11001/*
2 * Calculate a CRC T10-DIF with vpmsum acceleration
3 *
4 * Copyright 2017, Daniel Axtens, IBM Corporation.
5 * [based on crc32c-vpmsum_glue.c]
6 *
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.
11 */
12
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>
21
22#define VMX_ALIGN 16
23#define VMX_ALIGN_MASK (VMX_ALIGN-1)
24
25#define VECTOR_BREAKPOINT 64
26
27u32 __crct10dif_vpmsum(u32 crc, unsigned char const *p, size_t len);
28
29static u16 crct10dif_vpmsum(u16 crci, unsigned char const *p, size_t len)
30{
31 unsigned int prealign;
32 unsigned int tail;
33 u32 crc = crci;
34
35 if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || in_interrupt())
36 return crc_t10dif_generic(crc, p, len);
37
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);
41 len -= prealign;
42 p += prealign;
43 }
44
45 if (len & ~VMX_ALIGN_MASK) {
46 crc <<= 16;
Michael Ellerman0f89f6e2017-04-20 15:35:09 +100047 preempt_disable();
Daniel Axtensb01df1c2017-03-15 23:37:36 +110048 pagefault_disable();
49 enable_kernel_altivec();
50 crc = __crct10dif_vpmsum(crc, p, len & ~VMX_ALIGN_MASK);
Michael Ellerman0f89f6e2017-04-20 15:35:09 +100051 disable_kernel_altivec();
Daniel Axtensb01df1c2017-03-15 23:37:36 +110052 pagefault_enable();
Michael Ellerman0f89f6e2017-04-20 15:35:09 +100053 preempt_enable();
Daniel Axtensb01df1c2017-03-15 23:37:36 +110054 crc >>= 16;
55 }
56
57 tail = len & VMX_ALIGN_MASK;
58 if (tail) {
59 p += len & ~VMX_ALIGN_MASK;
60 crc = crc_t10dif_generic(crc, p, tail);
61 }
62
63 return crc & 0xffff;
64}
65
66static int crct10dif_vpmsum_init(struct shash_desc *desc)
67{
68 u16 *crc = shash_desc_ctx(desc);
69
70 *crc = 0;
71 return 0;
72}
73
74static int crct10dif_vpmsum_update(struct shash_desc *desc, const u8 *data,
75 unsigned int length)
76{
77 u16 *crc = shash_desc_ctx(desc);
78
79 *crc = crct10dif_vpmsum(*crc, data, length);
80
81 return 0;
82}
83
84
85static int crct10dif_vpmsum_final(struct shash_desc *desc, u8 *out)
86{
87 u16 *crcp = shash_desc_ctx(desc);
88
89 *(u16 *)out = *crcp;
90 return 0;
91}
92
93static 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,
99 .base = {
100 .cra_name = "crct10dif",
101 .cra_driver_name = "crct10dif-vpmsum",
102 .cra_priority = 200,
103 .cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
104 .cra_module = THIS_MODULE,
105 }
106};
107
108static int __init crct10dif_vpmsum_mod_init(void)
109{
110 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
111 return -ENODEV;
112
113 return crypto_register_shash(&alg);
114}
115
116static void __exit crct10dif_vpmsum_mod_fini(void)
117{
118 crypto_unregister_shash(&alg);
119}
120
121module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO, crct10dif_vpmsum_mod_init);
122module_exit(crct10dif_vpmsum_mod_fini);
123
124MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
125MODULE_DESCRIPTION("CRCT10DIF using vector polynomial multiply-sum instructions");
126MODULE_LICENSE("GPL");
127MODULE_ALIAS_CRYPTO("crct10dif");
128MODULE_ALIAS_CRYPTO("crct10dif-vpmsum");