1 /* $Id: checksum.h,v 1.13 1999/07/30 09:31:13 davem Exp $ */
2 #ifndef __SPARC64_CHECKSUM_H
3 #define __SPARC64_CHECKSUM_H
5 /* checksum.h: IP/UDP/TCP checksum routines on the V9.
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>
21 /* computes the checksum of a memory block at buff, length len,
22 * and adds in "sum" (32-bit)
24 * returns a 32-bit number suitable for feeding into itself
25 * or csum_tcpudp_magic
27 * this function must be called with even lengths, except
28 * for the last fragment, which may be odd
30 * it's best to have buff aligned on a 32-bit boundary
32 extern unsigned int csum_partial(unsigned char * buff
, int len
, unsigned int sum
);
34 /* the same as csum_partial, but copies from user space while it
37 * here even more important to align src and dst on a 32-bit (or even
38 * better 64-bit) boundary
40 /* FIXME: Remove these macros ASAP */
41 #define csum_partial_copy(src, dst, len, sum) \
42 csum_partial_copy_nocheck(src,dst,len,sum)
43 #define csum_partial_copy_fromuser(s, d, l, w) \
44 csum_partial_copy_from_user((char *) (s), (d), (l), (w), NULL)
46 extern unsigned int csum_partial_copy_sparc64(const char *src
, char *dst
, int len
, unsigned int sum
);
48 extern __inline__
unsigned int
49 csum_partial_copy_nocheck (const char *src
, char *dst
, int len
,
53 unsigned char cur_ds
= current
->thread
.current_ds
.seg
;
54 __asm__
__volatile__ ("wr %%g0, %0, %%asi" : : "i" (ASI_P
));
55 ret
= csum_partial_copy_sparc64(src
, dst
, len
, sum
);
56 __asm__
__volatile__ ("wr %%g0, %0, %%asi" : : "r" (cur_ds
));
60 extern __inline__
unsigned int
61 csum_partial_copy_from_user(const char *src
, char *dst
, int len
,
62 unsigned int sum
, int *err
)
64 __asm__
__volatile__ ("stx %0, [%%sp + 0x7ff + 128]"
66 return csum_partial_copy_sparc64(src
, dst
, len
, sum
);
70 /* Not implemented, but nobody uses it yet... */
71 extern __inline__
unsigned int
72 csum_partial_copy_to_user(const char *src
, char *dst
, int len
,
73 unsigned int sum
, int *err
)
79 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
80 * the majority of the time.
82 extern __inline__
unsigned short ip_fast_csum(__const__
unsigned char *iph
,
87 /* Note: We must read %2 before we touch %0 for the first time,
88 * because GCC can legitimately use the same register for
91 __asm__
__volatile__("
92 sub %2, 4, %%g7 ! IEU0
93 lduw [%1 + 0x00], %0 ! Load Group
94 lduw [%1 + 0x04], %%g2 ! Load Group
95 lduw [%1 + 0x08], %%g3 ! Load Group
96 addcc %%g2, %0, %0 ! IEU1 1 Load Bubble + Group
97 lduw [%1 + 0x0c], %%g2 ! Load
98 addccc %%g3, %0, %0 ! Sngle Group no Bubble
99 lduw [%1 + 0x10], %%g3 ! Load Group
100 addccc %%g2, %0, %0 ! Sngle Group no Bubble
101 addc %0, %%g0, %0 ! Sngle Group
102 1: addcc %%g3, %0, %0 ! IEU1 Group no Bubble
104 addccc %0, %%g0, %0 ! Sngle Group no Bubble
105 subcc %%g7, 1, %%g7 ! IEU1 Group
106 be,a,pt %%icc, 2f ! CTI
107 sll %0, 16, %%g2 ! IEU0
108 lduw [%1 + 0x10], %%g3 ! Load Group
109 ba,pt %%xcc, 1b ! CTI
111 2: addcc %0, %%g2, %%g2 ! IEU1 Group
112 srl %%g2, 16, %0 ! IEU0 Group regdep XXX Scheisse!
113 addc %0, %%g0, %0 ! Sngle Group
114 xnor %%g0, %0, %0 ! IEU0 Group
115 srl %0, 0, %0 ! IEU0 Group XXX Scheisse!
116 " : "=r" (sum
), "=&r" (iph
)
117 : "r" (ihl
), "1" (iph
)
118 : "g2", "g3", "g7", "cc");
122 /* Fold a partial checksum without adding pseudo headers. */
123 extern __inline__
unsigned short csum_fold(unsigned int sum
)
127 __asm__
__volatile__("
132 " : "=&r" (sum
), "=r" (tmp
)
133 : "0" (sum
), "1" (sum
<<16)
135 return (sum
& 0xffff);
138 extern __inline__
unsigned long csum_tcpudp_nofold(unsigned long saddr
,
141 unsigned short proto
,
144 __asm__
__volatile__("
149 " : "=r" (sum
), "=r" (saddr
)
150 : "r" (daddr
), "r" ((proto
<<16)+len
), "0" (sum
), "1" (saddr
)
156 * computes the checksum of the TCP/UDP pseudo-header
157 * returns a 16-bit checksum, already complemented
159 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr
,
162 unsigned short proto
,
165 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
168 #define _HAVE_ARCH_IPV6_CSUM
170 static __inline__
unsigned short int csum_ipv6_magic(struct in6_addr
*saddr
,
171 struct in6_addr
*daddr
,
173 unsigned short proto
,
176 __asm__
__volatile__ ("
178 addccc %5, %%g7, %%g7
179 lduw [%2 + 0x0c], %%g2
180 lduw [%2 + 0x08], %%g3
181 addccc %%g2, %%g7, %%g7
182 lduw [%2 + 0x04], %%g2
183 addccc %%g3, %%g7, %%g7
184 lduw [%2 + 0x00], %%g3
185 addccc %%g2, %%g7, %%g7
186 lduw [%1 + 0x0c], %%g2
187 addccc %%g3, %%g7, %%g7
188 lduw [%1 + 0x08], %%g3
189 addccc %%g2, %%g7, %%g7
190 lduw [%1 + 0x04], %%g2
191 addccc %%g3, %%g7, %%g7
192 lduw [%1 + 0x00], %%g3
193 addccc %%g2, %%g7, %%g7
194 addccc %%g3, %%g7, %0
197 : "r" (saddr
), "r" (daddr
), "r"(htonl((__u32
) (len
))),
198 "r"(htonl(proto
)), "r"(sum
)
199 : "g2", "g3", "g7", "cc");
201 return csum_fold(sum
);
204 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
205 extern __inline__
unsigned short ip_compute_csum(unsigned char * buff
, int len
)
207 return csum_fold(csum_partial(buff
, len
, 0));
210 #endif /* !(__SPARC64_CHECKSUM_H) */