blob: 37c362d89fad62210c78d8068b953c66679f11dc [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 */
30static inline unsigned int
31csum_partial(const unsigned char * buff, int len, unsigned int sum)
32{
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 */
52static inline unsigned int
53csum_partial_copy_from_user(const char __user *src, char *dst,
54 int len, unsigned int sum,
55 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
69static inline unsigned int
70csum_partial_copy_nocheck (const char *src, char *dst, int len, unsigned int sum)
71{
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 */
79static inline unsigned short
80csum_fold(unsigned int sum)
81{
82#ifndef __s390x__
83 register_pair rp;
84
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020085 asm volatile(
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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#else /* __s390x__ */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020094 asm volatile(
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 : "+&d" (sum) : : "cc", "2", "3");
102#endif /* __s390x__ */
103 return ((unsigned short) ~sum);
104}
105
106/*
107 * This is a version of ip_compute_csum() optimized for IP headers,
108 * which always checksum on 4 octet boundaries.
109 *
110 */
111static inline unsigned short
112ip_fast_csum(unsigned char *iph, unsigned int ihl)
113{
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200114 return csum_fold(csum_partial(iph, ihl*4, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/*
118 * computes the checksum of the TCP/UDP pseudo-header
119 * returns a 32-bit checksum
120 */
121static inline unsigned int
122csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr,
123 unsigned short len, unsigned short proto,
124 unsigned int sum)
125{
126#ifndef __s390x__
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200127 asm volatile(
128 " alr %0,%1\n" /* sum += saddr */
129 " brc 12,0f\n"
130 " ahi %0,1\n" /* add carry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 "0:"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200132 : "+&d" (sum) : "d" (saddr) : "cc");
133 asm volatile(
134 " alr %0,%1\n" /* sum += daddr */
135 " brc 12,1f\n"
136 " ahi %0,1\n" /* add carry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 "1:"
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200138 : "+&d" (sum) : "d" (daddr) : "cc");
139 asm volatile(
140 " alr %0,%1\n" /* sum += (len<<16) + (proto<<8) */
141 " brc 12,2f\n"
142 " ahi %0,1\n" /* add carry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 "2:"
144 : "+&d" (sum)
145 : "d" (((unsigned int) len<<16) + (unsigned int) proto)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200146 : "cc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#else /* __s390x__ */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200148 asm volatile(
149 " lgfr %0,%0\n"
150 " algr %0,%1\n" /* sum += saddr */
151 " brc 12,0f\n"
152 " aghi %0,1\n" /* add carry */
153 "0: algr %0,%2\n" /* sum += daddr */
154 " brc 12,1f\n"
155 " aghi %0,1\n" /* add carry */
156 "1: algfr %0,%3\n" /* sum += (len<<16) + proto */
157 " brc 12,2f\n"
158 " aghi %0,1\n" /* add carry */
159 "2: srlg 0,%0,32\n"
160 " alr %0,0\n" /* fold to 32 bits */
161 " brc 12,3f\n"
162 " ahi %0,1\n" /* add carry */
163 "3: llgfr %0,%0"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 : "+&d" (sum)
165 : "d" (saddr), "d" (daddr),
166 "d" (((unsigned int) len<<16) + (unsigned int) proto)
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +0200167 : "cc", "0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#endif /* __s390x__ */
169 return sum;
170}
171
172/*
173 * computes the checksum of the TCP/UDP pseudo-header
174 * returns a 16-bit checksum, already complemented
175 */
176
177static inline unsigned short int
178csum_tcpudp_magic(unsigned long saddr, unsigned long daddr,
179 unsigned short len, unsigned short proto,
180 unsigned int sum)
181{
182 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
183}
184
185/*
186 * this routine is used for miscellaneous IP-like checksums, mainly
187 * in icmp.c
188 */
189
190static inline unsigned short
191ip_compute_csum(unsigned char * buff, int len)
192{
193 return csum_fold(csum_partial(buff, len, 0));
194}
195
196#endif /* _S390_CHECKSUM_H */
197
198