2 * arch/sh/lib64/c-checksum.c
4 * This file contains network checksum routines that are better done
5 * in an architecture-specific manner due to speed..
7 #include <linux/string.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <asm/byteorder.h>
12 #include <asm/uaccess.h>
14 static inline unsigned short from64to16(unsigned long long x)
16 /* add up 32-bit words for 33 bits */
17 x = (x & 0xffffffff) + (x >> 32);
18 /* add up 16-bit and 17-bit words for 17+c bits */
19 x = (x & 0xffff) + (x >> 16);
20 /* add up 16-bit and 2-bit for 16+c bit */
21 x = (x & 0xffff) + (x >> 16);
23 x = (x & 0xffff) + (x >> 16);
27 static inline unsigned short foldto16(unsigned long x)
29 /* add up 16-bit for 17 bits */
30 x = (x & 0xffff) + (x >> 16);
32 x = (x & 0xffff) + (x >> 16);
36 static inline unsigned short myfoldto16(unsigned long long x)
38 /* Fold down to 32-bits so we don't lose in the typedef-less
41 x = (x & 0xffffffff) + (x >> 32);
43 x = (x & 0xffffffff) + (x >> 32);
45 /* add up 16-bit for 17 bits */
46 x = (x & 0xffff) + (x >> 16);
48 x = (x & 0xffff) + (x >> 16);
52 #define odd(x) ((x)&1)
53 #define U16(x) ntohs(x)
55 static unsigned long do_csum(const unsigned char *buff, int len)
58 unsigned long result = 0;
60 pr_debug("do_csum buff %p, len %d (0x%x)\n", buff, len, len);
62 for (i = 0; i < len; i++) {
65 printk("%02X ", buff[i]);
72 odd = 1 & (unsigned long) buff;
78 count = len >> 1; /* nr of 16-bit words.. */
80 if (2 & (unsigned long) buff) {
81 result += *(unsigned short *) buff;
86 count >>= 1; /* nr of 32-bit words.. */
88 unsigned long carry = 0;
90 unsigned long w = *(unsigned long *) buff;
98 result = (result & 0xffff) + (result >> 16);
101 result += *(unsigned short *) buff;
107 result = foldto16(result);
109 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
111 pr_debug("\nCHECKSUM is 0x%lx\n", result);
117 /* computes the checksum of a memory block at buff, length len,
118 and adds in "sum" (32-bit) */
119 __wsum csum_partial(const void *buff, int len, __wsum sum)
121 unsigned long long result = do_csum(buff, len);
123 /* add in old sum, and carry.. */
124 result += (__force u32)sum;
125 /* 32+c bits -> 32 bits */
126 result = (result & 0xffffffff) + (result >> 32);
128 pr_debug("csum_partial, buff %p len %d sum 0x%x result=0x%016Lx\n",
129 buff, len, sum, result);
131 return (__force __wsum)result;
134 /* Copy while checksumming, otherwise like csum_partial. */
136 csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
138 sum = csum_partial(src, len, sum);
139 memcpy(dst, src, len);
144 /* Copy from userspace and compute checksum. If we catch an exception
145 then zero the rest of the buffer. */
147 csum_partial_copy_from_user(const void __user *src, void *dst, int len,
148 __wsum sum, int *err_ptr)
153 ("csum_partial_copy_from_user src %p, dest %p, len %d, sum %08x, err_ptr %p\n",
154 src, dst, len, sum, err_ptr);
155 missing = copy_from_user(dst, src, len);
156 pr_debug(" access_ok %d\n", __access_ok((unsigned long) src, len));
157 pr_debug(" missing %d\n", missing);
159 memset(dst + len - missing, 0, missing);
163 return csum_partial(dst, len, sum);
166 /* Copy to userspace and compute checksum. */
168 csum_partial_copy_to_user(const unsigned char *src, unsigned char *dst, int len,
169 __wsum sum, int *err_ptr)
171 sum = csum_partial(src, len, sum);
173 if (copy_to_user(dst, src, len))
180 * This is a version of ip_compute_csum() optimized for IP headers,
181 * which always checksum on 4 octet boundaries.
183 __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
185 pr_debug("ip_fast_csum %p,%d\n", iph, ihl);
187 return (__force __sum16)~do_csum(iph, ihl * 4);
190 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
192 unsigned short proto, __wsum sum)
194 unsigned long long result;
196 pr_debug("ntohs(0x%x)=0x%x\n", 0xdead, ntohs(0xdead));
197 pr_debug("htons(0x%x)=0x%x\n", 0xdead, htons(0xdead));
199 result = (__force u64) saddr + (__force u64) daddr +
200 (__force u64) sum + ((len + proto) << 8);
202 /* Fold down to 32-bits so we don't lose in the typedef-less
205 result = (result & 0xffffffff) + (result >> 32);
207 result = (result & 0xffffffff) + (result >> 32);
209 pr_debug("%s saddr %x daddr %x len %x proto %x sum %x result %08Lx\n",
210 __func__, saddr, daddr, len, proto, sum, result);
212 return (__wsum)result;
214 EXPORT_SYMBOL(csum_tcpudp_nofold);