xref: /openbmc/linux/arch/parisc/include/asm/checksum.h (revision 82df5b73)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PARISC_CHECKSUM_H
3 #define _PARISC_CHECKSUM_H
4 
5 #include <linux/in6.h>
6 
7 /*
8  * computes the checksum of a memory block at buff, length len,
9  * and adds in "sum" (32-bit)
10  *
11  * returns a 32-bit number suitable for feeding into itself
12  * or csum_tcpudp_magic
13  *
14  * this function must be called with even lengths, except
15  * for the last fragment, which may be odd
16  *
17  * it's best to have buff aligned on a 32-bit boundary
18  */
19 extern __wsum csum_partial(const void *, int, __wsum);
20 
21 /*
22  * The same as csum_partial, but copies from src while it checksums.
23  *
24  * Here even more important to align src and dst on a 32-bit (or even
25  * better 64-bit) boundary
26  */
27 extern __wsum csum_partial_copy_nocheck(const void *, void *, int, __wsum);
28 
29 /*
30  *	Optimized for IP headers, which always checksum on 4 octet boundaries.
31  *
32  *	Written by Randolph Chung <tausq@debian.org>, and then mucked with by
33  *	LaMont Jones <lamont@debian.org>
34  */
35 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
36 {
37 	unsigned int sum;
38 	unsigned long t0, t1, t2;
39 
40 	__asm__ __volatile__ (
41 "	ldws,ma		4(%1), %0\n"
42 "	addib,<=	-4, %2, 2f\n"
43 "\n"
44 "	ldws		4(%1), %4\n"
45 "	ldws		8(%1), %5\n"
46 "	add		%0, %4, %0\n"
47 "	ldws,ma		12(%1), %3\n"
48 "	addc		%0, %5, %0\n"
49 "	addc		%0, %3, %0\n"
50 "1:	ldws,ma		4(%1), %3\n"
51 "	addib,<		0, %2, 1b\n"
52 "	addc		%0, %3, %0\n"
53 "\n"
54 "	extru		%0, 31, 16, %4\n"
55 "	extru		%0, 15, 16, %5\n"
56 "	addc		%4, %5, %0\n"
57 "	extru		%0, 15, 16, %5\n"
58 "	add		%0, %5, %0\n"
59 "	subi		-1, %0, %0\n"
60 "2:\n"
61 	: "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (t0), "=r" (t1), "=r" (t2)
62 	: "1" (iph), "2" (ihl)
63 	: "memory");
64 
65 	return (__force __sum16)sum;
66 }
67 
68 /*
69  *	Fold a partial checksum
70  */
71 static inline __sum16 csum_fold(__wsum csum)
72 {
73 	u32 sum = (__force u32)csum;
74 	/* add the swapped two 16-bit halves of sum,
75 	   a possible carry from adding the two 16-bit halves,
76 	   will carry from the lower half into the upper half,
77 	   giving us the correct sum in the upper half. */
78 	sum += (sum << 16) + (sum >> 16);
79 	return (__force __sum16)(~sum >> 16);
80 }
81 
82 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
83 					__u32 len, __u8 proto,
84 					__wsum sum)
85 {
86 	__asm__(
87 	"	add  %1, %0, %0\n"
88 	"	addc %2, %0, %0\n"
89 	"	addc %3, %0, %0\n"
90 	"	addc %%r0, %0, %0\n"
91 		: "=r" (sum)
92 		: "r" (daddr), "r"(saddr), "r"(proto+len), "0"(sum));
93 	return sum;
94 }
95 
96 /*
97  * computes the checksum of the TCP/UDP pseudo-header
98  * returns a 16-bit checksum, already complemented
99  */
100 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
101 					__u32 len, __u8 proto,
102 					__wsum sum)
103 {
104 	return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
105 }
106 
107 /*
108  * this routine is used for miscellaneous IP-like checksums, mainly
109  * in icmp.c
110  */
111 static inline __sum16 ip_compute_csum(const void *buf, int len)
112 {
113 	 return csum_fold (csum_partial(buf, len, 0));
114 }
115 
116 
117 #define _HAVE_ARCH_IPV6_CSUM
118 static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
119 					  const struct in6_addr *daddr,
120 					  __u32 len, __u8 proto,
121 					  __wsum sum)
122 {
123 	unsigned long t0, t1, t2, t3;
124 
125 	len += proto;	/* add 16-bit proto + len */
126 
127 	__asm__ __volatile__ (
128 
129 #if BITS_PER_LONG > 32
130 
131 	/*
132 	** We can execute two loads and two adds per cycle on PA 8000.
133 	** But add insn's get serialized waiting for the carry bit.
134 	** Try to keep 4 registers with "live" values ahead of the ALU.
135 	*/
136 
137 "	ldd,ma		8(%1), %4\n"	/* get 1st saddr word */
138 "	ldd,ma		8(%2), %5\n"	/* get 1st daddr word */
139 "	add		%4, %0, %0\n"
140 "	ldd,ma		8(%1), %6\n"	/* 2nd saddr */
141 "	ldd,ma		8(%2), %7\n"	/* 2nd daddr */
142 "	add,dc		%5, %0, %0\n"
143 "	add,dc		%6, %0, %0\n"
144 "	add,dc		%7, %0, %0\n"
145 "	add,dc		%3, %0, %0\n"  /* fold in proto+len | carry bit */
146 "	extrd,u		%0, 31, 32, %4\n"/* copy upper half down */
147 "	depdi		0, 31, 32, %0\n"/* clear upper half */
148 "	add		%4, %0, %0\n"	/* fold into 32-bits */
149 "	addc		0, %0, %0\n"	/* add carry */
150 
151 #else
152 
153 	/*
154 	** For PA 1.x, the insn order doesn't matter as much.
155 	** Insn stream is serialized on the carry bit here too.
156 	** result from the previous operation (eg r0 + x)
157 	*/
158 "	ldw,ma		4(%1), %4\n"	/* get 1st saddr word */
159 "	ldw,ma		4(%2), %5\n"	/* get 1st daddr word */
160 "	add		%4, %0, %0\n"
161 "	ldw,ma		4(%1), %6\n"	/* 2nd saddr */
162 "	addc		%5, %0, %0\n"
163 "	ldw,ma		4(%2), %7\n"	/* 2nd daddr */
164 "	addc		%6, %0, %0\n"
165 "	ldw,ma		4(%1), %4\n"	/* 3rd saddr */
166 "	addc		%7, %0, %0\n"
167 "	ldw,ma		4(%2), %5\n"	/* 3rd daddr */
168 "	addc		%4, %0, %0\n"
169 "	ldw,ma		4(%1), %6\n"	/* 4th saddr */
170 "	addc		%5, %0, %0\n"
171 "	ldw,ma		4(%2), %7\n"	/* 4th daddr */
172 "	addc		%6, %0, %0\n"
173 "	addc		%7, %0, %0\n"
174 "	addc		%3, %0, %0\n"	/* fold in proto+len, catch carry */
175 
176 #endif
177 	: "=r" (sum), "=r" (saddr), "=r" (daddr), "=r" (len),
178 	  "=r" (t0), "=r" (t1), "=r" (t2), "=r" (t3)
179 	: "0" (sum), "1" (saddr), "2" (daddr), "3" (len)
180 	: "memory");
181 	return csum_fold(sum);
182 }
183 
184 /*
185  *	Copy and checksum to user
186  */
187 #define HAVE_CSUM_COPY_USER
188 static __inline__ __wsum csum_and_copy_to_user(const void *src,
189 						      void __user *dst,
190 						      int len, __wsum sum,
191 						      int *err_ptr)
192 {
193 	/* code stolen from include/asm-mips64 */
194 	sum = csum_partial(src, len, sum);
195 
196 	if (copy_to_user(dst, src, len)) {
197 		*err_ptr = -EFAULT;
198 		return (__force __wsum)-1;
199 	}
200 
201 	return sum;
202 }
203 
204 #endif
205 
206