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 __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
16 extern __sum16 ip_compute_csum(const void *buff, int len);
17 
18 /*
19  * Fold a partial checksum
20  */
csum_fold(__wsum sum)21 static inline __sum16 csum_fold(__wsum sum)
22 {
23 	__asm__ __volatile__(
24 		"add	%0, %1, %0\n"
25 		"cmpltu	r8, %0, %1\n"
26 		"srli	%0, %0, 16\n"
27 		"add	%0, %0, r8\n"
28 		"nor	%0, %0, %0\n"
29 		: "=r" (sum)
30 		: "r" (sum << 16), "0" (sum)
31 		: "r8");
32 	return (__force __sum16) sum;
33 }
34 
35 /*
36  * computes the checksum of the TCP/UDP pseudo-header
37  * returns a 16-bit checksum, already complemented
38  */
39 #define csum_tcpudp_nofold csum_tcpudp_nofold
csum_tcpudp_nofold(__be32 saddr,__be32 daddr,__u32 len,__u8 proto,__wsum sum)40 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
41 					__u32 len, __u8 proto,
42 					__wsum sum)
43 {
44 	__asm__ __volatile__(
45 		"add	%0, %1, %0\n"
46 		"cmpltu	r8, %0, %1\n"
47 		"add	%0, %0, r8\n"	/* add carry */
48 		"add	%0, %2, %0\n"
49 		"cmpltu	r8, %0, %2\n"
50 		"add	%0, %0, r8\n"	/* add carry */
51 		"add	%0, %3, %0\n"
52 		"cmpltu	r8, %0, %3\n"
53 		"add	%0, %0, r8\n"	/* add carry */
54 		: "=r" (sum), "=r" (saddr)
55 		: "r" (daddr), "r" ((len + proto) << 8),
56 		  "0" (sum),
57 		  "1" (saddr)
58 		: "r8");
59 
60 	return sum;
61 }
62 
csum_tcpudp_magic(__be32 saddr,__be32 daddr,__u32 len,__u8 proto,__wsum sum)63 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
64 					__u32 len, __u8 proto,
65 					__wsum sum)
66 {
67 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
68 }
69 
70 #endif /* _ASM_NIOS_CHECKSUM_H */
71