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