1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This host program runs at kernel build time and generates the lookup tables
4 * used by the generic CRC64 code.
5 *
6 * Copyright 2018 SUSE Linux.
7 * Author: Coly Li <colyli@suse.de>
8 */
9 #include <inttypes.h>
10 #include <stdio.h>
11
12 #define CRC64_ECMA182_POLY 0x42F0E1EBA9EA3693ULL
13 #define CRC64_NVME_POLY 0x9A6C9329AC4BC9B5ULL
14
15 static uint64_t crc64_table[256] = {0};
16 static uint64_t crc64_nvme_table[256] = {0};
17
generate_reflected_crc64_table(uint64_t table[256],uint64_t poly)18 static void generate_reflected_crc64_table(uint64_t table[256], uint64_t poly)
19 {
20 uint64_t i, j, c, crc;
21
22 for (i = 0; i < 256; i++) {
23 crc = 0ULL;
24 c = i;
25
26 for (j = 0; j < 8; j++) {
27 if ((crc ^ (c >> j)) & 1)
28 crc = (crc >> 1) ^ poly;
29 else
30 crc >>= 1;
31 }
32 table[i] = crc;
33 }
34 }
35
generate_crc64_table(uint64_t table[256],uint64_t poly)36 static void generate_crc64_table(uint64_t table[256], uint64_t poly)
37 {
38 uint64_t i, j, c, crc;
39
40 for (i = 0; i < 256; i++) {
41 crc = 0;
42 c = i << 56;
43
44 for (j = 0; j < 8; j++) {
45 if ((crc ^ c) & 0x8000000000000000ULL)
46 crc = (crc << 1) ^ poly;
47 else
48 crc <<= 1;
49 c <<= 1;
50 }
51
52 table[i] = crc;
53 }
54 }
55
output_table(uint64_t table[256])56 static void output_table(uint64_t table[256])
57 {
58 int i;
59
60 for (i = 0; i < 256; i++) {
61 printf("\t0x%016" PRIx64 "ULL", table[i]);
62 if (i & 0x1)
63 printf(",\n");
64 else
65 printf(", ");
66 }
67 printf("};\n");
68 }
69
print_crc64_tables(void)70 static void print_crc64_tables(void)
71 {
72 printf("/* this file is generated - do not edit */\n\n");
73 printf("#include <linux/types.h>\n");
74 printf("#include <linux/cache.h>\n\n");
75 printf("static const u64 ____cacheline_aligned crc64table[256] = {\n");
76 output_table(crc64_table);
77
78 printf("\nstatic const u64 ____cacheline_aligned crc64nvmetable[256] = {\n");
79 output_table(crc64_nvme_table);
80 }
81
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 generate_crc64_table(crc64_table, CRC64_ECMA182_POLY);
85 generate_reflected_crc64_table(crc64_nvme_table, CRC64_NVME_POLY);
86 print_crc64_tables();
87 return 0;
88 }
89