xref: /openbmc/linux/arch/nios2/include/asm/checksum.h (revision 26cfd12b)
1 /*
2  * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
3  * Copyright (C) 2004 Microtronix Datacom Ltd.
4  *
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License. See the file "COPYING" in the main directory of this archive
7  * for more details.
8  */
9 
10 #ifndef _ASM_NIOS_CHECKSUM_H
11 #define _ASM_NIOS_CHECKSUM_H
12 
13 /* Take these from lib/checksum.c */
14 extern __wsum csum_partial(const void *buff, int len, __wsum sum);
15 extern __wsum csum_partial_copy(const void *src, void *dst, int len,
16 				__wsum sum);
17 #define csum_partial_copy_nocheck(src, dst, len, sum)	\
18 	csum_partial_copy((src), (dst), (len), (sum))
19 
20 extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
21 extern __sum16 ip_compute_csum(const void *buff, int len);
22 
23 /*
24  * Fold a partial checksum
25  */
26 static inline __sum16 csum_fold(__wsum sum)
27 {
28 	__asm__ __volatile__(
29 		"add	%0, %1, %0\n"
30 		"cmpltu	r8, %0, %1\n"
31 		"srli	%0, %0, 16\n"
32 		"add	%0, %0, r8\n"
33 		"nor	%0, %0, %0\n"
34 		: "=r" (sum)
35 		: "r" (sum << 16), "0" (sum)
36 		: "r8");
37 	return (__force __sum16) sum;
38 }
39 
40 /*
41  * computes the checksum of the TCP/UDP pseudo-header
42  * returns a 16-bit checksum, already complemented
43  */
44 #define csum_tcpudp_nofold csum_tcpudp_nofold
45 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
46 					__u32 len, __u8 proto,
47 					__wsum sum)
48 {
49 	__asm__ __volatile__(
50 		"add	%0, %1, %0\n"
51 		"cmpltu	r8, %0, %1\n"
52 		"add	%0, %0, r8\n"	/* add carry */
53 		"add	%0, %2, %0\n"
54 		"cmpltu	r8, %0, %2\n"
55 		"add	%0, %0, r8\n"	/* add carry */
56 		"add	%0, %3, %0\n"
57 		"cmpltu	r8, %0, %3\n"
58 		"add	%0, %0, r8\n"	/* add carry */
59 		: "=r" (sum), "=r" (saddr)
60 		: "r" (daddr), "r" ((len + proto) << 8),
61 		  "0" (sum),
62 		  "1" (saddr)
63 		: "r8");
64 
65 	return sum;
66 }
67 
68 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
69 					__u32 len, __u8 proto,
70 					__wsum sum)
71 {
72 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
73 }
74 
75 #endif /* _ASM_NIOS_CHECKSUM_H */
76