xref: /linux/arch/xtensa/include/asm/checksum.h (revision dc16c8a9ce980d03cfeedbc2559744140d134130)
1 /*
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 
14 #include <linux/in6.h>
15 #include <linux/uaccess.h>
16 #include <asm/core.h>
17 
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  */
30 asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
31 
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 
40 asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,
41 					    int len, __wsum sum,
42 					    int *src_err_ptr, int *dst_err_ptr);
43 
44 #define _HAVE_ARCH_CSUM_AND_COPY
45 /*
46  *	Note: when you get a NULL pointer exception here this means someone
47  *	passed in an incorrect kernel address to one of these functions.
48  */
49 static inline
50 __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len)
51 {
52 	return csum_partial_copy_generic(src, dst, len, 0, NULL, NULL);
53 }
54 
55 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
56 static inline
57 __wsum csum_and_copy_from_user(const void __user *src, void *dst,
58 				   int len)
59 {
60 	int err = 0;
61 
62 	if (!access_ok(src, len))
63 		return 0;
64 
65 	sum = csum_partial_copy_generic((__force const void *)src, dst,
66 					len, ~0U, &err, NULL);
67 	return err ? 0 : sum;
68 }
69 
70 /*
71  *	Fold a partial checksum
72  */
73 
74 static __inline__ __sum16 csum_fold(__wsum sum)
75 {
76 	unsigned int __dummy;
77 	__asm__("extui	%1, %0, 16, 16\n\t"
78 		"extui	%0 ,%0, 0, 16\n\t"
79 		"add	%0, %0, %1\n\t"
80 		"slli	%1, %0, 16\n\t"
81 		"add	%0, %0, %1\n\t"
82 		"extui	%0, %0, 16, 16\n\t"
83 		"neg	%0, %0\n\t"
84 		"addi	%0, %0, -1\n\t"
85 		"extui	%0, %0, 0, 16\n\t"
86 		: "=r" (sum), "=&r" (__dummy)
87 		: "0" (sum));
88 	return (__force __sum16)sum;
89 }
90 
91 /*
92  *	This is a version of ip_compute_csum() optimized for IP headers,
93  *	which always checksum on 4 octet boundaries.
94  */
95 static __inline__ __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
96 {
97 	unsigned int sum, tmp, endaddr;
98 
99 	__asm__ __volatile__(
100 		"sub		%0, %0, %0\n\t"
101 #if XCHAL_HAVE_LOOPS
102 		"loopgtz	%2, 2f\n\t"
103 #else
104 		"beqz		%2, 2f\n\t"
105 		"slli		%4, %2, 2\n\t"
106 		"add		%4, %4, %1\n\t"
107 		"0:\t"
108 #endif
109 		"l32i		%3, %1, 0\n\t"
110 		"add		%0, %0, %3\n\t"
111 		"bgeu		%0, %3, 1f\n\t"
112 		"addi		%0, %0, 1\n\t"
113 		"1:\t"
114 		"addi		%1, %1, 4\n\t"
115 #if !XCHAL_HAVE_LOOPS
116 		"blt		%1, %4, 0b\n\t"
117 #endif
118 		"2:\t"
119 	/* Since the input registers which are loaded with iph and ihl
120 	   are modified, we must also specify them as outputs, or gcc
121 	   will assume they contain their original values. */
122 		: "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp),
123 		  "=&r" (endaddr)
124 		: "1" (iph), "2" (ihl)
125 		: "memory");
126 
127 	return	csum_fold(sum);
128 }
129 
130 static __inline__ __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
131 					    __u32 len, __u8 proto,
132 					    __wsum sum)
133 {
134 
135 #ifdef __XTENSA_EL__
136 	unsigned long len_proto = (len + proto) << 8;
137 #elif defined(__XTENSA_EB__)
138 	unsigned long len_proto = len + proto;
139 #else
140 # error processor byte order undefined!
141 #endif
142 	__asm__("add	%0, %0, %1\n\t"
143 		"bgeu	%0, %1, 1f\n\t"
144 		"addi	%0, %0, 1\n\t"
145 		"1:\t"
146 		"add	%0, %0, %2\n\t"
147 		"bgeu	%0, %2, 1f\n\t"
148 		"addi	%0, %0, 1\n\t"
149 		"1:\t"
150 		"add	%0, %0, %3\n\t"
151 		"bgeu	%0, %3, 1f\n\t"
152 		"addi	%0, %0, 1\n\t"
153 		"1:\t"
154 		: "=r" (sum), "=r" (len_proto)
155 		: "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
156 	return sum;
157 }
158 
159 /*
160  * computes the checksum of the TCP/UDP pseudo-header
161  * returns a 16-bit checksum, already complemented
162  */
163 static __inline__ __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
164 					    __u32 len, __u8 proto,
165 					    __wsum sum)
166 {
167 	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
168 }
169 
170 /*
171  * this routine is used for miscellaneous IP-like checksums, mainly
172  * in icmp.c
173  */
174 
175 static __inline__ __sum16 ip_compute_csum(const void *buff, int len)
176 {
177 	return csum_fold (csum_partial(buff, len, 0));
178 }
179 
180 #define _HAVE_ARCH_IPV6_CSUM
181 static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
182 					  const struct in6_addr *daddr,
183 					  __u32 len, __u8 proto,
184 					  __wsum sum)
185 {
186 	unsigned int __dummy;
187 	__asm__("l32i	%1, %2, 0\n\t"
188 		"add	%0, %0, %1\n\t"
189 		"bgeu	%0, %1, 1f\n\t"
190 		"addi	%0, %0, 1\n\t"
191 		"1:\t"
192 		"l32i	%1, %2, 4\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, 8\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, 12\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, %3, 0\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, 4\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, 8\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, 12\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 		"add	%0, %0, %4\n\t"
228 		"bgeu	%0, %4, 1f\n\t"
229 		"addi	%0, %0, 1\n\t"
230 		"1:\t"
231 		"add	%0, %0, %5\n\t"
232 		"bgeu	%0, %5, 1f\n\t"
233 		"addi	%0, %0, 1\n\t"
234 		"1:\t"
235 		: "=r" (sum), "=&r" (__dummy)
236 		: "r" (saddr), "r" (daddr),
237 		  "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
238 		: "memory");
239 
240 	return csum_fold(sum);
241 }
242 
243 /*
244  *	Copy and checksum to user
245  */
246 #define HAVE_CSUM_COPY_USER
247 static __inline__ __wsum csum_and_copy_to_user(const void *src,
248 					       void __user *dst, int len)
249 {
250 	int err = 0;
251 	__wsum sum = ~0U;
252 
253 	if (!access_ok(dst, len))
254 		return 0;
255 
256 	sum = csum_partial_copy_generic(src,dst,len,sum,NULL,&err);
257 	return err ? 0 : sum;
258 }
259 #endif
260