ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-sparc / checksum.h
1 /* $Id: checksum.h,v 1.33 2002/02/01 22:01:05 davem Exp $ */
2 #ifndef __SPARC_CHECKSUM_H
3 #define __SPARC_CHECKSUM_H
4
5 /*  checksum.h:  IP/UDP/TCP checksum routines on the Sparc.
6  *
7  *  Copyright(C) 1995 Linus Torvalds
8  *  Copyright(C) 1995 Miguel de Icaza
9  *  Copyright(C) 1996 David S. Miller
10  *  Copyright(C) 1996 Eddie C. Dost
11  *  Copyright(C) 1997 Jakub Jelinek
12  *
13  * derived from:
14  *      Alpha checksum c-code
15  *      ix86 inline assembly
16  *      RFC1071 Computing the Internet Checksum
17  */
18  
19 #include <linux/in6.h>
20 #include <asm/uaccess.h>
21
22 /* computes the checksum of a memory block at buff, length len,
23  * and adds in "sum" (32-bit)
24  *
25  * returns a 32-bit number suitable for feeding into itself
26  * or csum_tcpudp_magic
27  *
28  * this function must be called with even lengths, except
29  * for the last fragment, which may be odd
30  *
31  * it's best to have buff aligned on a 32-bit boundary
32  */
33 extern unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
34
35 /* the same as csum_partial, but copies from fs:src while it
36  * checksums
37  *
38  * here even more important to align src and dst on a 32-bit (or even
39  * better 64-bit) boundary
40  */
41
42 extern unsigned int __csum_partial_copy_sparc_generic (const char *, char *);
43
44 static inline unsigned int 
45 csum_partial_copy_nocheck (const char *src, char *dst, int len, 
46                            unsigned int sum)
47 {
48         register unsigned int ret asm("o0") = (unsigned int)src;
49         register char *d asm("o1") = dst;
50         register int l asm("g1") = len;
51
52         __asm__ __volatile__ (
53                 "call __csum_partial_copy_sparc_generic\n\t"
54                 " mov %6, %%g7\n"
55         : "=&r" (ret), "=&r" (d), "=&r" (l)
56         : "0" (ret), "1" (d), "2" (l), "r" (sum)
57         : "o2", "o3", "o4", "o5", "o7",
58           "g2", "g3", "g4", "g5", "g7",
59           "memory", "cc");
60         return ret;
61 }
62
63 static inline unsigned int 
64 csum_partial_copy_from_user(const char *src, char *dst, int len, 
65                             unsigned int sum, int *err)
66   {
67         if (!access_ok (VERIFY_READ, src, len)) {
68                 *err = -EFAULT;
69                 memset (dst, 0, len);
70                 return sum;
71         } else {
72                 register unsigned int ret asm("o0") = (unsigned int)src;
73                 register char *d asm("o1") = dst;
74                 register int l asm("g1") = len;
75                 register unsigned int s asm("g7") = sum;
76
77                 __asm__ __volatile__ (
78                 ".section __ex_table,#alloc\n\t"
79                 ".align 4\n\t"
80                 ".word 1f,2\n\t"
81                 ".previous\n"
82                 "1:\n\t"
83                 "call __csum_partial_copy_sparc_generic\n\t"
84                 " st %8, [%%sp + 64]\n"
85                 : "=&r" (ret), "=&r" (d), "=&r" (l), "=&r" (s)
86                 : "0" (ret), "1" (d), "2" (l), "3" (s), "r" (err)
87                 : "o2", "o3", "o4", "o5", "o7", "g2", "g3", "g4", "g5",
88                   "cc", "memory");
89                 return ret;
90         }
91   }
92   
93 static inline unsigned int 
94 csum_partial_copy_to_user(const char *src, char *dst, int len, 
95                           unsigned int sum, int *err)
96 {
97         if (!access_ok (VERIFY_WRITE, dst, len)) {
98                 *err = -EFAULT;
99                 return sum;
100         } else {
101                 register unsigned int ret asm("o0") = (unsigned int)src;
102                 register char *d asm("o1") = dst;
103                 register int l asm("g1") = len;
104                 register unsigned int s asm("g7") = sum;
105
106                 __asm__ __volatile__ (
107                 ".section __ex_table,#alloc\n\t"
108                 ".align 4\n\t"
109                 ".word 1f,1\n\t"
110                 ".previous\n"
111                 "1:\n\t"
112                 "call __csum_partial_copy_sparc_generic\n\t"
113                 " st %8, [%%sp + 64]\n"
114                 : "=&r" (ret), "=&r" (d), "=&r" (l), "=&r" (s)
115                 : "0" (ret), "1" (d), "2" (l), "3" (s), "r" (err)
116                 : "o2", "o3", "o4", "o5", "o7",
117                   "g2", "g3", "g4", "g5",
118                   "cc", "memory");
119                 return ret;
120         }
121 }
122
123 #define HAVE_CSUM_COPY_USER
124 #define csum_and_copy_to_user csum_partial_copy_to_user
125
126 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
127  * the majority of the time.
128  */
129 static inline unsigned short ip_fast_csum(const unsigned char *iph,
130                                           unsigned int ihl)
131 {
132         unsigned short sum;
133
134         /* Note: We must read %2 before we touch %0 for the first time,
135          *       because GCC can legitimately use the same register for
136          *       both operands.
137          */
138         __asm__ __volatile__("sub\t%2, 4, %%g4\n\t"
139                              "ld\t[%1 + 0x00], %0\n\t"
140                              "ld\t[%1 + 0x04], %%g2\n\t"
141                              "ld\t[%1 + 0x08], %%g3\n\t"
142                              "addcc\t%%g2, %0, %0\n\t"
143                              "addxcc\t%%g3, %0, %0\n\t"
144                              "ld\t[%1 + 0x0c], %%g2\n\t"
145                              "ld\t[%1 + 0x10], %%g3\n\t"
146                              "addxcc\t%%g2, %0, %0\n\t"
147                              "addx\t%0, %%g0, %0\n"
148                              "1:\taddcc\t%%g3, %0, %0\n\t"
149                              "add\t%1, 4, %1\n\t"
150                              "addxcc\t%0, %%g0, %0\n\t"
151                              "subcc\t%%g4, 1, %%g4\n\t"
152                              "be,a\t2f\n\t"
153                              "sll\t%0, 16, %%g2\n\t"
154                              "b\t1b\n\t"
155                              "ld\t[%1 + 0x10], %%g3\n"
156                              "2:\taddcc\t%0, %%g2, %%g2\n\t"
157                              "srl\t%%g2, 16, %0\n\t"
158                              "addx\t%0, %%g0, %0\n\t"
159                              "xnor\t%%g0, %0, %0"
160                              : "=r" (sum), "=&r" (iph)
161                              : "r" (ihl), "1" (iph)
162                              : "g2", "g3", "g4", "cc");
163         return sum;
164 }
165
166 /* Fold a partial checksum without adding pseudo headers. */
167 static inline unsigned int csum_fold(unsigned int sum)
168 {
169         unsigned int tmp;
170
171         __asm__ __volatile__("addcc\t%0, %1, %1\n\t"
172                              "srl\t%1, 16, %1\n\t"
173                              "addx\t%1, %%g0, %1\n\t"
174                              "xnor\t%%g0, %1, %0"
175                              : "=&r" (sum), "=r" (tmp)
176                              : "0" (sum), "1" (sum<<16)
177                              : "cc");
178         return sum;
179 }
180
181 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
182                                                unsigned long daddr,
183                                                unsigned int len,
184                                                unsigned short proto,
185                                                unsigned int sum)
186 {
187         __asm__ __volatile__("addcc\t%1, %0, %0\n\t"
188                              "addxcc\t%2, %0, %0\n\t"
189                              "addxcc\t%3, %0, %0\n\t"
190                              "addx\t%0, %%g0, %0\n\t"
191                              : "=r" (sum), "=r" (saddr)
192                              : "r" (daddr), "r" ((proto<<16)+len), "0" (sum),
193                                "1" (saddr)
194                              : "cc");
195         return sum;
196 }
197
198 /*
199  * computes the checksum of the TCP/UDP pseudo-header
200  * returns a 16-bit checksum, already complemented
201  */
202 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
203                                                    unsigned long daddr,
204                                                    unsigned short len,
205                                                    unsigned short proto,
206                                                    unsigned int sum) 
207 {
208         return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
209 }
210
211 #define _HAVE_ARCH_IPV6_CSUM
212
213 static inline unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
214                                                  struct in6_addr *daddr,
215                                                  __u32 len,
216                                                  unsigned short proto,
217                                                  unsigned int sum) 
218 {
219         __asm__ __volatile__ (
220                 "addcc  %3, %4, %%g4\n\t"
221                 "addxcc %5, %%g4, %%g4\n\t"
222                 "ld     [%2 + 0x0c], %%g2\n\t"
223                 "ld     [%2 + 0x08], %%g3\n\t"
224                 "addxcc %%g2, %%g4, %%g4\n\t"
225                 "ld     [%2 + 0x04], %%g2\n\t"
226                 "addxcc %%g3, %%g4, %%g4\n\t"
227                 "ld     [%2 + 0x00], %%g3\n\t"
228                 "addxcc %%g2, %%g4, %%g4\n\t"
229                 "ld     [%1 + 0x0c], %%g2\n\t"
230                 "addxcc %%g3, %%g4, %%g4\n\t"
231                 "ld     [%1 + 0x08], %%g3\n\t"
232                 "addxcc %%g2, %%g4, %%g4\n\t"
233                 "ld     [%1 + 0x04], %%g2\n\t"
234                 "addxcc %%g3, %%g4, %%g4\n\t"
235                 "ld     [%1 + 0x00], %%g3\n\t"
236                 "addxcc %%g2, %%g4, %%g4\n\t"
237                 "addxcc %%g3, %%g4, %0\n\t"
238                 "addx   0, %0, %0\n"
239                 : "=&r" (sum)
240                 : "r" (saddr), "r" (daddr), 
241                   "r"(htonl(len)), "r"(htonl(proto)), "r"(sum)
242                 : "g2", "g3", "g4", "cc");
243
244         return csum_fold(sum);
245 }
246
247 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
248 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
249 {
250         return csum_fold(csum_partial(buff, len, 0));
251 }
252
253 #endif /* !(__SPARC_CHECKSUM_H) */