1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* -*- linux-c -*- ------------------------------------------------------- * |
3 | * |
4 | * Copyright 2002-2007 H. Peter Anvin - All Rights Reserved |
5 | * |
6 | * ----------------------------------------------------------------------- */ |
7 | |
8 | /* |
9 | * mktables.c |
10 | * |
11 | * Make RAID-6 tables. This is a host user space program to be run at |
12 | * compile time. |
13 | */ |
14 | |
15 | #include <stdio.h> |
16 | #include <string.h> |
17 | #include <inttypes.h> |
18 | #include <stdlib.h> |
19 | #include <time.h> |
20 | |
21 | static uint8_t gfmul(uint8_t a, uint8_t b) |
22 | { |
23 | uint8_t v = 0; |
24 | |
25 | while (b) { |
26 | if (b & 1) |
27 | v ^= a; |
28 | a = (a << 1) ^ (a & 0x80 ? 0x1d : 0); |
29 | b >>= 1; |
30 | } |
31 | |
32 | return v; |
33 | } |
34 | |
35 | static uint8_t gfpow(uint8_t a, int b) |
36 | { |
37 | uint8_t v = 1; |
38 | |
39 | b %= 255; |
40 | if (b < 0) |
41 | b += 255; |
42 | |
43 | while (b) { |
44 | if (b & 1) |
45 | v = gfmul(a: v, b: a); |
46 | a = gfmul(a, b: a); |
47 | b >>= 1; |
48 | } |
49 | |
50 | return v; |
51 | } |
52 | |
53 | int main(int argc, char *argv[]) |
54 | { |
55 | int i, j, k; |
56 | uint8_t v; |
57 | uint8_t exptbl[256], invtbl[256]; |
58 | |
59 | printf(format: "#ifdef __KERNEL__\n" ); |
60 | printf(format: "#include <linux/export.h>\n" ); |
61 | printf(format: "#endif\n" ); |
62 | printf(format: "#include <linux/raid/pq.h>\n" ); |
63 | |
64 | /* Compute multiplication table */ |
65 | printf(format: "\nconst u8 __attribute__((aligned(256)))\n" |
66 | "raid6_gfmul[256][256] =\n" |
67 | "{\n" ); |
68 | for (i = 0; i < 256; i++) { |
69 | printf(format: "\t{\n" ); |
70 | for (j = 0; j < 256; j += 8) { |
71 | printf(format: "\t\t" ); |
72 | for (k = 0; k < 8; k++) |
73 | printf(format: "0x%02x,%c" , gfmul(a: i, b: j + k), |
74 | (k == 7) ? '\n' : ' '); |
75 | } |
76 | printf(format: "\t},\n" ); |
77 | } |
78 | printf(format: "};\n" ); |
79 | printf(format: "#ifdef __KERNEL__\n" ); |
80 | printf(format: "EXPORT_SYMBOL(raid6_gfmul);\n" ); |
81 | printf(format: "#endif\n" ); |
82 | |
83 | /* Compute vector multiplication table */ |
84 | printf(format: "\nconst u8 __attribute__((aligned(256)))\n" |
85 | "raid6_vgfmul[256][32] =\n" |
86 | "{\n" ); |
87 | for (i = 0; i < 256; i++) { |
88 | printf(format: "\t{\n" ); |
89 | for (j = 0; j < 16; j += 8) { |
90 | printf(format: "\t\t" ); |
91 | for (k = 0; k < 8; k++) |
92 | printf(format: "0x%02x,%c" , gfmul(a: i, b: j + k), |
93 | (k == 7) ? '\n' : ' '); |
94 | } |
95 | for (j = 0; j < 16; j += 8) { |
96 | printf(format: "\t\t" ); |
97 | for (k = 0; k < 8; k++) |
98 | printf(format: "0x%02x,%c" , gfmul(a: i, b: (j + k) << 4), |
99 | (k == 7) ? '\n' : ' '); |
100 | } |
101 | printf(format: "\t},\n" ); |
102 | } |
103 | printf(format: "};\n" ); |
104 | printf(format: "#ifdef __KERNEL__\n" ); |
105 | printf(format: "EXPORT_SYMBOL(raid6_vgfmul);\n" ); |
106 | printf(format: "#endif\n" ); |
107 | |
108 | /* Compute power-of-2 table (exponent) */ |
109 | v = 1; |
110 | printf(format: "\nconst u8 __attribute__((aligned(256)))\n" |
111 | "raid6_gfexp[256] =\n" "{\n" ); |
112 | for (i = 0; i < 256; i += 8) { |
113 | printf(format: "\t" ); |
114 | for (j = 0; j < 8; j++) { |
115 | exptbl[i + j] = v; |
116 | printf(format: "0x%02x,%c" , v, (j == 7) ? '\n' : ' '); |
117 | v = gfmul(a: v, b: 2); |
118 | if (v == 1) |
119 | v = 0; /* For entry 255, not a real entry */ |
120 | } |
121 | } |
122 | printf(format: "};\n" ); |
123 | printf(format: "#ifdef __KERNEL__\n" ); |
124 | printf(format: "EXPORT_SYMBOL(raid6_gfexp);\n" ); |
125 | printf(format: "#endif\n" ); |
126 | |
127 | /* Compute log-of-2 table */ |
128 | printf(format: "\nconst u8 __attribute__((aligned(256)))\n" |
129 | "raid6_gflog[256] =\n" "{\n" ); |
130 | for (i = 0; i < 256; i += 8) { |
131 | printf(format: "\t" ); |
132 | for (j = 0; j < 8; j++) { |
133 | v = 255; |
134 | for (k = 0; k < 256; k++) |
135 | if (exptbl[k] == (i + j)) { |
136 | v = k; |
137 | break; |
138 | } |
139 | printf(format: "0x%02x,%c" , v, (j == 7) ? '\n' : ' '); |
140 | } |
141 | } |
142 | printf(format: "};\n" ); |
143 | printf(format: "#ifdef __KERNEL__\n" ); |
144 | printf(format: "EXPORT_SYMBOL(raid6_gflog);\n" ); |
145 | printf(format: "#endif\n" ); |
146 | |
147 | /* Compute inverse table x^-1 == x^254 */ |
148 | printf(format: "\nconst u8 __attribute__((aligned(256)))\n" |
149 | "raid6_gfinv[256] =\n" "{\n" ); |
150 | for (i = 0; i < 256; i += 8) { |
151 | printf(format: "\t" ); |
152 | for (j = 0; j < 8; j++) { |
153 | invtbl[i + j] = v = gfpow(a: i + j, b: 254); |
154 | printf(format: "0x%02x,%c" , v, (j == 7) ? '\n' : ' '); |
155 | } |
156 | } |
157 | printf(format: "};\n" ); |
158 | printf(format: "#ifdef __KERNEL__\n" ); |
159 | printf(format: "EXPORT_SYMBOL(raid6_gfinv);\n" ); |
160 | printf(format: "#endif\n" ); |
161 | |
162 | /* Compute inv(2^x + 1) (exponent-xor-inverse) table */ |
163 | printf(format: "\nconst u8 __attribute__((aligned(256)))\n" |
164 | "raid6_gfexi[256] =\n" "{\n" ); |
165 | for (i = 0; i < 256; i += 8) { |
166 | printf(format: "\t" ); |
167 | for (j = 0; j < 8; j++) |
168 | printf(format: "0x%02x,%c" , invtbl[exptbl[i + j] ^ 1], |
169 | (j == 7) ? '\n' : ' '); |
170 | } |
171 | printf(format: "};\n" ); |
172 | printf(format: "#ifdef __KERNEL__\n" ); |
173 | printf(format: "EXPORT_SYMBOL(raid6_gfexi);\n" ); |
174 | printf(format: "#endif\n" ); |
175 | |
176 | return 0; |
177 | } |
178 | |