blob: 03114f8d1e18e436a4677514158cfab9e89a7564 [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>
15#include <xtensa/config/core.h>
16
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 */
29asmlinkage unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
30
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
39asmlinkage unsigned int csum_partial_copy_generic( const char *src, char *dst, int len, int sum,
40 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
Chris Zankel9a8fd552005-06-23 22:01:26 -070049unsigned int csum_partial_copy_nocheck ( const char *src, char *dst,
50 int len, int sum)
51{
52 return csum_partial_copy_generic ( src, dst, len, sum, NULL, NULL);
53}
54
Adrian Bunkd99cf712005-09-03 15:57:53 -070055static inline
Chris Zankel9a8fd552005-06-23 22:01:26 -070056unsigned int csum_partial_copy_from_user ( const char *src, char *dst,
57 int len, int sum, int *err_ptr)
58{
59 return csum_partial_copy_generic ( src, dst, len, sum, err_ptr, NULL);
60}
61
62/*
63 * These are the old (and unsafe) way of doing checksums, a warning message will be
64 * printed if they are used and an exeption occurs.
65 *
66 * these functions should go away after some time.
67 */
68
69#define csum_partial_copy_fromuser csum_partial_copy
70unsigned int csum_partial_copy( const char *src, char *dst, int len, int sum);
71
72/*
73 * Fold a partial checksum
74 */
75
76static __inline__ unsigned int csum_fold(unsigned int sum)
77{
78 unsigned int __dummy;
79 __asm__("extui %1, %0, 16, 16\n\t"
80 "extui %0 ,%0, 0, 16\n\t"
81 "add %0, %0, %1\n\t"
82 "slli %1, %0, 16\n\t"
83 "add %0, %0, %1\n\t"
84 "extui %0, %0, 16, 16\n\t"
85 "neg %0, %0\n\t"
86 "addi %0, %0, -1\n\t"
87 "extui %0, %0, 0, 16\n\t"
88 : "=r" (sum), "=&r" (__dummy)
89 : "0" (sum));
90 return sum;
91}
92
93/*
94 * This is a version of ip_compute_csum() optimized for IP headers,
95 * which always checksum on 4 octet boundaries.
96 */
97static __inline__ unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
98{
99 unsigned int sum, tmp, endaddr;
100
101 __asm__ __volatile__(
102 "sub %0, %0, %0\n\t"
103#if XCHAL_HAVE_LOOPS
104 "loopgtz %2, 2f\n\t"
105#else
106 "beqz %2, 2f\n\t"
107 "slli %4, %2, 2\n\t"
108 "add %4, %4, %1\n\t"
109 "0:\t"
110#endif
111 "l32i %3, %1, 0\n\t"
112 "add %0, %0, %3\n\t"
113 "bgeu %0, %3, 1f\n\t"
114 "addi %0, %0, 1\n\t"
115 "1:\t"
116 "addi %1, %1, 4\n\t"
117#if !XCHAL_HAVE_LOOPS
118 "blt %1, %4, 0b\n\t"
119#endif
120 "2:\t"
121 /* Since the input registers which are loaded with iph and ihl
122 are modified, we must also specify them as outputs, or gcc
123 will assume they contain their original values. */
124 : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp), "=&r" (endaddr)
125 : "1" (iph), "2" (ihl));
126
127 return csum_fold(sum);
128}
129
130static __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
131 unsigned long daddr,
132 unsigned short len,
133 unsigned short proto,
134 unsigned int sum)
135{
136
137#ifdef __XTENSA_EL__
138 unsigned long len_proto = (ntohs(len)<<16)+proto*256;
139#elif defined(__XTENSA_EB__)
140 unsigned long len_proto = (proto<<16)+len;
141#else
142# error processor byte order undefined!
143#endif
144 __asm__("add %0, %0, %1\n\t"
145 "bgeu %0, %1, 1f\n\t"
146 "addi %0, %0, 1\n\t"
147 "1:\t"
148 "add %0, %0, %2\n\t"
149 "bgeu %0, %2, 1f\n\t"
150 "addi %0, %0, 1\n\t"
151 "1:\t"
152 "add %0, %0, %3\n\t"
153 "bgeu %0, %3, 1f\n\t"
154 "addi %0, %0, 1\n\t"
155 "1:\t"
156 : "=r" (sum), "=r" (len_proto)
157 : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
158 return sum;
159}
160
161/*
162 * computes the checksum of the TCP/UDP pseudo-header
163 * returns a 16-bit checksum, already complemented
164 */
165static __inline__ unsigned short int csum_tcpudp_magic(unsigned long saddr,
166 unsigned long daddr,
167 unsigned short len,
168 unsigned short proto,
169 unsigned int sum)
170{
171 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
172}
173
174/*
175 * this routine is used for miscellaneous IP-like checksums, mainly
176 * in icmp.c
177 */
178
179static __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len)
180{
181 return csum_fold (csum_partial(buff, len, 0));
182}
183
184#define _HAVE_ARCH_IPV6_CSUM
185static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
186 struct in6_addr *daddr,
187 __u32 len,
188 unsigned short proto,
189 unsigned int sum)
190{
191 unsigned int __dummy;
192 __asm__("l32i %1, %2, 0\n\t"
193 "add %0, %0, %1\n\t"
194 "bgeu %0, %1, 1f\n\t"
195 "addi %0, %0, 1\n\t"
196 "1:\t"
197 "l32i %1, %2, 4\n\t"
198 "add %0, %0, %1\n\t"
199 "bgeu %0, %1, 1f\n\t"
200 "addi %0, %0, 1\n\t"
201 "1:\t"
202 "l32i %1, %2, 8\n\t"
203 "add %0, %0, %1\n\t"
204 "bgeu %0, %1, 1f\n\t"
205 "addi %0, %0, 1\n\t"
206 "1:\t"
207 "l32i %1, %2, 12\n\t"
208 "add %0, %0, %1\n\t"
209 "bgeu %0, %1, 1f\n\t"
210 "addi %0, %0, 1\n\t"
211 "1:\t"
212 "l32i %1, %3, 0\n\t"
213 "add %0, %0, %1\n\t"
214 "bgeu %0, %1, 1f\n\t"
215 "addi %0, %0, 1\n\t"
216 "1:\t"
217 "l32i %1, %3, 4\n\t"
218 "add %0, %0, %1\n\t"
219 "bgeu %0, %1, 1f\n\t"
220 "addi %0, %0, 1\n\t"
221 "1:\t"
222 "l32i %1, %3, 8\n\t"
223 "add %0, %0, %1\n\t"
224 "bgeu %0, %1, 1f\n\t"
225 "addi %0, %0, 1\n\t"
226 "1:\t"
227 "l32i %1, %3, 12\n\t"
228 "add %0, %0, %1\n\t"
229 "bgeu %0, %1, 1f\n\t"
230 "addi %0, %0, 1\n\t"
231 "1:\t"
232 "add %0, %0, %4\n\t"
233 "bgeu %0, %4, 1f\n\t"
234 "addi %0, %0, 1\n\t"
235 "1:\t"
236 "add %0, %0, %5\n\t"
237 "bgeu %0, %5, 1f\n\t"
238 "addi %0, %0, 1\n\t"
239 "1:\t"
240 : "=r" (sum), "=&r" (__dummy)
241 : "r" (saddr), "r" (daddr),
242 "r" (htonl(len)), "r" (htonl(proto)), "0" (sum));
243
244 return csum_fold(sum);
245}
246
247/*
248 * Copy and checksum to user
249 */
250#define HAVE_CSUM_COPY_USER
251static __inline__ unsigned int csum_and_copy_to_user (const char *src, char *dst,
252 int len, int sum, int *err_ptr)
253{
254 if (access_ok(VERIFY_WRITE, dst, len))
255 return csum_partial_copy_generic(src, dst, len, sum, NULL, err_ptr);
256
257 if (len)
258 *err_ptr = -EFAULT;
259
260 return -1; /* invalid checksum */
261}
262#endif