1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_SCORE_CHECKSUM_H
3 #define _ASM_SCORE_CHECKSUM_H
6 #include <linux/uaccess.h>
9 * computes the checksum of a memory block at buff, length len,
10 * and adds in "sum" (32-bit)
12 * returns a 32-bit number suitable for feeding into itself
13 * or csum_tcpudp_magic
15 * this function must be called with even lengths, except
16 * for the last fragment, which may be odd
18 * it's best to have buff aligned on a 32-bit boundary
20 unsigned int csum_partial(const void *buff
, int len
, __wsum sum
);
21 unsigned int csum_partial_copy_from_user(const char *src
, char *dst
, int len
,
22 unsigned int sum
, int *csum_err
);
23 unsigned int csum_partial_copy(const char *src
, char *dst
,
24 int len
, unsigned int sum
);
27 * this is a new version of the above that records errors it finds in *errp,
28 * but continues and zeros the rest of the buffer.
32 * Copy and checksum to user
34 #define HAVE_CSUM_COPY_USER
36 __wsum
csum_and_copy_to_user(const void *src
, void __user
*dst
, int len
,
37 __wsum sum
, int *err_ptr
)
39 sum
= csum_partial(src
, len
, sum
);
40 if (copy_to_user(dst
, src
, len
)) {
42 return (__force __wsum
) -1; /* invalid checksum */
48 #define csum_partial_copy_nocheck csum_partial_copy
50 * Fold a partial checksum without adding pseudo headers
53 static inline __sum16
csum_fold(__wsum sum
)
55 /* the while loop is unnecessary really, it's always enough with two
63 "srli\t%0, %0, 16\n\t"
66 "1:ldi\tr30, 0xffff\n\t"
67 "xor\t%0, %0, r30\n\t"
68 "slli\t%0, %0, 16\n\t"
69 "srli\t%0, %0, 16\n\t"
78 * This is a version of ip_compute_csum() optimized for IP headers,
79 * which always checksum on 4 octet boundaries.
81 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
84 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
93 "subri\t%2, %2, 4\n\t"
103 "add\t%0, %0, %3\n\t"
105 "lw\t%3, [%1, 12]\n\t"
108 "1:add\t%0, %0, %3\n\t"
113 "1:\tlw\t%3, [%1, 16]\n\t"
115 "add\t%0, %0, %3\n\t"
119 "2:cmp.c\t%2, %1\n\t"
124 : "=&r" (sum
), "=&r" (iph
), "=&r" (ihl
), "=&r" (dummy
)
125 : "1" (iph
), "2" (ihl
));
127 return csum_fold(sum
);
131 csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
, __u32 len
,
132 __u8 proto
, __wsum sum
)
134 unsigned long tmp
= (len
+ proto
) << 8;
135 __asm__
__volatile__(
137 "add\t%0, %0, %2\n\t"
142 "add\t%0, %0, %3\n\t"
147 "add\t%0, %0, %4\n\t"
154 : "0" (daddr
), "r"(saddr
),
161 * computes the checksum of the TCP/UDP pseudo-header
162 * returns a 16-bit checksum, already complemented
164 static inline __sum16
165 csum_tcpudp_magic(__be32 saddr
, __be32 daddr
, __u32 len
,
166 __u8 proto
, __wsum sum
)
168 return csum_fold(csum_tcpudp_nofold(saddr
, daddr
, len
, proto
, sum
));
172 * this routine is used for miscellaneous IP-like checksums, mainly
176 static inline unsigned short ip_compute_csum(const void *buff
, int len
)
178 return csum_fold(csum_partial(buff
, len
, 0));
181 #define _HAVE_ARCH_IPV6_CSUM
182 static inline __sum16
csum_ipv6_magic(const struct in6_addr
*saddr
,
183 const struct in6_addr
*daddr
,
184 __u32 len
, __u8 proto
, __wsum sum
)
186 __asm__
__volatile__(
187 ".set\tvolatile\t\t\t# csum_ipv6_magic\n\t"
188 "add\t%0, %0, %5\t\t\t# proto (long in network byte order)\n\t"
192 "1:add\t%0, %0, %6\t\t\t# csum\n\t"
194 "lw\t%1, [%2, 0]\t\t\t# four words source address\n\t"
197 "1:add\t%0, %0, %1\n\t"
199 "1:lw\t%1, [%2, 4]\n\t"
202 "1:add\t%0, %0, %1\n\t"
207 "1:add\t%0, %0, %1\n\t"
209 "lw\t%1, [%2, 12]\n\t"
212 "1:add\t%0, %0,%1\n\t"
214 "lw\t%1, [%3, 0]\n\t"
217 "1:add\t%0, %0, %1\n\t"
219 "lw\t%1, [%3, 4]\n\t"
222 "1:add\t%0, %0, %1\n\t"
224 "lw\t%1, [%3, 8]\n\t"
227 "1:add\t%0, %0, %1\n\t"
229 "lw\t%1, [%3, 12]\n\t"
232 "1:add\t%0, %0, %1\n\t"
238 : "=r" (sum
), "=r" (proto
)
239 : "r" (saddr
), "r" (daddr
),
240 "0" (htonl(len
)), "1" (htonl(proto
)), "r" (sum
));
242 return csum_fold(sum
);
244 #endif /* _ASM_SCORE_CHECKSUM_H */