xref: /openbmc/linux/arch/s390/include/asm/checksum.h (revision 582603a9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *    S390 fast network checksum routines
4  *
5  *  S390 version
6  *    Copyright IBM Corp. 1999
7  *    Author(s): Ulrich Hild        (first version)
8  *               Martin Schwidefsky (heavily optimized CKSM version)
9  *               D.J. Barrow        (third attempt)
10  */
11 
12 #ifndef _S390_CHECKSUM_H
13 #define _S390_CHECKSUM_H
14 
15 #ifdef CONFIG_GENERIC_CSUM
16 
17 #include <asm-generic/checksum.h>
18 
19 #else /* CONFIG_GENERIC_CSUM */
20 
21 #include <linux/uaccess.h>
22 #include <linux/in6.h>
23 
24 /*
25  * Computes the checksum of a memory block at buff, length len,
26  * and adds in "sum" (32-bit).
27  *
28  * Returns a 32-bit number suitable for feeding into itself
29  * or csum_tcpudp_magic.
30  *
31  * This function must be called with even lengths, except
32  * for the last fragment, which may be odd.
33  *
34  * It's best to have buff aligned on a 32-bit boundary.
35  */
36 static inline __wsum csum_partial(const void *buff, int len, __wsum sum)
37 {
38 	union register_pair rp = {
39 		.even = (unsigned long) buff,
40 		.odd = (unsigned long) len,
41 	};
42 
43 	asm volatile(
44 		"0:	cksm	%[sum],%[rp]\n"
45 		"	jo	0b\n"
46 		: [sum] "+&d" (sum), [rp] "+&d" (rp.pair) : : "cc", "memory");
47 	return sum;
48 }
49 
50 /*
51  * Fold a partial checksum without adding pseudo headers.
52  */
53 static inline __sum16 csum_fold(__wsum sum)
54 {
55 	u32 csum = (__force u32) sum;
56 
57 	csum += (csum >> 16) | (csum << 16);
58 	csum >>= 16;
59 	return (__force __sum16) ~csum;
60 }
61 
62 /*
63  * This is a version of ip_compute_csum() optimized for IP headers,
64  * which always checksums on 4 octet boundaries.
65  */
66 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
67 {
68 	__u64 csum = 0;
69 	__u32 *ptr = (u32 *)iph;
70 
71 	csum += *ptr++;
72 	csum += *ptr++;
73 	csum += *ptr++;
74 	csum += *ptr++;
75 	ihl -= 4;
76 	while (ihl--)
77 		csum += *ptr++;
78 	csum += (csum >> 32) | (csum << 32);
79 	return csum_fold((__force __wsum)(csum >> 32));
80 }
81 
82 /*
83  * Computes the checksum of the TCP/UDP pseudo-header.
84  * Returns a 32-bit checksum.
85  */
86 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
87 					__u8 proto, __wsum sum)
88 {
89 	__u64 csum = (__force __u64)sum;
90 
91 	csum += (__force __u32)saddr;
92 	csum += (__force __u32)daddr;
93 	csum += len;
94 	csum += proto;
95 	csum += (csum >> 32) | (csum << 32);
96 	return (__force __wsum)(csum >> 32);
97 }
98 
99 /*
100  * Computes the checksum of the TCP/UDP pseudo-header.
101  * Returns a 16-bit checksum, already complemented.
102  */
103 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
104 					__u8 proto, __wsum sum)
105 {
106 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
107 }
108 
109 /*
110  * Used for miscellaneous IP-like checksums, mainly icmp.
111  */
112 static inline __sum16 ip_compute_csum(const void *buff, int len)
113 {
114 	return csum_fold(csum_partial(buff, len, 0));
115 }
116 
117 #define _HAVE_ARCH_IPV6_CSUM
118 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
119 				      const struct in6_addr *daddr,
120 				      __u32 len, __u8 proto, __wsum csum)
121 {
122 	__u64 sum = (__force __u64)csum;
123 
124 	sum += (__force __u32)saddr->s6_addr32[0];
125 	sum += (__force __u32)saddr->s6_addr32[1];
126 	sum += (__force __u32)saddr->s6_addr32[2];
127 	sum += (__force __u32)saddr->s6_addr32[3];
128 	sum += (__force __u32)daddr->s6_addr32[0];
129 	sum += (__force __u32)daddr->s6_addr32[1];
130 	sum += (__force __u32)daddr->s6_addr32[2];
131 	sum += (__force __u32)daddr->s6_addr32[3];
132 	sum += len;
133 	sum += proto;
134 	sum += (sum >> 32) | (sum << 32);
135 	return csum_fold((__force __wsum)(sum >> 32));
136 }
137 
138 #endif /* CONFIG_GENERIC_CSUM */
139 #endif /* _S390_CHECKSUM_H */
140