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