Sparseify MIPS.
[linux-2.6/verdex.git] / include / asm-mips / checksum.h
blob436d26cd6f6f134b17fbad5a7d15f05d050d2448
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, 96, 97, 98, 99, 2001 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 <linux/config.h>
15 #include <linux/in6.h>
17 #include <asm/uaccess.h>
20 * computes the checksum of a memory block at buff, length len,
21 * and adds in "sum" (32-bit)
23 * returns a 32-bit number suitable for feeding into itself
24 * or csum_tcpudp_magic
26 * this function must be called with even lengths, except
27 * for the last fragment, which may be odd
29 * it's best to have buff aligned on a 32-bit boundary
31 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
34 * this is a new version of the above that records errors it finds in *errp,
35 * but continues and zeros the rest of the buffer.
37 unsigned int csum_partial_copy_from_user(const unsigned char __user *src,
38 unsigned char *dst, int len,
39 unsigned int sum, int *errp);
42 * Copy and checksum to user
44 #define HAVE_CSUM_COPY_USER
45 static inline unsigned int csum_and_copy_to_user (const unsigned char *src,
46 unsigned char __user *dst,
47 int len, int sum,
48 int *err_ptr)
50 might_sleep();
51 sum = csum_partial(src, len, sum);
53 if (copy_to_user(dst, src, len)) {
54 *err_ptr = -EFAULT;
55 return -1;
58 return sum;
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 unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
66 int len, unsigned int sum);
69 * Fold a partial checksum without adding pseudo headers
71 static inline unsigned short int csum_fold(unsigned int sum)
73 __asm__(
74 ".set\tnoat\t\t\t# csum_fold\n\t"
75 "sll\t$1,%0,16\n\t"
76 "addu\t%0,$1\n\t"
77 "sltu\t$1,%0,$1\n\t"
78 "srl\t%0,%0,16\n\t"
79 "addu\t%0,$1\n\t"
80 "xori\t%0,0xffff\n\t"
81 ".set\tat"
82 : "=r" (sum)
83 : "0" (sum));
85 return sum;
89 * This is a version of ip_compute_csum() optimized for IP headers,
90 * which always checksum on 4 octet boundaries.
92 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
93 * Arnt Gulbrandsen.
95 static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
97 unsigned int *word = (unsigned int *) iph;
98 unsigned int *stop = word + ihl;
99 unsigned int csum;
100 int carry;
102 csum = word[0];
103 csum += word[1];
104 carry = (csum < word[1]);
105 csum += carry;
107 csum += word[2];
108 carry = (csum < word[2]);
109 csum += carry;
111 csum += word[3];
112 carry = (csum < word[3]);
113 csum += carry;
115 word += 4;
116 do {
117 csum += *word;
118 carry = (csum < *word);
119 csum += carry;
120 word++;
121 } while (word != stop);
123 return csum_fold(csum);
126 static inline unsigned int csum_tcpudp_nofold(unsigned long saddr,
127 unsigned long daddr, unsigned short len, unsigned short proto,
128 unsigned int sum)
130 __asm__(
131 ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t"
132 #ifdef CONFIG_32BIT
133 "addu\t%0, %2\n\t"
134 "sltu\t$1, %0, %2\n\t"
135 "addu\t%0, $1\n\t"
137 "addu\t%0, %3\n\t"
138 "sltu\t$1, %0, %3\n\t"
139 "addu\t%0, $1\n\t"
141 "addu\t%0, %4\n\t"
142 "sltu\t$1, %0, %4\n\t"
143 "addu\t%0, $1\n\t"
144 #endif
145 #ifdef CONFIG_64BIT
146 "daddu\t%0, %2\n\t"
147 "daddu\t%0, %3\n\t"
148 "daddu\t%0, %4\n\t"
149 "dsll32\t$1, %0, 0\n\t"
150 "daddu\t%0, $1\n\t"
151 "dsrl32\t%0, %0, 0\n\t"
152 #endif
153 ".set\tat"
154 : "=r" (sum)
155 : "0" (daddr), "r"(saddr),
156 #ifdef __MIPSEL__
157 "r" (((unsigned long)htons(len)<<16) + proto*256),
158 #else
159 "r" (((unsigned long)(proto)<<16) + len),
160 #endif
161 "r" (sum));
163 return sum;
167 * computes the checksum of the TCP/UDP pseudo-header
168 * returns a 16-bit checksum, already complemented
170 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
171 unsigned long daddr,
172 unsigned short len,
173 unsigned short proto,
174 unsigned int sum)
176 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
180 * this routine is used for miscellaneous IP-like checksums, mainly
181 * in icmp.c
183 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
185 return csum_fold(csum_partial(buff, len, 0));
188 #define _HAVE_ARCH_IPV6_CSUM
189 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
190 struct in6_addr *daddr,
191 __u32 len,
192 unsigned short proto,
193 unsigned int sum)
195 __asm__(
196 ".set\tpush\t\t\t# csum_ipv6_magic\n\t"
197 ".set\tnoreorder\n\t"
198 ".set\tnoat\n\t"
199 "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t"
200 "sltu\t$1, %0, %5\n\t"
201 "addu\t%0, $1\n\t"
203 "addu\t%0, %6\t\t\t# csum\n\t"
204 "sltu\t$1, %0, %6\n\t"
205 "lw\t%1, 0(%2)\t\t\t# four words source address\n\t"
206 "addu\t%0, $1\n\t"
207 "addu\t%0, %1\n\t"
208 "sltu\t$1, %0, %1\n\t"
210 "lw\t%1, 4(%2)\n\t"
211 "addu\t%0, $1\n\t"
212 "addu\t%0, %1\n\t"
213 "sltu\t$1, %0, %1\n\t"
215 "lw\t%1, 8(%2)\n\t"
216 "addu\t%0, $1\n\t"
217 "addu\t%0, %1\n\t"
218 "sltu\t$1, %0, %1\n\t"
220 "lw\t%1, 12(%2)\n\t"
221 "addu\t%0, $1\n\t"
222 "addu\t%0, %1\n\t"
223 "sltu\t$1, %0, %1\n\t"
225 "lw\t%1, 0(%3)\n\t"
226 "addu\t%0, $1\n\t"
227 "addu\t%0, %1\n\t"
228 "sltu\t$1, %0, %1\n\t"
230 "lw\t%1, 4(%3)\n\t"
231 "addu\t%0, $1\n\t"
232 "addu\t%0, %1\n\t"
233 "sltu\t$1, %0, %1\n\t"
235 "lw\t%1, 8(%3)\n\t"
236 "addu\t%0, $1\n\t"
237 "addu\t%0, %1\n\t"
238 "sltu\t$1, %0, %1\n\t"
240 "lw\t%1, 12(%3)\n\t"
241 "addu\t%0, $1\n\t"
242 "addu\t%0, %1\n\t"
243 "sltu\t$1, %0, %1\n\t"
245 "addu\t%0, $1\t\t\t# Add final carry\n\t"
246 ".set\tpop"
247 : "=r" (sum), "=r" (proto)
248 : "r" (saddr), "r" (daddr),
249 "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
251 return csum_fold(sum);
254 #endif /* _ASM_CHECKSUM_H */