blob: 7403648563554604e19b6cfbd0873bee868c7644 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * S390 fast network checksum routines
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * S390 version
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Ulrich Hild (first version)
7 * Martin Schwidefsky (heavily optimized CKSM version)
8 * D.J. Barrow (third attempt)
9 */
10
Heiko Carstensa53c8fa2012-07-20 11:15:04 +020011#ifndef _S390_CHECKSUM_H
12#define _S390_CHECKSUM_H
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/uaccess.h>
15
16/*
17 * computes the checksum of a memory block at buff, length len,
18 * and adds in "sum" (32-bit)
19 *
20 * returns a 32-bit number suitable for feeding into itself
21 * or csum_tcpudp_magic
22 *
23 * this function must be called with even lengths, except
24 * for the last fragment, which may be odd
25 *
26 * it's best to have buff aligned on a 32-bit boundary
27 */
Al Virof994aae2006-11-14 21:22:18 -080028static inline __wsum
29csum_partial(const void *buff, int len, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020031 register unsigned long reg2 asm("2") = (unsigned long) buff;
32 register unsigned long reg3 asm("3") = (unsigned long) len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020034 asm volatile(
35 "0: cksm %0,%1\n" /* do checksum on longs */
36 " jo 0b\n"
37 : "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return sum;
39}
40
41/*
42 * the same as csum_partial_copy, but copies from user space.
43 *
44 * here even more important to align src and dst on a 32-bit (or even
45 * better 64-bit) boundary
46 *
Heiko Carstensd72d2bb2014-02-24 16:33:08 +010047 * Copy from userspace and compute checksum.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Al Virof994aae2006-11-14 21:22:18 -080049static inline __wsum
50csum_partial_copy_from_user(const void __user *src, void *dst,
51 int len, __wsum sum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int *err_ptr)
53{
Heiko Carstensd72d2bb2014-02-24 16:33:08 +010054 if (unlikely(copy_from_user(dst, src, len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *err_ptr = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return csum_partial(dst, len, sum);
57}
58
59
Al Virof994aae2006-11-14 21:22:18 -080060static inline __wsum
61csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 memcpy(dst,src,len);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020064 return csum_partial(dst, len, sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/*
68 * Fold a partial checksum without adding pseudo headers
69 */
Al Virof994aae2006-11-14 21:22:18 -080070static inline __sum16 csum_fold(__wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Heiko Carstens04efc3b2009-09-11 10:28:32 +020072 u32 csum = (__force u32) sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Heiko Carstens04efc3b2009-09-11 10:28:32 +020074 csum += (csum >> 16) + (csum << 16);
75 csum >>= 16;
76 return (__force __sum16) ~csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79/*
80 * This is a version of ip_compute_csum() optimized for IP headers,
81 * which always checksum on 4 octet boundaries.
82 *
83 */
Al Virof994aae2006-11-14 21:22:18 -080084static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020086 return csum_fold(csum_partial(iph, ihl*4, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * computes the checksum of the TCP/UDP pseudo-header
91 * returns a 32-bit checksum
92 */
Al Virof994aae2006-11-14 21:22:18 -080093static inline __wsum
94csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 unsigned short len, unsigned short proto,
Al Virof994aae2006-11-14 21:22:18 -080096 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Heiko Carstensafbc1e92007-03-26 20:42:39 +020098 __u32 csum = (__force __u32)sum;
99
100 csum += (__force __u32)saddr;
101 if (csum < (__force __u32)saddr)
102 csum++;
103
104 csum += (__force __u32)daddr;
105 if (csum < (__force __u32)daddr)
106 csum++;
107
108 csum += len + proto;
109 if (csum < len + proto)
110 csum++;
111
112 return (__force __wsum)csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * computes the checksum of the TCP/UDP pseudo-header
117 * returns a 16-bit checksum, already complemented
118 */
119
Al Virof994aae2006-11-14 21:22:18 -0800120static inline __sum16
121csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned short len, unsigned short proto,
Al Virof994aae2006-11-14 21:22:18 -0800123 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
126}
127
128/*
129 * this routine is used for miscellaneous IP-like checksums, mainly
130 * in icmp.c
131 */
132
Al Virof994aae2006-11-14 21:22:18 -0800133static inline __sum16 ip_compute_csum(const void *buff, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 return csum_fold(csum_partial(buff, len, 0));
136}
137
138#endif /* _S390_CHECKSUM_H */
139
140