more changes on original files
[linux-2.4.git] / include / asm-mips / checksum.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995, 1996, 1997, 1998, 2001 by Ralf Baechle
7  */
8 #ifndef _ASM_CHECKSUM_H
9 #define _ASM_CHECKSUM_H
10
11 #include <asm/uaccess.h>
12
13 /*
14  * computes the checksum of a memory block at buff, length len,
15  * and adds in "sum" (32-bit)
16  *
17  * returns a 32-bit number suitable for feeding into itself
18  * or csum_tcpudp_magic
19  *
20  * this function must be called with even lengths, except
21  * for the last fragment, which may be odd
22  *
23  * it's best to have buff aligned on a 32-bit boundary
24  */
25 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
26
27 /*
28  * this is a new version of the above that records errors it finds in *errp,
29  * but continues and zeros the rest of the buffer.
30  */
31 #define csum_partial_copy_nocheck csum_partial_copy
32
33 /*
34  * this is a new version of the above that records errors it finds in *errp,
35  * but continues and zeros the rest of the buffer.
36  */
37 unsigned int csum_partial_copy_from_user(const char *src, char *dst, int len,
38                                          unsigned int sum, int *errp);
39
40 /*
41  * Copy and checksum to user
42  */
43 #define HAVE_CSUM_COPY_USER
44 static inline unsigned int csum_and_copy_to_user (const char *src, char *dst,
45                                                   int len, int sum,
46                                                   int *err_ptr)
47 {
48         sum = csum_partial(src, len, sum);
49
50         if (copy_to_user(dst, src, len)) {
51                 *err_ptr = -EFAULT;
52                 return -1;
53         }
54
55         return sum;
56 }
57
58 /*
59  * the same as csum_partial, but copies from user space (but on MIPS
60  * we have just one address space, so this is identical to the above)
61  *
62  * this is obsolete and will go away.
63  */
64 #define csum_partial_copy_fromuser csum_partial_copy
65 unsigned int csum_partial_copy(const char *src, char *dst, int len,
66                                unsigned int sum);
67
68 /*
69  *      Fold a partial checksum without adding pseudo headers
70  */
71 static inline unsigned short int csum_fold(unsigned int sum)
72 {
73         __asm__(
74         ".set\tnoat\t\t\t# csum_fold\n\t"
75         "sll\t$1,%0,16\n\t"
76         "addu\t%0,$1\n\t"
77         "sltu\t$1,%0,$1\n\t"
78         "srl\t%0,%0,16\n\t"
79         "addu\t%0,$1\n\t"
80         "xori\t%0,0xffff\n\t"
81         ".set\tat"
82         : "=r" (sum)
83         : "0" (sum));
84
85         return sum;
86 }
87
88 /*
89  *      This is a version of ip_compute_csum() optimized for IP headers,
90  *      which always checksum on 4 octet boundaries.
91  *
92  *      By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
93  *      Arnt Gulbrandsen.
94  */
95 static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
96 {
97         unsigned int *word = (unsigned int *) iph;
98         unsigned int *stop = word + ihl;
99         unsigned int csum;
100         int carry;
101
102         csum = word[0];
103         csum += word[1];
104         carry = (csum < word[1]);
105         csum += carry;
106
107         csum += word[2];
108         carry = (csum < word[2]);
109         csum += carry;
110
111         csum += word[3];
112         carry = (csum < word[3]);
113         csum += carry;
114
115         word += 4;
116         do {
117                 csum += *word;
118                 carry = (csum < *word);
119                 csum += carry;
120                 word++;
121         } while (word != stop);
122
123         return csum_fold(csum);
124 }
125
126 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
127                                                unsigned long daddr,
128                                                unsigned short len,
129                                                unsigned short proto,
130                                                unsigned int sum)
131 {
132         __asm__(
133         ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t"
134         "addu\t%0, %2\n\t"
135         "sltu\t$1, %0, %2\n\t"
136         "addu\t%0, $1\n\t"
137
138         "addu\t%0, %3\n\t"
139         "sltu\t$1, %0, %3\n\t"
140         "addu\t%0, $1\n\t"
141
142         "addu\t%0, %4\n\t"
143         "sltu\t$1, %0, %4\n\t"
144         "addu\t%0, $1\n\t"
145         ".set\tat"
146         : "=r" (sum)
147         : "0" (daddr), "r"(saddr),
148 #ifdef __MIPSEL__
149           "r" ((ntohs(len)<<16)+proto*256),
150 #else
151           "r" (((proto)<<16)+len),
152 #endif
153           "r" (sum));
154
155         return sum;
156 }
157
158 /*
159  * computes the checksum of the TCP/UDP pseudo-header
160  * returns a 16-bit checksum, already complemented
161  */
162 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
163                                                    unsigned long daddr,
164                                                    unsigned short len,
165                                                    unsigned short proto,
166                                                    unsigned int sum)
167 {
168         return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
169 }
170
171 /*
172  * this routine is used for miscellaneous IP-like checksums, mainly
173  * in icmp.c
174  */
175 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
176 {
177         return csum_fold(csum_partial(buff, len, 0));
178 }
179
180 #define _HAVE_ARCH_IPV6_CSUM
181 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
182                                                      struct in6_addr *daddr,
183                                                      __u32 len,
184                                                      unsigned short proto,
185                                                      unsigned int sum)
186 {
187         __asm__(
188         ".set\tpush\t\t\t# csum_ipv6_magic\n\t"
189         ".set\tnoreorder\n\t"
190         ".set\tnoat\n\t"
191         "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t"
192         "sltu\t$1, %0, %5\n\t"
193         "addu\t%0, $1\n\t"
194
195         "addu\t%0, %6\t\t\t# csum\n\t"
196         "sltu\t$1, %0, %6\n\t"
197         "lw\t%1, 0(%2)\t\t\t# four words source address\n\t"
198         "addu\t%0, $1\n\t"
199         "addu\t%0, %1\n\t"
200         "sltu\t$1, %0, %1\n\t"
201
202         "lw\t%1, 4(%2)\n\t"
203         "addu\t%0, $1\n\t"
204         "addu\t%0, %1\n\t"
205         "sltu\t$1, %0, %1\n\t"
206
207         "lw\t%1, 8(%2)\n\t"
208         "addu\t%0, $1\n\t"
209         "addu\t%0, %1\n\t"
210         "sltu\t$1, %0, %1\n\t"
211
212         "lw\t%1, 12(%2)\n\t"
213         "addu\t%0, $1\n\t"
214         "addu\t%0, %1\n\t"
215         "sltu\t$1, %0, %1\n\t"
216
217         "lw\t%1, 0(%3)\n\t"
218         "addu\t%0, $1\n\t"
219         "addu\t%0, %1\n\t"
220         "sltu\t$1, %0, %1\n\t"
221
222         "lw\t%1, 4(%3)\n\t"
223         "addu\t%0, $1\n\t"
224         "addu\t%0, %1\n\t"
225         "sltu\t$1, %0, %1\n\t"
226
227         "lw\t%1, 8(%3)\n\t"
228         "addu\t%0, $1\n\t"
229         "addu\t%0, %1\n\t"
230         "sltu\t$1, %0, %1\n\t"
231
232         "lw\t%1, 12(%3)\n\t"
233         "addu\t%0, $1\n\t"
234         "addu\t%0, %1\n\t"
235         "sltu\t$1, %0, %1\n\t"
236
237         "addu\t%0, $1\t\t\t# Add final carry\n\t"
238         ".set\tpop"
239         : "=r" (sum), "=r" (proto)
240         : "r" (saddr), "r" (daddr),
241           "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
242
243         return csum_fold(sum);
244 }
245
246 #endif /* _ASM_CHECKSUM_H */