History

lib: add rocksoft model crc64

The NVM Express specification extended data integrity fields to 64 bits using the Rocksoft parameters. Add the poly to the crc64 table generation, and provide a generic library routine implementing the algorithm. The Rocksoft 64-bit CRC model parameters are as follows: Poly: 0xAD93D23594C93659 Initial value: 0xFFFFFFFFFFFFFFFF Reflected Input: True Reflected Output: True Xor Final: 0xFFFFFFFFFFFFFFFF Since this model used reflected bits, the implementation generates the reflected table so the result is ordered consistently.

Keith Busch committed 4 years ago

Commit cbc0a401 parent 7ee88093 files · +70 −11Browse files

Unified · Split

3 files
  1. include/linux/crc64.h +2 −0
  2. lib/crc64.c +28 −0
  3. lib/gen_crc64table.c +40 −11
include/linux/crc64.h+2−0
@@ -8,4 +8,6 @@ · 7 unmodified lines
8#include <linux/types.h>8#include <linux/types.h>
9 9
10u64 __pure crc64_be(u64 crc, const void *p, size_t len);10u64 __pure crc64_be(u64 crc, const void *p, size_t len);
11u64 __pure crc64_rocksoft_generic(u64 crc, const void *p, size_t len);
12
11#endif /* _LINUX_CRC64_H */13#endif /* _LINUX_CRC64_H */
lib/crc64.c+28−0
@@ -22,6 +22,13 @@ · 21 unmodified lines
22 * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +22 * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
23 * x^7 + x^4 + x + 123 * x^7 + x^4 + x + 1
24 *24 *
25 * crc64rocksoft[256] table is from the Rocksoft specification polynomial
26 * defined as,
27 *
28 * x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 +
29 * x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 +
30 * x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1
31 *
25 * Copyright 2018 SUSE Linux.32 * Copyright 2018 SUSE Linux.
26 * Author: Coly Li <colyli@suse.de>33 * Author: Coly Li <colyli@suse.de>
27 */34 */
@@ -55,3 +62,24 @@ u64 __pure crc64_be(u64 crc, const void *p, size_t len) · 27 unmodified lines
55 return crc;62 return crc;
56}63}
57EXPORT_SYMBOL_GPL(crc64_be);64EXPORT_SYMBOL_GPL(crc64_be);
65
66/**
67 * crc64_rocksoft_generic - Calculate bitwise Rocksoft CRC64
68 * @crc: seed value for computation. 0 for a new CRC calculation, or the
69 * previous crc64 value if computing incrementally.
70 * @p: pointer to buffer over which CRC64 is run
71 * @len: length of buffer @p
72 */
73u64 __pure crc64_rocksoft_generic(u64 crc, const void *p, size_t len)
74{
75 const unsigned char *_p = p;
76 size_t i;
77
78 crc = ~crc;
79
80 for (i = 0; i < len; i++)
81 crc = (crc >> 8) ^ crc64rocksofttable[(crc & 0xff) ^ *_p++];
82
83 return ~crc;
84}
85EXPORT_SYMBOL_GPL(crc64_rocksoft_generic);
lib/gen_crc64table.c+40−11
@@ -17,10 +17,30 @@ · 16 unmodified lines
17#include <stdio.h>17#include <stdio.h>
18 18
19#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL19#define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
20#define CRC64_ROCKSOFT_POLY 0x9A6C9329AC4BC9B5ULL
20 21
21static uint64_t crc64_table[256] = {0};22static uint64_t crc64_table[256] = {0};
23static uint64_t crc64_rocksoft_table[256] = {0};
22 24
23static void generate_crc64_table(void)25static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
26{
27 uint64_t i, j, c, crc;
28
29 for (i = 0; i < 256; i++) {
30 crc = 0ULL;
31 c = i;
32
33 for (j = 0; j < 8; j++) {
34 if ((crc ^ (c >> j)) & 1)
35 crc = (crc >> 1) ^ poly;
36 else
37 crc >>= 1;
38 }
39 table[i] = crc;
40 }
41}
42
43static void generate_crc64_table(uint64_t table[256], uint64_t poly)
24{44{
25 uint64_t i, j, c, crc;45 uint64_t i, j, c, crc;
26 46
@@ -30,26 +50,22 @@ static void generate_crc64_table(void) · 3 unmodified lines
30 50
31 for (j = 0; j < 8; j++) {51 for (j = 0; j < 8; j++) {
32 if ((crc ^ c) & 0x8000000000000000ULL)52 if ((crc ^ c) & 0x8000000000000000ULL)
33 crc = (crc << 1) ^ CRC64_ECMA182_POLY;53 crc = (crc << 1) ^ poly;
34 else54 else
35 crc <<= 1;55 crc <<= 1;
36 c <<= 1;56 c <<= 1;
37 }57 }
38 58
39 crc64_table[i] = crc;59 table[i] = crc;
40 }60 }
41}61}
42 62
43static void print_crc64_table(void)63static void output_table(uint64_t table[256])
44{64{
45 int i;65 int i;
46 66
47 printf("/* this file is generated - do not edit */\n\n");
48 printf("#include <linux/types.h>\n");
49 printf("#include <linux/cache.h>\n\n");
50 printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
51 for (i = 0; i < 256; i++) {67 for (i = 0; i < 256; i++) {
52 printf("\t0x%016" PRIx64 "ULL", crc64_table[i]);68 printf("\t0x%016" PRIx64 "ULL", table[i]);
53 if (i & 0x1)69 if (i & 0x1)
54 printf(",\n");70 printf(",\n");
55 else71 else
@@ -58,9 +74,22 @@ static void print_crc64_table(void) · 2 unmodified lines
58 printf("};\n");74 printf("};\n");
59}75}
60 76
77static void print_crc64_tables(void)
78{
79 printf("/* this file is generated - do not edit */\n\n");
80 printf("#include <linux/types.h>\n");
81 printf("#include <linux/cache.h>\n\n");
82 printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
83 output_table(crc64_table);
84
85 printf("\nstatic const u64 ____cacheline_aligned crc64rocksofttable[256] = {\n");
86 output_table(crc64_rocksoft_table);
87}
88
61int main(int argc, char *argv[])89int main(int argc, char *argv[])
62{90{
63 generate_crc64_table();91 generate_crc64_table(crc64_table, CRC64_ECMA182_POLY);
64 print_crc64_table();92 generate_reflected_crc64_table(crc64_rocksoft_table, CRC64_ROCKSOFT_POLY);
93 print_crc64_tables();
65 return 0;94 return 0;
66}95}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t