2 * arch/sh/lib/csum_parial.c
4 * This file contains network checksum routines that are better done
5 * in an architecture-specific manner due to speed..
10 #include <linux/config.h>
11 #include <linux/string.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <asm/byteorder.h>
15 #include <asm/uaccess.h>
17 static inline unsigned short from64to16(unsigned long long x
)
19 /* add up 32-bit words for 33 bits */
20 x
= (x
& 0xffffffff) + (x
>> 32);
21 /* add up 16-bit and 17-bit words for 17+c bits */
22 x
= (x
& 0xffff) + (x
>> 16);
23 /* add up 16-bit and 2-bit for 16+c bit */
24 x
= (x
& 0xffff) + (x
>> 16);
26 x
= (x
& 0xffff) + (x
>> 16);
30 static inline unsigned short foldto16(unsigned long x
)
32 /* add up 16-bit for 17 bits */
33 x
= (x
& 0xffff) + (x
>> 16);
35 x
= (x
& 0xffff) + (x
>> 16);
39 static inline unsigned short myfoldto16(unsigned long long x
)
41 /* Fold down to 32-bits so we don't loose in the typedef-less
44 x
= (x
& 0xffffffff) + (x
>> 32);
46 x
= (x
& 0xffffffff) + (x
>> 32);
48 /* add up 16-bit for 17 bits */
49 x
= (x
& 0xffff) + (x
>> 16);
51 x
= (x
& 0xffff) + (x
>> 16);
55 #define odd(x) ((x)&1)
56 #define U16(x) ntohs(x)
58 static unsigned long do_csum(const unsigned char *buff
, int len
)
61 unsigned long result
= 0;
63 pr_debug("do_csum buff %p, len %d (0x%x)\n", buff
, len
, len
);
65 for (i
= 0; i
< len
; i
++) {
68 printk("%02X ", buff
[i
]);
75 odd
= 1 & (unsigned long) buff
;
81 count
= len
>> 1; /* nr of 16-bit words.. */
83 if (2 & (unsigned long) buff
) {
84 result
+= *(unsigned short *) buff
;
89 count
>>= 1; /* nr of 32-bit words.. */
91 unsigned long carry
= 0;
93 unsigned long w
= *(unsigned long *) buff
;
101 result
= (result
& 0xffff) + (result
>> 16);
104 result
+= *(unsigned short *) buff
;
110 result
= foldto16(result
);
112 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
114 pr_debug("\nCHECKSUM is 0x%x\n", result
);
120 /* computes the checksum of a memory block at buff, length len,
121 and adds in "sum" (32-bit) */
122 unsigned int csum_partial(const unsigned char *buff
, int len
, unsigned int sum
)
124 unsigned long long result
= do_csum(buff
, len
);
126 /* add in old sum, and carry.. */
128 /* 32+c bits -> 32 bits */
129 result
= (result
& 0xffffffff) + (result
>> 32);
131 pr_debug("csum_partial, buff %p len %d sum 0x%x result=0x%016Lx\n",
132 buff
, len
, sum
, result
);
137 /* Copy while checksumming, otherwise like csum_partial. */
139 csum_partial_copy(const unsigned char *src
, unsigned char *dst
, int len
, unsigned int sum
)
141 sum
= csum_partial(src
, len
, sum
);
142 memcpy(dst
, src
, len
);
147 /* Copy from userspace and compute checksum. If we catch an exception
148 then zero the rest of the buffer. */
150 csum_partial_copy_from_user(const unsigned char *src
, unsigned char *dst
, int len
,
151 unsigned int sum
, int *err_ptr
)
156 ("csum_partial_copy_from_user src %p, dest %p, len %d, sum %08x, err_ptr %p\n",
157 src
, dst
, len
, sum
, err_ptr
);
158 missing
= copy_from_user(dst
, src
, len
);
159 pr_debug(" access_ok %d\n", __access_ok((unsigned long) src
, len
));
160 pr_debug(" missing %d\n", missing
);
162 memset(dst
+ len
- missing
, 0, missing
);
166 return csum_partial(dst
, len
, sum
);
169 /* Copy to userspace and compute checksum. */
171 csum_partial_copy_to_user(const unsigned char *src
, unsigned char *dst
, int len
,
172 unsigned int sum
, int *err_ptr
)
174 sum
= csum_partial(src
, len
, sum
);
176 if (copy_to_user(dst
, src
, len
))
183 * This is a version of ip_compute_csum() optimized for IP headers,
184 * which always checksum on 4 octet boundaries.
186 unsigned short ip_fast_csum(unsigned char *iph
, unsigned int ihl
)
188 pr_debug("ip_fast_csum %p,%d\n", iph
, ihl
);
190 return ~do_csum(iph
, ihl
* 4);
193 unsigned int csum_tcpudp_nofold(unsigned long saddr
,
196 unsigned short proto
, unsigned int sum
)
198 unsigned long long result
;
200 pr_debug("ntohs(0x%x)=0x%x\n", 0xdead, ntohs(0xdead));
201 pr_debug("htons(0x%x)=0x%x\n", 0xdead, htons(0xdead));
203 result
= ((unsigned long long) saddr
+
204 (unsigned long long) daddr
+
205 (unsigned long long) sum
+
206 ((unsigned long long) ntohs(len
) << 16) +
207 ((unsigned long long) proto
<< 8));
209 /* Fold down to 32-bits so we don't loose in the typedef-less
212 result
= (result
& 0xffffffff) + (result
>> 32);
214 result
= (result
& 0xffffffff) + (result
>> 32);
216 pr_debug("%s saddr %x daddr %x len %x proto %x sum %x result %08Lx\n",
217 __FUNCTION__
, saddr
, daddr
, len
, proto
, sum
, result
);
224 csum_partial_copy_nocheck(const unsigned char *src
, unsigned char *dst
, int len
, unsigned int sum
)
227 pr_debug("csum_partial_copy_nocheck src %p dst %p len %d\n", src
, dst
,
230 return csum_partial_copy(src
, dst
, len
, sum
);