| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <asm/byteorder.h> |
| 17 |
#include <linux/minmax.h> |
| 18 |
|
| 19 |
#define CRC_BITS (8 * sizeof(crc_t)) |
| 20 |
|
| 21 |
static inline unsigned long clmul(unsigned long a, unsigned long b) |
| 22 |
{ |
| 23 |
unsigned long res; |
| 24 |
|
| 25 |
asm(".option push\n" |
| 26 |
".option arch,+zbc\n" |
| 27 |
"clmul %0, %1, %2\n" |
| 28 |
".option pop\n" |
| 29 |
: "=r" (res) : "r" (a), "r" (b)); |
| 30 |
return res; |
| 31 |
} |
| 32 |
|
| 33 |
static inline unsigned long clmulh(unsigned long a, unsigned long b) |
| 34 |
{ |
| 35 |
unsigned long res; |
| 36 |
|
| 37 |
asm(".option push\n" |
| 38 |
".option arch,+zbc\n" |
| 39 |
"clmulh %0, %1, %2\n" |
| 40 |
".option pop\n" |
| 41 |
: "=r" (res) : "r" (a), "r" (b)); |
| 42 |
return res; |
| 43 |
} |
| 44 |
|
| 45 |
static inline unsigned long clmulr(unsigned long a, unsigned long b) |
| 46 |
{ |
| 47 |
unsigned long res; |
| 48 |
|
| 49 |
asm(".option push\n" |
| 50 |
".option arch,+zbc\n" |
| 51 |
"clmulr %0, %1, %2\n" |
| 52 |
".option pop\n" |
| 53 |
: "=r" (res) : "r" (a), "r" (b)); |
| 54 |
return res; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#ifdef CONFIG_64BIT |
| 62 |
# if LSB_CRC |
| 63 |
# define crc_load_long(x) le64_to_cpup(x) |
| 64 |
# else |
| 65 |
# define crc_load_long(x) be64_to_cpup(x) |
| 66 |
# endif |
| 67 |
#else |
| 68 |
# if LSB_CRC |
| 69 |
# define crc_load_long(x) le32_to_cpup(x) |
| 70 |
# else |
| 71 |
# define crc_load_long(x) be32_to_cpup(x) |
| 72 |
# endif |
| 73 |
#endif |
| 74 |
|
| 75 |
|
| 76 |
static inline unsigned long |
| 77 |
crc_clmul_prep(crc_t crc, unsigned long msgpoly) |
| 78 |
{ |
| 79 |
#if LSB_CRC |
| 80 |
return msgpoly ^ crc; |
| 81 |
#else |
| 82 |
return msgpoly ^ ((unsigned long)crc << (BITS_PER_LONG - CRC_BITS)); |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static inline crc_t |
| 91 |
crc_clmul_long(unsigned long msgpoly, const struct crc_clmul_consts *consts) |
| 92 |
{ |
| 93 |
unsigned long tmp; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#if LSB_CRC |
| 118 |
tmp = clmul(msgpoly, consts->barrett_reduction_const_1); |
| 119 |
#else |
| 120 |
tmp = clmulr(msgpoly, consts->barrett_reduction_const_1); |
| 121 |
#endif |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#if LSB_CRC |
| 146 |
return clmulr(tmp, consts->barrett_reduction_const_2); |
| 147 |
#else |
| 148 |
return clmul(tmp, consts->barrett_reduction_const_2); |
| 149 |
#endif |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
static inline crc_t |
| 154 |
crc_clmul_update_long(crc_t crc, unsigned long msgpoly, |
| 155 |
const struct crc_clmul_consts *consts) |
| 156 |
{ |
| 157 |
return crc_clmul_long(crc_clmul_prep(crc, msgpoly), consts); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static inline crc_t |
| 162 |
crc_clmul_update_partial(crc_t crc, const u8 *p, size_t len, |
| 163 |
const struct crc_clmul_consts *consts) |
| 164 |
{ |
| 165 |
unsigned long msgpoly; |
| 166 |
size_t i; |
| 167 |
|
| 168 |
#if LSB_CRC |
| 169 |
msgpoly = (unsigned long)p[0] << (BITS_PER_LONG - 8); |
| 170 |
for (i = 1; i < len; i++) |
| 171 |
msgpoly = (msgpoly >> 8) ^ ((unsigned long)p[i] << (BITS_PER_LONG - 8)); |
| 172 |
#else |
| 173 |
msgpoly = p[0]; |
| 174 |
for (i = 1; i < len; i++) |
| 175 |
msgpoly = (msgpoly << 8) ^ p[i]; |
| 176 |
#endif |
| 177 |
|
| 178 |
if (len >= sizeof(crc_t)) { |
| 179 |
#if LSB_CRC |
| 180 |
msgpoly ^= (unsigned long)crc << (BITS_PER_LONG - 8*len); |
| 181 |
#else |
| 182 |
msgpoly ^= (unsigned long)crc << (8*len - CRC_BITS); |
| 183 |
#endif |
| 184 |
return crc_clmul_long(msgpoly, consts); |
| 185 |
} |
| 186 |
#if LSB_CRC |
| 187 |
msgpoly ^= (unsigned long)crc << (BITS_PER_LONG - 8*len); |
| 188 |
return crc_clmul_long(msgpoly, consts) ^ (crc >> (8*len)); |
| 189 |
#else |
| 190 |
msgpoly ^= crc >> (CRC_BITS - 8*len); |
| 191 |
return crc_clmul_long(msgpoly, consts) ^ (crc << (8*len)); |
| 192 |
#endif |
| 193 |
} |
| 194 |
|
| 195 |
static inline crc_t |
| 196 |
crc_clmul(crc_t crc, const void *p, size_t len, |
| 197 |
const struct crc_clmul_consts *consts) |
| 198 |
{ |
| 199 |
size_t align; |
| 200 |
|
| 201 |
|
| 202 |
BUILD_BUG_ON(sizeof(crc_t) > sizeof(unsigned long)); |
| 203 |
|
| 204 |
|
| 205 |
align = (unsigned long)p % sizeof(unsigned long); |
| 206 |
if (align && len) { |
| 207 |
align = min(sizeof(unsigned long) - align, len); |
| 208 |
crc = crc_clmul_update_partial(crc, p, align, consts); |
| 209 |
p += align; |
| 210 |
len -= align; |
| 211 |
} |
| 212 |
|
| 213 |
if (len >= 4 * sizeof(unsigned long)) { |
| 214 |
unsigned long m0, m1; |
| 215 |
|
| 216 |
m0 = crc_clmul_prep(crc, crc_load_long(p)); |
| 217 |
m1 = crc_load_long(p + sizeof(unsigned long)); |
| 218 |
p += 2 * sizeof(unsigned long); |
| 219 |
len -= 2 * sizeof(unsigned long); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
do { |
| 237 |
unsigned long p0, p1, p2, p3; |
| 238 |
|
| 239 |
p0 = clmulh(m0, consts->fold_across_2_longs_const_hi); |
| 240 |
p1 = clmul(m0, consts->fold_across_2_longs_const_hi); |
| 241 |
p2 = clmulh(m1, consts->fold_across_2_longs_const_lo); |
| 242 |
p3 = clmul(m1, consts->fold_across_2_longs_const_lo); |
| 243 |
m0 = (LSB_CRC ? p1 ^ p3 : p0 ^ p2) ^ crc_load_long(p); |
| 244 |
m1 = (LSB_CRC ? p0 ^ p2 : p1 ^ p3) ^ |
| 245 |
crc_load_long(p + sizeof(unsigned long)); |
| 246 |
|
| 247 |
p += 2 * sizeof(unsigned long); |
| 248 |
len -= 2 * sizeof(unsigned long); |
| 249 |
} while (len >= 2 * sizeof(unsigned long)); |
| 250 |
|
| 251 |
crc = crc_clmul_long(m0, consts); |
| 252 |
crc = crc_clmul_update_long(crc, m1, consts); |
| 253 |
} |
| 254 |
|
| 255 |
while (len >= sizeof(unsigned long)) { |
| 256 |
crc = crc_clmul_update_long(crc, crc_load_long(p), consts); |
| 257 |
p += sizeof(unsigned long); |
| 258 |
len -= sizeof(unsigned long); |
| 259 |
} |
| 260 |
|
| 261 |
if (len) |
| 262 |
crc = crc_clmul_update_partial(crc, p, len, consts); |
| 263 |
|
| 264 |
return crc; |
| 265 |
} |