blob: 0593de689b565131c9b0cd53081b6d2c245718f3 [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>
Max Filippovb0c438e2013-02-21 13:55:02 +040015#include <asm/uaccess.h>
Chris Zankel367b8112008-11-06 06:40:46 -080016#include <variant/core.h>
Chris Zankel9a8fd552005-06-23 22:01:26 -070017
18/*
19 * computes the checksum of a memory block at buff, length len,
20 * and adds in "sum" (32-bit)
21 *
22 * returns a 32-bit number suitable for feeding into itself
23 * or csum_tcpudp_magic
24 *
25 * this function must be called with even lengths, except
26 * for the last fragment, which may be odd
27 *
28 * it's best to have buff aligned on a 32-bit boundary
29 */
Al Virob8e4e012006-11-14 21:23:40 -080030asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
Chris Zankel9a8fd552005-06-23 22:01:26 -070031
32/*
33 * the same as csum_partial, but copies from src while it
34 * checksums, and handles user-space pointer exceptions correctly, when needed.
35 *
36 * here even more important to align src and dst on a 32-bit (or even
37 * better 64-bit) boundary
38 */
39
Chris Zankelc4c45942012-11-28 16:53:51 -080040asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,
41 int len, __wsum sum,
42 int *src_err_ptr, int *dst_err_ptr);
Chris Zankel9a8fd552005-06-23 22:01:26 -070043
44/*
45 * Note: when you get a NULL pointer exception here this means someone
46 * passed in an incorrect kernel address to one of these functions.
47 *
Jesper Juhlb9e122c2006-06-23 02:05:04 -070048 * If you use these functions directly please don't forget the access_ok().
Chris Zankel9a8fd552005-06-23 22:01:26 -070049 */
Adrian Bunkd99cf712005-09-03 15:57:53 -070050static inline
Al Virob8e4e012006-11-14 21:23:40 -080051__wsum csum_partial_copy_nocheck(const void *src, void *dst,
52 int len, __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -070053{
Al Virob8e4e012006-11-14 21:23:40 -080054 return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
Chris Zankel9a8fd552005-06-23 22:01:26 -070055}
56
Adrian Bunkd99cf712005-09-03 15:57:53 -070057static inline
Al Virob8e4e012006-11-14 21:23:40 -080058__wsum csum_partial_copy_from_user(const void __user *src, void *dst,
Chris Zankelc4c45942012-11-28 16:53:51 -080059 int len, __wsum sum, int *err_ptr)
Chris Zankel9a8fd552005-06-23 22:01:26 -070060{
Al Virob8e4e012006-11-14 21:23:40 -080061 return csum_partial_copy_generic((__force const void *)src, dst,
62 len, sum, err_ptr, NULL);
Chris Zankel9a8fd552005-06-23 22:01:26 -070063}
64
65/*
Chris Zankel9a8fd552005-06-23 22:01:26 -070066 * Fold a partial checksum
67 */
68
Al Virob8e4e012006-11-14 21:23:40 -080069static __inline__ __sum16 csum_fold(__wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -070070{
71 unsigned int __dummy;
72 __asm__("extui %1, %0, 16, 16\n\t"
73 "extui %0 ,%0, 0, 16\n\t"
74 "add %0, %0, %1\n\t"
75 "slli %1, %0, 16\n\t"
76 "add %0, %0, %1\n\t"
77 "extui %0, %0, 16, 16\n\t"
78 "neg %0, %0\n\t"
79 "addi %0, %0, -1\n\t"
80 "extui %0, %0, 0, 16\n\t"
81 : "=r" (sum), "=&r" (__dummy)
82 : "0" (sum));
Al Virob8e4e012006-11-14 21:23:40 -080083 return (__force __sum16)sum;
Chris Zankel9a8fd552005-06-23 22:01:26 -070084}
85
86/*
87 * This is a version of ip_compute_csum() optimized for IP headers,
88 * which always checksum on 4 octet boundaries.
89 */
Al Virob8e4e012006-11-14 21:23:40 -080090static __inline__ __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Chris Zankel9a8fd552005-06-23 22:01:26 -070091{
92 unsigned int sum, tmp, endaddr;
93
94 __asm__ __volatile__(
95 "sub %0, %0, %0\n\t"
96#if XCHAL_HAVE_LOOPS
97 "loopgtz %2, 2f\n\t"
98#else
99 "beqz %2, 2f\n\t"
100 "slli %4, %2, 2\n\t"
101 "add %4, %4, %1\n\t"
102 "0:\t"
103#endif
104 "l32i %3, %1, 0\n\t"
105 "add %0, %0, %3\n\t"
106 "bgeu %0, %3, 1f\n\t"
107 "addi %0, %0, 1\n\t"
108 "1:\t"
109 "addi %1, %1, 4\n\t"
110#if !XCHAL_HAVE_LOOPS
111 "blt %1, %4, 0b\n\t"
112#endif
113 "2:\t"
114 /* Since the input registers which are loaded with iph and ihl
115 are modified, we must also specify them as outputs, or gcc
116 will assume they contain their original values. */
Chris Zankelc4c45942012-11-28 16:53:51 -0800117 : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp),
118 "=&r" (endaddr)
Chris Zankel7dbe5c52009-04-16 00:28:09 -0700119 : "1" (iph), "2" (ihl)
120 : "memory");
Chris Zankel9a8fd552005-06-23 22:01:26 -0700121
122 return csum_fold(sum);
123}
124
Al Virob8e4e012006-11-14 21:23:40 -0800125static __inline__ __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Chris Zankel9a8fd552005-06-23 22:01:26 -0700126 unsigned short len,
127 unsigned short proto,
Al Virob8e4e012006-11-14 21:23:40 -0800128 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700129{
130
131#ifdef __XTENSA_EL__
Al Virob8e4e012006-11-14 21:23:40 -0800132 unsigned long len_proto = (len + proto) << 8;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700133#elif defined(__XTENSA_EB__)
Al Virob8e4e012006-11-14 21:23:40 -0800134 unsigned long len_proto = len + proto;
Chris Zankel9a8fd552005-06-23 22:01:26 -0700135#else
136# error processor byte order undefined!
137#endif
138 __asm__("add %0, %0, %1\n\t"
139 "bgeu %0, %1, 1f\n\t"
140 "addi %0, %0, 1\n\t"
141 "1:\t"
142 "add %0, %0, %2\n\t"
143 "bgeu %0, %2, 1f\n\t"
144 "addi %0, %0, 1\n\t"
145 "1:\t"
146 "add %0, %0, %3\n\t"
147 "bgeu %0, %3, 1f\n\t"
148 "addi %0, %0, 1\n\t"
149 "1:\t"
150 : "=r" (sum), "=r" (len_proto)
151 : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
152 return sum;
153}
154
155/*
156 * computes the checksum of the TCP/UDP pseudo-header
157 * returns a 16-bit checksum, already complemented
158 */
Al Virob8e4e012006-11-14 21:23:40 -0800159static __inline__ __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Chris Zankel9a8fd552005-06-23 22:01:26 -0700160 unsigned short len,
161 unsigned short proto,
Al Virob8e4e012006-11-14 21:23:40 -0800162 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700163{
164 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
165}
166
167/*
168 * this routine is used for miscellaneous IP-like checksums, mainly
169 * in icmp.c
170 */
171
Al Virob8e4e012006-11-14 21:23:40 -0800172static __inline__ __sum16 ip_compute_csum(const void *buff, int len)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700173{
Chris Zankelc4c45942012-11-28 16:53:51 -0800174 return csum_fold (csum_partial(buff, len, 0));
Chris Zankel9a8fd552005-06-23 22:01:26 -0700175}
176
177#define _HAVE_ARCH_IPV6_CSUM
Al Virob8e4e012006-11-14 21:23:40 -0800178static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
179 const struct in6_addr *daddr,
180 __u32 len, unsigned short proto,
181 __wsum sum)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700182{
183 unsigned int __dummy;
184 __asm__("l32i %1, %2, 0\n\t"
185 "add %0, %0, %1\n\t"
186 "bgeu %0, %1, 1f\n\t"
187 "addi %0, %0, 1\n\t"
188 "1:\t"
189 "l32i %1, %2, 4\n\t"
190 "add %0, %0, %1\n\t"
191 "bgeu %0, %1, 1f\n\t"
192 "addi %0, %0, 1\n\t"
193 "1:\t"
194 "l32i %1, %2, 8\n\t"
195 "add %0, %0, %1\n\t"
196 "bgeu %0, %1, 1f\n\t"
197 "addi %0, %0, 1\n\t"
198 "1:\t"
199 "l32i %1, %2, 12\n\t"
200 "add %0, %0, %1\n\t"
201 "bgeu %0, %1, 1f\n\t"
202 "addi %0, %0, 1\n\t"
203 "1:\t"
204 "l32i %1, %3, 0\n\t"
205 "add %0, %0, %1\n\t"
206 "bgeu %0, %1, 1f\n\t"
207 "addi %0, %0, 1\n\t"
208 "1:\t"
209 "l32i %1, %3, 4\n\t"
210 "add %0, %0, %1\n\t"
211 "bgeu %0, %1, 1f\n\t"
212 "addi %0, %0, 1\n\t"
213 "1:\t"
214 "l32i %1, %3, 8\n\t"
215 "add %0, %0, %1\n\t"
216 "bgeu %0, %1, 1f\n\t"
217 "addi %0, %0, 1\n\t"
218 "1:\t"
219 "l32i %1, %3, 12\n\t"
220 "add %0, %0, %1\n\t"
221 "bgeu %0, %1, 1f\n\t"
222 "addi %0, %0, 1\n\t"
223 "1:\t"
224 "add %0, %0, %4\n\t"
225 "bgeu %0, %4, 1f\n\t"
226 "addi %0, %0, 1\n\t"
227 "1:\t"
228 "add %0, %0, %5\n\t"
229 "bgeu %0, %5, 1f\n\t"
230 "addi %0, %0, 1\n\t"
231 "1:\t"
232 : "=r" (sum), "=&r" (__dummy)
233 : "r" (saddr), "r" (daddr),
Chris Zankel7dbe5c52009-04-16 00:28:09 -0700234 "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
235 : "memory");
Chris Zankel9a8fd552005-06-23 22:01:26 -0700236
237 return csum_fold(sum);
238}
239
240/*
241 * Copy and checksum to user
242 */
243#define HAVE_CSUM_COPY_USER
Chris Zankelc4c45942012-11-28 16:53:51 -0800244static __inline__ __wsum csum_and_copy_to_user(const void *src,
245 void __user *dst, int len,
246 __wsum sum, int *err_ptr)
Chris Zankel9a8fd552005-06-23 22:01:26 -0700247{
248 if (access_ok(VERIFY_WRITE, dst, len))
Chris Zankelc4c45942012-11-28 16:53:51 -0800249 return csum_partial_copy_generic(src,dst,len,sum,NULL,err_ptr);
Chris Zankel9a8fd552005-06-23 22:01:26 -0700250
251 if (len)
252 *err_ptr = -EFAULT;
253
Al Virob8e4e012006-11-14 21:23:40 -0800254 return (__force __wsum)-1; /* invalid checksum */
Chris Zankel9a8fd552005-06-23 22:01:26 -0700255}
256#endif