Thomas Gleixner | d691005 | 2019-05-22 09:51:29 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 2 | /* -*- linux-c -*- ------------------------------------------------------- * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 4 | * Copyright 2002-2007 H. Peter Anvin - All Rights Reserved |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 23 | uint8_t v = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static uint8_t gfpow(uint8_t a, int b) |
| 36 | { |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 37 | uint8_t v = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 39 | b %= 255; |
| 40 | if (b < 0) |
| 41 | b += 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 43 | while (b) { |
| 44 | if (b & 1) |
| 45 | v = gfmul(v, a); |
| 46 | a = gfmul(a, a); |
| 47 | b >>= 1; |
| 48 | } |
| 49 | |
| 50 | return v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int main(int argc, char *argv[]) |
| 54 | { |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 55 | int i, j, k; |
| 56 | uint8_t v; |
| 57 | uint8_t exptbl[256], invtbl[256]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Paul Gortmaker | daaa5f7 | 2011-05-27 15:50:58 -0400 | [diff] [blame] | 59 | printf("#include <linux/export.h>\n"); |
Zhengyuan Liu | 5e5ac01 | 2019-12-20 10:21:27 +0800 | [diff] [blame] | 60 | printf("#include <linux/raid/pq.h>\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 62 | /* Compute multiplication table */ |
| 63 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 64 | "raid6_gfmul[256][256] =\n" |
| 65 | "{\n"); |
| 66 | for (i = 0; i < 256; i++) { |
| 67 | printf("\t{\n"); |
| 68 | for (j = 0; j < 256; j += 8) { |
| 69 | printf("\t\t"); |
| 70 | for (k = 0; k < 8; k++) |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 71 | printf("0x%02x,%c", gfmul(i, j + k), |
| 72 | (k == 7) ? '\n' : ' '); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 73 | } |
| 74 | printf("\t},\n"); |
| 75 | } |
| 76 | printf("};\n"); |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 77 | printf("#ifdef __KERNEL__\n"); |
| 78 | printf("EXPORT_SYMBOL(raid6_gfmul);\n"); |
| 79 | printf("#endif\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Jim Kukunas | 048a8b8 | 2012-05-22 13:54:18 +1000 | [diff] [blame] | 81 | /* Compute vector multiplication table */ |
| 82 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 83 | "raid6_vgfmul[256][32] =\n" |
| 84 | "{\n"); |
| 85 | for (i = 0; i < 256; i++) { |
| 86 | printf("\t{\n"); |
| 87 | for (j = 0; j < 16; j += 8) { |
| 88 | printf("\t\t"); |
| 89 | for (k = 0; k < 8; k++) |
| 90 | printf("0x%02x,%c", gfmul(i, j + k), |
| 91 | (k == 7) ? '\n' : ' '); |
| 92 | } |
| 93 | for (j = 0; j < 16; j += 8) { |
| 94 | printf("\t\t"); |
| 95 | for (k = 0; k < 8; k++) |
| 96 | printf("0x%02x,%c", gfmul(i, (j + k) << 4), |
| 97 | (k == 7) ? '\n' : ' '); |
| 98 | } |
| 99 | printf("\t},\n"); |
| 100 | } |
| 101 | printf("};\n"); |
| 102 | printf("#ifdef __KERNEL__\n"); |
| 103 | printf("EXPORT_SYMBOL(raid6_vgfmul);\n"); |
| 104 | printf("#endif\n"); |
| 105 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 106 | /* Compute power-of-2 table (exponent) */ |
| 107 | v = 1; |
| 108 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 109 | "raid6_gfexp[256] =\n" "{\n"); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 110 | for (i = 0; i < 256; i += 8) { |
| 111 | printf("\t"); |
| 112 | for (j = 0; j < 8; j++) { |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 113 | exptbl[i + j] = v; |
| 114 | printf("0x%02x,%c", v, (j == 7) ? '\n' : ' '); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 115 | v = gfmul(v, 2); |
| 116 | if (v == 1) |
| 117 | v = 0; /* For entry 255, not a real entry */ |
| 118 | } |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 119 | } |
| 120 | printf("};\n"); |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 121 | printf("#ifdef __KERNEL__\n"); |
| 122 | printf("EXPORT_SYMBOL(raid6_gfexp);\n"); |
| 123 | printf("#endif\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Anup Patel | b5dceda | 2017-05-15 10:34:52 +0530 | [diff] [blame] | 125 | /* Compute log-of-2 table */ |
| 126 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 127 | "raid6_gflog[256] =\n" "{\n"); |
| 128 | for (i = 0; i < 256; i += 8) { |
| 129 | printf("\t"); |
| 130 | for (j = 0; j < 8; j++) { |
| 131 | v = 255; |
| 132 | for (k = 0; k < 256; k++) |
| 133 | if (exptbl[k] == (i + j)) { |
| 134 | v = k; |
| 135 | break; |
| 136 | } |
| 137 | printf("0x%02x,%c", v, (j == 7) ? '\n' : ' '); |
| 138 | } |
| 139 | } |
| 140 | printf("};\n"); |
| 141 | printf("#ifdef __KERNEL__\n"); |
| 142 | printf("EXPORT_SYMBOL(raid6_gflog);\n"); |
| 143 | printf("#endif\n"); |
| 144 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 145 | /* Compute inverse table x^-1 == x^254 */ |
| 146 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 147 | "raid6_gfinv[256] =\n" "{\n"); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 148 | for (i = 0; i < 256; i += 8) { |
| 149 | printf("\t"); |
| 150 | for (j = 0; j < 8; j++) { |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 151 | invtbl[i + j] = v = gfpow(i + j, 254); |
| 152 | printf("0x%02x,%c", v, (j == 7) ? '\n' : ' '); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 153 | } |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 154 | } |
| 155 | printf("};\n"); |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 156 | printf("#ifdef __KERNEL__\n"); |
| 157 | printf("EXPORT_SYMBOL(raid6_gfinv);\n"); |
| 158 | printf("#endif\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 160 | /* Compute inv(2^x + 1) (exponent-xor-inverse) table */ |
| 161 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 162 | "raid6_gfexi[256] =\n" "{\n"); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 163 | for (i = 0; i < 256; i += 8) { |
| 164 | printf("\t"); |
| 165 | for (j = 0; j < 8; j++) |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 166 | printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1], |
| 167 | (j == 7) ? '\n' : ' '); |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 168 | } |
H. Peter Anvin | 98ec302 | 2008-02-06 01:39:48 -0800 | [diff] [blame] | 169 | printf("};\n"); |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 170 | printf("#ifdef __KERNEL__\n"); |
| 171 | printf("EXPORT_SYMBOL(raid6_gfexi);\n"); |
| 172 | printf("#endif\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame] | 174 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |