1 /* $Id: checksum.h,v 1.29 1999/03/21 05:22:07 davem 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 <asm/uaccess.h>
20 #include <asm/cprefix.h>
22 /* computes the checksum of a memory block at buff, length len,
23 * and adds in "sum" (32-bit)
25 * returns a 32-bit number suitable for feeding into itself
26 * or csum_tcpudp_magic
28 * this function must be called with even lengths, except
29 * for the last fragment, which may be odd
31 * it's best to have buff aligned on a 32-bit boundary
33 extern unsigned int csum_partial(unsigned char * buff
, int len
, unsigned int sum
);
35 /* the same as csum_partial, but copies from fs:src while it
38 * here even more important to align src and dst on a 32-bit (or even
39 * better 64-bit) boundary
42 /* FIXME: Remove these two macros ASAP */
43 #define csum_partial_copy(src, dst, len, sum) \
44 csum_partial_copy_nocheck(src,dst,len,sum)
45 #define csum_partial_copy_fromuser(s, d, l, w) \
46 csum_partial_copy((char *) (s), (d), (l), (w))
48 extern unsigned int __csum_partial_copy_sparc_generic (const char *, char *);
50 extern __inline__
unsigned int
51 csum_partial_copy_nocheck (const char *src
, char *dst
, int len
,
54 register unsigned int ret
asm("o0") = (unsigned int)src
;
55 register char *d
asm("o1") = dst
;
56 register int l
asm("g1") = len
;
58 __asm__
__volatile__ ("
59 call " C_LABEL_STR(__csum_partial_copy_sparc_generic
) "
61 " : "=r" (ret
) : "0" (ret
), "r" (d
), "r" (l
), "r" (sum
) :
62 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
66 extern __inline__
unsigned int
67 csum_partial_copy_from_user(const char *src
, char *dst
, int len
,
68 unsigned int sum
, int *err
)
70 if (!access_ok (VERIFY_READ
, src
, len
)) {
75 register unsigned int ret
asm("o0") = (unsigned int)src
;
76 register char *d
asm("o1") = dst
;
77 register int l
asm("g1") = len
;
78 register unsigned int s
asm("g7") = sum
;
80 __asm__
__volatile__ ("
81 .section __ex_table,#alloc
86 call " C_LABEL_STR(__csum_partial_copy_sparc_generic
) "
88 " : "=r" (ret
) : "0" (ret
), "r" (d
), "r" (l
), "r" (s
), "r" (err
) :
89 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
94 extern __inline__
unsigned int
95 csum_partial_copy_to_user(const char *src
, char *dst
, int len
,
96 unsigned int sum
, int *err
)
98 if (!access_ok (VERIFY_WRITE
, dst
, len
)) {
102 register unsigned int ret
asm("o0") = (unsigned int)src
;
103 register char *d
asm("o1") = dst
;
104 register int l
asm("g1") = len
;
105 register unsigned int s
asm("g7") = sum
;
107 __asm__
__volatile__ ("
108 .section __ex_table,#alloc
113 call " C_LABEL_STR(__csum_partial_copy_sparc_generic
) "
115 " : "=r" (ret
) : "0" (ret
), "r" (d
), "r" (l
), "r" (s
), "r" (err
) :
116 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7");
121 #define HAVE_CSUM_COPY_USER
122 #define csum_and_copy_to_user csum_partial_copy_to_user
124 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
125 * the majority of the time.
127 extern __inline__
unsigned short ip_fast_csum(__const__
unsigned char *iph
,
132 /* Note: We must read %2 before we touch %0 for the first time,
133 * because GCC can legitimately use the same register for
136 __asm__
__volatile__("sub\t%2, 4, %%g4\n\t"
137 "ld\t[%1 + 0x00], %0\n\t"
138 "ld\t[%1 + 0x04], %%g2\n\t"
139 "ld\t[%1 + 0x08], %%g3\n\t"
140 "addcc\t%%g2, %0, %0\n\t"
141 "addxcc\t%%g3, %0, %0\n\t"
142 "ld\t[%1 + 0x0c], %%g2\n\t"
143 "ld\t[%1 + 0x10], %%g3\n\t"
144 "addxcc\t%%g2, %0, %0\n\t"
145 "addx\t%0, %%g0, %0\n"
146 "1:\taddcc\t%%g3, %0, %0\n\t"
148 "addxcc\t%0, %%g0, %0\n\t"
149 "subcc\t%%g4, 1, %%g4\n\t"
151 "sll\t%0, 16, %%g2\n\t"
153 "ld\t[%1 + 0x10], %%g3\n"
154 "2:\taddcc\t%0, %%g2, %%g2\n\t"
155 "srl\t%%g2, 16, %0\n\t"
156 "addx\t%0, %%g0, %0\n\t"
158 : "=r" (sum
), "=&r" (iph
)
159 : "r" (ihl
), "1" (iph
)
160 : "g2", "g3", "g4", "cc");
164 /* Fold a partial checksum without adding pseudo headers. */
165 extern __inline__
unsigned int csum_fold(unsigned int sum
)
169 __asm__
__volatile__("addcc\t%0, %1, %1\n\t"
170 "srl\t%1, 16, %1\n\t"
171 "addx\t%1, %%g0, %1\n\t"
173 : "=&r" (sum
), "=r" (tmp
)
174 : "0" (sum
), "1" (sum
<<16)
179 extern __inline__
unsigned long csum_tcpudp_nofold(unsigned long saddr
,
182 unsigned short proto
,
185 __asm__
__volatile__("addcc\t%1, %0, %0\n\t"
186 "addxcc\t%2, %0, %0\n\t"
187 "addxcc\t%3, %0, %0\n\t"
188 "addx\t%0, %%g0, %0\n\t"
189 : "=r" (sum
), "=r" (saddr
)
190 : "r" (daddr
), "r" ((proto
<<16)+len
), "0" (sum
),
197 * computes the checksum of the TCP/UDP pseudo-header
198 * returns a 16-bit checksum, already complemented
200 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr
,
203 unsigned short proto
,
206 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
209 #define _HAVE_ARCH_IPV6_CSUM
211 static __inline__
unsigned short int csum_ipv6_magic(struct in6_addr
*saddr
,
212 struct in6_addr
*daddr
,
214 unsigned short proto
,
217 __asm__
__volatile__ ("
219 addxcc %5, %%g4, %%g4
222 addxcc %%g2, %%g4, %%g4
224 addxcc %%g3, %%g4, %%g4
226 addxcc %%g2, %%g4, %%g4
228 addxcc %%g3, %%g4, %%g4
230 addxcc %%g2, %%g4, %%g4
232 addxcc %%g3, %%g4, %%g4
234 addxcc %%g2, %%g4, %%g4
235 addxcc %%g3, %%g4, %0
239 : "r" (saddr
), "r" (daddr
),
240 "r"(htonl((__u32
) (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) */