blob: e4d831a307720c4c70b6390c139a0bed9a401ef1 [file] [log] [blame]
Chris Zankel9a8fd552005-06-23 22:01:26 -07001/*
2 * include/asm-xtensa/checksum.h
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
7 *
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
9 */
10
11#ifndef _XTENSA_CHECKSUM_H
12#define _XTENSA_CHECKSUM_H
13
Chris Zankel9a8fd552005-06-23 22:01:26 -070014#include <linux/in6.h>
Chris Zankel367b8112008-11-06 06:40:46 -080015#include <variant/core.h>
Chris Zankel9a8fd552005-06-23 22:01:26 -070016
17/*
18 * computes the checksum of a memory block at buff, length len,
19 * and adds in "sum" (32-bit)
20 *
21 * returns a 32-bit number suitable for feeding into itself
22 * or csum_tcpudp_magic
23 *
24 * this function must be called with even lengths, except
25 * for the last fragment, which may be odd
26 *
27 * it's best to have buff aligned on a 32-bit boundary
28 */
Al Virob8e4e012006-11-14 21:23:40 -080029asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
Chris Zankel9a8fd552005-06-23 22:01:26 -070030
31/*
32 * the same as csum_partial, but copies from src while it
33 * checksums, and handles user-space pointer exceptions correctly, when needed.
34 *
35 * here even more important to align src and dst on a 32-bit (or even
36 * better 64-bit) boundary
37 */
38
Al Virob8e4e012006-11-14 21:23:40 -080039asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst, int len, __wsum sum,
Chris Zankel9a8fd552005-06-23 22:01:26 -070040 int *src_err_ptr, int *dst_err_ptr);
41
42/*
43 * Note: when you get a NULL pointer exception here this means someone
44 * passed in an incorrect kernel address to one of these functions.
45 *
Jesper Juhlb9e122c2006-06-23 02:05:04 -070046 * If you use these functions directly please don't forget the access_ok().
Chris Zankel9a8fd552005-06-23 22:01:26 -070047 */
Adrian Bunkd99cf712005-09-03 15:57:53 -070048static inline
Al Virob8e4e012006-11-14 21:23:40 -080049__wsum csum_partial_copy_nocheck(const void *src, void *dst,
50 int len, __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -070051{
Al Virob8e4e012006-11-14 21:23:40 -080052 return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
Chris Zankel9a8fd552005-06-23 22:01:26 -070053}
54
Adrian Bunkd99cf712005-09-03 15:57:53 -070055static inline
Al Virob8e4e012006-11-14 21:23:40 -080056__wsum csum_partial_copy_from_user(const void __user *src, void *dst,
57 int len, __wsum sum, int *err_ptr)
Chris Zankel9a8fd552005-06-23 22:01:26 -070058{
Al Virob8e4e012006-11-14 21:23:40 -080059 return csum_partial_copy_generic((__force const void *)src, dst,
60 len, sum, err_ptr, NULL);
Chris Zankel9a8fd552005-06-23 22:01:26 -070061}
62
63/*
Chris Zankel9a8fd552005-06-23 22:01:26 -070064 * Fold a partial checksum
65 */
66
Al Virob8e4e012006-11-14 21:23:40 -080067static __inline__ __sum16 csum_fold(__wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -070068{
69 unsigned int __dummy;
70 __asm__("extui %1, %0, 16, 16\n\t"
71 "extui %0 ,%0, 0, 16\n\t"
72 "add %0, %0, %1\n\t"
73 "slli %1, %0, 16\n\t"
74 "add %0, %0, %1\n\t"
75 "extui %0, %0, 16, 16\n\t"
76 "neg %0, %0\n\t"
77 "addi %0, %0, -1\n\t"
78 "extui %0, %0, 0, 16\n\t"
79 : "=r" (sum), "=&r" (__dummy)
80 : "0" (sum));
Al Virob8e4e012006-11-14 21:23:40 -080081 return (__force __sum16)sum;
Chris Zankel9a8fd552005-06-23 22:01:26 -070082}
83
84/*
85 * This is a version of ip_compute_csum() optimized for IP headers,
86 * which always checksum on 4 octet boundaries.
87 */
Al Virob8e4e012006-11-14 21:23:40 -080088static __inline__ __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Chris Zankel9a8fd552005-06-23 22:01:26 -070089{
90 unsigned int sum, tmp, endaddr;
91
92 __asm__ __volatile__(
93 "sub %0, %0, %0\n\t"
94#if XCHAL_HAVE_LOOPS
95 "loopgtz %2, 2f\n\t"
96#else
97 "beqz %2, 2f\n\t"
98 "slli %4, %2, 2\n\t"
99 "add %4, %4, %1\n\t"
100 "0:\t"
101#endif
102 "l32i %3, %1, 0\n\t"
103 "add %0, %0, %3\n\t"
104 "bgeu %0, %3, 1f\n\t"
105 "addi %0, %0, 1\n\t"
106 "1:\t"
107 "addi %1, %1, 4\n\t"
108#if !XCHAL_HAVE_LOOPS
109 "blt %1, %4, 0b\n\t"
110#endif
111 "2:\t"
112 /* Since the input registers which are loaded with iph and ihl
113 are modified, we must also specify them as outputs, or gcc
114 will assume they contain their original values. */
115 : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp), "=&r" (endaddr)
Chris Zankel7dbe5c52009-04-16 00:28:09 -0700116 : "1" (iph), "2" (ihl)
117 : "memory");
Chris Zankel9a8fd552005-06-23 22:01:26 -0700118
119 return csum_fold(sum);
120}
121
Al Virob8e4e012006-11-14 21:23:40 -0800122static __inline__ __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Chris Zankel9a8fd552005-06-23 22:01:26 -0700123 unsigned short len,
124 unsigned short proto,
Al Virob8e4e012006-11-14 21:23:40 -0800125 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700126{
127
128#ifdef __XTENSA_EL__
Al Virob8e4e012006-11-14 21:23:40 -0800129 unsigned long len_proto = (len + proto) << 8;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700130#elif defined(__XTENSA_EB__)
Al Virob8e4e012006-11-14 21:23:40 -0800131 unsigned long len_proto = len + proto;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700132#else
133# error processor byte order undefined!
134#endif
135 __asm__("add %0, %0, %1\n\t"
136 "bgeu %0, %1, 1f\n\t"
137 "addi %0, %0, 1\n\t"
138 "1:\t"
139 "add %0, %0, %2\n\t"
140 "bgeu %0, %2, 1f\n\t"
141 "addi %0, %0, 1\n\t"
142 "1:\t"
143 "add %0, %0, %3\n\t"
144 "bgeu %0, %3, 1f\n\t"
145 "addi %0, %0, 1\n\t"
146 "1:\t"
147 : "=r" (sum), "=r" (len_proto)
148 : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
149 return sum;
150}
151
152/*
153 * computes the checksum of the TCP/UDP pseudo-header
154 * returns a 16-bit checksum, already complemented
155 */
Al Virob8e4e012006-11-14 21:23:40 -0800156static __inline__ __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Chris Zankel9a8fd552005-06-23 22:01:26 -0700157 unsigned short len,
158 unsigned short proto,
Al Virob8e4e012006-11-14 21:23:40 -0800159 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700160{
161 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
162}
163
164/*
165 * this routine is used for miscellaneous IP-like checksums, mainly
166 * in icmp.c
167 */
168
Al Virob8e4e012006-11-14 21:23:40 -0800169static __inline__ __sum16 ip_compute_csum(const void *buff, int len)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700170{
171 return csum_fold (csum_partial(buff, len, 0));
172}
173
174#define _HAVE_ARCH_IPV6_CSUM
Al Virob8e4e012006-11-14 21:23:40 -0800175static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
176 const struct in6_addr *daddr,
177 __u32 len, unsigned short proto,
178 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700179{
180 unsigned int __dummy;
181 __asm__("l32i %1, %2, 0\n\t"
182 "add %0, %0, %1\n\t"
183 "bgeu %0, %1, 1f\n\t"
184 "addi %0, %0, 1\n\t"
185 "1:\t"
186 "l32i %1, %2, 4\n\t"
187 "add %0, %0, %1\n\t"
188 "bgeu %0, %1, 1f\n\t"
189 "addi %0, %0, 1\n\t"
190 "1:\t"
191 "l32i %1, %2, 8\n\t"
192 "add %0, %0, %1\n\t"
193 "bgeu %0, %1, 1f\n\t"
194 "addi %0, %0, 1\n\t"
195 "1:\t"
196 "l32i %1, %2, 12\n\t"
197 "add %0, %0, %1\n\t"
198 "bgeu %0, %1, 1f\n\t"
199 "addi %0, %0, 1\n\t"
200 "1:\t"
201 "l32i %1, %3, 0\n\t"
202 "add %0, %0, %1\n\t"
203 "bgeu %0, %1, 1f\n\t"
204 "addi %0, %0, 1\n\t"
205 "1:\t"
206 "l32i %1, %3, 4\n\t"
207 "add %0, %0, %1\n\t"
208 "bgeu %0, %1, 1f\n\t"
209 "addi %0, %0, 1\n\t"
210 "1:\t"
211 "l32i %1, %3, 8\n\t"
212 "add %0, %0, %1\n\t"
213 "bgeu %0, %1, 1f\n\t"
214 "addi %0, %0, 1\n\t"
215 "1:\t"
216 "l32i %1, %3, 12\n\t"
217 "add %0, %0, %1\n\t"
218 "bgeu %0, %1, 1f\n\t"
219 "addi %0, %0, 1\n\t"
220 "1:\t"
221 "add %0, %0, %4\n\t"
222 "bgeu %0, %4, 1f\n\t"
223 "addi %0, %0, 1\n\t"
224 "1:\t"
225 "add %0, %0, %5\n\t"
226 "bgeu %0, %5, 1f\n\t"
227 "addi %0, %0, 1\n\t"
228 "1:\t"
229 : "=r" (sum), "=&r" (__dummy)
230 : "r" (saddr), "r" (daddr),
Chris Zankel7dbe5c52009-04-16 00:28:09 -0700231 "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
232 : "memory");
Chris Zankel9a8fd552005-06-23 22:01:26 -0700233
234 return csum_fold(sum);
235}
236
237/*
238 * Copy and checksum to user
239 */
240#define HAVE_CSUM_COPY_USER
Al Virob8e4e012006-11-14 21:23:40 -0800241static __inline__ __wsum csum_and_copy_to_user(const void *src, void __user *dst,
242 int len, __wsum sum, int *err_ptr)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700243{
244 if (access_ok(VERIFY_WRITE, dst, len))
245 return csum_partial_copy_generic(src, dst, len, sum, NULL, err_ptr);
246
247 if (len)
248 *err_ptr = -EFAULT;
249
Al Virob8e4e012006-11-14 21:23:40 -0800250 return (__force __wsum)-1; /* invalid checksum */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700251}
252#endif