xref: /openbmc/linux/arch/powerpc/include/asm/checksum.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_CHECKSUM_H
3b8b572e1SStephen Rothwell #define _ASM_POWERPC_CHECKSUM_H
4b8b572e1SStephen Rothwell #ifdef __KERNEL__
5b8b572e1SStephen Rothwell 
6b8b572e1SStephen Rothwell /*
7b8b572e1SStephen Rothwell  */
8b8b572e1SStephen Rothwell 
955a0edf0SChristophe Leroy #include <linux/bitops.h>
10e9c4943aSChristophe Leroy #include <linux/in6.h>
11b8b572e1SStephen Rothwell /*
12b8b572e1SStephen Rothwell  * Computes the checksum of a memory block at src, length len,
13b8b572e1SStephen Rothwell  * and adds in "sum" (32-bit), while copying the block to dst.
14b8b572e1SStephen Rothwell  * If an access exception occurs on src or dst, it stores -EFAULT
15b8b572e1SStephen Rothwell  * to *src_err or *dst_err respectively (if that pointer is not
16b8b572e1SStephen Rothwell  * NULL), and, for an error on src, zeroes the rest of dst.
17b8b572e1SStephen Rothwell  *
18b8b572e1SStephen Rothwell  * Like csum_partial, this must be called with even lengths,
19b8b572e1SStephen Rothwell  * except for the last fragment.
20b8b572e1SStephen Rothwell  */
2170d65cd5SAl Viro extern __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
22fdd374b6SAnton Blanchard 
23fdd374b6SAnton Blanchard #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
24fdd374b6SAnton Blanchard extern __wsum csum_and_copy_from_user(const void __user *src, void *dst,
25c693cc46SAl Viro 				      int len);
268c773914SAnton Blanchard #define HAVE_CSUM_COPY_USER
278c773914SAnton Blanchard extern __wsum csum_and_copy_to_user(const void *src, void __user *dst,
28c693cc46SAl Viro 				    int len);
29b8b572e1SStephen Rothwell 
306e41c585SAl Viro #define _HAVE_ARCH_CSUM_AND_COPY
31cc44c17bSAl Viro #define csum_partial_copy_nocheck(src, dst, len)   \
3270d65cd5SAl Viro         csum_partial_copy_generic((src), (dst), (len))
33b8b572e1SStephen Rothwell 
34b8b572e1SStephen Rothwell 
35b8b572e1SStephen Rothwell /*
36b8b572e1SStephen Rothwell  * turns a 32-bit partial checksum (e.g. from csum_partial) into a
37b8b572e1SStephen Rothwell  * 1's complement 16-bit checksum.
38b8b572e1SStephen Rothwell  */
csum_fold(__wsum sum)39b8b572e1SStephen Rothwell static inline __sum16 csum_fold(__wsum sum)
40b8b572e1SStephen Rothwell {
41*a1ae4317SChristophe Leroy 	u32 tmp = (__force u32)sum;
42b8b572e1SStephen Rothwell 
43*a1ae4317SChristophe Leroy 	/*
44*a1ae4317SChristophe Leroy 	 * swap the two 16-bit halves of sum
45*a1ae4317SChristophe Leroy 	 * if there is a carry from adding the two 16-bit halves,
46*a1ae4317SChristophe Leroy 	 * it will carry from the lower half into the upper half,
47*a1ae4317SChristophe Leroy 	 * giving us the correct sum in the upper half.
48*a1ae4317SChristophe Leroy 	 */
49*a1ae4317SChristophe Leroy 	return (__force __sum16)(~(tmp + rol32(tmp, 16)) >> 16);
50b8b572e1SStephen Rothwell }
51b8b572e1SStephen Rothwell 
from64to32(u64 x)52b492f7e4SPaul Mackerras static inline u32 from64to32(u64 x)
53b492f7e4SPaul Mackerras {
5455a0edf0SChristophe Leroy 	return (x + ror64(x, 32)) >> 32;
55b492f7e4SPaul Mackerras }
56b492f7e4SPaul Mackerras 
csum_tcpudp_nofold(__be32 saddr,__be32 daddr,__u32 len,__u8 proto,__wsum sum)57f9d4286bSIvan Vecera static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
58f9d4286bSIvan Vecera 					__u8 proto, __wsum sum)
59b8b572e1SStephen Rothwell {
60b8b572e1SStephen Rothwell #ifdef __powerpc64__
61b492f7e4SPaul Mackerras 	u64 s = (__force u32)sum;
62b8b572e1SStephen Rothwell 
63b8b572e1SStephen Rothwell 	s += (__force u32)saddr;
64b8b572e1SStephen Rothwell 	s += (__force u32)daddr;
65d4fde568SPaul Mackerras #ifdef __BIG_ENDIAN__
66b8b572e1SStephen Rothwell 	s += proto + len;
67d4fde568SPaul Mackerras #else
68d4fde568SPaul Mackerras 	s += (proto + len) << 8;
69d4fde568SPaul Mackerras #endif
70b492f7e4SPaul Mackerras 	return (__force __wsum) from64to32(s);
71b8b572e1SStephen Rothwell #else
72b8b572e1SStephen Rothwell     __asm__("\n\
73b8b572e1SStephen Rothwell 	addc %0,%0,%1 \n\
74b8b572e1SStephen Rothwell 	adde %0,%0,%2 \n\
75b8b572e1SStephen Rothwell 	adde %0,%0,%3 \n\
76b8b572e1SStephen Rothwell 	addze %0,%0 \n\
77b8b572e1SStephen Rothwell 	"
78b8b572e1SStephen Rothwell 	: "=r" (sum)
79b8b572e1SStephen Rothwell 	: "r" (daddr), "r"(saddr), "r"(proto + len), "0"(sum));
80b8b572e1SStephen Rothwell 	return sum;
81b8b572e1SStephen Rothwell #endif
82b8b572e1SStephen Rothwell }
837a332b0cSAnton Blanchard 
8492c985f1SLEROY Christophe /*
8592c985f1SLEROY Christophe  * computes the checksum of the TCP/UDP pseudo-header
8692c985f1SLEROY Christophe  * returns a 16-bit checksum, already complemented
8792c985f1SLEROY Christophe  */
csum_tcpudp_magic(__be32 saddr,__be32 daddr,__u32 len,__u8 proto,__wsum sum)88f9d4286bSIvan Vecera static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
89f9d4286bSIvan Vecera 					__u8 proto, __wsum sum)
9092c985f1SLEROY Christophe {
9192c985f1SLEROY Christophe 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
9292c985f1SLEROY Christophe }
9392c985f1SLEROY Christophe 
94501c8de7SLEROY Christophe #define HAVE_ARCH_CSUM_ADD
csum_add(__wsum csum,__wsum addend)954423eff7SChristophe Leroy static __always_inline __wsum csum_add(__wsum csum, __wsum addend)
96501c8de7SLEROY Christophe {
97501c8de7SLEROY Christophe #ifdef __powerpc64__
98501c8de7SLEROY Christophe 	u64 res = (__force u64)csum;
99f206fdd9SChristophe Leroy 
100f206fdd9SChristophe Leroy 	res += (__force u64)addend;
101f206fdd9SChristophe Leroy 	return (__force __wsum)((u32)res + (res >> 32));
102f206fdd9SChristophe Leroy #else
1035a8847c8SChristophe Leroy 	if (__builtin_constant_p(csum) && csum == 0)
1045a8847c8SChristophe Leroy 		return addend;
1055a8847c8SChristophe Leroy 	if (__builtin_constant_p(addend) && addend == 0)
1065a8847c8SChristophe Leroy 		return csum;
107501c8de7SLEROY Christophe 
108501c8de7SLEROY Christophe 	asm("addc %0,%0,%1;"
109501c8de7SLEROY Christophe 	    "addze %0,%0;"
11011dfbf58SChristophe Leroy 	    : "+r" (csum) : "r" (addend) : "xer");
111501c8de7SLEROY Christophe 	return csum;
112501c8de7SLEROY Christophe #endif
113501c8de7SLEROY Christophe }
114501c8de7SLEROY Christophe 
1153af722cbSChristophe Leroy #define HAVE_ARCH_CSUM_SHIFT
csum_shift(__wsum sum,int offset)1163af722cbSChristophe Leroy static __always_inline __wsum csum_shift(__wsum sum, int offset)
1173af722cbSChristophe Leroy {
1183af722cbSChristophe Leroy 	/* rotate sum to align it with a 16b boundary */
1193af722cbSChristophe Leroy 	return (__force __wsum)rol32((__force u32)sum, (offset & 1) << 3);
1203af722cbSChristophe Leroy }
1213af722cbSChristophe Leroy 
12237e08cadSChristophe Leroy /*
12337e08cadSChristophe Leroy  * This is a version of ip_compute_csum() optimized for IP headers,
12437e08cadSChristophe Leroy  * which always checksum on 4 octet boundaries.  ihl is the number
12537e08cadSChristophe Leroy  * of 32-bit words and is always >= 5.
12637e08cadSChristophe Leroy  */
ip_fast_csum_nofold(const void * iph,unsigned int ihl)12737e08cadSChristophe Leroy static inline __wsum ip_fast_csum_nofold(const void *iph, unsigned int ihl)
12837e08cadSChristophe Leroy {
12937e08cadSChristophe Leroy 	const u32 *ptr = (const u32 *)iph + 1;
13037e08cadSChristophe Leroy #ifdef __powerpc64__
13137e08cadSChristophe Leroy 	unsigned int i;
13237e08cadSChristophe Leroy 	u64 s = *(const u32 *)iph;
13337e08cadSChristophe Leroy 
13437e08cadSChristophe Leroy 	for (i = 0; i < ihl - 1; i++, ptr++)
13537e08cadSChristophe Leroy 		s += *ptr;
136b492f7e4SPaul Mackerras 	return (__force __wsum)from64to32(s);
13737e08cadSChristophe Leroy #else
13837e08cadSChristophe Leroy 	__wsum sum, tmp;
13937e08cadSChristophe Leroy 
14037e08cadSChristophe Leroy 	asm("mtctr %3;"
14137e08cadSChristophe Leroy 	    "addc %0,%4,%5;"
14237e08cadSChristophe Leroy 	    "1: lwzu %1, 4(%2);"
14337e08cadSChristophe Leroy 	    "adde %0,%0,%1;"
14437e08cadSChristophe Leroy 	    "bdnz 1b;"
14537e08cadSChristophe Leroy 	    "addze %0,%0;"
14637e08cadSChristophe Leroy 	    : "=r" (sum), "=r" (tmp), "+b" (ptr)
14737e08cadSChristophe Leroy 	    : "r" (ihl - 2), "r" (*(const u32 *)iph), "r" (*ptr)
14837e08cadSChristophe Leroy 	    : "ctr", "xer", "memory");
14937e08cadSChristophe Leroy 
15037e08cadSChristophe Leroy 	return sum;
15137e08cadSChristophe Leroy #endif
15237e08cadSChristophe Leroy }
15337e08cadSChristophe Leroy 
ip_fast_csum(const void * iph,unsigned int ihl)15437e08cadSChristophe Leroy static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
15537e08cadSChristophe Leroy {
15637e08cadSChristophe Leroy 	return csum_fold(ip_fast_csum_nofold(iph, ihl));
15737e08cadSChristophe Leroy }
15837e08cadSChristophe Leroy 
1597e393220SChristophe Leroy /*
1607e393220SChristophe Leroy  * computes the checksum of a memory block at buff, length len,
1617e393220SChristophe Leroy  * and adds in "sum" (32-bit)
1627e393220SChristophe Leroy  *
1637e393220SChristophe Leroy  * returns a 32-bit number suitable for feeding into itself
1647e393220SChristophe Leroy  * or csum_tcpudp_magic
1657e393220SChristophe Leroy  *
1667e393220SChristophe Leroy  * this function must be called with even lengths, except
1677e393220SChristophe Leroy  * for the last fragment, which may be odd
1687e393220SChristophe Leroy  *
1697e393220SChristophe Leroy  * it's best to have buff aligned on a 32-bit boundary
1707e393220SChristophe Leroy  */
1717e393220SChristophe Leroy __wsum __csum_partial(const void *buff, int len, __wsum sum);
1727e393220SChristophe Leroy 
csum_partial(const void * buff,int len,__wsum sum)173328e7e48SChristophe Leroy static __always_inline __wsum csum_partial(const void *buff, int len, __wsum sum)
1747e393220SChristophe Leroy {
1757e393220SChristophe Leroy 	if (__builtin_constant_p(len) && len <= 16 && (len & 1) == 0) {
1767e393220SChristophe Leroy 		if (len == 2)
1777e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)*(const u16 *)buff);
1787e393220SChristophe Leroy 		if (len >= 4)
1797e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)*(const u32 *)buff);
1807e393220SChristophe Leroy 		if (len == 6)
1817e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)
1827e393220SChristophe Leroy 					    *(const u16 *)(buff + 4));
1837e393220SChristophe Leroy 		if (len >= 8)
1847e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)
1857e393220SChristophe Leroy 					    *(const u32 *)(buff + 4));
1867e393220SChristophe Leroy 		if (len == 10)
1877e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)
1887e393220SChristophe Leroy 					    *(const u16 *)(buff + 8));
1897e393220SChristophe Leroy 		if (len >= 12)
1907e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)
1917e393220SChristophe Leroy 					    *(const u32 *)(buff + 8));
1927e393220SChristophe Leroy 		if (len == 14)
1937e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)
1947e393220SChristophe Leroy 					    *(const u16 *)(buff + 12));
1957e393220SChristophe Leroy 		if (len >= 16)
1967e393220SChristophe Leroy 			sum = csum_add(sum, (__force __wsum)
1977e393220SChristophe Leroy 					    *(const u32 *)(buff + 12));
1987e393220SChristophe Leroy 	} else if (__builtin_constant_p(len) && (len & 3) == 0) {
1997e393220SChristophe Leroy 		sum = csum_add(sum, ip_fast_csum_nofold(buff, len >> 2));
2007e393220SChristophe Leroy 	} else {
2017e393220SChristophe Leroy 		sum = __csum_partial(buff, len, sum);
2027e393220SChristophe Leroy 	}
2037e393220SChristophe Leroy 	return sum;
2047e393220SChristophe Leroy }
2057e393220SChristophe Leroy 
2067e393220SChristophe Leroy /*
2077e393220SChristophe Leroy  * this routine is used for miscellaneous IP-like checksums, mainly
2087e393220SChristophe Leroy  * in icmp.c
2097e393220SChristophe Leroy  */
ip_compute_csum(const void * buff,int len)2107e393220SChristophe Leroy static inline __sum16 ip_compute_csum(const void *buff, int len)
2117e393220SChristophe Leroy {
2127e393220SChristophe Leroy 	return csum_fold(csum_partial(buff, len, 0));
2137e393220SChristophe Leroy }
2147e393220SChristophe Leroy 
215e9c4943aSChristophe Leroy #define _HAVE_ARCH_IPV6_CSUM
216e9c4943aSChristophe Leroy __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
217e9c4943aSChristophe Leroy 			const struct in6_addr *daddr,
218e9c4943aSChristophe Leroy 			__u32 len, __u8 proto, __wsum sum);
219e9c4943aSChristophe Leroy 
220b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
221b8b572e1SStephen Rothwell #endif
222