treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / s390 / include / asm / checksum.h
blob91e376b0d28c25edfbbd5aa8d7f4305f43387e7f
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * S390 fast network checksum routines
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)
12 #ifndef _S390_CHECKSUM_H
13 #define _S390_CHECKSUM_H
15 #include <linux/uaccess.h>
18 * computes the checksum of a memory block at buff, length len,
19 * and adds in "sum" (32-bit)
21 * returns a 32-bit number suitable for feeding into itself
22 * or csum_tcpudp_magic
24 * this function must be called with even lengths, except
25 * for the last fragment, which may be odd
27 * it's best to have buff aligned on a 32-bit boundary
29 static inline __wsum
30 csum_partial(const void *buff, int len, __wsum sum)
32 register unsigned long reg2 asm("2") = (unsigned long) buff;
33 register unsigned long reg3 asm("3") = (unsigned long) len;
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;
43 * the same as csum_partial_copy, but copies from user space.
45 * here even more important to align src and dst on a 32-bit (or even
46 * better 64-bit) boundary
48 * Copy from userspace and compute checksum.
50 static inline __wsum
51 csum_partial_copy_from_user(const void __user *src, void *dst,
52 int len, __wsum sum,
53 int *err_ptr)
55 if (unlikely(copy_from_user(dst, src, len)))
56 *err_ptr = -EFAULT;
57 return csum_partial(dst, len, sum);
61 static inline __wsum
62 csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum)
64 memcpy(dst,src,len);
65 return csum_partial(dst, len, sum);
69 * Fold a partial checksum without adding pseudo headers
71 static inline __sum16 csum_fold(__wsum sum)
73 u32 csum = (__force u32) sum;
75 csum += (csum >> 16) + (csum << 16);
76 csum >>= 16;
77 return (__force __sum16) ~csum;
81 * This is a version of ip_compute_csum() optimized for IP headers,
82 * which always checksum on 4 octet boundaries.
85 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
87 return csum_fold(csum_partial(iph, ihl*4, 0));
91 * computes the checksum of the TCP/UDP pseudo-header
92 * returns a 32-bit checksum
94 static inline __wsum
95 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto,
96 __wsum sum)
98 __u32 csum = (__force __u32)sum;
100 csum += (__force __u32)saddr;
101 if (csum < (__force __u32)saddr)
102 csum++;
104 csum += (__force __u32)daddr;
105 if (csum < (__force __u32)daddr)
106 csum++;
108 csum += len + proto;
109 if (csum < len + proto)
110 csum++;
112 return (__force __wsum)csum;
116 * computes the checksum of the TCP/UDP pseudo-header
117 * returns a 16-bit checksum, already complemented
120 static inline __sum16
121 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto,
122 __wsum sum)
124 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
128 * this routine is used for miscellaneous IP-like checksums, mainly
129 * in icmp.c
132 static inline __sum16 ip_compute_csum(const void *buff, int len)
134 return csum_fold(csum_partial(buff, len, 0));
137 #endif /* _S390_CHECKSUM_H */