2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by Ralf Baechle
7 * Copyright (C) 1999 Silicon Graphics, Inc.
8 * Copyright (C) 2001 Thiemo Seufer.
9 * Copyright (C) 2002 Maciej W. Rozycki
11 #ifndef _ASM_CHECKSUM_H
12 #define _ASM_CHECKSUM_H
14 #include <asm/uaccess.h>
17 * computes the checksum of a memory block at buff, length len,
18 * and adds in "sum" (32-bit)
20 * returns a 32-bit number suitable for feeding into itself
21 * or csum_tcpudp_magic
23 * this function must be called with even lengths, except
24 * for the last fragment, which may be odd
26 * it's best to have buff aligned on a 32-bit boundary
28 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
31 * this is a new version of the above that records errors it finds in *errp,
32 * but continues and zeros the rest of the buffer.
34 #define csum_partial_copy_nocheck csum_partial_copy
37 * this is a new version of the above that records errors it finds in *errp,
38 * but continues and zeros the rest of the buffer.
40 unsigned int csum_partial_copy_from_user(const char *src, char *dst, int len,
41 unsigned int sum, int *errp);
44 * Copy and checksum to user
46 #define HAVE_CSUM_COPY_USER
47 static inline unsigned int csum_and_copy_to_user (const char *src, char *dst,
51 sum = csum_partial(src, len, sum);
53 if (copy_to_user(dst, src, len)) {
62 * the same as csum_partial, but copies from user space (but on MIPS
63 * we have just one address space, so this is identical to the above)
65 * this is obsolete and will go away.
67 #define csum_partial_copy_fromuser csum_partial_copy
68 unsigned int csum_partial_copy(const char *src, char *dst, int len,
72 * Fold a partial checksum without adding pseudo headers
74 static inline unsigned short int csum_fold(unsigned int sum)
77 ".set\tnoat\t\t\t# csum_fold\n\t"
92 * This is a version of ip_compute_csum() optimized for IP headers,
93 * which always checksum on 4 octet boundaries.
95 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
98 static inline unsigned short ip_fast_csum(unsigned char *iph,
105 * This is for 32-bit processors ... but works just fine for 64-bit
106 * processors for now ... XXX
108 __asm__ __volatile__(
109 ".set\tnoreorder\t\t\t# ip_fast_csum\n\t"
117 "sltu\t$1, %0, %3\n\t"
121 "sltu\t$1, %0, %3\n\t"
125 "sltu\t$1, %0, %3\n\t"
128 "1:\tlw\t%3, 16(%1)\n\t"
131 "sltu\t$1, %0, %3\n\t"
132 "bne\t%2, %1, 1b\n\t"
137 : "=&r" (sum), "=&r" (iph), "=&r" (ihl), "=&r" (dummy)
138 : "1" (iph), "2" (ihl));
140 return csum_fold(sum);
144 * computes the checksum of the TCP/UDP pseudo-header
145 * returns a 16-bit checksum, already complemented
147 * Cast unsigned short expressions to unsigned long explicitly
148 * to avoid surprises resulting from implicit promotions to
149 * signed int. --macro
151 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
154 unsigned short proto,
158 ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t"
162 "dsll32\t$1, %0, 0\n\t"
164 "dsrl32\t%0, %0, 0\n\t"
167 : "0" (daddr), "r"(saddr),
169 "r" (((unsigned long)ntohs(len)<<16)+proto*256),
171 "r" (((unsigned long)(proto)<<16)+len),
179 * computes the checksum of the TCP/UDP pseudo-header
180 * returns a 16-bit checksum, already complemented
182 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
185 unsigned short proto,
188 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
192 * this routine is used for miscellaneous IP-like checksums, mainly
195 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
197 return csum_fold(csum_partial(buff, len, 0));
200 #define _HAVE_ARCH_IPV6_CSUM
201 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
202 struct in6_addr *daddr,
204 unsigned short proto,
208 ".set\tnoreorder\t\t\t# csum_ipv6_magic\n\t"
210 "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t"
211 "sltu\t$1, %0, %5\n\t"
214 "addu\t%0, %6\t\t\t# csum\n\t"
215 "sltu\t$1, %0, %6\n\t"
216 "lw\t%1, 0(%2)\t\t\t# four words source address\n\t"
219 "sltu\t$1, %0, %1\n\t"
224 "sltu\t$1, %0, %1\n\t"
229 "sltu\t$1, %0, %1\n\t"
234 "sltu\t$1, %0, %1\n\t"
239 "sltu\t$1, %0, %1\n\t"
244 "sltu\t$1, %0, %1\n\t"
249 "sltu\t$1, %0, %1\n\t"
254 "sltu\t$1, %0, %1\n\t"
256 "addu\t%0, $1\t\t\t# Add final carry\n\t"
259 : "=&r" (sum), "=&r" (proto)
260 : "r" (saddr), "r" (daddr),
261 "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
263 return csum_fold(sum);
266 #endif /* _ASM_CHECKSUM_H */