ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / include / asm-sparc64 / checksum.h
1 /* $Id: checksum.h,v 1.19 2002/02/09 19:49:31 davem Exp $ */
2 #ifndef __SPARC64_CHECKSUM_H
3 #define __SPARC64_CHECKSUM_H
4
5 /*  checksum.h:  IP/UDP/TCP checksum routines on the V9.
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 user space 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 extern unsigned int csum_partial_copy_sparc64(const char *src, char *dst, int len, unsigned int sum);
42                         
43 static __inline__ unsigned int 
44 csum_partial_copy_nocheck (const char *src, char *dst, int len, 
45                            unsigned int sum)
46 {
47         int ret;
48         unsigned char cur_ds = get_thread_current_ds();
49         __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "i" (ASI_P));
50         ret = csum_partial_copy_sparc64(src, dst, len, sum);
51         __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "r" (cur_ds));
52         return ret;
53 }
54
55 static __inline__ unsigned int 
56 csum_partial_copy_from_user(const char *src, char *dst, int len, 
57                             unsigned int sum, int *err)
58 {
59         __asm__ __volatile__ ("stx      %0, [%%sp + 0x7ff + 128]"
60                               : : "r" (err));
61         return csum_partial_copy_sparc64(src, dst, len, sum);
62 }
63
64 /* 
65  *      Copy and checksum to user
66  */
67 #define HAVE_CSUM_COPY_USER
68 extern unsigned int csum_partial_copy_user_sparc64(const char *src, char *dst, int len, unsigned int sum);
69 static __inline__ unsigned int 
70 csum_and_copy_to_user(const char *src, char *dst, int len, 
71                       unsigned int sum, int *err)
72 {
73         __asm__ __volatile__ ("stx      %0, [%%sp + 0x7ff + 128]"
74                               : : "r" (err));
75         return csum_partial_copy_user_sparc64(src, dst, len, sum);
76 }
77   
78 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
79  * the majority of the time.
80  */
81 extern unsigned short ip_fast_csum(__const__ unsigned char *iph,
82                                    unsigned int ihl);
83
84 /* Fold a partial checksum without adding pseudo headers. */
85 static __inline__ unsigned short csum_fold(unsigned int sum)
86 {
87         unsigned int tmp;
88
89         __asm__ __volatile__(
90 "       addcc           %0, %1, %1\n"
91 "       srl             %1, 16, %1\n"
92 "       addc            %1, %%g0, %1\n"
93 "       xnor            %%g0, %1, %0\n"
94         : "=&r" (sum), "=r" (tmp)
95         : "0" (sum), "1" (sum<<16)
96         : "cc");
97         return (sum & 0xffff);
98 }
99
100 static __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
101                                                    unsigned long daddr,
102                                                    unsigned int len,
103                                                    unsigned short proto,
104                                                    unsigned int sum)
105 {
106         __asm__ __volatile__(
107 "       addcc           %1, %0, %0\n"
108 "       addccc          %2, %0, %0\n"
109 "       addccc          %3, %0, %0\n"
110 "       addc            %0, %%g0, %0\n"
111         : "=r" (sum), "=r" (saddr)
112         : "r" (daddr), "r" ((proto<<16)+len), "0" (sum), "1" (saddr)
113         : "cc");
114         return sum;
115 }
116
117 /*
118  * computes the checksum of the TCP/UDP pseudo-header
119  * returns a 16-bit checksum, already complemented
120  */
121 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
122                                                    unsigned long daddr,
123                                                    unsigned short len,
124                                                    unsigned short proto,
125                                                    unsigned int sum) 
126 {
127         return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
128 }
129
130 #define _HAVE_ARCH_IPV6_CSUM
131
132 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
133                                                      struct in6_addr *daddr,
134                                                      __u32 len,
135                                                      unsigned short proto,
136                                                      unsigned int sum) 
137 {
138         __asm__ __volatile__ (
139 "       addcc           %3, %4, %%g7\n"
140 "       addccc          %5, %%g7, %%g7\n"
141 "       lduw            [%2 + 0x0c], %%g2\n"
142 "       lduw            [%2 + 0x08], %%g3\n"
143 "       addccc          %%g2, %%g7, %%g7\n"
144 "       lduw            [%2 + 0x04], %%g2\n"
145 "       addccc          %%g3, %%g7, %%g7\n"
146 "       lduw            [%2 + 0x00], %%g3\n"
147 "       addccc          %%g2, %%g7, %%g7\n"
148 "       lduw            [%1 + 0x0c], %%g2\n"
149 "       addccc          %%g3, %%g7, %%g7\n"
150 "       lduw            [%1 + 0x08], %%g3\n"
151 "       addccc          %%g2, %%g7, %%g7\n"
152 "       lduw            [%1 + 0x04], %%g2\n"
153 "       addccc          %%g3, %%g7, %%g7\n"
154 "       lduw            [%1 + 0x00], %%g3\n"
155 "       addccc          %%g2, %%g7, %%g7\n"
156 "       addccc          %%g3, %%g7, %0\n"
157 "       addc            0, %0, %0\n"
158         : "=&r" (sum)
159         : "r" (saddr), "r" (daddr), "r"(htonl(len)),
160           "r"(htonl(proto)), "r"(sum)
161         : "g2", "g3", "g7", "cc");
162
163         return csum_fold(sum);
164 }
165
166 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
167 static __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len)
168 {
169         return csum_fold(csum_partial(buff, len, 0));
170 }
171
172 #endif /* !(__SPARC64_CHECKSUM_H) */