Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / parisc / lib / checksum.c
blobba6384da6ade7458c149e1701a5b809532651dbd
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * MIPS specific IP/TCP/UDP checksumming routines
8 * Authors: Ralf Baechle, <ralf@waldorf-gmbh.de>
9 * Lots of code moved from tcp.c and ip.c; see those files
10 * for more names.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 #include <linux/module.h>
18 #include <linux/types.h>
20 #include <net/checksum.h>
21 #include <asm/byteorder.h>
22 #include <asm/string.h>
23 #include <linux/uaccess.h>
25 #define addc(_t,_r) \
26 __asm__ __volatile__ ( \
27 " add %0, %1, %0\n" \
28 " addc %0, %%r0, %0\n" \
29 : "=r"(_t) \
30 : "r"(_r), "0"(_t));
32 static inline unsigned short from32to16(unsigned int x)
34 /* 32 bits --> 16 bits + carry */
35 x = (x & 0xffff) + (x >> 16);
36 /* 16 bits + carry --> 16 bits including carry */
37 x = (x & 0xffff) + (x >> 16);
38 return (unsigned short)x;
41 static inline unsigned int do_csum(const unsigned char * buff, int len)
43 int odd, count;
44 unsigned int result = 0;
46 if (len <= 0)
47 goto out;
48 odd = 1 & (unsigned long) buff;
49 if (odd) {
50 result = be16_to_cpu(*buff);
51 len--;
52 buff++;
54 count = len >> 1; /* nr of 16-bit words.. */
55 if (count) {
56 if (2 & (unsigned long) buff) {
57 result += *(unsigned short *) buff;
58 count--;
59 len -= 2;
60 buff += 2;
62 count >>= 1; /* nr of 32-bit words.. */
63 if (count) {
64 while (count >= 4) {
65 unsigned int r1, r2, r3, r4;
66 r1 = *(unsigned int *)(buff + 0);
67 r2 = *(unsigned int *)(buff + 4);
68 r3 = *(unsigned int *)(buff + 8);
69 r4 = *(unsigned int *)(buff + 12);
70 addc(result, r1);
71 addc(result, r2);
72 addc(result, r3);
73 addc(result, r4);
74 count -= 4;
75 buff += 16;
77 while (count) {
78 unsigned int w = *(unsigned int *) buff;
79 count--;
80 buff += 4;
81 addc(result, w);
83 result = (result & 0xffff) + (result >> 16);
85 if (len & 2) {
86 result += *(unsigned short *) buff;
87 buff += 2;
90 if (len & 1)
91 result += le16_to_cpu(*buff);
92 result = from32to16(result);
93 if (odd)
94 result = swab16(result);
95 out:
96 return result;
100 * computes a partial checksum, e.g. for TCP/UDP fragments
103 * why bother folding?
105 __wsum csum_partial(const void *buff, int len, __wsum sum)
107 unsigned int result = do_csum(buff, len);
108 addc(result, sum);
109 return (__force __wsum)from32to16(result);
112 EXPORT_SYMBOL(csum_partial);
115 * copy while checksumming, otherwise like csum_partial
117 __wsum csum_partial_copy_nocheck(const void *src, void *dst,
118 int len, __wsum sum)
121 * It's 2:30 am and I don't feel like doing it real ...
122 * This is lots slower than the real thing (tm)
124 sum = csum_partial(src, len, sum);
125 memcpy(dst, src, len);
127 return sum;
129 EXPORT_SYMBOL(csum_partial_copy_nocheck);
132 * Copy from userspace and compute checksum. If we catch an exception
133 * then zero the rest of the buffer.
135 __wsum csum_partial_copy_from_user(const void __user *src,
136 void *dst, int len,
137 __wsum sum, int *err_ptr)
139 int missing;
141 missing = copy_from_user(dst, src, len);
142 if (missing) {
143 memset(dst + len - missing, 0, missing);
144 *err_ptr = -EFAULT;
147 return csum_partial(dst, len, sum);
149 EXPORT_SYMBOL(csum_partial_copy_from_user);