xref: /linux/arch/s390/include/asm/checksum.h (revision c01044cc819160323f3ca4acd44fca487c4432e6)
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/uaccess.h>
16 
17 /*
18  * computes the checksum of a memory block at buff, length len,
19  * and adds in "sum" (32-bit)
20  *
21  * returns a 32-bit number suitable for feeding into itself
22  * or csum_tcpudp_magic
23  *
24  * this function must be called with even lengths, except
25  * for the last fragment, which may be odd
26  *
27  * it's best to have buff aligned on a 32-bit boundary
28  */
29 static inline __wsum
30 csum_partial(const void *buff, int len, __wsum sum)
31 {
32 	register unsigned long reg2 asm("2") = (unsigned long) buff;
33 	register unsigned long reg3 asm("3") = (unsigned long) len;
34 
35 	asm volatile(
36 		"0:	cksm	%0,%1\n"	/* do checksum on longs */
37 		"	jo	0b\n"
38 		: "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");
39 	return sum;
40 }
41 
42 /*
43  *      Fold a partial checksum without adding pseudo headers
44  */
45 static inline __sum16 csum_fold(__wsum sum)
46 {
47 	u32 csum = (__force u32) sum;
48 
49 	csum += (csum >> 16) + (csum << 16);
50 	csum >>= 16;
51 	return (__force __sum16) ~csum;
52 }
53 
54 /*
55  *	This is a version of ip_compute_csum() optimized for IP headers,
56  *	which always checksum on 4 octet boundaries.
57  *
58  */
59 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
60 {
61 	return csum_fold(csum_partial(iph, ihl*4, 0));
62 }
63 
64 /*
65  * computes the checksum of the TCP/UDP pseudo-header
66  * returns a 32-bit checksum
67  */
68 static inline __wsum
69 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto,
70                    __wsum sum)
71 {
72 	__u32 csum = (__force __u32)sum;
73 
74 	csum += (__force __u32)saddr;
75 	if (csum < (__force __u32)saddr)
76 		csum++;
77 
78 	csum += (__force __u32)daddr;
79 	if (csum < (__force __u32)daddr)
80 		csum++;
81 
82 	csum += len + proto;
83 	if (csum < len + proto)
84 		csum++;
85 
86 	return (__force __wsum)csum;
87 }
88 
89 /*
90  * computes the checksum of the TCP/UDP pseudo-header
91  * returns a 16-bit checksum, already complemented
92  */
93 
94 static inline __sum16
95 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto,
96                   __wsum sum)
97 {
98 	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
99 }
100 
101 /*
102  * this routine is used for miscellaneous IP-like checksums, mainly
103  * in icmp.c
104  */
105 
106 static inline __sum16 ip_compute_csum(const void *buff, int len)
107 {
108 	return csum_fold(csum_partial(buff, len, 0));
109 }
110 
111 #endif /* _S390_CHECKSUM_H */
112 
113 
114