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
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 <linux/in6.h>
20 #include <asm/uaccess.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(const unsigned char * buff, int len, unsigned int sum);
35 /* the same as csum_partial, but copies from user space while it
38 * here even more important to align src and dst on a 32-bit (or even
39 * better 64-bit) boundary
41 extern unsigned int csum_partial_copy_sparc64(const unsigned char *src, unsigned char *dst,
42 int len, unsigned int sum);
44 static inline unsigned int
45 csum_partial_copy_nocheck (const unsigned char *src, unsigned char *dst, int len,
49 unsigned char cur_ds = get_thread_current_ds();
50 __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "i" (ASI_P));
51 ret = csum_partial_copy_sparc64(src, dst, len, sum);
52 __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "r" (cur_ds));
56 static inline unsigned int
57 csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst, int len,
58 unsigned int sum, int *err)
60 __asm__ __volatile__ ("stx %0, [%%sp + 0x7ff + 128]"
62 return csum_partial_copy_sparc64((__force const char *) src,
67 * Copy and checksum to user
69 #define HAVE_CSUM_COPY_USER
70 extern unsigned int csum_partial_copy_user_sparc64(const unsigned char *src, unsigned char __user *dst,
71 int len, unsigned int sum);
73 static inline unsigned int
74 csum_and_copy_to_user(const unsigned char *src, unsigned char __user *dst, int len,
75 unsigned int sum, int *err)
77 __asm__ __volatile__ ("stx %0, [%%sp + 0x7ff + 128]"
79 return csum_partial_copy_user_sparc64(src, dst, len, sum);
82 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
83 * the majority of the time.
85 extern unsigned short ip_fast_csum(__const__ unsigned char *iph,
88 /* Fold a partial checksum without adding pseudo headers. */
89 static inline unsigned short csum_fold(unsigned int sum)
96 " addc %1, %%g0, %1\n"
97 " xnor %%g0, %1, %0\n"
98 : "=&r" (sum), "=r" (tmp)
99 : "0" (sum), "1" (sum<<16)
101 return (sum & 0xffff);
104 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
107 unsigned short proto,
110 __asm__ __volatile__(
111 " addcc %1, %0, %0\n"
112 " addccc %2, %0, %0\n"
113 " addccc %3, %0, %0\n"
114 " addc %0, %%g0, %0\n"
115 : "=r" (sum), "=r" (saddr)
116 : "r" (daddr), "r" ((proto<<16)+len), "0" (sum), "1" (saddr)
122 * computes the checksum of the TCP/UDP pseudo-header
123 * returns a 16-bit checksum, already complemented
125 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
128 unsigned short proto,
131 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
134 #define _HAVE_ARCH_IPV6_CSUM
136 static inline unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
137 struct in6_addr *daddr,
139 unsigned short proto,
142 __asm__ __volatile__ (
143 " addcc %3, %4, %%g7\n"
144 " addccc %5, %%g7, %%g7\n"
145 " lduw [%2 + 0x0c], %%g2\n"
146 " lduw [%2 + 0x08], %%g3\n"
147 " addccc %%g2, %%g7, %%g7\n"
148 " lduw [%2 + 0x04], %%g2\n"
149 " addccc %%g3, %%g7, %%g7\n"
150 " lduw [%2 + 0x00], %%g3\n"
151 " addccc %%g2, %%g7, %%g7\n"
152 " lduw [%1 + 0x0c], %%g2\n"
153 " addccc %%g3, %%g7, %%g7\n"
154 " lduw [%1 + 0x08], %%g3\n"
155 " addccc %%g2, %%g7, %%g7\n"
156 " lduw [%1 + 0x04], %%g2\n"
157 " addccc %%g3, %%g7, %%g7\n"
158 " lduw [%1 + 0x00], %%g3\n"
159 " addccc %%g2, %%g7, %%g7\n"
160 " addccc %%g3, %%g7, %0\n"
163 : "r" (saddr), "r" (daddr), "r"(htonl(len)),
164 "r"(htonl(proto)), "r"(sum)
165 : "g2", "g3", "g7", "cc");
167 return csum_fold(sum);
170 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
171 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
173 return csum_fold(csum_partial(buff, len, 0));
176 #endif /* !(__SPARC64_CHECKSUM_H) */