xref: /linux/arch/powerpc/include/asm/checksum.h (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_POWERPC_CHECKSUM_H
3 #define _ASM_POWERPC_CHECKSUM_H
4 #ifdef __KERNEL__
5 
6 /*
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/in6.h>
11 #include <linux/uaccess.h>
12 /*
13  * Computes the checksum of a memory block at src, length len,
14  * and adds in "sum" (32-bit), while copying the block to dst.
15  * If an access exception occurs on src or dst, it stores -EFAULT
16  * to *src_err or *dst_err respectively (if that pointer is not
17  * NULL), and, for an error on src, zeroes the rest of dst.
18  *
19  * Like csum_partial, this must be called with even lengths,
20  * except for the last fragment.
21  */
22 extern __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
23 
24 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
25 static inline __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len)
26 {
27 	scoped_user_read_access_size(src, len, efault)
28 		return csum_partial_copy_generic((void __force *)src, dst, len);
29 
30 efault:
31 	return 0;
32 }
33 
34 #define HAVE_CSUM_COPY_USER
35 static inline __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len)
36 {
37 	scoped_user_write_access_size(dst, len, efault)
38 		return csum_partial_copy_generic(src, (void __force *)dst, len);
39 
40 efault:
41 	return 0;
42 }
43 
44 #define _HAVE_ARCH_CSUM_AND_COPY
45 #define csum_partial_copy_nocheck(src, dst, len)   \
46         csum_partial_copy_generic((src), (dst), (len))
47 
48 
49 /*
50  * turns a 32-bit partial checksum (e.g. from csum_partial) into a
51  * 1's complement 16-bit checksum.
52  */
53 static inline __sum16 csum_fold(__wsum sum)
54 {
55 	u32 tmp = (__force u32)sum;
56 
57 	/*
58 	 * swap the two 16-bit halves of sum
59 	 * if there is a carry from adding the two 16-bit halves,
60 	 * it will carry from the lower half into the upper half,
61 	 * giving us the correct sum in the upper half.
62 	 */
63 	return (__force __sum16)(~(tmp + rol32(tmp, 16)) >> 16);
64 }
65 
66 static inline u32 from64to32(u64 x)
67 {
68 	return (x + ror64(x, 32)) >> 32;
69 }
70 
71 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
72 					__u8 proto, __wsum sum)
73 {
74 #ifdef __powerpc64__
75 	u64 s = (__force u32)sum;
76 
77 	s += (__force u32)saddr;
78 	s += (__force u32)daddr;
79 #ifdef __BIG_ENDIAN__
80 	s += proto + len;
81 #else
82 	s += (proto + len) << 8;
83 #endif
84 	return (__force __wsum) from64to32(s);
85 #else
86     __asm__("\n\
87 	addc %0,%0,%1 \n\
88 	adde %0,%0,%2 \n\
89 	adde %0,%0,%3 \n\
90 	addze %0,%0 \n\
91 	"
92 	: "=r" (sum)
93 	: "r" (daddr), "r"(saddr), "r"(proto + len), "0"(sum));
94 	return sum;
95 #endif
96 }
97 
98 /*
99  * computes the checksum of the TCP/UDP pseudo-header
100  * returns a 16-bit checksum, already complemented
101  */
102 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
103 					__u8 proto, __wsum sum)
104 {
105 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
106 }
107 
108 #define HAVE_ARCH_CSUM_ADD
109 static __always_inline __wsum csum_add(__wsum csum, __wsum addend)
110 {
111 #ifdef __powerpc64__
112 	u64 res = (__force u64)csum;
113 
114 	res += (__force u64)addend;
115 	return (__force __wsum)((u32)res + (res >> 32));
116 #else
117 	if (__builtin_constant_p(csum) && csum == 0)
118 		return addend;
119 	if (__builtin_constant_p(addend) && addend == 0)
120 		return csum;
121 
122 	asm("addc %0,%0,%1;"
123 	    "addze %0,%0;"
124 	    : "+r" (csum) : "r" (addend) : "xer");
125 	return csum;
126 #endif
127 }
128 
129 #define HAVE_ARCH_CSUM_SHIFT
130 static __always_inline __wsum csum_shift(__wsum sum, int offset)
131 {
132 	/* rotate sum to align it with a 16b boundary */
133 	return (__force __wsum)rol32((__force u32)sum, (offset & 1) << 3);
134 }
135 
136 /*
137  * This is a version of ip_compute_csum() optimized for IP headers,
138  * which always checksum on 4 octet boundaries.  ihl is the number
139  * of 32-bit words and is always >= 5.
140  */
141 static inline __wsum ip_fast_csum_nofold(const void *iph, unsigned int ihl)
142 {
143 	const u32 *ptr = (const u32 *)iph + 1;
144 #ifdef __powerpc64__
145 	unsigned int i;
146 	u64 s = *(const u32 *)iph;
147 
148 	for (i = 0; i < ihl - 1; i++, ptr++)
149 		s += *ptr;
150 	return (__force __wsum)from64to32(s);
151 #else
152 	__wsum sum, tmp;
153 
154 	asm("mtctr %3;"
155 	    "addc %0,%4,%5;"
156 	    "1: lwzu %1, 4(%2);"
157 	    "adde %0,%0,%1;"
158 	    "bdnz 1b;"
159 	    "addze %0,%0;"
160 	    : "=r" (sum), "=r" (tmp), "+b" (ptr)
161 	    : "r" (ihl - 2), "r" (*(const u32 *)iph), "r" (*ptr)
162 	    : "ctr", "xer", "memory");
163 
164 	return sum;
165 #endif
166 }
167 
168 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
169 {
170 	return csum_fold(ip_fast_csum_nofold(iph, ihl));
171 }
172 
173 /*
174  * computes the checksum of a memory block at buff, length len,
175  * and adds in "sum" (32-bit)
176  *
177  * returns a 32-bit number suitable for feeding into itself
178  * or csum_tcpudp_magic
179  *
180  * this function must be called with even lengths, except
181  * for the last fragment, which may be odd
182  *
183  * it's best to have buff aligned on a 32-bit boundary
184  */
185 __wsum __csum_partial(const void *buff, int len, __wsum sum);
186 
187 static __always_inline __wsum csum_partial(const void *buff, int len, __wsum sum)
188 {
189 	if (__builtin_constant_p(len) && len <= 16 && (len & 1) == 0) {
190 		if (len == 2)
191 			sum = csum_add(sum, (__force __wsum)*(const u16 *)buff);
192 		if (len >= 4)
193 			sum = csum_add(sum, (__force __wsum)*(const u32 *)buff);
194 		if (len == 6)
195 			sum = csum_add(sum, (__force __wsum)
196 					    *(const u16 *)(buff + 4));
197 		if (len >= 8)
198 			sum = csum_add(sum, (__force __wsum)
199 					    *(const u32 *)(buff + 4));
200 		if (len == 10)
201 			sum = csum_add(sum, (__force __wsum)
202 					    *(const u16 *)(buff + 8));
203 		if (len >= 12)
204 			sum = csum_add(sum, (__force __wsum)
205 					    *(const u32 *)(buff + 8));
206 		if (len == 14)
207 			sum = csum_add(sum, (__force __wsum)
208 					    *(const u16 *)(buff + 12));
209 		if (len >= 16)
210 			sum = csum_add(sum, (__force __wsum)
211 					    *(const u32 *)(buff + 12));
212 	} else if (__builtin_constant_p(len) && (len & 3) == 0) {
213 		sum = csum_add(sum, ip_fast_csum_nofold(buff, len >> 2));
214 	} else {
215 		sum = __csum_partial(buff, len, sum);
216 	}
217 	return sum;
218 }
219 
220 /*
221  * this routine is used for miscellaneous IP-like checksums, mainly
222  * in icmp.c
223  */
224 static inline __sum16 ip_compute_csum(const void *buff, int len)
225 {
226 	return csum_fold(csum_partial(buff, len, 0));
227 }
228 
229 #define _HAVE_ARCH_IPV6_CSUM
230 __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
231 			const struct in6_addr *daddr,
232 			__u32 len, __u8 proto, __wsum sum);
233 
234 #endif /* __KERNEL__ */
235 #endif
236