include upstream ip1000a driver version 2.09f
[linux-2.4.git] / arch / sh64 / lib / c-checksum.c
1 /*
2  * arch/sh/lib/csum_parial.c
3  *
4  * This file contains network checksum routines that are better done
5  * in an architecture-specific manner due to speed..
6  */
7
8 #include <linux/config.h>
9 #include <linux/string.h>
10 #include <linux/errno.h>
11 #include <asm/byteorder.h>
12 #include <asm/uaccess.h>
13
14 #if 1
15 #define dprintk(x...)
16 #else
17 #define dprintk(x...) printk(x)
18 #endif
19
20 static inline unsigned short from64to16(unsigned long long x)
21 {
22         /* add up 32-bit words for 33 bits */
23         x = (x & 0xffffffff) + (x >> 32);
24         /* add up 16-bit and 17-bit words for 17+c bits */
25         x = (x & 0xffff) + (x >> 16);
26         /* add up 16-bit and 2-bit for 16+c bit */
27         x = (x & 0xffff) + (x >> 16);
28         /* add up carry.. */
29         x = (x & 0xffff) + (x >> 16);
30         return x;
31 }
32
33 static inline unsigned short foldto16(unsigned long x)
34 {
35         /* add up 16-bit for 17 bits */
36         x = (x & 0xffff) + (x >> 16);
37         /* add up carry.. */
38         x = (x & 0xffff) + (x >> 16);
39         return x;
40 }
41
42 static inline unsigned short myfoldto16(unsigned long long x)
43 {
44         /* Fold down to 32-bits so we don't loose in the typedef-less 
45            network stack.  */
46         /* 64 to 33 */
47         x = (x & 0xffffffff) + (x >> 32);
48         /* 33 to 32 */
49         x = (x & 0xffffffff) + (x >> 32);
50
51         /* add up 16-bit for 17 bits */
52         x = (x & 0xffff) + (x >> 16);
53         /* add up carry.. */
54         x = (x & 0xffff) + (x >> 16);
55         return x;
56 }
57
58 #define odd(x) ((x)&1)
59 /*
60 typedef unsigned short u16;
61 typedef unsigned  u32;
62 */
63 #define U16(x) ntohs(x)
64
65 static unsigned long do_moronic_csum(const unsigned char *a16p, int cnt)
66 {
67         u32 sum;
68         int oddb;
69
70         /* This algorithm is correct only for those values of cnt < 65536 */
71
72         oddb = odd(cnt);
73         cnt >>= 1;              /* convert to a word count */
74         sum = 0;                /* do a straight two's complement sum */
75         while (cnt--)
76                 sum += U16(*a16p++);
77         if (oddb)               /* pick up the odd byte */
78                 sum += U16(*a16p++) & (u16) 0xFF00;
79         /* add in the sum of the `carry' bits, making this one's complement */
80         sum = (sum & (u32) 0xFFFF) + ((sum >> 16) & (u32) 0xFFFF);
81         if (sum & (u32) 0x10000)        /* one last possible carry */
82                 sum = (sum + 1) & (u32) 0xFFFF;
83         if (sum == (u32) 0xFFFF)        /* remove the -0 ambiguity */
84                 sum = (u32) 0;
85         return sum;
86
87 }
88
89 #if 0
90 static inline unsigned long do_csum(const unsigned char *buff, int len)
91 {
92         int odd, count;
93         unsigned long long result = 0;
94
95         if (len <= 0)
96                 goto out;
97         odd = 1 & (unsigned long) buff;
98         if (odd) {
99                 result = *buff << 8;
100                 len--;
101                 buff++;
102         }
103         count = len >> 1;       /* nr of 16-bit words.. */
104         if (count) {
105                 if (2 & (unsigned long) buff) {
106                         result += *(unsigned short *) buff;
107                         count--;
108                         len -= 2;
109                         buff += 2;
110                 }
111                 count >>= 1;    /* nr of 32-bit words.. */
112                 if (count) {
113                         if (4 & (unsigned long) buff) {
114                                 result += *(unsigned int *) buff;
115                                 count--;
116                                 len -= 4;
117                                 buff += 4;
118                         }
119                         count >>= 1;    /* nr of 64-bit words.. */
120                         if (count) {
121                                 unsigned long carry = 0;
122                                 do {
123                                         unsigned long w =
124                                             *(unsigned long *) buff;
125                                         count--;
126                                         buff += 8;
127                                         result += carry;
128                                         result += w;
129                                         carry = (w > result);
130                                 } while (count);
131                                 result += carry;
132                                 result = (result & 0xffffffff) + (result >> 32);
133                         }
134                         if (len & 4) {
135                                 result += *(unsigned int *) buff;
136                                 buff += 4;
137                         }
138                 }
139                 if (len & 2) {
140                         result += *(unsigned short *) buff;
141                         buff += 2;
142                 }
143         }
144         if (len & 1)
145                 result += *buff;
146         result = from64to16(result);
147         if (odd)
148                 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
149       out:
150         return result;
151 }
152
153 #else
154
155 static unsigned long do_csum(const unsigned char *buff, int len)
156 {
157         int odd, count;
158         unsigned long result = 0;
159
160         dprintk("do_csum buff %p, len %d (0x%x)\n", buff, len, len);
161 #ifdef DEBUG
162         for (i = 0; i < len; i++) {
163                 if ((i % 26) == 0)
164                         printk("\n");
165                 printk("%02X ", buff[i]);
166         }
167 #endif
168
169         if (len <= 0)
170                 goto out;
171
172         odd = 1 & (unsigned long) buff;
173         if (odd) {
174                 result = *buff << 8;
175                 len--;
176                 buff++;
177         }
178         count = len >> 1;       /* nr of 16-bit words.. */
179         if (count) {
180                 if (2 & (unsigned long) buff) {
181                         result += *(unsigned short *) buff;
182                         count--;
183                         len -= 2;
184                         buff += 2;
185                 }
186                 count >>= 1;    /* nr of 32-bit words.. */
187                 if (count) {
188                         unsigned long carry = 0;
189                         do {
190                                 unsigned long w = *(unsigned long *) buff;
191                                 buff += 4;
192                                 count--;
193                                 result += carry;
194                                 result += w;
195                                 carry = (w > result);
196                         } while (count);
197                         result += carry;
198                         result = (result & 0xffff) + (result >> 16);
199                 }
200                 if (len & 2) {
201                         result += *(unsigned short *) buff;
202                         buff += 2;
203                 }
204         }
205         if (len & 1)
206                 result += *buff;
207         result = foldto16(result);
208         if (odd)
209                 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
210
211         dprintk("\nCHECKSUM is 0x%x\n", result);
212
213       out:
214         return result;
215 }
216
217 #endif
218
219 /* computes the checksum of a memory block at buff, length len,
220    and adds in "sum" (32-bit)  */
221 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
222 {
223         unsigned long long result = do_csum(buff, len);
224
225         /* add in old sum, and carry.. */
226         result += sum;
227         /* 32+c bits -> 32 bits */
228         result = (result & 0xffffffff) + (result >> 32);
229
230         dprintk("csum_partial, buff %p len %d sum 0x%x result=0x%016Lx\n",
231                 buff, len, sum, result);
232
233         return result;
234 }
235
236 /* Copy while checksumming, otherwise like csum_partial.  */
237 unsigned int
238 csum_partial_copy(const char *src, char *dst, int len, unsigned int sum)
239 {
240         sum = csum_partial(src, len, sum);
241         memcpy(dst, src, len);
242
243         return sum;
244 }
245
246 /* Copy from userspace and compute checksum.  If we catch an exception
247    then zero the rest of the buffer.  */
248 unsigned int
249 csum_partial_copy_from_user(const char *src, char *dst, int len,
250                             unsigned int sum, int *err_ptr)
251 {
252         int missing;
253
254         dprintk
255             ("csum_partial_copy_from_user src %p, dest %p, len %d, sum %08x, err_ptr %p\n",
256              src, dst, len, sum, err_ptr);
257         missing = copy_from_user(dst, src, len);
258         dprintk("  access_ok %d\n", __access_ok((unsigned long) src, len));
259         dprintk("  missing %d\n", missing);
260         if (missing) {
261                 memset(dst + len - missing, 0, missing);
262                 *err_ptr = -EFAULT;
263         }
264
265         return csum_partial(dst, len, sum);
266 }
267
268 /* Copy to userspace and compute checksum.  */
269 unsigned int
270 csum_partial_copy_to_user(const char *src, char *dst, int len,
271                           unsigned int sum, int *err_ptr)
272 {
273         sum = csum_partial(src, len, sum);
274
275         if (copy_to_user(dst, src, len))
276                 *err_ptr = -EFAULT;
277
278         return sum;
279 }
280
281 /*
282  *      This is a version of ip_compute_csum() optimized for IP headers,
283  *      which always checksum on 4 octet boundaries.
284  */
285 unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
286 {
287         dprintk("ip_fast_csum %p,%d\n", iph, ihl);
288
289         return ~do_csum(iph, ihl * 4);
290 }
291
292 unsigned int csum_tcpudp_nofold(unsigned long saddr,
293                                 unsigned long daddr,
294                                 unsigned short len,
295                                 unsigned short proto, unsigned int sum)
296 {
297         unsigned long long result;
298
299         dprintk("ntohs(0x%x)=0x%x\n", 0xdead, ntohs(0xdead));
300         dprintk("htons(0x%x)=0x%x\n", 0xdead, htons(0xdead));
301
302         result = ((unsigned long long) saddr +
303                   (unsigned long long) daddr +
304                   (unsigned long long) sum +
305                   ((unsigned long long) ntohs(len) << 16) +
306                   ((unsigned long long) proto << 8));
307
308         /* Fold down to 32-bits so we don't loose in the typedef-less 
309            network stack.  */
310         /* 64 to 33 */
311         result = (result & 0xffffffff) + (result >> 32);
312         /* 33 to 32 */
313         result = (result & 0xffffffff) + (result >> 32);
314
315         dprintk("%s saddr %x daddr %x len %x proto %x sum %x result %08Lx\n",
316                 __FUNCTION__, saddr, daddr, len, proto, sum, result);
317
318         return result;
319 }
320
321 // Post SIM:
322 unsigned int
323 csum_partial_copy_nocheck(const char *src, char *dst, int len, unsigned int sum)
324 {
325         //  unsigned dummy;
326         dprintk("csum_partial_copy_nocheck src %p dst %p len %d\n", src, dst,
327                 len);
328
329         return csum_partial_copy(src, dst, len, sum);
330 }