1 #ifndef _S390_CHECKSUM_H
2 #define _S390_CHECKSUM_H
5 * include/asm-s390/checksum.h
6 * S390 fast network checksum routines
7 * see also arch/S390/lib/checksum.c
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)
16 #include <asm/uaccess.h>
19 * computes the checksum of a memory block at buff, length len,
20 * and adds in "sum" (32-bit)
22 * returns a 32-bit number suitable for feeding into itself
23 * or csum_tcpudp_magic
25 * this function must be called with even lengths, except
26 * for the last fragment, which may be odd
28 * it's best to have buff aligned on a 32-bit boundary
30 static inline unsigned int
31 csum_partial(const unsigned char * buff, int len, unsigned int sum)
33 register unsigned long reg2 asm("2") = (unsigned long) buff;
34 register unsigned long reg3 asm("3") = (unsigned long) len;
37 "0: cksm %0,%1\n" /* do checksum on longs */
39 : "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");
44 * the same as csum_partial_copy, but copies from user space.
46 * here even more important to align src and dst on a 32-bit (or even
47 * better 64-bit) boundary
49 * Copy from userspace and compute checksum. If we catch an exception
50 * then zero the rest of the buffer.
52 static inline unsigned int
53 csum_partial_copy_from_user(const char __user *src, char *dst,
54 int len, unsigned int sum,
59 missing = copy_from_user(dst, src, len);
61 memset(dst + len - missing, 0, missing);
65 return csum_partial(dst, len, sum);
69 static inline unsigned int
70 csum_partial_copy_nocheck (const char *src, char *dst, int len, unsigned int sum)
73 return csum_partial(dst, len, sum);
77 * Fold a partial checksum without adding pseudo headers
79 static inline unsigned short
80 csum_fold(unsigned int sum)
86 " slr %N1,%N1\n" /* %0 = H L */
87 " lr %1,%0\n" /* %0 = H L, %1 = H L 0 0 */
88 " srdl %1,16\n" /* %0 = H L, %1 = 0 H L 0 */
89 " alr %1,%N1\n" /* %0 = H L, %1 = L H L 0 */
90 " alr %0,%1\n" /* %0 = H+L+C L+H */
91 " srl %0,16\n" /* %0 = H+L+C */
92 : "+&d" (sum), "=d" (rp) : : "cc");
95 " sr 3,3\n" /* %0 = H*65536 + L */
96 " lr 2,%0\n" /* %0 = H L, 2/3 = H L / 0 0 */
97 " srdl 2,16\n" /* %0 = H L, 2/3 = 0 H / L 0 */
98 " alr 2,3\n" /* %0 = H L, 2/3 = L H / L 0 */
99 " alr %0,2\n" /* %0 = H+L+C L+H */
100 " srl %0,16\n" /* %0 = H+L+C */
101 : "+&d" (sum) : : "cc", "2", "3");
102 #endif /* __s390x__ */
103 return ((unsigned short) ~sum);
107 * This is a version of ip_compute_csum() optimized for IP headers,
108 * which always checksum on 4 octet boundaries.
111 static inline unsigned short
112 ip_fast_csum(unsigned char *iph, unsigned int ihl)
114 return csum_fold(csum_partial(iph, ihl*4, 0));
118 * computes the checksum of the TCP/UDP pseudo-header
119 * returns a 32-bit checksum
121 static inline unsigned int
122 csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr,
123 unsigned short len, unsigned short proto,
128 " alr %0,%1\n" /* sum += saddr */
130 " ahi %0,1\n" /* add carry */
132 : "+&d" (sum) : "d" (saddr) : "cc");
134 " alr %0,%1\n" /* sum += daddr */
136 " ahi %0,1\n" /* add carry */
138 : "+&d" (sum) : "d" (daddr) : "cc");
140 " alr %0,%1\n" /* sum += (len<<16) + (proto<<8) */
142 " ahi %0,1\n" /* add carry */
145 : "d" (((unsigned int) len<<16) + (unsigned int) proto)
147 #else /* __s390x__ */
150 " algr %0,%1\n" /* sum += saddr */
152 " aghi %0,1\n" /* add carry */
153 "0: algr %0,%2\n" /* sum += daddr */
155 " aghi %0,1\n" /* add carry */
156 "1: algfr %0,%3\n" /* sum += (len<<16) + proto */
158 " aghi %0,1\n" /* add carry */
160 " alr %0,0\n" /* fold to 32 bits */
162 " ahi %0,1\n" /* add carry */
165 : "d" (saddr), "d" (daddr),
166 "d" (((unsigned int) len<<16) + (unsigned int) proto)
168 #endif /* __s390x__ */
173 * computes the checksum of the TCP/UDP pseudo-header
174 * returns a 16-bit checksum, already complemented
177 static inline unsigned short int
178 csum_tcpudp_magic(unsigned long saddr, unsigned long daddr,
179 unsigned short len, unsigned short proto,
182 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
186 * this routine is used for miscellaneous IP-like checksums, mainly
190 static inline unsigned short
191 ip_compute_csum(unsigned char * buff, int len)
193 return csum_fold(csum_partial(buff, len, 0));
196 #endif /* _S390_CHECKSUM_H */