xref: /linux/arch/s390/include/asm/checksum.h (revision 75079df919efcc30eb5bf0427c83fb578f4fe4fc)
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 #include <linux/instrumented.h>
16 #include <linux/in6.h>
17 
18 static inline __wsum cksm(const void *buff, int len, __wsum sum)
19 {
20 	union register_pair rp = {
21 		.even = (unsigned long)buff,
22 		.odd = (unsigned long)len,
23 	};
24 
25 	instrument_read(buff, len);
26 	asm volatile("\n"
27 		"0:	cksm	%[sum],%[rp]\n"
28 		"	jo	0b\n"
29 		: [sum] "+&d" (sum), [rp] "+&d" (rp.pair) : : "cc", "memory");
30 	return sum;
31 }
32 
33 __wsum csum_partial(const void *buff, int len, __wsum sum);
34 
35 #define _HAVE_ARCH_CSUM_AND_COPY
36 __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len);
37 
38 /*
39  * Fold a partial checksum without adding pseudo headers.
40  */
41 static inline __sum16 csum_fold(__wsum sum)
42 {
43 	u32 csum = (__force u32) sum;
44 
45 	csum += (csum >> 16) | (csum << 16);
46 	csum >>= 16;
47 	return (__force __sum16) ~csum;
48 }
49 
50 /*
51  * This is a version of ip_compute_csum() optimized for IP headers,
52  * which always checksums on 4 octet boundaries.
53  */
54 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
55 {
56 	__u64 csum = 0;
57 	__u32 *ptr = (u32 *)iph;
58 
59 	csum += *ptr++;
60 	csum += *ptr++;
61 	csum += *ptr++;
62 	csum += *ptr++;
63 	ihl -= 4;
64 	while (ihl--)
65 		csum += *ptr++;
66 	csum += (csum >> 32) | (csum << 32);
67 	return csum_fold((__force __wsum)(csum >> 32));
68 }
69 
70 /*
71  * Computes the checksum of the TCP/UDP pseudo-header.
72  * Returns a 32-bit checksum.
73  */
74 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
75 					__u8 proto, __wsum sum)
76 {
77 	__u64 csum = (__force __u64)sum;
78 
79 	csum += (__force __u32)saddr;
80 	csum += (__force __u32)daddr;
81 	csum += len;
82 	csum += proto;
83 	csum += (csum >> 32) | (csum << 32);
84 	return (__force __wsum)(csum >> 32);
85 }
86 
87 /*
88  * Computes the checksum of the TCP/UDP pseudo-header.
89  * Returns a 16-bit checksum, already complemented.
90  */
91 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
92 					__u8 proto, __wsum sum)
93 {
94 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
95 }
96 
97 /*
98  * Used for miscellaneous IP-like checksums, mainly icmp.
99  */
100 static inline __sum16 ip_compute_csum(const void *buff, int len)
101 {
102 	return csum_fold(csum_partial(buff, len, 0));
103 }
104 
105 #define _HAVE_ARCH_IPV6_CSUM
106 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
107 				      const struct in6_addr *daddr,
108 				      __u32 len, __u8 proto, __wsum csum)
109 {
110 	__u64 sum = (__force __u64)csum;
111 
112 	sum += (__force __u32)saddr->s6_addr32[0];
113 	sum += (__force __u32)saddr->s6_addr32[1];
114 	sum += (__force __u32)saddr->s6_addr32[2];
115 	sum += (__force __u32)saddr->s6_addr32[3];
116 	sum += (__force __u32)daddr->s6_addr32[0];
117 	sum += (__force __u32)daddr->s6_addr32[1];
118 	sum += (__force __u32)daddr->s6_addr32[2];
119 	sum += (__force __u32)daddr->s6_addr32[3];
120 	sum += len;
121 	sum += proto;
122 	sum += (sum >> 32) | (sum << 32);
123 	return csum_fold((__force __wsum)(sum >> 32));
124 }
125 
126 #endif /* _S390_CHECKSUM_H */
127