1 #ifndef _ASM_SCORE_CHECKSUM_H
2 #define _ASM_SCORE_CHECKSUM_H
5 #include <asm/uaccess.h>
8 * computes the checksum of a memory block at buff, length len,
9 * and adds in "sum" (32-bit)
11 * returns a 32-bit number suitable for feeding into itself
12 * or csum_tcpudp_magic
14 * this function must be called with even lengths, except
15 * for the last fragment, which may be odd
17 * it's best to have buff aligned on a 32-bit boundary
19 unsigned int csum_partial(const void *buff
, int len
, __wsum sum
);
20 unsigned int csum_partial_copy_from_user(const char *src
, char *dst
, int len
,
21 unsigned int sum
, int *csum_err
);
22 unsigned int csum_partial_copy(const char *src
, char *dst
,
23 int len
, unsigned int sum
);
26 * this is a new version of the above that records errors it finds in *errp,
27 * but continues and zeros the rest of the buffer.
31 * Copy and checksum to user
33 #define HAVE_CSUM_COPY_USER
35 __wsum
csum_and_copy_to_user(const void *src
, void __user
*dst
, int len
,
36 __wsum sum
, int *err_ptr
)
38 sum
= csum_partial(src
, len
, sum
);
39 if (copy_to_user(dst
, src
, len
)) {
41 return (__force __wsum
) -1; /* invalid checksum */
47 #define csum_partial_copy_nocheck csum_partial_copy
49 * Fold a partial checksum without adding pseudo headers
52 static inline __sum16
csum_fold(__wsum sum
)
54 /* the while loop is unnecessary really, it's always enough with two
62 "srli\t%0, %0, 16\n\t"
65 "1:ldi\tr30, 0xffff\n\t"
66 "xor\t%0, %0, r30\n\t"
67 "slli\t%0, %0, 16\n\t"
68 "srli\t%0, %0, 16\n\t"
77 * This is a version of ip_compute_csum() optimized for IP headers,
78 * which always checksum on 4 octet boundaries.
80 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
83 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
92 "subri\t%2, %2, 4\n\t"
102 "add\t%0, %0, %3\n\t"
104 "lw\t%3, [%1, 12]\n\t"
107 "1:add\t%0, %0, %3\n\t"
112 "1:\tlw\t%3, [%1, 16]\n\t"
114 "add\t%0, %0, %3\n\t"
118 "2:cmp.c\t%2, %1\n\t"
123 : "=&r" (sum
), "=&r" (iph
), "=&r" (ihl
), "=&r" (dummy
)
124 : "1" (iph
), "2" (ihl
));
126 return csum_fold(sum
);
130 csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
, unsigned short len
,
131 unsigned short proto
, __wsum sum
)
133 unsigned long tmp
= (ntohs(len
) << 16) + proto
* 256;
134 __asm__
__volatile__(
136 "add\t%0, %0, %2\n\t"
141 "add\t%0, %0, %3\n\t"
146 "add\t%0, %0, %4\n\t"
153 : "0" (daddr
), "r"(saddr
),
160 * computes the checksum of the TCP/UDP pseudo-header
161 * returns a 16-bit checksum, already complemented
163 static inline __sum16
164 csum_tcpudp_magic(__be32 saddr
, __be32 daddr
, unsigned short len
,
165 unsigned short proto
, __wsum sum
)
167 return csum_fold(csum_tcpudp_nofold(saddr
, daddr
, len
, proto
, sum
));
171 * this routine is used for miscellaneous IP-like checksums, mainly
175 static inline unsigned short ip_compute_csum(const void *buff
, int len
)
177 return csum_fold(csum_partial(buff
, len
, 0));
180 #define _HAVE_ARCH_IPV6_CSUM
181 static inline __sum16
csum_ipv6_magic(const struct in6_addr
*saddr
,
182 const struct in6_addr
*daddr
,
183 __u32 len
, unsigned short proto
,
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 */