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
5 /* checksum.h: IP/UDP/TCP checksum routines on the Sparc.
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
14 * Alpha checksum c-code
15 * ix86 inline assembly
16 * RFC1071 Computing the Internet Checksum
19 #include <linux/in6.h>
20 #include <asm/uaccess.h>
21 #include <asm/cprefix.h>
23 /* computes the checksum of a memory block at buff, length len,
24 * and adds in "sum" (32-bit)
26 * returns a 32-bit number suitable for feeding into itself
27 * or csum_tcpudp_magic
29 * this function must be called with even lengths, except
30 * for the last fragment, which may be odd
32 * it's best to have buff aligned on a 32-bit boundary
34 extern unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
36 /* the same as csum_partial, but copies from fs:src while it
39 * here even more important to align src and dst on a 32-bit (or even
40 * better 64-bit) boundary
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))
49 extern unsigned int __csum_partial_copy_sparc_generic (const char *, char *);
51 extern __inline__ unsigned int
52 csum_partial_copy_nocheck (const char *src, char *dst, int len,
55 register unsigned int ret asm("o0") = (unsigned int)src;
56 register char *d asm("o1") = dst;
57 register int l asm("g1") = len;
59 __asm__ __volatile__ (
60 "call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "\n\t"
62 : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (sum) :
63 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
67 extern __inline__ unsigned int
68 csum_partial_copy_from_user(const char *src, char *dst, int len,
69 unsigned int sum, int *err)
71 if (!access_ok (VERIFY_READ, src, len)) {
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;
81 __asm__ __volatile__ (
82 ".section __ex_table,#alloc\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");
95 extern __inline__ unsigned int
96 csum_partial_copy_to_user(const char *src, char *dst, int len,
97 unsigned int sum, int *err)
99 if (!access_ok (VERIFY_WRITE, dst, len)) {
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;
108 __asm__ __volatile__ (
109 ".section __ex_table,#alloc\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");
122 #define HAVE_CSUM_COPY_USER
123 #define csum_and_copy_to_user csum_partial_copy_to_user
125 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
126 * the majority of the time.
128 extern __inline__ unsigned short ip_fast_csum(__const__ unsigned char *iph,
133 /* Note: We must read %2 before we touch %0 for the first time,
134 * because GCC can legitimately use the same register for
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"
149 "addxcc\t%0, %%g0, %0\n\t"
150 "subcc\t%%g4, 1, %%g4\n\t"
152 "sll\t%0, 16, %%g2\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"
159 : "=r" (sum), "=&r" (iph)
160 : "r" (ihl), "1" (iph)
161 : "g2", "g3", "g4", "cc");
165 /* Fold a partial checksum without adding pseudo headers. */
166 extern __inline__ unsigned int csum_fold(unsigned int sum)
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"
174 : "=&r" (sum), "=r" (tmp)
175 : "0" (sum), "1" (sum<<16)
180 extern __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
183 unsigned short proto,
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),
198 * computes the checksum of the TCP/UDP pseudo-header
199 * returns a 16-bit checksum, already complemented
201 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
204 unsigned short proto,
207 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
210 #define _HAVE_ARCH_IPV6_CSUM
212 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
213 struct in6_addr *daddr,
215 unsigned short proto,
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"
239 : "r" (saddr), "r" (daddr),
240 "r"(htonl(len)), "r"(htonl(proto)), "r"(sum)
241 : "g2", "g3", "g4", "cc");
243 return csum_fold(sum);
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)
249 return csum_fold(csum_partial(buff, len, 0));
252 #endif /* !(__SPARC_CHECKSUM_H) */