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