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