2 * S390 fast network checksum routines
5 * Copyright IBM Corp. 1999
6 * Author(s): Ulrich Hild (first version)
7 * Martin Schwidefsky (heavily optimized CKSM version)
8 * D.J. Barrow (third attempt)
11 #ifndef _S390_CHECKSUM_H
12 #define _S390_CHECKSUM_H
14 #include <asm/uaccess.h>
17 * computes the checksum of a memory block at buff, length len,
18 * and adds in "sum" (32-bit)
20 * returns a 32-bit number suitable for feeding into itself
21 * or csum_tcpudp_magic
23 * this function must be called with even lengths, except
24 * for the last fragment, which may be odd
26 * it's best to have buff aligned on a 32-bit boundary
29 csum_partial(const void *buff
, int len
, __wsum sum
)
31 register unsigned long reg2
asm("2") = (unsigned long) buff
;
32 register unsigned long reg3
asm("3") = (unsigned long) len
;
35 "0: cksm %0,%1\n" /* do checksum on longs */
37 : "+d" (sum
), "+d" (reg2
), "+d" (reg3
) : : "cc", "memory");
42 * the same as csum_partial_copy, but copies from user space.
44 * here even more important to align src and dst on a 32-bit (or even
45 * better 64-bit) boundary
47 * Copy from userspace and compute checksum.
50 csum_partial_copy_from_user(const void __user
*src
, void *dst
,
54 if (unlikely(copy_from_user(dst
, src
, len
)))
56 return csum_partial(dst
, len
, sum
);
61 csum_partial_copy_nocheck (const void *src
, void *dst
, int len
, __wsum sum
)
64 return csum_partial(dst
, len
, sum
);
68 * Fold a partial checksum without adding pseudo headers
70 static inline __sum16
csum_fold(__wsum sum
)
72 u32 csum
= (__force u32
) sum
;
74 csum
+= (csum
>> 16) + (csum
<< 16);
76 return (__force __sum16
) ~csum
;
80 * This is a version of ip_compute_csum() optimized for IP headers,
81 * which always checksum on 4 octet boundaries.
84 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
86 return csum_fold(csum_partial(iph
, ihl
*4, 0));
90 * computes the checksum of the TCP/UDP pseudo-header
91 * returns a 32-bit checksum
94 csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
,
95 unsigned short len
, unsigned short proto
,
98 __u32 csum
= (__force __u32
)sum
;
100 csum
+= (__force __u32
)saddr
;
101 if (csum
< (__force __u32
)saddr
)
104 csum
+= (__force __u32
)daddr
;
105 if (csum
< (__force __u32
)daddr
)
109 if (csum
< len
+ proto
)
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
,
122 unsigned short len
, unsigned short proto
,
125 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
129 * this routine is used for miscellaneous IP-like checksums, mainly
133 static inline __sum16
ip_compute_csum(const void *buff
, int len
)
135 return csum_fold(csum_partial(buff
, len
, 0));
138 #endif /* _S390_CHECKSUM_H */