Fix IP22 timer calibration.
[linux-2.6/linux-mips.git] / include / asm-mips / checksum.h
blob756e6a6429c8734611e9f1fa9ed0cb69f895f75e
1 /*
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
4 * for more details.
6 * Copyright (C) 1995, 1996, 1997, 1998, 2001 by Ralf Baechle
7 */
8 #ifndef _ASM_CHECKSUM_H
9 #define _ASM_CHECKSUM_H
11 #include <asm/uaccess.h>
12 #include <linux/in6.h>
15 * computes the checksum of a memory block at buff, length len,
16 * and adds in "sum" (32-bit)
18 * returns a 32-bit number suitable for feeding into itself
19 * or csum_tcpudp_magic
21 * this function must be called with even lengths, except
22 * for the last fragment, which may be odd
24 * it's best to have buff aligned on a 32-bit boundary
26 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
29 * this is a new version of the above that records errors it finds in *errp,
30 * but continues and zeros the rest of the buffer.
32 unsigned int csum_partial_copy_from_user(const char *src, char *dst, int len,
33 unsigned int sum, int *errp);
36 * Copy and checksum to user
38 #define HAVE_CSUM_COPY_USER
39 static inline unsigned int csum_and_copy_to_user (const char *src, char *dst,
40 int len, int sum,
41 int *err_ptr)
43 sum = csum_partial(src, len, sum);
45 if (copy_to_user(dst, src, len)) {
46 *err_ptr = -EFAULT;
47 return -1;
50 return sum;
54 * the same as csum_partial, but copies from user space (but on MIPS
55 * we have just one address space, so this is identical to the above)
57 unsigned int csum_partial_copy_nocheck(const char *src, char *dst, int len,
58 unsigned int sum);
61 * Fold a partial checksum without adding pseudo headers
63 static inline unsigned short int csum_fold(unsigned int sum)
65 __asm__(
66 ".set\tnoat\t\t\t# csum_fold\n\t"
67 "sll\t$1,%0,16\n\t"
68 "addu\t%0,$1\n\t"
69 "sltu\t$1,%0,$1\n\t"
70 "srl\t%0,%0,16\n\t"
71 "addu\t%0,$1\n\t"
72 "xori\t%0,0xffff\n\t"
73 ".set\tat"
74 : "=r" (sum)
75 : "0" (sum));
77 return sum;
81 * This is a version of ip_compute_csum() optimized for IP headers,
82 * which always checksum on 4 octet boundaries.
84 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
85 * Arnt Gulbrandsen.
87 static inline unsigned short ip_fast_csum(unsigned char *iph,
88 unsigned int ihl)
90 unsigned int sum;
91 unsigned long dummy;
94 * This is for 32-bit MIPS processors.
96 __asm__ __volatile__(
97 ".set\tnoreorder\t\t\t# ip_fast_csum\n\t"
98 ".set\tnoat\n\t"
99 "lw\t%0, (%1)\n\t"
100 "subu\t%2, 4\n\t"
101 "#blez\t%2, 2f\n\t"
102 " sll\t%2, 2\n\t"
103 "lw\t%3, 4(%1)\n\t"
104 "addu\t%2, %1\n\t"
105 "addu\t%0, %3\n\t"
106 "sltu\t$1, %0, %3\n\t"
107 "lw\t%3, 8(%1)\n\t"
108 "addu\t%0, $1\n\t"
109 "addu\t%0, %3\n\t"
110 "sltu\t$1, %0, %3\n\t"
111 "lw\t%3, 12(%1)\n\t"
112 "addu\t%0, $1\n\t"
113 "addu\t%0, %3\n\t"
114 "sltu\t$1, %0, %3\n\t"
115 "addu\t%0, $1\n"
117 "1:\tlw\t%3, 16(%1)\n\t"
118 "addiu\t%1, 4\n\t"
119 "addu\t%0, %3\n\t"
120 "sltu\t$1, %0, %3\n\t"
121 "bne\t%2, %1, 1b\n\t"
122 " addu\t%0, $1\n"
124 "2:\t.set\tat\n\t"
125 ".set\treorder"
126 : "=&r" (sum), "=&r" (iph), "=&r" (ihl), "=&r" (dummy)
127 : "1" (iph), "2" (ihl));
129 return csum_fold(sum);
133 * computes the checksum of the TCP/UDP pseudo-header
134 * returns a 16-bit checksum, already complemented
136 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
137 unsigned long daddr,
138 unsigned short len,
139 unsigned short proto,
140 unsigned int sum)
142 __asm__(
143 ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t"
144 "addu\t%0, %2\n\t"
145 "sltu\t$1, %0, %2\n\t"
146 "addu\t%0, $1\n\t"
148 "addu\t%0, %3\n\t"
149 "sltu\t$1, %0, %3\n\t"
150 "addu\t%0, $1\n\t"
152 "addu\t%0, %4\n\t"
153 "sltu\t$1, %0, %4\n\t"
154 "addu\t%0, $1\n\t"
155 ".set\tat"
156 : "=r" (sum)
157 : "0" (daddr), "r"(saddr),
158 #ifdef __MIPSEL__
159 "r" ((ntohs(len)<<16)+proto*256),
160 #else
161 "r" (((proto)<<16)+len),
162 #endif
163 "r" (sum));
165 return sum;
169 * computes the checksum of the TCP/UDP pseudo-header
170 * returns a 16-bit checksum, already complemented
172 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
173 unsigned long daddr,
174 unsigned short len,
175 unsigned short proto,
176 unsigned int sum)
178 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
182 * this routine is used for miscellaneous IP-like checksums, mainly
183 * in icmp.c
185 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
187 return csum_fold(csum_partial(buff, len, 0));
190 #define _HAVE_ARCH_IPV6_CSUM
191 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
192 struct in6_addr *daddr,
193 __u32 len,
194 unsigned short proto,
195 unsigned int sum)
197 __asm__(
198 ".set\tpush\t\t\t# csum_ipv6_magic\n\t"
199 ".set\tnoreorder\n\t"
200 ".set\tnoat\n\t"
201 "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t"
202 "sltu\t$1, %0, %5\n\t"
203 "addu\t%0, $1\n\t"
205 "addu\t%0, %6\t\t\t# csum\n\t"
206 "sltu\t$1, %0, %6\n\t"
207 "lw\t%1, 0(%2)\t\t\t# four words source address\n\t"
208 "addu\t%0, $1\n\t"
209 "addu\t%0, %1\n\t"
210 "sltu\t$1, %0, %1\n\t"
212 "lw\t%1, 4(%2)\n\t"
213 "addu\t%0, $1\n\t"
214 "addu\t%0, %1\n\t"
215 "sltu\t$1, %0, %1\n\t"
217 "lw\t%1, 8(%2)\n\t"
218 "addu\t%0, $1\n\t"
219 "addu\t%0, %1\n\t"
220 "sltu\t$1, %0, %1\n\t"
222 "lw\t%1, 12(%2)\n\t"
223 "addu\t%0, $1\n\t"
224 "addu\t%0, %1\n\t"
225 "sltu\t$1, %0, %1\n\t"
227 "lw\t%1, 0(%3)\n\t"
228 "addu\t%0, $1\n\t"
229 "addu\t%0, %1\n\t"
230 "sltu\t$1, %0, %1\n\t"
232 "lw\t%1, 4(%3)\n\t"
233 "addu\t%0, $1\n\t"
234 "addu\t%0, %1\n\t"
235 "sltu\t$1, %0, %1\n\t"
237 "lw\t%1, 8(%3)\n\t"
238 "addu\t%0, $1\n\t"
239 "addu\t%0, %1\n\t"
240 "sltu\t$1, %0, %1\n\t"
242 "lw\t%1, 12(%3)\n\t"
243 "addu\t%0, $1\n\t"
244 "addu\t%0, %1\n\t"
245 "sltu\t$1, %0, %1\n\t"
247 "addu\t%0, $1\t\t\t# Add final carry\n\t"
248 ".set\tpop"
249 : "=r" (sum), "=r" (proto)
250 : "r" (saddr), "r" (daddr),
251 "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
253 return csum_fold(sum);
256 #endif /* _ASM_CHECKSUM_H */