1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* -*- linux-c -*- ------------------------------------------------------- *
3 *
4 *   Copyright 2003 H. Peter Anvin - All Rights Reserved
5 *
6 * ----------------------------------------------------------------------- */
7
8#ifndef LINUX_RAID_RAID6_H
9#define LINUX_RAID_RAID6_H
10
11#ifdef __KERNEL__
12
13/* Set to 1 to use kernel-wide empty_zero_page */
14#define RAID6_USE_EMPTY_ZERO_PAGE 0
15#include <linux/blkdev.h>
16
17/* We need a pre-zeroed page... if we don't want to use the kernel-provided
18   one define it here */
19#if RAID6_USE_EMPTY_ZERO_PAGE
20# define raid6_empty_zero_page empty_zero_page
21#else
22extern const char raid6_empty_zero_page[PAGE_SIZE];
23#endif
24
25#else /* ! __KERNEL__ */
26/* Used for testing in user space */
27
28#include <errno.h>
29#include <inttypes.h>
30#include <stddef.h>
31#include <string.h>
32#include <sys/mman.h>
33#include <sys/time.h>
34#include <sys/types.h>
35
36/* Not standard, but glibc defines it */
37#define BITS_PER_LONG __WORDSIZE
38
39typedef uint8_t  u8;
40typedef uint16_t u16;
41typedef uint32_t u32;
42typedef uint64_t u64;
43
44#ifndef PAGE_SIZE
45# define PAGE_SIZE 4096
46#endif
47#ifndef PAGE_SHIFT
48# define PAGE_SHIFT 12
49#endif
50extern const char raid6_empty_zero_page[PAGE_SIZE];
51
52#define __init
53#define __exit
54#ifndef __attribute_const__
55# define __attribute_const__ __attribute__((const))
56#endif
57#define noinline __attribute__((noinline))
58
59#define preempt_enable()
60#define preempt_disable()
61#define cpu_has_feature(x) 1
62#define enable_kernel_altivec()
63#define disable_kernel_altivec()
64
65#undef	EXPORT_SYMBOL
66#define EXPORT_SYMBOL(sym)
67#undef	EXPORT_SYMBOL_GPL
68#define EXPORT_SYMBOL_GPL(sym)
69#define MODULE_LICENSE(licence)
70#define MODULE_DESCRIPTION(desc)
71#define subsys_initcall(x)
72#define module_exit(x)
73
74#define IS_ENABLED(x) (x)
75#define CONFIG_RAID6_PQ_BENCHMARK 1
76#endif /* __KERNEL__ */
77
78/* Routine choices */
79struct raid6_calls {
80	void (*gen_syndrome)(int, size_t, void **);
81	void (*xor_syndrome)(int, int, int, size_t, void **);
82	int  (*valid)(void);	/* Returns 1 if this routine set is usable */
83	const char *name;	/* Name of this routine set */
84	int priority;		/* Relative priority ranking if non-zero */
85};
86
87/* Selected algorithm */
88extern struct raid6_calls raid6_call;
89
90/* Various routine sets */
91extern const struct raid6_calls raid6_intx1;
92extern const struct raid6_calls raid6_intx2;
93extern const struct raid6_calls raid6_intx4;
94extern const struct raid6_calls raid6_intx8;
95extern const struct raid6_calls raid6_intx16;
96extern const struct raid6_calls raid6_intx32;
97extern const struct raid6_calls raid6_mmxx1;
98extern const struct raid6_calls raid6_mmxx2;
99extern const struct raid6_calls raid6_sse1x1;
100extern const struct raid6_calls raid6_sse1x2;
101extern const struct raid6_calls raid6_sse2x1;
102extern const struct raid6_calls raid6_sse2x2;
103extern const struct raid6_calls raid6_sse2x4;
104extern const struct raid6_calls raid6_altivec1;
105extern const struct raid6_calls raid6_altivec2;
106extern const struct raid6_calls raid6_altivec4;
107extern const struct raid6_calls raid6_altivec8;
108extern const struct raid6_calls raid6_avx2x1;
109extern const struct raid6_calls raid6_avx2x2;
110extern const struct raid6_calls raid6_avx2x4;
111extern const struct raid6_calls raid6_avx512x1;
112extern const struct raid6_calls raid6_avx512x2;
113extern const struct raid6_calls raid6_avx512x4;
114extern const struct raid6_calls raid6_s390vx8;
115extern const struct raid6_calls raid6_vpermxor1;
116extern const struct raid6_calls raid6_vpermxor2;
117extern const struct raid6_calls raid6_vpermxor4;
118extern const struct raid6_calls raid6_vpermxor8;
119
120struct raid6_recov_calls {
121	void (*data2)(int, size_t, int, int, void **);
122	void (*datap)(int, size_t, int, void **);
123	int  (*valid)(void);
124	const char *name;
125	int priority;
126};
127
128extern const struct raid6_recov_calls raid6_recov_intx1;
129extern const struct raid6_recov_calls raid6_recov_ssse3;
130extern const struct raid6_recov_calls raid6_recov_avx2;
131extern const struct raid6_recov_calls raid6_recov_avx512;
132extern const struct raid6_recov_calls raid6_recov_s390xc;
133extern const struct raid6_recov_calls raid6_recov_neon;
134
135extern const struct raid6_calls raid6_neonx1;
136extern const struct raid6_calls raid6_neonx2;
137extern const struct raid6_calls raid6_neonx4;
138extern const struct raid6_calls raid6_neonx8;
139
140/* Algorithm list */
141extern const struct raid6_calls * const raid6_algos[];
142extern const struct raid6_recov_calls *const raid6_recov_algos[];
143int raid6_select_algo(void);
144
145/* Return values from chk_syndrome */
146#define RAID6_OK	0
147#define RAID6_P_BAD	1
148#define RAID6_Q_BAD	2
149#define RAID6_PQ_BAD	3
150
151/* Galois field tables */
152extern const u8 raid6_gfmul[256][256] __attribute__((aligned(256)));
153extern const u8 raid6_vgfmul[256][32] __attribute__((aligned(256)));
154extern const u8 raid6_gfexp[256]      __attribute__((aligned(256)));
155extern const u8 raid6_gflog[256]      __attribute__((aligned(256)));
156extern const u8 raid6_gfinv[256]      __attribute__((aligned(256)));
157extern const u8 raid6_gfexi[256]      __attribute__((aligned(256)));
158
159/* Recovery routines */
160extern void (*raid6_2data_recov)(int disks, size_t bytes, int faila, int failb,
161		       void **ptrs);
162extern void (*raid6_datap_recov)(int disks, size_t bytes, int faila,
163			void **ptrs);
164void raid6_dual_recov(int disks, size_t bytes, int faila, int failb,
165		      void **ptrs);
166
167/* Some definitions to allow code to be compiled for testing in userspace */
168#ifndef __KERNEL__
169
170# define jiffies	raid6_jiffies()
171# define printk 	printf
172# define pr_err(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
173# define pr_info(format, ...) fprintf(stdout, format, ## __VA_ARGS__)
174# define GFP_KERNEL	0
175# define __get_free_pages(x, y)	((unsigned long)mmap(NULL, PAGE_SIZE << (y), \
176						     PROT_READ|PROT_WRITE,   \
177						     MAP_PRIVATE|MAP_ANONYMOUS,\
178						     0, 0))
179# define free_pages(x, y)	munmap((void *)(x), PAGE_SIZE << (y))
180
181static inline void cpu_relax(void)
182{
183	/* Nothing */
184}
185
186#undef  HZ
187#define HZ 1000
188static inline uint32_t raid6_jiffies(void)
189{
190	struct timeval tv;
191	gettimeofday(&tv, NULL);
192	return tv.tv_sec*1000 + tv.tv_usec/1000;
193}
194
195#endif /* ! __KERNEL__ */
196
197#endif /* LINUX_RAID_RAID6_H */
198