18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* -*- linux-c -*- ------------------------------------------------------- *
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci *   Copyright 2002-2007 H. Peter Anvin - All Rights Reserved
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * ----------------------------------------------------------------------- */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * mktables.c
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Make RAID-6 tables.  This is a host user space program to be run at
128c2ecf20Sopenharmony_ci * compile time.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <stdio.h>
168c2ecf20Sopenharmony_ci#include <string.h>
178c2ecf20Sopenharmony_ci#include <inttypes.h>
188c2ecf20Sopenharmony_ci#include <stdlib.h>
198c2ecf20Sopenharmony_ci#include <time.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic uint8_t gfmul(uint8_t a, uint8_t b)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	uint8_t v = 0;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	while (b) {
268c2ecf20Sopenharmony_ci		if (b & 1)
278c2ecf20Sopenharmony_ci			v ^= a;
288c2ecf20Sopenharmony_ci		a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
298c2ecf20Sopenharmony_ci		b >>= 1;
308c2ecf20Sopenharmony_ci	}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	return v;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic uint8_t gfpow(uint8_t a, int b)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	uint8_t v = 1;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	b %= 255;
408c2ecf20Sopenharmony_ci	if (b < 0)
418c2ecf20Sopenharmony_ci		b += 255;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	while (b) {
448c2ecf20Sopenharmony_ci		if (b & 1)
458c2ecf20Sopenharmony_ci			v = gfmul(v, a);
468c2ecf20Sopenharmony_ci		a = gfmul(a, a);
478c2ecf20Sopenharmony_ci		b >>= 1;
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return v;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciint main(int argc, char *argv[])
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	int i, j, k;
568c2ecf20Sopenharmony_ci	uint8_t v;
578c2ecf20Sopenharmony_ci	uint8_t exptbl[256], invtbl[256];
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	printf("#include <linux/export.h>\n");
608c2ecf20Sopenharmony_ci	printf("#include <linux/raid/pq.h>\n");
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* Compute multiplication table */
638c2ecf20Sopenharmony_ci	printf("\nconst u8  __attribute__((aligned(256)))\n"
648c2ecf20Sopenharmony_ci		"raid6_gfmul[256][256] =\n"
658c2ecf20Sopenharmony_ci		"{\n");
668c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i++) {
678c2ecf20Sopenharmony_ci		printf("\t{\n");
688c2ecf20Sopenharmony_ci		for (j = 0; j < 256; j += 8) {
698c2ecf20Sopenharmony_ci			printf("\t\t");
708c2ecf20Sopenharmony_ci			for (k = 0; k < 8; k++)
718c2ecf20Sopenharmony_ci				printf("0x%02x,%c", gfmul(i, j + k),
728c2ecf20Sopenharmony_ci				       (k == 7) ? '\n' : ' ');
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci		printf("\t},\n");
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci	printf("};\n");
778c2ecf20Sopenharmony_ci	printf("#ifdef __KERNEL__\n");
788c2ecf20Sopenharmony_ci	printf("EXPORT_SYMBOL(raid6_gfmul);\n");
798c2ecf20Sopenharmony_ci	printf("#endif\n");
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Compute vector multiplication table */
828c2ecf20Sopenharmony_ci	printf("\nconst u8  __attribute__((aligned(256)))\n"
838c2ecf20Sopenharmony_ci		"raid6_vgfmul[256][32] =\n"
848c2ecf20Sopenharmony_ci		"{\n");
858c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i++) {
868c2ecf20Sopenharmony_ci		printf("\t{\n");
878c2ecf20Sopenharmony_ci		for (j = 0; j < 16; j += 8) {
888c2ecf20Sopenharmony_ci			printf("\t\t");
898c2ecf20Sopenharmony_ci			for (k = 0; k < 8; k++)
908c2ecf20Sopenharmony_ci				printf("0x%02x,%c", gfmul(i, j + k),
918c2ecf20Sopenharmony_ci				       (k == 7) ? '\n' : ' ');
928c2ecf20Sopenharmony_ci		}
938c2ecf20Sopenharmony_ci		for (j = 0; j < 16; j += 8) {
948c2ecf20Sopenharmony_ci			printf("\t\t");
958c2ecf20Sopenharmony_ci			for (k = 0; k < 8; k++)
968c2ecf20Sopenharmony_ci				printf("0x%02x,%c", gfmul(i, (j + k) << 4),
978c2ecf20Sopenharmony_ci				       (k == 7) ? '\n' : ' ');
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci		printf("\t},\n");
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci	printf("};\n");
1028c2ecf20Sopenharmony_ci	printf("#ifdef __KERNEL__\n");
1038c2ecf20Sopenharmony_ci	printf("EXPORT_SYMBOL(raid6_vgfmul);\n");
1048c2ecf20Sopenharmony_ci	printf("#endif\n");
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* Compute power-of-2 table (exponent) */
1078c2ecf20Sopenharmony_ci	v = 1;
1088c2ecf20Sopenharmony_ci	printf("\nconst u8 __attribute__((aligned(256)))\n"
1098c2ecf20Sopenharmony_ci	       "raid6_gfexp[256] =\n" "{\n");
1108c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i += 8) {
1118c2ecf20Sopenharmony_ci		printf("\t");
1128c2ecf20Sopenharmony_ci		for (j = 0; j < 8; j++) {
1138c2ecf20Sopenharmony_ci			exptbl[i + j] = v;
1148c2ecf20Sopenharmony_ci			printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
1158c2ecf20Sopenharmony_ci			v = gfmul(v, 2);
1168c2ecf20Sopenharmony_ci			if (v == 1)
1178c2ecf20Sopenharmony_ci				v = 0;	/* For entry 255, not a real entry */
1188c2ecf20Sopenharmony_ci		}
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci	printf("};\n");
1218c2ecf20Sopenharmony_ci	printf("#ifdef __KERNEL__\n");
1228c2ecf20Sopenharmony_ci	printf("EXPORT_SYMBOL(raid6_gfexp);\n");
1238c2ecf20Sopenharmony_ci	printf("#endif\n");
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* Compute log-of-2 table */
1268c2ecf20Sopenharmony_ci	printf("\nconst u8 __attribute__((aligned(256)))\n"
1278c2ecf20Sopenharmony_ci	       "raid6_gflog[256] =\n" "{\n");
1288c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i += 8) {
1298c2ecf20Sopenharmony_ci		printf("\t");
1308c2ecf20Sopenharmony_ci		for (j = 0; j < 8; j++) {
1318c2ecf20Sopenharmony_ci			v = 255;
1328c2ecf20Sopenharmony_ci			for (k = 0; k < 256; k++)
1338c2ecf20Sopenharmony_ci				if (exptbl[k] == (i + j)) {
1348c2ecf20Sopenharmony_ci					v = k;
1358c2ecf20Sopenharmony_ci					break;
1368c2ecf20Sopenharmony_ci				}
1378c2ecf20Sopenharmony_ci			printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
1388c2ecf20Sopenharmony_ci		}
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci	printf("};\n");
1418c2ecf20Sopenharmony_ci	printf("#ifdef __KERNEL__\n");
1428c2ecf20Sopenharmony_ci	printf("EXPORT_SYMBOL(raid6_gflog);\n");
1438c2ecf20Sopenharmony_ci	printf("#endif\n");
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* Compute inverse table x^-1 == x^254 */
1468c2ecf20Sopenharmony_ci	printf("\nconst u8 __attribute__((aligned(256)))\n"
1478c2ecf20Sopenharmony_ci	       "raid6_gfinv[256] =\n" "{\n");
1488c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i += 8) {
1498c2ecf20Sopenharmony_ci		printf("\t");
1508c2ecf20Sopenharmony_ci		for (j = 0; j < 8; j++) {
1518c2ecf20Sopenharmony_ci			invtbl[i + j] = v = gfpow(i + j, 254);
1528c2ecf20Sopenharmony_ci			printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	printf("};\n");
1568c2ecf20Sopenharmony_ci	printf("#ifdef __KERNEL__\n");
1578c2ecf20Sopenharmony_ci	printf("EXPORT_SYMBOL(raid6_gfinv);\n");
1588c2ecf20Sopenharmony_ci	printf("#endif\n");
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* Compute inv(2^x + 1) (exponent-xor-inverse) table */
1618c2ecf20Sopenharmony_ci	printf("\nconst u8 __attribute__((aligned(256)))\n"
1628c2ecf20Sopenharmony_ci	       "raid6_gfexi[256] =\n" "{\n");
1638c2ecf20Sopenharmony_ci	for (i = 0; i < 256; i += 8) {
1648c2ecf20Sopenharmony_ci		printf("\t");
1658c2ecf20Sopenharmony_ci		for (j = 0; j < 8; j++)
1668c2ecf20Sopenharmony_ci			printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
1678c2ecf20Sopenharmony_ci			       (j == 7) ? '\n' : ' ');
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci	printf("};\n");
1708c2ecf20Sopenharmony_ci	printf("#ifdef __KERNEL__\n");
1718c2ecf20Sopenharmony_ci	printf("EXPORT_SYMBOL(raid6_gfexi);\n");
1728c2ecf20Sopenharmony_ci	printf("#endif\n");
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return 0;
1758c2ecf20Sopenharmony_ci}
176