blob: 8a3780902cec8ca4141a3646169a341be2b3c7a8 [file] [log] [blame]
H. Peter Anvin98ec3022008-02-06 01:39:48 -08001/* -*- linux-c -*- ------------------------------------------------------- *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
H. Peter Anvin98ec3022008-02-06 01:39:48 -08003 * Copyright 2002-2007 H. Peter Anvin - All Rights Reserved
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
H. Peter Anvin98ec3022008-02-06 01:39:48 -08005 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2 or (at your
7 * option) any later version; incorporated herein by reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * mktables.c
13 *
14 * Make RAID-6 tables. This is a host user space program to be run at
15 * compile time.
16 */
17
18#include <stdio.h>
19#include <string.h>
20#include <inttypes.h>
21#include <stdlib.h>
22#include <time.h>
23
24static uint8_t gfmul(uint8_t a, uint8_t b)
25{
Oliver Pinter54212cf2008-02-06 01:39:47 -080026 uint8_t v = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Oliver Pinter54212cf2008-02-06 01:39:47 -080028 while (b) {
29 if (b & 1)
30 v ^= a;
31 a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
32 b >>= 1;
33 }
34
35 return v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38static uint8_t gfpow(uint8_t a, int b)
39{
Oliver Pinter54212cf2008-02-06 01:39:47 -080040 uint8_t v = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Oliver Pinter54212cf2008-02-06 01:39:47 -080042 b %= 255;
43 if (b < 0)
44 b += 255;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Oliver Pinter54212cf2008-02-06 01:39:47 -080046 while (b) {
47 if (b & 1)
48 v = gfmul(v, a);
49 a = gfmul(a, a);
50 b >>= 1;
51 }
52
53 return v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56int main(int argc, char *argv[])
57{
Oliver Pinter54212cf2008-02-06 01:39:47 -080058 int i, j, k;
59 uint8_t v;
60 uint8_t exptbl[256], invtbl[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Dan Williamsf701d582009-03-31 15:09:39 +110062 printf("#include <linux/raid/pq.h>\n");
Paul Gortmakerdaaa5f72011-05-27 15:50:58 -040063 printf("#include <linux/export.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Oliver Pinter54212cf2008-02-06 01:39:47 -080065 /* Compute multiplication table */
66 printf("\nconst u8 __attribute__((aligned(256)))\n"
67 "raid6_gfmul[256][256] =\n"
68 "{\n");
69 for (i = 0; i < 256; i++) {
70 printf("\t{\n");
71 for (j = 0; j < 256; j += 8) {
72 printf("\t\t");
73 for (k = 0; k < 8; k++)
H. Peter Anvin98ec3022008-02-06 01:39:48 -080074 printf("0x%02x,%c", gfmul(i, j + k),
75 (k == 7) ? '\n' : ' ');
Oliver Pinter54212cf2008-02-06 01:39:47 -080076 }
77 printf("\t},\n");
78 }
79 printf("};\n");
Dan Williamsf701d582009-03-31 15:09:39 +110080 printf("#ifdef __KERNEL__\n");
81 printf("EXPORT_SYMBOL(raid6_gfmul);\n");
82 printf("#endif\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Oliver Pinter54212cf2008-02-06 01:39:47 -080084 /* Compute power-of-2 table (exponent) */
85 v = 1;
86 printf("\nconst u8 __attribute__((aligned(256)))\n"
H. Peter Anvin98ec3022008-02-06 01:39:48 -080087 "raid6_gfexp[256] =\n" "{\n");
Oliver Pinter54212cf2008-02-06 01:39:47 -080088 for (i = 0; i < 256; i += 8) {
89 printf("\t");
90 for (j = 0; j < 8; j++) {
H. Peter Anvin98ec3022008-02-06 01:39:48 -080091 exptbl[i + j] = v;
92 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
Oliver Pinter54212cf2008-02-06 01:39:47 -080093 v = gfmul(v, 2);
94 if (v == 1)
95 v = 0; /* For entry 255, not a real entry */
96 }
Oliver Pinter54212cf2008-02-06 01:39:47 -080097 }
98 printf("};\n");
Dan Williamsf701d582009-03-31 15:09:39 +110099 printf("#ifdef __KERNEL__\n");
100 printf("EXPORT_SYMBOL(raid6_gfexp);\n");
101 printf("#endif\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Oliver Pinter54212cf2008-02-06 01:39:47 -0800103 /* Compute inverse table x^-1 == x^254 */
104 printf("\nconst u8 __attribute__((aligned(256)))\n"
H. Peter Anvin98ec3022008-02-06 01:39:48 -0800105 "raid6_gfinv[256] =\n" "{\n");
Oliver Pinter54212cf2008-02-06 01:39:47 -0800106 for (i = 0; i < 256; i += 8) {
107 printf("\t");
108 for (j = 0; j < 8; j++) {
H. Peter Anvin98ec3022008-02-06 01:39:48 -0800109 invtbl[i + j] = v = gfpow(i + j, 254);
110 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
Oliver Pinter54212cf2008-02-06 01:39:47 -0800111 }
Oliver Pinter54212cf2008-02-06 01:39:47 -0800112 }
113 printf("};\n");
Dan Williamsf701d582009-03-31 15:09:39 +1100114 printf("#ifdef __KERNEL__\n");
115 printf("EXPORT_SYMBOL(raid6_gfinv);\n");
116 printf("#endif\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Oliver Pinter54212cf2008-02-06 01:39:47 -0800118 /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
119 printf("\nconst u8 __attribute__((aligned(256)))\n"
H. Peter Anvin98ec3022008-02-06 01:39:48 -0800120 "raid6_gfexi[256] =\n" "{\n");
Oliver Pinter54212cf2008-02-06 01:39:47 -0800121 for (i = 0; i < 256; i += 8) {
122 printf("\t");
123 for (j = 0; j < 8; j++)
H. Peter Anvin98ec3022008-02-06 01:39:48 -0800124 printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
125 (j == 7) ? '\n' : ' ');
Oliver Pinter54212cf2008-02-06 01:39:47 -0800126 }
H. Peter Anvin98ec3022008-02-06 01:39:48 -0800127 printf("};\n");
Dan Williamsf701d582009-03-31 15:09:39 +1100128 printf("#ifdef __KERNEL__\n");
129 printf("EXPORT_SYMBOL(raid6_gfexi);\n");
130 printf("#endif\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Oliver Pinter54212cf2008-02-06 01:39:47 -0800132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}