xref: /freebsd/sys/contrib/openzfs/module/zcommon/zfs_fletcher.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
7eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
8eda14cbcSMatt Macy  *
9eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
11eda14cbcSMatt Macy  * See the License for the specific language governing permissions
12eda14cbcSMatt Macy  * and limitations under the License.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
15eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
17eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
18eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
19eda14cbcSMatt Macy  *
20eda14cbcSMatt Macy  * CDDL HEADER END
21eda14cbcSMatt Macy  */
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Use is subject to license terms.
25eda14cbcSMatt Macy  * Copyright (C) 2016 Gvozden Nešković. All rights reserved.
26eda14cbcSMatt Macy  */
27eda14cbcSMatt Macy /*
28eda14cbcSMatt Macy  * Copyright 2013 Saso Kiselkov. All rights reserved.
29eda14cbcSMatt Macy  */
30eda14cbcSMatt Macy 
31eda14cbcSMatt Macy /*
32eda14cbcSMatt Macy  * Copyright (c) 2016 by Delphix. All rights reserved.
33eda14cbcSMatt Macy  */
34eda14cbcSMatt Macy 
35eda14cbcSMatt Macy /*
36eda14cbcSMatt Macy  * Fletcher Checksums
37eda14cbcSMatt Macy  * ------------------
38eda14cbcSMatt Macy  *
39eda14cbcSMatt Macy  * ZFS's 2nd and 4th order Fletcher checksums are defined by the following
40eda14cbcSMatt Macy  * recurrence relations:
41eda14cbcSMatt Macy  *
42eda14cbcSMatt Macy  *	a  = a    + f
43eda14cbcSMatt Macy  *	 i    i-1    i-1
44eda14cbcSMatt Macy  *
45eda14cbcSMatt Macy  *	b  = b    + a
46eda14cbcSMatt Macy  *	 i    i-1    i
47eda14cbcSMatt Macy  *
48eda14cbcSMatt Macy  *	c  = c    + b		(fletcher-4 only)
49eda14cbcSMatt Macy  *	 i    i-1    i
50eda14cbcSMatt Macy  *
51eda14cbcSMatt Macy  *	d  = d    + c		(fletcher-4 only)
52eda14cbcSMatt Macy  *	 i    i-1    i
53eda14cbcSMatt Macy  *
54eda14cbcSMatt Macy  * Where
55eda14cbcSMatt Macy  *	a_0 = b_0 = c_0 = d_0 = 0
56eda14cbcSMatt Macy  * and
57eda14cbcSMatt Macy  *	f_0 .. f_(n-1) are the input data.
58eda14cbcSMatt Macy  *
59eda14cbcSMatt Macy  * Using standard techniques, these translate into the following series:
60eda14cbcSMatt Macy  *
61eda14cbcSMatt Macy  *	     __n_			     __n_
62eda14cbcSMatt Macy  *	     \   |			     \   |
63eda14cbcSMatt Macy  *	a  =  >     f			b  =  >     i * f
64eda14cbcSMatt Macy  *	 n   /___|   n - i		 n   /___|	 n - i
65eda14cbcSMatt Macy  *	     i = 1			     i = 1
66eda14cbcSMatt Macy  *
67eda14cbcSMatt Macy  *
68eda14cbcSMatt Macy  *	     __n_			     __n_
69eda14cbcSMatt Macy  *	     \   |  i*(i+1)		     \   |  i*(i+1)*(i+2)
70eda14cbcSMatt Macy  *	c  =  >     ------- f		d  =  >     ------------- f
71eda14cbcSMatt Macy  *	 n   /___|     2     n - i	 n   /___|	  6	   n - i
72eda14cbcSMatt Macy  *	     i = 1			     i = 1
73eda14cbcSMatt Macy  *
74eda14cbcSMatt Macy  * For fletcher-2, the f_is are 64-bit, and [ab]_i are 64-bit accumulators.
75eda14cbcSMatt Macy  * Since the additions are done mod (2^64), errors in the high bits may not
76eda14cbcSMatt Macy  * be noticed.  For this reason, fletcher-2 is deprecated.
77eda14cbcSMatt Macy  *
78eda14cbcSMatt Macy  * For fletcher-4, the f_is are 32-bit, and [abcd]_i are 64-bit accumulators.
79eda14cbcSMatt Macy  * A conservative estimate of how big the buffer can get before we overflow
80eda14cbcSMatt Macy  * can be estimated using f_i = 0xffffffff for all i:
81eda14cbcSMatt Macy  *
82eda14cbcSMatt Macy  * % bc
83eda14cbcSMatt Macy  *  f=2^32-1;d=0; for (i = 1; d<2^64; i++) { d += f*i*(i+1)*(i+2)/6 }; (i-1)*4
84eda14cbcSMatt Macy  * 2264
85eda14cbcSMatt Macy  *  quit
86eda14cbcSMatt Macy  * %
87eda14cbcSMatt Macy  *
88eda14cbcSMatt Macy  * So blocks of up to 2k will not overflow.  Our largest block size is
89eda14cbcSMatt Macy  * 128k, which has 32k 4-byte words, so we can compute the largest possible
90eda14cbcSMatt Macy  * accumulators, then divide by 2^64 to figure the max amount of overflow:
91eda14cbcSMatt Macy  *
92eda14cbcSMatt Macy  * % bc
93eda14cbcSMatt Macy  *  a=b=c=d=0; f=2^32-1; for (i=1; i<=32*1024; i++) { a+=f; b+=a; c+=b; d+=c }
94eda14cbcSMatt Macy  *  a/2^64;b/2^64;c/2^64;d/2^64
95eda14cbcSMatt Macy  * 0
96eda14cbcSMatt Macy  * 0
97eda14cbcSMatt Macy  * 1365
98eda14cbcSMatt Macy  * 11186858
99eda14cbcSMatt Macy  *  quit
100eda14cbcSMatt Macy  * %
101eda14cbcSMatt Macy  *
102eda14cbcSMatt Macy  * So a and b cannot overflow.  To make sure each bit of input has some
103eda14cbcSMatt Macy  * effect on the contents of c and d, we can look at what the factors of
104eda14cbcSMatt Macy  * the coefficients in the equations for c_n and d_n are.  The number of 2s
105eda14cbcSMatt Macy  * in the factors determines the lowest set bit in the multiplier.  Running
106eda14cbcSMatt Macy  * through the cases for n*(n+1)/2 reveals that the highest power of 2 is
107eda14cbcSMatt Macy  * 2^14, and for n*(n+1)*(n+2)/6 it is 2^15.  So while some data may overflow
108eda14cbcSMatt Macy  * the 64-bit accumulators, every bit of every f_i effects every accumulator,
109eda14cbcSMatt Macy  * even for 128k blocks.
110eda14cbcSMatt Macy  *
111eda14cbcSMatt Macy  * If we wanted to make a stronger version of fletcher4 (fletcher4c?),
112eda14cbcSMatt Macy  * we could do our calculations mod (2^32 - 1) by adding in the carries
113eda14cbcSMatt Macy  * periodically, and store the number of carries in the top 32-bits.
114eda14cbcSMatt Macy  *
115eda14cbcSMatt Macy  * --------------------
116eda14cbcSMatt Macy  * Checksum Performance
117eda14cbcSMatt Macy  * --------------------
118eda14cbcSMatt Macy  *
119eda14cbcSMatt Macy  * There are two interesting components to checksum performance: cached and
120eda14cbcSMatt Macy  * uncached performance.  With cached data, fletcher-2 is about four times
121eda14cbcSMatt Macy  * faster than fletcher-4.  With uncached data, the performance difference is
122eda14cbcSMatt Macy  * negligible, since the cost of a cache fill dominates the processing time.
123eda14cbcSMatt Macy  * Even though fletcher-4 is slower than fletcher-2, it is still a pretty
124eda14cbcSMatt Macy  * efficient pass over the data.
125eda14cbcSMatt Macy  *
126eda14cbcSMatt Macy  * In normal operation, the data which is being checksummed is in a buffer
127eda14cbcSMatt Macy  * which has been filled either by:
128eda14cbcSMatt Macy  *
129eda14cbcSMatt Macy  *	1. a compression step, which will be mostly cached, or
130da5137abSMartin Matuska  *	2. a memcpy() or copyin(), which will be uncached
131da5137abSMartin Matuska  *	   (because the copy is cache-bypassing).
132eda14cbcSMatt Macy  *
133eda14cbcSMatt Macy  * For both cached and uncached data, both fletcher checksums are much faster
134eda14cbcSMatt Macy  * than sha-256, and slower than 'off', which doesn't touch the data at all.
135eda14cbcSMatt Macy  */
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy #include <sys/types.h>
138eda14cbcSMatt Macy #include <sys/sysmacros.h>
139eda14cbcSMatt Macy #include <sys/byteorder.h>
140eda14cbcSMatt Macy #include <sys/simd.h>
1412a58b312SMartin Matuska #include <sys/spa.h>
142eda14cbcSMatt Macy #include <sys/zio_checksum.h>
143eda14cbcSMatt Macy #include <sys/zfs_context.h>
144eda14cbcSMatt Macy #include <zfs_fletcher.h>
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy #define	FLETCHER_MIN_SIMD_SIZE	64
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy static void fletcher_4_scalar_init(fletcher_4_ctx_t *ctx);
149eda14cbcSMatt Macy static void fletcher_4_scalar_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp);
150eda14cbcSMatt Macy static void fletcher_4_scalar_native(fletcher_4_ctx_t *ctx,
151eda14cbcSMatt Macy     const void *buf, uint64_t size);
152eda14cbcSMatt Macy static void fletcher_4_scalar_byteswap(fletcher_4_ctx_t *ctx,
153eda14cbcSMatt Macy     const void *buf, uint64_t size);
154eda14cbcSMatt Macy static boolean_t fletcher_4_scalar_valid(void);
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy static const fletcher_4_ops_t fletcher_4_scalar_ops = {
157eda14cbcSMatt Macy 	.init_native = fletcher_4_scalar_init,
158eda14cbcSMatt Macy 	.fini_native = fletcher_4_scalar_fini,
159eda14cbcSMatt Macy 	.compute_native = fletcher_4_scalar_native,
160eda14cbcSMatt Macy 	.init_byteswap = fletcher_4_scalar_init,
161eda14cbcSMatt Macy 	.fini_byteswap = fletcher_4_scalar_fini,
162eda14cbcSMatt Macy 	.compute_byteswap = fletcher_4_scalar_byteswap,
163eda14cbcSMatt Macy 	.valid = fletcher_4_scalar_valid,
1642a58b312SMartin Matuska 	.uses_fpu = B_FALSE,
165eda14cbcSMatt Macy 	.name = "scalar"
166eda14cbcSMatt Macy };
167eda14cbcSMatt Macy 
168eda14cbcSMatt Macy static fletcher_4_ops_t fletcher_4_fastest_impl = {
169eda14cbcSMatt Macy 	.name = "fastest",
170eda14cbcSMatt Macy 	.valid = fletcher_4_scalar_valid
171eda14cbcSMatt Macy };
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy static const fletcher_4_ops_t *fletcher_4_impls[] = {
174eda14cbcSMatt Macy 	&fletcher_4_scalar_ops,
175eda14cbcSMatt Macy 	&fletcher_4_superscalar_ops,
176eda14cbcSMatt Macy 	&fletcher_4_superscalar4_ops,
177eda14cbcSMatt Macy #if defined(HAVE_SSE2)
178eda14cbcSMatt Macy 	&fletcher_4_sse2_ops,
179eda14cbcSMatt Macy #endif
180eda14cbcSMatt Macy #if defined(HAVE_SSE2) && defined(HAVE_SSSE3)
181eda14cbcSMatt Macy 	&fletcher_4_ssse3_ops,
182eda14cbcSMatt Macy #endif
183eda14cbcSMatt Macy #if defined(HAVE_AVX) && defined(HAVE_AVX2)
184eda14cbcSMatt Macy 	&fletcher_4_avx2_ops,
185eda14cbcSMatt Macy #endif
186eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_AVX512F)
187eda14cbcSMatt Macy 	&fletcher_4_avx512f_ops,
188eda14cbcSMatt Macy #endif
189eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_AVX512BW)
190eda14cbcSMatt Macy 	&fletcher_4_avx512bw_ops,
191eda14cbcSMatt Macy #endif
192ac0bf12eSMatt Macy #if defined(__aarch64__) && !defined(__FreeBSD__)
193eda14cbcSMatt Macy 	&fletcher_4_aarch64_neon_ops,
194eda14cbcSMatt Macy #endif
195eda14cbcSMatt Macy };
196eda14cbcSMatt Macy 
197eda14cbcSMatt Macy /* Hold all supported implementations */
198eda14cbcSMatt Macy static uint32_t fletcher_4_supp_impls_cnt = 0;
199eda14cbcSMatt Macy static fletcher_4_ops_t *fletcher_4_supp_impls[ARRAY_SIZE(fletcher_4_impls)];
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy /* Select fletcher4 implementation */
202eda14cbcSMatt Macy #define	IMPL_FASTEST	(UINT32_MAX)
203eda14cbcSMatt Macy #define	IMPL_CYCLE	(UINT32_MAX - 1)
204eda14cbcSMatt Macy #define	IMPL_SCALAR	(0)
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy static uint32_t fletcher_4_impl_chosen = IMPL_FASTEST;
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy #define	IMPL_READ(i)	(*(volatile uint32_t *) &(i))
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy static struct fletcher_4_impl_selector {
211eda14cbcSMatt Macy 	const char	*fis_name;
212eda14cbcSMatt Macy 	uint32_t	fis_sel;
213eda14cbcSMatt Macy } fletcher_4_impl_selectors[] = {
214eda14cbcSMatt Macy 	{ "cycle",	IMPL_CYCLE },
215eda14cbcSMatt Macy 	{ "fastest",	IMPL_FASTEST },
216eda14cbcSMatt Macy 	{ "scalar",	IMPL_SCALAR }
217eda14cbcSMatt Macy };
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy #if defined(_KERNEL)
220eda14cbcSMatt Macy static kstat_t *fletcher_4_kstat;
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy static struct fletcher_4_kstat {
223eda14cbcSMatt Macy 	uint64_t native;
224eda14cbcSMatt Macy 	uint64_t byteswap;
225eda14cbcSMatt Macy } fletcher_4_stat_data[ARRAY_SIZE(fletcher_4_impls) + 1];
226eda14cbcSMatt Macy #endif
227eda14cbcSMatt Macy 
228eda14cbcSMatt Macy /* Indicate that benchmark has been completed */
229eda14cbcSMatt Macy static boolean_t fletcher_4_initialized = B_FALSE;
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy void
fletcher_init(zio_cksum_t * zcp)232eda14cbcSMatt Macy fletcher_init(zio_cksum_t *zcp)
233eda14cbcSMatt Macy {
234eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
235eda14cbcSMatt Macy }
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy int
fletcher_2_incremental_native(void * buf,size_t size,void * data)238eda14cbcSMatt Macy fletcher_2_incremental_native(void *buf, size_t size, void *data)
239eda14cbcSMatt Macy {
240eda14cbcSMatt Macy 	zio_cksum_t *zcp = data;
241eda14cbcSMatt Macy 
242eda14cbcSMatt Macy 	const uint64_t *ip = buf;
243eda14cbcSMatt Macy 	const uint64_t *ipend = ip + (size / sizeof (uint64_t));
244eda14cbcSMatt Macy 	uint64_t a0, b0, a1, b1;
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy 	a0 = zcp->zc_word[0];
247eda14cbcSMatt Macy 	a1 = zcp->zc_word[1];
248eda14cbcSMatt Macy 	b0 = zcp->zc_word[2];
249eda14cbcSMatt Macy 	b1 = zcp->zc_word[3];
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 	for (; ip < ipend; ip += 2) {
252eda14cbcSMatt Macy 		a0 += ip[0];
253eda14cbcSMatt Macy 		a1 += ip[1];
254eda14cbcSMatt Macy 		b0 += a0;
255eda14cbcSMatt Macy 		b1 += a1;
256eda14cbcSMatt Macy 	}
257eda14cbcSMatt Macy 
258eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
259eda14cbcSMatt Macy 	return (0);
260eda14cbcSMatt Macy }
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy void
fletcher_2_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)263eda14cbcSMatt Macy fletcher_2_native(const void *buf, uint64_t size,
264eda14cbcSMatt Macy     const void *ctx_template, zio_cksum_t *zcp)
265eda14cbcSMatt Macy {
266e92ffd9bSMartin Matuska 	(void) ctx_template;
267eda14cbcSMatt Macy 	fletcher_init(zcp);
268eda14cbcSMatt Macy 	(void) fletcher_2_incremental_native((void *) buf, size, zcp);
269eda14cbcSMatt Macy }
270eda14cbcSMatt Macy 
271eda14cbcSMatt Macy int
fletcher_2_incremental_byteswap(void * buf,size_t size,void * data)272eda14cbcSMatt Macy fletcher_2_incremental_byteswap(void *buf, size_t size, void *data)
273eda14cbcSMatt Macy {
274eda14cbcSMatt Macy 	zio_cksum_t *zcp = data;
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy 	const uint64_t *ip = buf;
277eda14cbcSMatt Macy 	const uint64_t *ipend = ip + (size / sizeof (uint64_t));
278eda14cbcSMatt Macy 	uint64_t a0, b0, a1, b1;
279eda14cbcSMatt Macy 
280eda14cbcSMatt Macy 	a0 = zcp->zc_word[0];
281eda14cbcSMatt Macy 	a1 = zcp->zc_word[1];
282eda14cbcSMatt Macy 	b0 = zcp->zc_word[2];
283eda14cbcSMatt Macy 	b1 = zcp->zc_word[3];
284eda14cbcSMatt Macy 
285eda14cbcSMatt Macy 	for (; ip < ipend; ip += 2) {
286eda14cbcSMatt Macy 		a0 += BSWAP_64(ip[0]);
287eda14cbcSMatt Macy 		a1 += BSWAP_64(ip[1]);
288eda14cbcSMatt Macy 		b0 += a0;
289eda14cbcSMatt Macy 		b1 += a1;
290eda14cbcSMatt Macy 	}
291eda14cbcSMatt Macy 
292eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
293eda14cbcSMatt Macy 	return (0);
294eda14cbcSMatt Macy }
295eda14cbcSMatt Macy 
296eda14cbcSMatt Macy void
fletcher_2_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)297eda14cbcSMatt Macy fletcher_2_byteswap(const void *buf, uint64_t size,
298eda14cbcSMatt Macy     const void *ctx_template, zio_cksum_t *zcp)
299eda14cbcSMatt Macy {
300e92ffd9bSMartin Matuska 	(void) ctx_template;
301eda14cbcSMatt Macy 	fletcher_init(zcp);
302eda14cbcSMatt Macy 	(void) fletcher_2_incremental_byteswap((void *) buf, size, zcp);
303eda14cbcSMatt Macy }
304eda14cbcSMatt Macy 
305eda14cbcSMatt Macy static void
fletcher_4_scalar_init(fletcher_4_ctx_t * ctx)306eda14cbcSMatt Macy fletcher_4_scalar_init(fletcher_4_ctx_t *ctx)
307eda14cbcSMatt Macy {
308eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(&ctx->scalar, 0, 0, 0, 0);
309eda14cbcSMatt Macy }
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy static void
fletcher_4_scalar_fini(fletcher_4_ctx_t * ctx,zio_cksum_t * zcp)312eda14cbcSMatt Macy fletcher_4_scalar_fini(fletcher_4_ctx_t *ctx, zio_cksum_t *zcp)
313eda14cbcSMatt Macy {
314eda14cbcSMatt Macy 	memcpy(zcp, &ctx->scalar, sizeof (zio_cksum_t));
315eda14cbcSMatt Macy }
316eda14cbcSMatt Macy 
317eda14cbcSMatt Macy static void
fletcher_4_scalar_native(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)318eda14cbcSMatt Macy fletcher_4_scalar_native(fletcher_4_ctx_t *ctx, const void *buf,
319eda14cbcSMatt Macy     uint64_t size)
320eda14cbcSMatt Macy {
321eda14cbcSMatt Macy 	const uint32_t *ip = buf;
322eda14cbcSMatt Macy 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
323eda14cbcSMatt Macy 	uint64_t a, b, c, d;
324eda14cbcSMatt Macy 
325eda14cbcSMatt Macy 	a = ctx->scalar.zc_word[0];
326eda14cbcSMatt Macy 	b = ctx->scalar.zc_word[1];
327eda14cbcSMatt Macy 	c = ctx->scalar.zc_word[2];
328eda14cbcSMatt Macy 	d = ctx->scalar.zc_word[3];
329eda14cbcSMatt Macy 
330eda14cbcSMatt Macy 	for (; ip < ipend; ip++) {
331eda14cbcSMatt Macy 		a += ip[0];
332eda14cbcSMatt Macy 		b += a;
333eda14cbcSMatt Macy 		c += b;
334eda14cbcSMatt Macy 		d += c;
335eda14cbcSMatt Macy 	}
336eda14cbcSMatt Macy 
337eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(&ctx->scalar, a, b, c, d);
338eda14cbcSMatt Macy }
339eda14cbcSMatt Macy 
340eda14cbcSMatt Macy static void
fletcher_4_scalar_byteswap(fletcher_4_ctx_t * ctx,const void * buf,uint64_t size)341eda14cbcSMatt Macy fletcher_4_scalar_byteswap(fletcher_4_ctx_t *ctx, const void *buf,
342eda14cbcSMatt Macy     uint64_t size)
343eda14cbcSMatt Macy {
344eda14cbcSMatt Macy 	const uint32_t *ip = buf;
345eda14cbcSMatt Macy 	const uint32_t *ipend = ip + (size / sizeof (uint32_t));
346eda14cbcSMatt Macy 	uint64_t a, b, c, d;
347eda14cbcSMatt Macy 
348eda14cbcSMatt Macy 	a = ctx->scalar.zc_word[0];
349eda14cbcSMatt Macy 	b = ctx->scalar.zc_word[1];
350eda14cbcSMatt Macy 	c = ctx->scalar.zc_word[2];
351eda14cbcSMatt Macy 	d = ctx->scalar.zc_word[3];
352eda14cbcSMatt Macy 
353eda14cbcSMatt Macy 	for (; ip < ipend; ip++) {
354eda14cbcSMatt Macy 		a += BSWAP_32(ip[0]);
355eda14cbcSMatt Macy 		b += a;
356eda14cbcSMatt Macy 		c += b;
357eda14cbcSMatt Macy 		d += c;
358eda14cbcSMatt Macy 	}
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(&ctx->scalar, a, b, c, d);
361eda14cbcSMatt Macy }
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy static boolean_t
fletcher_4_scalar_valid(void)364eda14cbcSMatt Macy fletcher_4_scalar_valid(void)
365eda14cbcSMatt Macy {
366eda14cbcSMatt Macy 	return (B_TRUE);
367eda14cbcSMatt Macy }
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy int
fletcher_4_impl_set(const char * val)370eda14cbcSMatt Macy fletcher_4_impl_set(const char *val)
371eda14cbcSMatt Macy {
372eda14cbcSMatt Macy 	int err = -EINVAL;
373eda14cbcSMatt Macy 	uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
374eda14cbcSMatt Macy 	size_t i, val_len;
375eda14cbcSMatt Macy 
376eda14cbcSMatt Macy 	val_len = strlen(val);
377eda14cbcSMatt Macy 	while ((val_len > 0) && !!isspace(val[val_len-1])) /* trim '\n' */
378eda14cbcSMatt Macy 		val_len--;
379eda14cbcSMatt Macy 
380eda14cbcSMatt Macy 	/* check mandatory implementations */
381eda14cbcSMatt Macy 	for (i = 0; i < ARRAY_SIZE(fletcher_4_impl_selectors); i++) {
382eda14cbcSMatt Macy 		const char *name = fletcher_4_impl_selectors[i].fis_name;
383eda14cbcSMatt Macy 
384eda14cbcSMatt Macy 		if (val_len == strlen(name) &&
385eda14cbcSMatt Macy 		    strncmp(val, name, val_len) == 0) {
386eda14cbcSMatt Macy 			impl = fletcher_4_impl_selectors[i].fis_sel;
387eda14cbcSMatt Macy 			err = 0;
388eda14cbcSMatt Macy 			break;
389eda14cbcSMatt Macy 		}
390eda14cbcSMatt Macy 	}
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy 	if (err != 0 && fletcher_4_initialized) {
393eda14cbcSMatt Macy 		/* check all supported implementations */
394eda14cbcSMatt Macy 		for (i = 0; i < fletcher_4_supp_impls_cnt; i++) {
395eda14cbcSMatt Macy 			const char *name = fletcher_4_supp_impls[i]->name;
396eda14cbcSMatt Macy 
397eda14cbcSMatt Macy 			if (val_len == strlen(name) &&
398eda14cbcSMatt Macy 			    strncmp(val, name, val_len) == 0) {
399eda14cbcSMatt Macy 				impl = i;
400eda14cbcSMatt Macy 				err = 0;
401eda14cbcSMatt Macy 				break;
402eda14cbcSMatt Macy 			}
403eda14cbcSMatt Macy 		}
404eda14cbcSMatt Macy 	}
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 	if (err == 0) {
407eda14cbcSMatt Macy 		atomic_swap_32(&fletcher_4_impl_chosen, impl);
408eda14cbcSMatt Macy 		membar_producer();
409eda14cbcSMatt Macy 	}
410eda14cbcSMatt Macy 
411eda14cbcSMatt Macy 	return (err);
412eda14cbcSMatt Macy }
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy /*
415eda14cbcSMatt Macy  * Returns the Fletcher 4 operations for checksums.   When a SIMD
416eda14cbcSMatt Macy  * implementation is not allowed in the current context, then fallback
417eda14cbcSMatt Macy  * to the fastest generic implementation.
418eda14cbcSMatt Macy  */
419eda14cbcSMatt Macy static inline const fletcher_4_ops_t *
fletcher_4_impl_get(void)420eda14cbcSMatt Macy fletcher_4_impl_get(void)
421eda14cbcSMatt Macy {
422eda14cbcSMatt Macy 	if (!kfpu_allowed())
423eda14cbcSMatt Macy 		return (&fletcher_4_superscalar4_ops);
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 	const fletcher_4_ops_t *ops = NULL;
426eda14cbcSMatt Macy 	uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy 	switch (impl) {
429eda14cbcSMatt Macy 	case IMPL_FASTEST:
430eda14cbcSMatt Macy 		ASSERT(fletcher_4_initialized);
431eda14cbcSMatt Macy 		ops = &fletcher_4_fastest_impl;
432eda14cbcSMatt Macy 		break;
433eda14cbcSMatt Macy 	case IMPL_CYCLE:
434eda14cbcSMatt Macy 		/* Cycle through supported implementations */
435eda14cbcSMatt Macy 		ASSERT(fletcher_4_initialized);
436eda14cbcSMatt Macy 		ASSERT3U(fletcher_4_supp_impls_cnt, >, 0);
437eda14cbcSMatt Macy 		static uint32_t cycle_count = 0;
438eda14cbcSMatt Macy 		uint32_t idx = (++cycle_count) % fletcher_4_supp_impls_cnt;
439eda14cbcSMatt Macy 		ops = fletcher_4_supp_impls[idx];
440eda14cbcSMatt Macy 		break;
441eda14cbcSMatt Macy 	default:
442eda14cbcSMatt Macy 		ASSERT3U(fletcher_4_supp_impls_cnt, >, 0);
443eda14cbcSMatt Macy 		ASSERT3U(impl, <, fletcher_4_supp_impls_cnt);
444eda14cbcSMatt Macy 		ops = fletcher_4_supp_impls[impl];
445eda14cbcSMatt Macy 		break;
446eda14cbcSMatt Macy 	}
447eda14cbcSMatt Macy 
448eda14cbcSMatt Macy 	ASSERT3P(ops, !=, NULL);
449eda14cbcSMatt Macy 
450eda14cbcSMatt Macy 	return (ops);
451eda14cbcSMatt Macy }
452eda14cbcSMatt Macy 
453eda14cbcSMatt Macy static inline void
fletcher_4_native_impl(const void * buf,uint64_t size,zio_cksum_t * zcp)454eda14cbcSMatt Macy fletcher_4_native_impl(const void *buf, uint64_t size, zio_cksum_t *zcp)
455eda14cbcSMatt Macy {
456eda14cbcSMatt Macy 	fletcher_4_ctx_t ctx;
457eda14cbcSMatt Macy 	const fletcher_4_ops_t *ops = fletcher_4_impl_get();
458eda14cbcSMatt Macy 
4592a58b312SMartin Matuska 	if (ops->uses_fpu == B_TRUE) {
4602a58b312SMartin Matuska 		kfpu_begin();
4612a58b312SMartin Matuska 	}
462eda14cbcSMatt Macy 	ops->init_native(&ctx);
463eda14cbcSMatt Macy 	ops->compute_native(&ctx, buf, size);
464eda14cbcSMatt Macy 	ops->fini_native(&ctx, zcp);
4652a58b312SMartin Matuska 	if (ops->uses_fpu == B_TRUE) {
4662a58b312SMartin Matuska 		kfpu_end();
4672a58b312SMartin Matuska 	}
468eda14cbcSMatt Macy }
469eda14cbcSMatt Macy 
470eda14cbcSMatt Macy void
fletcher_4_native(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)471eda14cbcSMatt Macy fletcher_4_native(const void *buf, uint64_t size,
472eda14cbcSMatt Macy     const void *ctx_template, zio_cksum_t *zcp)
473eda14cbcSMatt Macy {
474e92ffd9bSMartin Matuska 	(void) ctx_template;
475aca928a5SMartin Matuska 	const uint64_t p2size = P2ALIGN_TYPED(size, FLETCHER_MIN_SIMD_SIZE,
476aca928a5SMartin Matuska 	    uint64_t);
477eda14cbcSMatt Macy 
478eda14cbcSMatt Macy 	ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 	if (size == 0 || p2size == 0) {
481eda14cbcSMatt Macy 		ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy 		if (size > 0)
484eda14cbcSMatt Macy 			fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp,
485eda14cbcSMatt Macy 			    buf, size);
486eda14cbcSMatt Macy 	} else {
487eda14cbcSMatt Macy 		fletcher_4_native_impl(buf, p2size, zcp);
488eda14cbcSMatt Macy 
489eda14cbcSMatt Macy 		if (p2size < size)
490eda14cbcSMatt Macy 			fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp,
491eda14cbcSMatt Macy 			    (char *)buf + p2size, size - p2size);
492eda14cbcSMatt Macy 	}
493eda14cbcSMatt Macy }
494eda14cbcSMatt Macy 
495eda14cbcSMatt Macy void
fletcher_4_native_varsize(const void * buf,uint64_t size,zio_cksum_t * zcp)496eda14cbcSMatt Macy fletcher_4_native_varsize(const void *buf, uint64_t size, zio_cksum_t *zcp)
497eda14cbcSMatt Macy {
498eda14cbcSMatt Macy 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
499eda14cbcSMatt Macy 	fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp, buf, size);
500eda14cbcSMatt Macy }
501eda14cbcSMatt Macy 
502eda14cbcSMatt Macy static inline void
fletcher_4_byteswap_impl(const void * buf,uint64_t size,zio_cksum_t * zcp)503eda14cbcSMatt Macy fletcher_4_byteswap_impl(const void *buf, uint64_t size, zio_cksum_t *zcp)
504eda14cbcSMatt Macy {
505eda14cbcSMatt Macy 	fletcher_4_ctx_t ctx;
506eda14cbcSMatt Macy 	const fletcher_4_ops_t *ops = fletcher_4_impl_get();
507eda14cbcSMatt Macy 
5082a58b312SMartin Matuska 	if (ops->uses_fpu == B_TRUE) {
5092a58b312SMartin Matuska 		kfpu_begin();
5102a58b312SMartin Matuska 	}
511eda14cbcSMatt Macy 	ops->init_byteswap(&ctx);
512eda14cbcSMatt Macy 	ops->compute_byteswap(&ctx, buf, size);
513eda14cbcSMatt Macy 	ops->fini_byteswap(&ctx, zcp);
5142a58b312SMartin Matuska 	if (ops->uses_fpu == B_TRUE) {
5152a58b312SMartin Matuska 		kfpu_end();
5162a58b312SMartin Matuska 	}
517eda14cbcSMatt Macy }
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy void
fletcher_4_byteswap(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)520eda14cbcSMatt Macy fletcher_4_byteswap(const void *buf, uint64_t size,
521eda14cbcSMatt Macy     const void *ctx_template, zio_cksum_t *zcp)
522eda14cbcSMatt Macy {
523e92ffd9bSMartin Matuska 	(void) ctx_template;
524aca928a5SMartin Matuska 	const uint64_t p2size = P2ALIGN_TYPED(size, FLETCHER_MIN_SIMD_SIZE,
525aca928a5SMartin Matuska 	    uint64_t);
526eda14cbcSMatt Macy 
527eda14cbcSMatt Macy 	ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
528eda14cbcSMatt Macy 
529eda14cbcSMatt Macy 	if (size == 0 || p2size == 0) {
530eda14cbcSMatt Macy 		ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
531eda14cbcSMatt Macy 
532eda14cbcSMatt Macy 		if (size > 0)
533eda14cbcSMatt Macy 			fletcher_4_scalar_byteswap((fletcher_4_ctx_t *)zcp,
534eda14cbcSMatt Macy 			    buf, size);
535eda14cbcSMatt Macy 	} else {
536eda14cbcSMatt Macy 		fletcher_4_byteswap_impl(buf, p2size, zcp);
537eda14cbcSMatt Macy 
538eda14cbcSMatt Macy 		if (p2size < size)
539eda14cbcSMatt Macy 			fletcher_4_scalar_byteswap((fletcher_4_ctx_t *)zcp,
540eda14cbcSMatt Macy 			    (char *)buf + p2size, size - p2size);
541eda14cbcSMatt Macy 	}
542eda14cbcSMatt Macy }
543eda14cbcSMatt Macy 
544eda14cbcSMatt Macy /* Incremental Fletcher 4 */
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy #define	ZFS_FLETCHER_4_INC_MAX_SIZE	(8ULL << 20)
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy static inline void
fletcher_4_incremental_combine(zio_cksum_t * zcp,const uint64_t size,const zio_cksum_t * nzcp)549eda14cbcSMatt Macy fletcher_4_incremental_combine(zio_cksum_t *zcp, const uint64_t size,
550eda14cbcSMatt Macy     const zio_cksum_t *nzcp)
551eda14cbcSMatt Macy {
552eda14cbcSMatt Macy 	const uint64_t c1 = size / sizeof (uint32_t);
553eda14cbcSMatt Macy 	const uint64_t c2 = c1 * (c1 + 1) / 2;
554eda14cbcSMatt Macy 	const uint64_t c3 = c2 * (c1 + 2) / 3;
555eda14cbcSMatt Macy 
556eda14cbcSMatt Macy 	/*
557eda14cbcSMatt Macy 	 * Value of 'c3' overflows on buffer sizes close to 16MiB. For that
558eda14cbcSMatt Macy 	 * reason we split incremental fletcher4 computation of large buffers
559eda14cbcSMatt Macy 	 * to steps of (ZFS_FLETCHER_4_INC_MAX_SIZE) size.
560eda14cbcSMatt Macy 	 */
561eda14cbcSMatt Macy 	ASSERT3U(size, <=, ZFS_FLETCHER_4_INC_MAX_SIZE);
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy 	zcp->zc_word[3] += nzcp->zc_word[3] + c1 * zcp->zc_word[2] +
564eda14cbcSMatt Macy 	    c2 * zcp->zc_word[1] + c3 * zcp->zc_word[0];
565eda14cbcSMatt Macy 	zcp->zc_word[2] += nzcp->zc_word[2] + c1 * zcp->zc_word[1] +
566eda14cbcSMatt Macy 	    c2 * zcp->zc_word[0];
567eda14cbcSMatt Macy 	zcp->zc_word[1] += nzcp->zc_word[1] + c1 * zcp->zc_word[0];
568eda14cbcSMatt Macy 	zcp->zc_word[0] += nzcp->zc_word[0];
569eda14cbcSMatt Macy }
570eda14cbcSMatt Macy 
571eda14cbcSMatt Macy static inline void
fletcher_4_incremental_impl(boolean_t native,const void * buf,uint64_t size,zio_cksum_t * zcp)572eda14cbcSMatt Macy fletcher_4_incremental_impl(boolean_t native, const void *buf, uint64_t size,
573eda14cbcSMatt Macy     zio_cksum_t *zcp)
574eda14cbcSMatt Macy {
575eda14cbcSMatt Macy 	while (size > 0) {
576eda14cbcSMatt Macy 		zio_cksum_t nzc;
577eda14cbcSMatt Macy 		uint64_t len = MIN(size, ZFS_FLETCHER_4_INC_MAX_SIZE);
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 		if (native)
580eda14cbcSMatt Macy 			fletcher_4_native(buf, len, NULL, &nzc);
581eda14cbcSMatt Macy 		else
582eda14cbcSMatt Macy 			fletcher_4_byteswap(buf, len, NULL, &nzc);
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy 		fletcher_4_incremental_combine(zcp, len, &nzc);
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy 		size -= len;
587eda14cbcSMatt Macy 		buf += len;
588eda14cbcSMatt Macy 	}
589eda14cbcSMatt Macy }
590eda14cbcSMatt Macy 
591eda14cbcSMatt Macy int
fletcher_4_incremental_native(void * buf,size_t size,void * data)592eda14cbcSMatt Macy fletcher_4_incremental_native(void *buf, size_t size, void *data)
593eda14cbcSMatt Macy {
594eda14cbcSMatt Macy 	zio_cksum_t *zcp = data;
595eda14cbcSMatt Macy 	/* Use scalar impl to directly update cksum of small blocks */
596eda14cbcSMatt Macy 	if (size < SPA_MINBLOCKSIZE)
597eda14cbcSMatt Macy 		fletcher_4_scalar_native((fletcher_4_ctx_t *)zcp, buf, size);
598eda14cbcSMatt Macy 	else
599eda14cbcSMatt Macy 		fletcher_4_incremental_impl(B_TRUE, buf, size, zcp);
600eda14cbcSMatt Macy 	return (0);
601eda14cbcSMatt Macy }
602eda14cbcSMatt Macy 
603eda14cbcSMatt Macy int
fletcher_4_incremental_byteswap(void * buf,size_t size,void * data)604eda14cbcSMatt Macy fletcher_4_incremental_byteswap(void *buf, size_t size, void *data)
605eda14cbcSMatt Macy {
606eda14cbcSMatt Macy 	zio_cksum_t *zcp = data;
607eda14cbcSMatt Macy 	/* Use scalar impl to directly update cksum of small blocks */
608eda14cbcSMatt Macy 	if (size < SPA_MINBLOCKSIZE)
609eda14cbcSMatt Macy 		fletcher_4_scalar_byteswap((fletcher_4_ctx_t *)zcp, buf, size);
610eda14cbcSMatt Macy 	else
611eda14cbcSMatt Macy 		fletcher_4_incremental_impl(B_FALSE, buf, size, zcp);
612eda14cbcSMatt Macy 	return (0);
613eda14cbcSMatt Macy }
614eda14cbcSMatt Macy 
615eda14cbcSMatt Macy #if defined(_KERNEL)
616eda14cbcSMatt Macy /*
617eda14cbcSMatt Macy  * Fletcher 4 kstats
618eda14cbcSMatt Macy  */
619eda14cbcSMatt Macy static int
fletcher_4_kstat_headers(char * buf,size_t size)620eda14cbcSMatt Macy fletcher_4_kstat_headers(char *buf, size_t size)
621eda14cbcSMatt Macy {
622eda14cbcSMatt Macy 	ssize_t off = 0;
623eda14cbcSMatt Macy 
624eda14cbcSMatt Macy 	off += snprintf(buf + off, size, "%-17s", "implementation");
625eda14cbcSMatt Macy 	off += snprintf(buf + off, size - off, "%-15s", "native");
626eda14cbcSMatt Macy 	(void) snprintf(buf + off, size - off, "%-15s\n", "byteswap");
627eda14cbcSMatt Macy 
628eda14cbcSMatt Macy 	return (0);
629eda14cbcSMatt Macy }
630eda14cbcSMatt Macy 
631eda14cbcSMatt Macy static int
fletcher_4_kstat_data(char * buf,size_t size,void * data)632eda14cbcSMatt Macy fletcher_4_kstat_data(char *buf, size_t size, void *data)
633eda14cbcSMatt Macy {
634eda14cbcSMatt Macy 	struct fletcher_4_kstat *fastest_stat =
635eda14cbcSMatt Macy 	    &fletcher_4_stat_data[fletcher_4_supp_impls_cnt];
636eda14cbcSMatt Macy 	struct fletcher_4_kstat *curr_stat = (struct fletcher_4_kstat *)data;
637eda14cbcSMatt Macy 	ssize_t off = 0;
638eda14cbcSMatt Macy 
639eda14cbcSMatt Macy 	if (curr_stat == fastest_stat) {
640eda14cbcSMatt Macy 		off += snprintf(buf + off, size - off, "%-17s", "fastest");
641eda14cbcSMatt Macy 		off += snprintf(buf + off, size - off, "%-15s",
642eda14cbcSMatt Macy 		    fletcher_4_supp_impls[fastest_stat->native]->name);
64315f0b8c3SMartin Matuska 		(void) snprintf(buf + off, size - off, "%-15s\n",
644eda14cbcSMatt Macy 		    fletcher_4_supp_impls[fastest_stat->byteswap]->name);
645eda14cbcSMatt Macy 	} else {
646eda14cbcSMatt Macy 		ptrdiff_t id = curr_stat - fletcher_4_stat_data;
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy 		off += snprintf(buf + off, size - off, "%-17s",
649eda14cbcSMatt Macy 		    fletcher_4_supp_impls[id]->name);
650eda14cbcSMatt Macy 		off += snprintf(buf + off, size - off, "%-15llu",
651eda14cbcSMatt Macy 		    (u_longlong_t)curr_stat->native);
65215f0b8c3SMartin Matuska 		(void) snprintf(buf + off, size - off, "%-15llu\n",
653eda14cbcSMatt Macy 		    (u_longlong_t)curr_stat->byteswap);
654eda14cbcSMatt Macy 	}
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy 	return (0);
657eda14cbcSMatt Macy }
658eda14cbcSMatt Macy 
659eda14cbcSMatt Macy static void *
fletcher_4_kstat_addr(kstat_t * ksp,loff_t n)660eda14cbcSMatt Macy fletcher_4_kstat_addr(kstat_t *ksp, loff_t n)
661eda14cbcSMatt Macy {
662eda14cbcSMatt Macy 	if (n <= fletcher_4_supp_impls_cnt)
663eda14cbcSMatt Macy 		ksp->ks_private = (void *) (fletcher_4_stat_data + n);
664eda14cbcSMatt Macy 	else
665eda14cbcSMatt Macy 		ksp->ks_private = NULL;
666eda14cbcSMatt Macy 
667eda14cbcSMatt Macy 	return (ksp->ks_private);
668eda14cbcSMatt Macy }
669eda14cbcSMatt Macy #endif
670eda14cbcSMatt Macy 
671eda14cbcSMatt Macy #define	FLETCHER_4_FASTEST_FN_COPY(type, src)				  \
672eda14cbcSMatt Macy {									  \
673eda14cbcSMatt Macy 	fletcher_4_fastest_impl.init_ ## type = src->init_ ## type;	  \
674eda14cbcSMatt Macy 	fletcher_4_fastest_impl.fini_ ## type = src->fini_ ## type;	  \
675eda14cbcSMatt Macy 	fletcher_4_fastest_impl.compute_ ## type = src->compute_ ## type; \
6762a58b312SMartin Matuska 	fletcher_4_fastest_impl.uses_fpu = src->uses_fpu;		  \
677eda14cbcSMatt Macy }
678eda14cbcSMatt Macy 
6797877fdebSMatt Macy #define	FLETCHER_4_BENCH_NS	(MSEC2NSEC(1))		/* 1ms */
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy typedef void fletcher_checksum_func_t(const void *, uint64_t, const void *,
682eda14cbcSMatt Macy 					zio_cksum_t *);
683eda14cbcSMatt Macy 
684eda14cbcSMatt Macy #if defined(_KERNEL)
685eda14cbcSMatt Macy static void
fletcher_4_benchmark_impl(boolean_t native,char * data,uint64_t data_size)686eda14cbcSMatt Macy fletcher_4_benchmark_impl(boolean_t native, char *data, uint64_t data_size)
687eda14cbcSMatt Macy {
688eda14cbcSMatt Macy 
689eda14cbcSMatt Macy 	struct fletcher_4_kstat *fastest_stat =
690eda14cbcSMatt Macy 	    &fletcher_4_stat_data[fletcher_4_supp_impls_cnt];
691eda14cbcSMatt Macy 	hrtime_t start;
692eda14cbcSMatt Macy 	uint64_t run_bw, run_time_ns, best_run = 0;
693eda14cbcSMatt Macy 	zio_cksum_t zc;
694eda14cbcSMatt Macy 	uint32_t i, l, sel_save = IMPL_READ(fletcher_4_impl_chosen);
695eda14cbcSMatt Macy 
696eda14cbcSMatt Macy 	fletcher_checksum_func_t *fletcher_4_test = native ?
697eda14cbcSMatt Macy 	    fletcher_4_native : fletcher_4_byteswap;
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	for (i = 0; i < fletcher_4_supp_impls_cnt; i++) {
700eda14cbcSMatt Macy 		struct fletcher_4_kstat *stat = &fletcher_4_stat_data[i];
701eda14cbcSMatt Macy 		uint64_t run_count = 0;
702eda14cbcSMatt Macy 
703eda14cbcSMatt Macy 		/* temporary set an implementation */
704eda14cbcSMatt Macy 		fletcher_4_impl_chosen = i;
705eda14cbcSMatt Macy 
706eda14cbcSMatt Macy 		kpreempt_disable();
707eda14cbcSMatt Macy 		start = gethrtime();
708eda14cbcSMatt Macy 		do {
709eda14cbcSMatt Macy 			for (l = 0; l < 32; l++, run_count++)
710eda14cbcSMatt Macy 				fletcher_4_test(data, data_size, NULL, &zc);
711eda14cbcSMatt Macy 
712eda14cbcSMatt Macy 			run_time_ns = gethrtime() - start;
713eda14cbcSMatt Macy 		} while (run_time_ns < FLETCHER_4_BENCH_NS);
714eda14cbcSMatt Macy 		kpreempt_enable();
715eda14cbcSMatt Macy 
716eda14cbcSMatt Macy 		run_bw = data_size * run_count * NANOSEC;
717eda14cbcSMatt Macy 		run_bw /= run_time_ns;	/* B/s */
718eda14cbcSMatt Macy 
719eda14cbcSMatt Macy 		if (native)
720eda14cbcSMatt Macy 			stat->native = run_bw;
721eda14cbcSMatt Macy 		else
722eda14cbcSMatt Macy 			stat->byteswap = run_bw;
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy 		if (run_bw > best_run) {
725eda14cbcSMatt Macy 			best_run = run_bw;
726eda14cbcSMatt Macy 
727eda14cbcSMatt Macy 			if (native) {
728eda14cbcSMatt Macy 				fastest_stat->native = i;
729eda14cbcSMatt Macy 				FLETCHER_4_FASTEST_FN_COPY(native,
730eda14cbcSMatt Macy 				    fletcher_4_supp_impls[i]);
731eda14cbcSMatt Macy 			} else {
732eda14cbcSMatt Macy 				fastest_stat->byteswap = i;
733eda14cbcSMatt Macy 				FLETCHER_4_FASTEST_FN_COPY(byteswap,
734eda14cbcSMatt Macy 				    fletcher_4_supp_impls[i]);
735eda14cbcSMatt Macy 			}
736eda14cbcSMatt Macy 		}
737eda14cbcSMatt Macy 	}
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy 	/* restore original selection */
740eda14cbcSMatt Macy 	atomic_swap_32(&fletcher_4_impl_chosen, sel_save);
741eda14cbcSMatt Macy }
742eda14cbcSMatt Macy #endif /* _KERNEL */
743eda14cbcSMatt Macy 
744eda14cbcSMatt Macy /*
745eda14cbcSMatt Macy  * Initialize and benchmark all supported implementations.
746eda14cbcSMatt Macy  */
747eda14cbcSMatt Macy static void
fletcher_4_benchmark(void)748eda14cbcSMatt Macy fletcher_4_benchmark(void)
749eda14cbcSMatt Macy {
750eda14cbcSMatt Macy 	fletcher_4_ops_t *curr_impl;
751eda14cbcSMatt Macy 	int i, c;
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy 	/* Move supported implementations into fletcher_4_supp_impls */
754eda14cbcSMatt Macy 	for (i = 0, c = 0; i < ARRAY_SIZE(fletcher_4_impls); i++) {
755eda14cbcSMatt Macy 		curr_impl = (fletcher_4_ops_t *)fletcher_4_impls[i];
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy 		if (curr_impl->valid && curr_impl->valid())
758eda14cbcSMatt Macy 			fletcher_4_supp_impls[c++] = curr_impl;
759eda14cbcSMatt Macy 	}
760eda14cbcSMatt Macy 	membar_producer();	/* complete fletcher_4_supp_impls[] init */
761eda14cbcSMatt Macy 	fletcher_4_supp_impls_cnt = c;	/* number of supported impl */
762eda14cbcSMatt Macy 
763eda14cbcSMatt Macy #if defined(_KERNEL)
764eda14cbcSMatt Macy 	static const size_t data_size = 1 << SPA_OLD_MAXBLOCKSHIFT; /* 128kiB */
765eda14cbcSMatt Macy 	char *databuf = vmem_alloc(data_size, KM_SLEEP);
766eda14cbcSMatt Macy 
767eda14cbcSMatt Macy 	for (i = 0; i < data_size / sizeof (uint64_t); i++)
768eda14cbcSMatt Macy 		((uint64_t *)databuf)[i] = (uintptr_t)(databuf+i); /* warm-up */
769eda14cbcSMatt Macy 
770eda14cbcSMatt Macy 	fletcher_4_benchmark_impl(B_FALSE, databuf, data_size);
771eda14cbcSMatt Macy 	fletcher_4_benchmark_impl(B_TRUE, databuf, data_size);
772eda14cbcSMatt Macy 
773eda14cbcSMatt Macy 	vmem_free(databuf, data_size);
774eda14cbcSMatt Macy #else
775eda14cbcSMatt Macy 	/*
776eda14cbcSMatt Macy 	 * Skip the benchmark in user space to avoid impacting libzpool
777eda14cbcSMatt Macy 	 * consumers (zdb, zhack, zinject, ztest).  The last implementation
778eda14cbcSMatt Macy 	 * is assumed to be the fastest and used by default.
779eda14cbcSMatt Macy 	 */
780eda14cbcSMatt Macy 	memcpy(&fletcher_4_fastest_impl,
781eda14cbcSMatt Macy 	    fletcher_4_supp_impls[fletcher_4_supp_impls_cnt - 1],
782eda14cbcSMatt Macy 	    sizeof (fletcher_4_fastest_impl));
783eda14cbcSMatt Macy 	fletcher_4_fastest_impl.name = "fastest";
784eda14cbcSMatt Macy 	membar_producer();
785eda14cbcSMatt Macy #endif /* _KERNEL */
786eda14cbcSMatt Macy }
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy void
fletcher_4_init(void)789eda14cbcSMatt Macy fletcher_4_init(void)
790eda14cbcSMatt Macy {
791eda14cbcSMatt Macy 	/* Determine the fastest available implementation. */
792eda14cbcSMatt Macy 	fletcher_4_benchmark();
793eda14cbcSMatt Macy 
794eda14cbcSMatt Macy #if defined(_KERNEL)
795eda14cbcSMatt Macy 	/* Install kstats for all implementations */
796eda14cbcSMatt Macy 	fletcher_4_kstat = kstat_create("zfs", 0, "fletcher_4_bench", "misc",
797eda14cbcSMatt Macy 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
798eda14cbcSMatt Macy 	if (fletcher_4_kstat != NULL) {
799eda14cbcSMatt Macy 		fletcher_4_kstat->ks_data = NULL;
800eda14cbcSMatt Macy 		fletcher_4_kstat->ks_ndata = UINT32_MAX;
801eda14cbcSMatt Macy 		kstat_set_raw_ops(fletcher_4_kstat,
802eda14cbcSMatt Macy 		    fletcher_4_kstat_headers,
803eda14cbcSMatt Macy 		    fletcher_4_kstat_data,
804eda14cbcSMatt Macy 		    fletcher_4_kstat_addr);
805eda14cbcSMatt Macy 		kstat_install(fletcher_4_kstat);
806eda14cbcSMatt Macy 	}
807eda14cbcSMatt Macy #endif
808eda14cbcSMatt Macy 
809eda14cbcSMatt Macy 	/* Finish initialization */
810eda14cbcSMatt Macy 	fletcher_4_initialized = B_TRUE;
811eda14cbcSMatt Macy }
812eda14cbcSMatt Macy 
813eda14cbcSMatt Macy void
fletcher_4_fini(void)814eda14cbcSMatt Macy fletcher_4_fini(void)
815eda14cbcSMatt Macy {
816eda14cbcSMatt Macy #if defined(_KERNEL)
817eda14cbcSMatt Macy 	if (fletcher_4_kstat != NULL) {
818eda14cbcSMatt Macy 		kstat_delete(fletcher_4_kstat);
819eda14cbcSMatt Macy 		fletcher_4_kstat = NULL;
820eda14cbcSMatt Macy 	}
821eda14cbcSMatt Macy #endif
822eda14cbcSMatt Macy }
823eda14cbcSMatt Macy 
824eda14cbcSMatt Macy /* ABD adapters */
825eda14cbcSMatt Macy 
826eda14cbcSMatt Macy static void
abd_fletcher_4_init(zio_abd_checksum_data_t * cdp)827eda14cbcSMatt Macy abd_fletcher_4_init(zio_abd_checksum_data_t *cdp)
828eda14cbcSMatt Macy {
829eda14cbcSMatt Macy 	const fletcher_4_ops_t *ops = fletcher_4_impl_get();
830eda14cbcSMatt Macy 	cdp->acd_private = (void *) ops;
831eda14cbcSMatt Macy 
8322a58b312SMartin Matuska 	if (ops->uses_fpu == B_TRUE) {
8332a58b312SMartin Matuska 		kfpu_begin();
8342a58b312SMartin Matuska 	}
835eda14cbcSMatt Macy 	if (cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE)
836eda14cbcSMatt Macy 		ops->init_native(cdp->acd_ctx);
837eda14cbcSMatt Macy 	else
838eda14cbcSMatt Macy 		ops->init_byteswap(cdp->acd_ctx);
8392a58b312SMartin Matuska 
840eda14cbcSMatt Macy }
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy static void
abd_fletcher_4_fini(zio_abd_checksum_data_t * cdp)843eda14cbcSMatt Macy abd_fletcher_4_fini(zio_abd_checksum_data_t *cdp)
844eda14cbcSMatt Macy {
845eda14cbcSMatt Macy 	fletcher_4_ops_t *ops = (fletcher_4_ops_t *)cdp->acd_private;
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy 	ASSERT(ops);
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy 	if (cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE)
850eda14cbcSMatt Macy 		ops->fini_native(cdp->acd_ctx, cdp->acd_zcp);
851eda14cbcSMatt Macy 	else
852eda14cbcSMatt Macy 		ops->fini_byteswap(cdp->acd_ctx, cdp->acd_zcp);
8532a58b312SMartin Matuska 
8542a58b312SMartin Matuska 	if (ops->uses_fpu == B_TRUE) {
8552a58b312SMartin Matuska 		kfpu_end();
856eda14cbcSMatt Macy 	}
8572a58b312SMartin Matuska }
8582a58b312SMartin Matuska 
859eda14cbcSMatt Macy 
860eda14cbcSMatt Macy static void
abd_fletcher_4_simd2scalar(boolean_t native,void * data,size_t size,zio_abd_checksum_data_t * cdp)861eda14cbcSMatt Macy abd_fletcher_4_simd2scalar(boolean_t native, void *data, size_t size,
862eda14cbcSMatt Macy     zio_abd_checksum_data_t *cdp)
863eda14cbcSMatt Macy {
864eda14cbcSMatt Macy 	zio_cksum_t *zcp = cdp->acd_zcp;
865eda14cbcSMatt Macy 
866eda14cbcSMatt Macy 	ASSERT3U(size, <, FLETCHER_MIN_SIMD_SIZE);
867eda14cbcSMatt Macy 
868eda14cbcSMatt Macy 	abd_fletcher_4_fini(cdp);
869eda14cbcSMatt Macy 	cdp->acd_private = (void *)&fletcher_4_scalar_ops;
870eda14cbcSMatt Macy 
871eda14cbcSMatt Macy 	if (native)
872eda14cbcSMatt Macy 		fletcher_4_incremental_native(data, size, zcp);
873eda14cbcSMatt Macy 	else
874eda14cbcSMatt Macy 		fletcher_4_incremental_byteswap(data, size, zcp);
875eda14cbcSMatt Macy }
876eda14cbcSMatt Macy 
877eda14cbcSMatt Macy static int
abd_fletcher_4_iter(void * data,size_t size,void * private)878eda14cbcSMatt Macy abd_fletcher_4_iter(void *data, size_t size, void *private)
879eda14cbcSMatt Macy {
880eda14cbcSMatt Macy 	zio_abd_checksum_data_t *cdp = (zio_abd_checksum_data_t *)private;
881eda14cbcSMatt Macy 	fletcher_4_ctx_t *ctx = cdp->acd_ctx;
882eda14cbcSMatt Macy 	fletcher_4_ops_t *ops = (fletcher_4_ops_t *)cdp->acd_private;
883eda14cbcSMatt Macy 	boolean_t native = cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE;
884aca928a5SMartin Matuska 	uint64_t asize = P2ALIGN_TYPED(size, FLETCHER_MIN_SIMD_SIZE, uint64_t);
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
887eda14cbcSMatt Macy 
888eda14cbcSMatt Macy 	if (asize > 0) {
889eda14cbcSMatt Macy 		if (native)
890eda14cbcSMatt Macy 			ops->compute_native(ctx, data, asize);
891eda14cbcSMatt Macy 		else
892eda14cbcSMatt Macy 			ops->compute_byteswap(ctx, data, asize);
893eda14cbcSMatt Macy 
894eda14cbcSMatt Macy 		size -= asize;
895eda14cbcSMatt Macy 		data = (char *)data + asize;
896eda14cbcSMatt Macy 	}
897eda14cbcSMatt Macy 
898eda14cbcSMatt Macy 	if (size > 0) {
899eda14cbcSMatt Macy 		ASSERT3U(size, <, FLETCHER_MIN_SIMD_SIZE);
900eda14cbcSMatt Macy 		/* At this point we have to switch to scalar impl */
901eda14cbcSMatt Macy 		abd_fletcher_4_simd2scalar(native, data, size, cdp);
902eda14cbcSMatt Macy 	}
903eda14cbcSMatt Macy 
904eda14cbcSMatt Macy 	return (0);
905eda14cbcSMatt Macy }
906eda14cbcSMatt Macy 
907eda14cbcSMatt Macy zio_abd_checksum_func_t fletcher_4_abd_ops = {
908eda14cbcSMatt Macy 	.acf_init = abd_fletcher_4_init,
909eda14cbcSMatt Macy 	.acf_fini = abd_fletcher_4_fini,
910eda14cbcSMatt Macy 	.acf_iter = abd_fletcher_4_iter
911eda14cbcSMatt Macy };
912eda14cbcSMatt Macy 
9137877fdebSMatt Macy #if defined(_KERNEL)
914eda14cbcSMatt Macy 
9157877fdebSMatt Macy #define	IMPL_FMT(impl, i)	(((impl) == (i)) ? "[%s] " : "%s ")
9167877fdebSMatt Macy 
9177877fdebSMatt Macy #if defined(__linux__)
918eda14cbcSMatt Macy 
919eda14cbcSMatt Macy static int
fletcher_4_param_get(char * buffer,zfs_kernel_param_t * unused)920eda14cbcSMatt Macy fletcher_4_param_get(char *buffer, zfs_kernel_param_t *unused)
921eda14cbcSMatt Macy {
922eda14cbcSMatt Macy 	const uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
923eda14cbcSMatt Macy 	char *fmt;
9247877fdebSMatt Macy 	int cnt = 0;
925eda14cbcSMatt Macy 
926eda14cbcSMatt Macy 	/* list fastest */
9277877fdebSMatt Macy 	fmt = IMPL_FMT(impl, IMPL_FASTEST);
928bb2d13b6SMartin Matuska 	cnt += kmem_scnprintf(buffer + cnt, PAGE_SIZE - cnt, fmt, "fastest");
929eda14cbcSMatt Macy 
930eda14cbcSMatt Macy 	/* list all supported implementations */
9317877fdebSMatt Macy 	for (uint32_t i = 0; i < fletcher_4_supp_impls_cnt; ++i) {
9327877fdebSMatt Macy 		fmt = IMPL_FMT(impl, i);
933bb2d13b6SMartin Matuska 		cnt += kmem_scnprintf(buffer + cnt, PAGE_SIZE - cnt, fmt,
934eda14cbcSMatt Macy 		    fletcher_4_supp_impls[i]->name);
935eda14cbcSMatt Macy 	}
936eda14cbcSMatt Macy 
937eda14cbcSMatt Macy 	return (cnt);
938eda14cbcSMatt Macy }
939eda14cbcSMatt Macy 
940eda14cbcSMatt Macy static int
fletcher_4_param_set(const char * val,zfs_kernel_param_t * unused)941eda14cbcSMatt Macy fletcher_4_param_set(const char *val, zfs_kernel_param_t *unused)
942eda14cbcSMatt Macy {
943eda14cbcSMatt Macy 	return (fletcher_4_impl_set(val));
944eda14cbcSMatt Macy }
945eda14cbcSMatt Macy 
9467877fdebSMatt Macy #else
9477877fdebSMatt Macy 
9487877fdebSMatt Macy #include <sys/sbuf.h>
9497877fdebSMatt Macy 
9507877fdebSMatt Macy static int
fletcher_4_param(ZFS_MODULE_PARAM_ARGS)9517877fdebSMatt Macy fletcher_4_param(ZFS_MODULE_PARAM_ARGS)
9527877fdebSMatt Macy {
9537877fdebSMatt Macy 	int err;
9547877fdebSMatt Macy 
9557877fdebSMatt Macy 	if (req->newptr == NULL) {
9567877fdebSMatt Macy 		const uint32_t impl = IMPL_READ(fletcher_4_impl_chosen);
9577877fdebSMatt Macy 		const int init_buflen = 64;
9587877fdebSMatt Macy 		const char *fmt;
9597877fdebSMatt Macy 		struct sbuf *s;
9607877fdebSMatt Macy 
9617877fdebSMatt Macy 		s = sbuf_new_for_sysctl(NULL, NULL, init_buflen, req);
9627877fdebSMatt Macy 
9637877fdebSMatt Macy 		/* list fastest */
9647877fdebSMatt Macy 		fmt = IMPL_FMT(impl, IMPL_FASTEST);
9657877fdebSMatt Macy 		(void) sbuf_printf(s, fmt, "fastest");
9667877fdebSMatt Macy 
9677877fdebSMatt Macy 		/* list all supported implementations */
9687877fdebSMatt Macy 		for (uint32_t i = 0; i < fletcher_4_supp_impls_cnt; ++i) {
9697877fdebSMatt Macy 			fmt = IMPL_FMT(impl, i);
9707877fdebSMatt Macy 			(void) sbuf_printf(s, fmt,
9717877fdebSMatt Macy 			    fletcher_4_supp_impls[i]->name);
9727877fdebSMatt Macy 		}
9737877fdebSMatt Macy 
9747877fdebSMatt Macy 		err = sbuf_finish(s);
9757877fdebSMatt Macy 		sbuf_delete(s);
9767877fdebSMatt Macy 
9777877fdebSMatt Macy 		return (err);
9787877fdebSMatt Macy 	}
9797877fdebSMatt Macy 
9807877fdebSMatt Macy 	char buf[16];
9817877fdebSMatt Macy 
9827877fdebSMatt Macy 	err = sysctl_handle_string(oidp, buf, sizeof (buf), req);
9837877fdebSMatt Macy 	if (err)
9847877fdebSMatt Macy 		return (err);
9857877fdebSMatt Macy 	return (-fletcher_4_impl_set(buf));
9867877fdebSMatt Macy }
9877877fdebSMatt Macy 
9887877fdebSMatt Macy #endif
9897877fdebSMatt Macy 
9907877fdebSMatt Macy #undef IMPL_FMT
9917877fdebSMatt Macy 
992eda14cbcSMatt Macy /*
993eda14cbcSMatt Macy  * Choose a fletcher 4 implementation in ZFS.
994eda14cbcSMatt Macy  * Users can choose "cycle" to exercise all implementations, but this is
995eda14cbcSMatt Macy  * for testing purpose therefore it can only be set in user space.
996eda14cbcSMatt Macy  */
9977877fdebSMatt Macy ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs, zfs_, fletcher_4_impl,
9987877fdebSMatt Macy     fletcher_4_param_set, fletcher_4_param_get, ZMOD_RW,
9997877fdebSMatt Macy 	"Select fletcher 4 implementation.");
1000eda14cbcSMatt Macy 
1001eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_init);
1002eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_2_incremental_native);
1003eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_2_incremental_byteswap);
1004eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_init);
1005eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_fini);
1006eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_2_native);
1007eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_2_byteswap);
1008eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_native);
1009eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_native_varsize);
1010eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_byteswap);
1011eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_incremental_native);
1012eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_incremental_byteswap);
1013eda14cbcSMatt Macy EXPORT_SYMBOL(fletcher_4_abd_ops);
1014eda14cbcSMatt Macy #endif
1015