blob: aed7ad68ca46e9a9a5f80ddef999ec8f003512d0 [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
Chris Zankelc4c45942012-11-28 16:53:51 -080039asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,
40 int len, __wsum sum,
41 int *src_err_ptr, int *dst_err_ptr);
Chris Zankel9a8fd552005-06-23 22:01:26 -070042
43/*
44 * Note: when you get a NULL pointer exception here this means someone
45 * passed in an incorrect kernel address to one of these functions.
46 *
Jesper Juhlb9e122c2006-06-23 02:05:04 -070047 * If you use these functions directly please don't forget the access_ok().
Chris Zankel9a8fd552005-06-23 22:01:26 -070048 */
Adrian Bunkd99cf712005-09-03 15:57:53 -070049static inline
Al Virob8e4e012006-11-14 21:23:40 -080050__wsum csum_partial_copy_nocheck(const void *src, void *dst,
51 int len, __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -070052{
Al Virob8e4e012006-11-14 21:23:40 -080053 return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
Chris Zankel9a8fd552005-06-23 22:01:26 -070054}
55
Adrian Bunkd99cf712005-09-03 15:57:53 -070056static inline
Al Virob8e4e012006-11-14 21:23:40 -080057__wsum csum_partial_copy_from_user(const void __user *src, void *dst,
Chris Zankelc4c45942012-11-28 16:53:51 -080058 int len, __wsum sum, int *err_ptr)
Chris Zankel9a8fd552005-06-23 22:01:26 -070059{
Al Virob8e4e012006-11-14 21:23:40 -080060 return csum_partial_copy_generic((__force const void *)src, dst,
61 len, sum, err_ptr, NULL);
Chris Zankel9a8fd552005-06-23 22:01:26 -070062}
63
64/*
Chris Zankel9a8fd552005-06-23 22:01:26 -070065 * Fold a partial checksum
66 */
67
Al Virob8e4e012006-11-14 21:23:40 -080068static __inline__ __sum16 csum_fold(__wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -070069{
70 unsigned int __dummy;
71 __asm__("extui %1, %0, 16, 16\n\t"
72 "extui %0 ,%0, 0, 16\n\t"
73 "add %0, %0, %1\n\t"
74 "slli %1, %0, 16\n\t"
75 "add %0, %0, %1\n\t"
76 "extui %0, %0, 16, 16\n\t"
77 "neg %0, %0\n\t"
78 "addi %0, %0, -1\n\t"
79 "extui %0, %0, 0, 16\n\t"
80 : "=r" (sum), "=&r" (__dummy)
81 : "0" (sum));
Al Virob8e4e012006-11-14 21:23:40 -080082 return (__force __sum16)sum;
Chris Zankel9a8fd552005-06-23 22:01:26 -070083}
84
85/*
86 * This is a version of ip_compute_csum() optimized for IP headers,
87 * which always checksum on 4 octet boundaries.
88 */
Al Virob8e4e012006-11-14 21:23:40 -080089static __inline__ __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Chris Zankel9a8fd552005-06-23 22:01:26 -070090{
91 unsigned int sum, tmp, endaddr;
92
93 __asm__ __volatile__(
94 "sub %0, %0, %0\n\t"
95#if XCHAL_HAVE_LOOPS
96 "loopgtz %2, 2f\n\t"
97#else
98 "beqz %2, 2f\n\t"
99 "slli %4, %2, 2\n\t"
100 "add %4, %4, %1\n\t"
101 "0:\t"
102#endif
103 "l32i %3, %1, 0\n\t"
104 "add %0, %0, %3\n\t"
105 "bgeu %0, %3, 1f\n\t"
106 "addi %0, %0, 1\n\t"
107 "1:\t"
108 "addi %1, %1, 4\n\t"
109#if !XCHAL_HAVE_LOOPS
110 "blt %1, %4, 0b\n\t"
111#endif
112 "2:\t"
113 /* Since the input registers which are loaded with iph and ihl
114 are modified, we must also specify them as outputs, or gcc
115 will assume they contain their original values. */
Chris Zankelc4c45942012-11-28 16:53:51 -0800116 : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp),
117 "=&r" (endaddr)
Chris Zankel7dbe5c52009-04-16 00:28:09 -0700118 : "1" (iph), "2" (ihl)
119 : "memory");
Chris Zankel9a8fd552005-06-23 22:01:26 -0700120
121 return csum_fold(sum);
122}
123
Al Virob8e4e012006-11-14 21:23:40 -0800124static __inline__ __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Chris Zankel9a8fd552005-06-23 22:01:26 -0700125 unsigned short len,
126 unsigned short proto,
Al Virob8e4e012006-11-14 21:23:40 -0800127 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700128{
129
130#ifdef __XTENSA_EL__
Al Virob8e4e012006-11-14 21:23:40 -0800131 unsigned long len_proto = (len + proto) << 8;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700132#elif defined(__XTENSA_EB__)
Al Virob8e4e012006-11-14 21:23:40 -0800133 unsigned long len_proto = len + proto;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700134#else
135# error processor byte order undefined!
136#endif
137 __asm__("add %0, %0, %1\n\t"
138 "bgeu %0, %1, 1f\n\t"
139 "addi %0, %0, 1\n\t"
140 "1:\t"
141 "add %0, %0, %2\n\t"
142 "bgeu %0, %2, 1f\n\t"
143 "addi %0, %0, 1\n\t"
144 "1:\t"
145 "add %0, %0, %3\n\t"
146 "bgeu %0, %3, 1f\n\t"
147 "addi %0, %0, 1\n\t"
148 "1:\t"
149 : "=r" (sum), "=r" (len_proto)
150 : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
151 return sum;
152}
153
154/*
155 * computes the checksum of the TCP/UDP pseudo-header
156 * returns a 16-bit checksum, already complemented
157 */
Al Virob8e4e012006-11-14 21:23:40 -0800158static __inline__ __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Chris Zankel9a8fd552005-06-23 22:01:26 -0700159 unsigned short len,
160 unsigned short proto,
Al Virob8e4e012006-11-14 21:23:40 -0800161 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700162{
163 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
164}
165
166/*
167 * this routine is used for miscellaneous IP-like checksums, mainly
168 * in icmp.c
169 */
170
Al Virob8e4e012006-11-14 21:23:40 -0800171static __inline__ __sum16 ip_compute_csum(const void *buff, int len)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700172{
Chris Zankelc4c45942012-11-28 16:53:51 -0800173 return csum_fold (csum_partial(buff, len, 0));
Chris Zankel9a8fd552005-06-23 22:01:26 -0700174}
175
176#define _HAVE_ARCH_IPV6_CSUM
Al Virob8e4e012006-11-14 21:23:40 -0800177static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
178 const struct in6_addr *daddr,
179 __u32 len, unsigned short proto,
180 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700181{
182 unsigned int __dummy;
183 __asm__("l32i %1, %2, 0\n\t"
184 "add %0, %0, %1\n\t"
185 "bgeu %0, %1, 1f\n\t"
186 "addi %0, %0, 1\n\t"
187 "1:\t"
188 "l32i %1, %2, 4\n\t"
189 "add %0, %0, %1\n\t"
190 "bgeu %0, %1, 1f\n\t"
191 "addi %0, %0, 1\n\t"
192 "1:\t"
193 "l32i %1, %2, 8\n\t"
194 "add %0, %0, %1\n\t"
195 "bgeu %0, %1, 1f\n\t"
196 "addi %0, %0, 1\n\t"
197 "1:\t"
198 "l32i %1, %2, 12\n\t"
199 "add %0, %0, %1\n\t"
200 "bgeu %0, %1, 1f\n\t"
201 "addi %0, %0, 1\n\t"
202 "1:\t"
203 "l32i %1, %3, 0\n\t"
204 "add %0, %0, %1\n\t"
205 "bgeu %0, %1, 1f\n\t"
206 "addi %0, %0, 1\n\t"
207 "1:\t"
208 "l32i %1, %3, 4\n\t"
209 "add %0, %0, %1\n\t"
210 "bgeu %0, %1, 1f\n\t"
211 "addi %0, %0, 1\n\t"
212 "1:\t"
213 "l32i %1, %3, 8\n\t"
214 "add %0, %0, %1\n\t"
215 "bgeu %0, %1, 1f\n\t"
216 "addi %0, %0, 1\n\t"
217 "1:\t"
218 "l32i %1, %3, 12\n\t"
219 "add %0, %0, %1\n\t"
220 "bgeu %0, %1, 1f\n\t"
221 "addi %0, %0, 1\n\t"
222 "1:\t"
223 "add %0, %0, %4\n\t"
224 "bgeu %0, %4, 1f\n\t"
225 "addi %0, %0, 1\n\t"
226 "1:\t"
227 "add %0, %0, %5\n\t"
228 "bgeu %0, %5, 1f\n\t"
229 "addi %0, %0, 1\n\t"
230 "1:\t"
231 : "=r" (sum), "=&r" (__dummy)
232 : "r" (saddr), "r" (daddr),
Chris Zankel7dbe5c52009-04-16 00:28:09 -0700233 "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
234 : "memory");
Chris Zankel9a8fd552005-06-23 22:01:26 -0700235
236 return csum_fold(sum);
237}
238
239/*
240 * Copy and checksum to user
241 */
242#define HAVE_CSUM_COPY_USER
Chris Zankelc4c45942012-11-28 16:53:51 -0800243static __inline__ __wsum csum_and_copy_to_user(const void *src,
244 void __user *dst, int len,
245 __wsum sum, int *err_ptr)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700246{
247 if (access_ok(VERIFY_WRITE, dst, len))
Chris Zankelc4c45942012-11-28 16:53:51 -0800248 return csum_partial_copy_generic(src,dst,len,sum,NULL,err_ptr);
Chris Zankel9a8fd552005-06-23 22:01:26 -0700249
250 if (len)
251 *err_ptr = -EFAULT;
252
Al Virob8e4e012006-11-14 21:23:40 -0800253 return (__force __wsum)-1; /* invalid checksum */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700254}
255#endif