blob: 0a3cd7ec84510128181f14db0a10fcccd141e8d2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _S390_CHECKSUM_H
2#define _S390_CHECKSUM_H
3
4/*
5 * include/asm-s390/checksum.h
6 * S390 fast network checksum routines
7 * see also arch/S390/lib/checksum.c
8 *
9 * S390 version
10 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
11 * Author(s): Ulrich Hild (first version)
12 * Martin Schwidefsky (heavily optimized CKSM version)
13 * D.J. Barrow (third attempt)
14 */
15
16#include <asm/uaccess.h>
17
18/*
19 * computes the checksum of a memory block at buff, length len,
20 * and adds in "sum" (32-bit)
21 *
22 * returns a 32-bit number suitable for feeding into itself
23 * or csum_tcpudp_magic
24 *
25 * this function must be called with even lengths, except
26 * for the last fragment, which may be odd
27 *
28 * it's best to have buff aligned on a 32-bit boundary
29 */
Al Virof994aae2006-11-14 21:22:18 -080030static inline __wsum
31csum_partial(const void *buff, int len, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020033 register unsigned long reg2 asm("2") = (unsigned long) buff;
34 register unsigned long reg3 asm("3") = (unsigned long) len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020036 asm volatile(
37 "0: cksm %0,%1\n" /* do checksum on longs */
38 " jo 0b\n"
39 : "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return sum;
41}
42
43/*
44 * the same as csum_partial_copy, but copies from user space.
45 *
46 * here even more important to align src and dst on a 32-bit (or even
47 * better 64-bit) boundary
48 *
49 * Copy from userspace and compute checksum. If we catch an exception
50 * then zero the rest of the buffer.
51 */
Al Virof994aae2006-11-14 21:22:18 -080052static inline __wsum
53csum_partial_copy_from_user(const void __user *src, void *dst,
54 int len, __wsum sum,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int *err_ptr)
56{
57 int missing;
58
59 missing = copy_from_user(dst, src, len);
60 if (missing) {
61 memset(dst + len - missing, 0, missing);
62 *err_ptr = -EFAULT;
63 }
64
65 return csum_partial(dst, len, sum);
66}
67
68
Al Virof994aae2006-11-14 21:22:18 -080069static inline __wsum
70csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 memcpy(dst,src,len);
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020073 return csum_partial(dst, len, sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76/*
77 * Fold a partial checksum without adding pseudo headers
78 */
Al Virof994aae2006-11-14 21:22:18 -080079static inline __sum16 csum_fold(__wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81#ifndef __s390x__
82 register_pair rp;
83
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020084 asm volatile(
85 " slr %N1,%N1\n" /* %0 = H L */
86 " lr %1,%0\n" /* %0 = H L, %1 = H L 0 0 */
87 " srdl %1,16\n" /* %0 = H L, %1 = 0 H L 0 */
88 " alr %1,%N1\n" /* %0 = H L, %1 = L H L 0 */
89 " alr %0,%1\n" /* %0 = H+L+C L+H */
90 " srl %0,16\n" /* %0 = H+L+C */
91 : "+&d" (sum), "=d" (rp) : : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#else /* __s390x__ */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020093 asm volatile(
94 " sr 3,3\n" /* %0 = H*65536 + L */
95 " lr 2,%0\n" /* %0 = H L, 2/3 = H L / 0 0 */
96 " srdl 2,16\n" /* %0 = H L, 2/3 = 0 H / L 0 */
97 " alr 2,3\n" /* %0 = H L, 2/3 = L H / L 0 */
98 " alr %0,2\n" /* %0 = H+L+C L+H */
99 " srl %0,16\n" /* %0 = H+L+C */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 : "+&d" (sum) : : "cc", "2", "3");
101#endif /* __s390x__ */
Al Virof994aae2006-11-14 21:22:18 -0800102 return (__force __sum16) ~sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * This is a version of ip_compute_csum() optimized for IP headers,
107 * which always checksum on 4 octet boundaries.
108 *
109 */
Al Virof994aae2006-11-14 21:22:18 -0800110static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200112 return csum_fold(csum_partial(iph, ihl*4, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * computes the checksum of the TCP/UDP pseudo-header
117 * returns a 32-bit checksum
118 */
Al Virof994aae2006-11-14 21:22:18 -0800119static inline __wsum
120csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned short len, unsigned short proto,
Al Virof994aae2006-11-14 21:22:18 -0800122 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124#ifndef __s390x__
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200125 asm volatile(
126 " alr %0,%1\n" /* sum += saddr */
127 " brc 12,0f\n"
128 " ahi %0,1\n" /* add carry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 "0:"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200130 : "+&d" (sum) : "d" (saddr) : "cc");
131 asm volatile(
132 " alr %0,%1\n" /* sum += daddr */
133 " brc 12,1f\n"
134 " ahi %0,1\n" /* add carry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 "1:"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200136 : "+&d" (sum) : "d" (daddr) : "cc");
137 asm volatile(
Al Virof994aae2006-11-14 21:22:18 -0800138 " alr %0,%1\n" /* sum += len + proto */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200139 " brc 12,2f\n"
140 " ahi %0,1\n" /* add carry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 "2:"
142 : "+&d" (sum)
Al Virof994aae2006-11-14 21:22:18 -0800143 : "d" (len + proto)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200144 : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#else /* __s390x__ */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200146 asm volatile(
147 " lgfr %0,%0\n"
148 " algr %0,%1\n" /* sum += saddr */
149 " brc 12,0f\n"
150 " aghi %0,1\n" /* add carry */
151 "0: algr %0,%2\n" /* sum += daddr */
152 " brc 12,1f\n"
153 " aghi %0,1\n" /* add carry */
Al Virof994aae2006-11-14 21:22:18 -0800154 "1: algfr %0,%3\n" /* sum += len + proto */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200155 " brc 12,2f\n"
156 " aghi %0,1\n" /* add carry */
157 "2: srlg 0,%0,32\n"
158 " alr %0,0\n" /* fold to 32 bits */
159 " brc 12,3f\n"
160 " ahi %0,1\n" /* add carry */
161 "3: llgfr %0,%0"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 : "+&d" (sum)
163 : "d" (saddr), "d" (daddr),
Al Virof994aae2006-11-14 21:22:18 -0800164 "d" (len + proto)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200165 : "cc", "0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif /* __s390x__ */
167 return sum;
168}
169
170/*
171 * computes the checksum of the TCP/UDP pseudo-header
172 * returns a 16-bit checksum, already complemented
173 */
174
Al Virof994aae2006-11-14 21:22:18 -0800175static inline __sum16
176csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned short len, unsigned short proto,
Al Virof994aae2006-11-14 21:22:18 -0800178 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
181}
182
183/*
184 * this routine is used for miscellaneous IP-like checksums, mainly
185 * in icmp.c
186 */
187
Al Virof994aae2006-11-14 21:22:18 -0800188static inline __sum16 ip_compute_csum(const void *buff, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 return csum_fold(csum_partial(buff, len, 0));
191}
192
193#endif /* _S390_CHECKSUM_H */
194
195