1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/string.h>
26
27#include <asm/byteorder.h>
28#include <net/checksum.h>
29#include <linux/uaccess.h>
30#include <asm/intrinsics.h>
31
32
33
34#define SIGN(x, y) ((0x8000ULL*x)<<y)
35#define CARRY(x, y) ((0x0002ULL*x)<<y)
36#define SELECT(x, y) ((0x0001ULL*x)<<y)
37
38#define VR_NEGATE(a, b, c, d) (SIGN(a, 48) + SIGN(b, 32) + SIGN(c, 16) \
39 + SIGN(d, 0))
40#define VR_CARRY(a, b, c, d) (CARRY(a, 48) + CARRY(b, 32) + CARRY(c, 16) \
41 + CARRY(d, 0))
42#define VR_SELECT(a, b, c, d) (SELECT(a, 48) + SELECT(b, 32) + SELECT(c, 16) \
43 + SELECT(d, 0))
44
45
46
47static inline unsigned short from64to16(u64 x)
48{
49 u64 sum;
50
51 sum = HEXAGON_P_vrmpyh_PP(x^VR_NEGATE(1, 1, 1, 1),
52 VR_SELECT(1, 1, 1, 1));
53 sum += VR_CARRY(0, 0, 1, 0);
54 sum = HEXAGON_P_vrmpyh_PP(sum, VR_SELECT(0, 0, 1, 1));
55
56 return 0xFFFF & sum;
57}
58
59
60
61
62
63__sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
64 __u32 len, __u8 proto, __wsum sum)
65{
66 return (__force __sum16)~from64to16(
67 (__force u64)saddr + (__force u64)daddr +
68 (__force u64)sum + ((len + proto) << 8));
69}
70
71__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
72 __u32 len, __u8 proto, __wsum sum)
73{
74 u64 result;
75
76 result = (__force u64)saddr + (__force u64)daddr +
77 (__force u64)sum + ((len + proto) << 8);
78
79
80
81
82 result = (result & 0xffffffffUL) + (result >> 32);
83
84 result = (result & 0xffffffffUL) + (result >> 32);
85 return (__force __wsum)result;
86}
87EXPORT_SYMBOL(csum_tcpudp_nofold);
88
89
90
91
92
93
94
95
96
97
98unsigned int do_csum(const void *voidptr, int len)
99{
100 u64 sum0, sum1, x0, x1, *ptr8_o, *ptr8_e, *ptr8;
101 int i, start, mid, end, mask;
102 const char *ptr = voidptr;
103 unsigned short *ptr2;
104 unsigned int *ptr4;
105
106 if (len <= 0)
107 return 0;
108
109 start = 0xF & (16-(((int) ptr) & 0xF)) ;
110 mask = 0x7fffffffUL >> HEXAGON_R_cl0_R(len);
111 start = start & mask ;
112
113 mid = len - start;
114 end = mid & 0xF;
115 mid = mid>>4;
116 sum0 = mid << 18;
117 sum1 = 0;
118
119 if (start & 1)
120 sum0 += (u64) (ptr[0] << 8);
121 ptr2 = (unsigned short *) &ptr[start & 1];
122 if (start & 2)
123 sum1 += (u64) ptr2[0];
124 ptr4 = (unsigned int *) &ptr[start & 3];
125 if (start & 4) {
126 sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
127 VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
128 VR_SELECT(0, 0, 1, 1));
129 sum0 += VR_SELECT(0, 0, 1, 0);
130 }
131 ptr8 = (u64 *) &ptr[start & 7];
132 if (start & 8) {
133 sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
134 VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
135 VR_SELECT(1, 1, 1, 1));
136 sum1 += VR_CARRY(0, 0, 1, 0);
137 }
138 ptr8_o = (u64 *) (ptr + start);
139 ptr8_e = (u64 *) (ptr + start + 8);
140
141 if (mid) {
142 x0 = *ptr8_e; ptr8_e += 2;
143 x1 = *ptr8_o; ptr8_o += 2;
144 if (mid > 1)
145 for (i = 0; i < mid-1; i++) {
146 sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
147 x0^VR_NEGATE(1, 1, 1, 1),
148 VR_SELECT(1, 1, 1, 1));
149 sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
150 x1^VR_NEGATE(1, 1, 1, 1),
151 VR_SELECT(1, 1, 1, 1));
152 x0 = *ptr8_e; ptr8_e += 2;
153 x1 = *ptr8_o; ptr8_o += 2;
154 }
155 sum0 = HEXAGON_P_vrmpyhacc_PP(sum0, x0^VR_NEGATE(1, 1, 1, 1),
156 VR_SELECT(1, 1, 1, 1));
157 sum1 = HEXAGON_P_vrmpyhacc_PP(sum1, x1^VR_NEGATE(1, 1, 1, 1),
158 VR_SELECT(1, 1, 1, 1));
159 }
160
161 ptr4 = (unsigned int *) &ptr[start + (mid * 16) + (end & 8)];
162 if (end & 4) {
163 sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
164 VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
165 VR_SELECT(0, 0, 1, 1));
166 sum1 += VR_SELECT(0, 0, 1, 0);
167 }
168 ptr2 = (unsigned short *) &ptr[start + (mid * 16) + (end & 12)];
169 if (end & 2)
170 sum0 += (u64) ptr2[0];
171
172 if (end & 1)
173 sum1 += (u64) ptr[start + (mid * 16) + (end & 14)];
174
175 ptr8 = (u64 *) &ptr[start + (mid * 16)];
176 if (end & 8) {
177 sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
178 VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
179 VR_SELECT(1, 1, 1, 1));
180 sum0 += VR_CARRY(0, 0, 1, 0);
181 }
182 sum0 = HEXAGON_P_vrmpyh_PP((sum0+sum1)^VR_NEGATE(0, 0, 0, 1),
183 VR_SELECT(0, 0, 1, 1));
184 sum0 += VR_NEGATE(0, 0, 0, 1);
185 sum0 = HEXAGON_P_vrmpyh_PP(sum0, VR_SELECT(0, 0, 1, 1));
186
187 if (start & 1)
188 sum0 = (sum0 << 8) | (0xFF & (sum0 >> 8));
189
190 return 0xFFFF & sum0;
191}
192
193
194
195
196__wsum
197csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
198{
199 memcpy(dst, src, len);
200 return csum_partial(dst, len, sum);
201}
202EXPORT_SYMBOL(csum_partial_copy_nocheck);
203