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