1 /* $Id: checksum.h,v 1.17.2.1 2002/03/01 11:40:54 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 extern __inline__ unsigned int
csum_partial_copy_nocheck(const char * src,char * dst,int len,unsigned int sum)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 = current->thread.current_ds.seg;
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 extern __inline__ unsigned int
csum_partial_copy_from_user(const char * src,char * dst,int len,unsigned int sum,int * err)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 extern __inline__ unsigned int
csum_and_copy_to_user(const char * src,char * dst,int len,unsigned int sum,int * err)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  */
ip_fast_csum(__const__ unsigned char * iph,unsigned int ihl)81 extern __inline__ unsigned short ip_fast_csum(__const__ unsigned char *iph,
82 					      unsigned int ihl)
83 {
84 	unsigned short sum;
85 
86 	/* Note: We must read %2 before we touch %0 for the first time,
87 	 *       because GCC can legitimately use the same register for
88 	 *       both operands.
89 	 */
90 	__asm__ __volatile__(
91 "	sub		%2, 4, %%g7		! IEU0\n"
92 "	lduw		[%1 + 0x00], %0		! Load	Group\n"
93 "	lduw		[%1 + 0x04], %%g2	! Load	Group\n"
94 "	lduw		[%1 + 0x08], %%g3	! Load	Group\n"
95 "	addcc		%%g2, %0, %0		! IEU1	1 Load Bubble + Group\n"
96 "	lduw		[%1 + 0x0c], %%g2	! Load\n"
97 "	addccc		%%g3, %0, %0		! Sngle	Group no Bubble\n"
98 "	lduw		[%1 + 0x10], %%g3	! Load	Group\n"
99 "	addccc		%%g2, %0, %0		! Sngle	Group no Bubble\n"
100 "	addc		%0, %%g0, %0		! Sngle Group\n"
101 "1:	addcc		%%g3, %0, %0		! IEU1	Group no Bubble\n"
102 "	add		%1, 4, %1		! IEU0\n"
103 "	addccc		%0, %%g0, %0		! Sngle Group no Bubble\n"
104 "	subcc		%%g7, 1, %%g7		! IEU1	Group\n"
105 "	be,a,pt		%%icc, 2f		! CTI\n"
106 "	 sll		%0, 16, %%g2		! IEU0\n"
107 "	lduw		[%1 + 0x10], %%g3	! Load	Group\n"
108 "	ba,pt		%%xcc, 1b		! CTI\n"
109 "	 nop					! IEU0\n"
110 "2:	addcc		%0, %%g2, %%g2		! IEU1	Group\n"
111 "	srl		%%g2, 16, %0		! IEU0	Group regdep	XXX Scheisse!\n"
112 "	addc		%0, %%g0, %0		! Sngle	Group\n"
113 "	xnor		%%g0, %0, %0		! IEU0	Group\n"
114 "	srl		%0, 0, %0		! IEU0	Group		XXX Scheisse!\n"
115 	: "=r" (sum), "=&r" (iph)
116 	: "r" (ihl), "1" (iph)
117 	: "g2", "g3", "g7", "cc");
118 	return sum;
119 }
120 
121 /* Fold a partial checksum without adding pseudo headers. */
csum_fold(unsigned int sum)122 extern __inline__ unsigned short csum_fold(unsigned int sum)
123 {
124 	unsigned int tmp;
125 
126 	__asm__ __volatile__(
127 "	addcc		%0, %1, %1\n"
128 "	srl		%1, 16, %1\n"
129 "	addc		%1, %%g0, %1\n"
130 "	xnor		%%g0, %1, %0\n"
131 	: "=&r" (sum), "=r" (tmp)
132 	: "0" (sum), "1" (sum<<16)
133 	: "cc");
134 	return (sum & 0xffff);
135 }
136 
csum_tcpudp_nofold(unsigned long saddr,unsigned long daddr,unsigned int len,unsigned short proto,unsigned int sum)137 extern __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
138 						   unsigned long daddr,
139 						   unsigned int len,
140 						   unsigned short proto,
141 						   unsigned int sum)
142 {
143 	__asm__ __volatile__(
144 "	addcc		%1, %0, %0\n"
145 "	addccc		%2, %0, %0\n"
146 "	addccc		%3, %0, %0\n"
147 "	addc		%0, %%g0, %0\n"
148 	: "=r" (sum), "=r" (saddr)
149 	: "r" (daddr), "r" ((proto<<16)+len), "0" (sum), "1" (saddr)
150 	: "cc");
151 	return sum;
152 }
153 
154 /*
155  * computes the checksum of the TCP/UDP pseudo-header
156  * returns a 16-bit checksum, already complemented
157  */
csum_tcpudp_magic(unsigned long saddr,unsigned long daddr,unsigned short len,unsigned short proto,unsigned int sum)158 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
159 						   unsigned long daddr,
160 						   unsigned short len,
161 						   unsigned short proto,
162 						   unsigned int sum)
163 {
164 	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
165 }
166 
167 #define _HAVE_ARCH_IPV6_CSUM
168 
csum_ipv6_magic(struct in6_addr * saddr,struct in6_addr * daddr,__u32 len,unsigned short proto,unsigned int sum)169 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
170 						     struct in6_addr *daddr,
171 						     __u32 len,
172 						     unsigned short proto,
173 						     unsigned int sum)
174 {
175 	__asm__ __volatile__ (
176 "	addcc		%3, %4, %%g7\n"
177 "	addccc		%5, %%g7, %%g7\n"
178 "	lduw		[%2 + 0x0c], %%g2\n"
179 "	lduw		[%2 + 0x08], %%g3\n"
180 "	addccc		%%g2, %%g7, %%g7\n"
181 "	lduw		[%2 + 0x04], %%g2\n"
182 "	addccc		%%g3, %%g7, %%g7\n"
183 "	lduw		[%2 + 0x00], %%g3\n"
184 "	addccc		%%g2, %%g7, %%g7\n"
185 "	lduw		[%1 + 0x0c], %%g2\n"
186 "	addccc		%%g3, %%g7, %%g7\n"
187 "	lduw		[%1 + 0x08], %%g3\n"
188 "	addccc		%%g2, %%g7, %%g7\n"
189 "	lduw		[%1 + 0x04], %%g2\n"
190 "	addccc		%%g3, %%g7, %%g7\n"
191 "	lduw		[%1 + 0x00], %%g3\n"
192 "	addccc		%%g2, %%g7, %%g7\n"
193 "	addccc		%%g3, %%g7, %0\n"
194 "	addc		0, %0, %0\n"
195 	: "=&r" (sum)
196 	: "r" (saddr), "r" (daddr), "r"(htonl(len)),
197 	  "r"(htonl(proto)), "r"(sum)
198 	: "g2", "g3", "g7", "cc");
199 
200 	return csum_fold(sum);
201 }
202 
203 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
ip_compute_csum(unsigned char * buff,int len)204 extern __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len)
205 {
206 	return csum_fold(csum_partial(buff, len, 0));
207 }
208 
209 #endif /* !(__SPARC64_CHECKSUM_H) */
210