Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[pandora-kernel.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/in6.h>
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  */
30 __wsum csum_partial(const void *buff, int len, __wsum sum);
31
32 /*
33  * this is a new version of the above that records errors it finds in *errp,
34  * but continues and zeros the rest of the buffer.
35  */
36 __wsum csum_partial_copy_from_user(const void __user *src,
37                                          void *dst, int len,
38                                          __wsum sum, int *errp);
39
40 /*
41  * Copy and checksum to user
42  */
43 #define HAVE_CSUM_COPY_USER
44 static inline __wsum csum_and_copy_to_user (const void *src, void __user *dst,
45                                                   int len, __wsum sum,
46                                                   int *err_ptr)
47 {
48         might_sleep();
49         sum = csum_partial(src, len, sum);
50
51         if (copy_to_user(dst, src, len)) {
52                 *err_ptr = -EFAULT;
53                 return (__force __wsum)-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 __wsum csum_partial_copy_nocheck(const void *src, void *dst,
64                                        int len, __wsum sum);
65
66 /*
67  *      Fold a partial checksum without adding pseudo headers
68  */
69 static inline __sum16 csum_fold(__wsum sum)
70 {
71         __asm__(
72         "       .set    push            # csum_fold\n"
73         "       .set    noat            \n"
74         "       sll     $1, %0, 16      \n"
75         "       addu    %0, $1          \n"
76         "       sltu    $1, %0, $1      \n"
77         "       srl     %0, %0, 16      \n"
78         "       addu    %0, $1          \n"
79         "       xori    %0, 0xffff      \n"
80         "       .set    pop"
81         : "=r" (sum)
82         : "0" (sum));
83
84         return (__force __sum16)sum;
85 }
86
87 /*
88  *      This is a version of ip_compute_csum() optimized for IP headers,
89  *      which always checksum on 4 octet boundaries.
90  *
91  *      By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
92  *      Arnt Gulbrandsen.
93  */
94 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
95 {
96         const unsigned int *word = iph;
97         const unsigned int *stop = word + ihl;
98         unsigned int csum;
99         int carry;
100
101         csum = word[0];
102         csum += word[1];
103         carry = (csum < word[1]);
104         csum += carry;
105
106         csum += word[2];
107         carry = (csum < word[2]);
108         csum += carry;
109
110         csum += word[3];
111         carry = (csum < word[3]);
112         csum += carry;
113
114         word += 4;
115         do {
116                 csum += *word;
117                 carry = (csum < *word);
118                 csum += carry;
119                 word++;
120         } while (word != stop);
121
122         return csum_fold(csum);
123 }
124
125 static inline __wsum csum_tcpudp_nofold(__be32 saddr,
126         __be32 daddr, unsigned short len, unsigned short proto,
127         __wsum sum)
128 {
129         __asm__(
130         "       .set    push            # csum_tcpudp_nofold\n"
131         "       .set    noat            \n"
132 #ifdef CONFIG_32BIT
133         "       addu    %0, %2          \n"
134         "       sltu    $1, %0, %2      \n"
135         "       addu    %0, $1          \n"
136
137         "       addu    %0, %3          \n"
138         "       sltu    $1, %0, %3      \n"
139         "       addu    %0, $1          \n"
140
141         "       addu    %0, %4          \n"
142         "       sltu    $1, %0, %4      \n"
143         "       addu    %0, $1          \n"
144 #endif
145 #ifdef CONFIG_64BIT
146         "       daddu   %0, %2          \n"
147         "       daddu   %0, %3          \n"
148         "       daddu   %0, %4          \n"
149         "       dsll32  $1, %0, 0       \n"
150         "       daddu   %0, $1          \n"
151         "       dsra32  %0, %0, 0       \n"
152 #endif
153         "       .set    pop"
154         : "=r" (sum)
155         : "0" (daddr), "r"(saddr),
156 #ifdef __MIPSEL__
157           "r" ((proto + len) << 8),
158 #else
159           "r" (proto + len),
160 #endif
161           "r" (sum));
162
163         return sum;
164 }
165
166 /*
167  * computes the checksum of the TCP/UDP pseudo-header
168  * returns a 16-bit checksum, already complemented
169  */
170 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
171                                                    unsigned short len,
172                                                    unsigned short proto,
173                                                    __wsum sum)
174 {
175         return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
176 }
177
178 /*
179  * this routine is used for miscellaneous IP-like checksums, mainly
180  * in icmp.c
181  */
182 static inline __sum16 ip_compute_csum(const void *buff, int len)
183 {
184         return csum_fold(csum_partial(buff, len, 0));
185 }
186
187 #define _HAVE_ARCH_IPV6_CSUM
188 static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
189                                           const struct in6_addr *daddr,
190                                           __u32 len, unsigned short proto,
191                                           __wsum sum)
192 {
193         __asm__(
194         "       .set    push            # csum_ipv6_magic\n"
195         "       .set    noreorder       \n"
196         "       .set    noat            \n"
197         "       addu    %0, %5          # proto (long in network byte order)\n"
198         "       sltu    $1, %0, %5      \n"
199         "       addu    %0, $1          \n"
200
201         "       addu    %0, %6          # csum\n"
202         "       sltu    $1, %0, %6      \n"
203         "       lw      %1, 0(%2)       # four words source address\n"
204         "       addu    %0, $1          \n"
205         "       addu    %0, %1          \n"
206         "       sltu    $1, %0, %1      \n"
207
208         "       lw      %1, 4(%2)       \n"
209         "       addu    %0, $1          \n"
210         "       addu    %0, %1          \n"
211         "       sltu    $1, %0, %1      \n"
212
213         "       lw      %1, 8(%2)       \n"
214         "       addu    %0, $1          \n"
215         "       addu    %0, %1          \n"
216         "       sltu    $1, %0, %1      \n"
217
218         "       lw      %1, 12(%2)      \n"
219         "       addu    %0, $1          \n"
220         "       addu    %0, %1          \n"
221         "       sltu    $1, %0, %1      \n"
222
223         "       lw      %1, 0(%3)       \n"
224         "       addu    %0, $1          \n"
225         "       addu    %0, %1          \n"
226         "       sltu    $1, %0, %1      \n"
227
228         "       lw      %1, 4(%3)       \n"
229         "       addu    %0, $1          \n"
230         "       addu    %0, %1          \n"
231         "       sltu    $1, %0, %1      \n"
232
233         "       lw      %1, 8(%3)       \n"
234         "       addu    %0, $1          \n"
235         "       addu    %0, %1          \n"
236         "       sltu    $1, %0, %1      \n"
237
238         "       lw      %1, 12(%3)      \n"
239         "       addu    %0, $1          \n"
240         "       addu    %0, %1          \n"
241         "       sltu    $1, %0, %1      \n"
242
243         "       addu    %0, $1          # Add final carry\n"
244         "       .set    pop"
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 */