xref: /linux/arch/microblaze/include/asm/checksum.h (revision e27ecdd94d81e5bc3d1f68591701db5adb342f0d)
1 /*
2  * Copyright (C) 2008 Michal Simek <monstr@monstr.eu>
3  * Copyright (C) 2006 Atmark Techno, Inc.
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_MICROBLAZE_CHECKSUM_H
11 #define _ASM_MICROBLAZE_CHECKSUM_H
12 
13 #include <linux/in6.h>
14 
15 /*
16  * computes the checksum of the TCP/UDP pseudo-header
17  * returns a 16-bit checksum, already complemented
18  */
19 static inline __wsum
20 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
21 		   unsigned short proto, __wsum sum)
22 {
23 	__asm__("add %0, %0, %1\n\t"
24 		"addc %0, %0, %2\n\t"
25 		"addc %0, %0, %3\n\t"
26 		"addc %0, %0, r0\n\t"
27 		: "+&d" (sum)
28 		: "d" (saddr), "d" (daddr), "d" (len + proto));
29 
30 	return sum;
31 }
32 
33 /*
34  * computes the checksum of a memory block at buff, length len,
35  * and adds in "sum" (32-bit)
36  *
37  * returns a 32-bit number suitable for feeding into itself
38  * or csum_tcpudp_magic
39  *
40  * this function must be called with even lengths, except
41  * for the last fragment, which may be odd
42  *
43  * it's best to have buff aligned on a 32-bit boundary
44  */
45 extern __wsum csum_partial(const void *buff, int len, __wsum sum);
46 
47 /*
48  * the same as csum_partial, but copies from src while it
49  * checksums
50  *
51  * here even more important to align src and dst on a 32-bit (or even
52  * better 64-bit) boundary
53  */
54 extern __wsum csum_partial_copy(const void *src, void *dst, int len,
55 								__wsum sum);
56 
57 /*
58  * the same as csum_partial_copy, but copies from user space.
59  *
60  * here even more important to align src and dst on a 32-bit (or even
61  * better 64-bit) boundary
62  */
63 extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
64 					int len, __wsum sum, int *csum_err);
65 
66 #define csum_partial_copy_nocheck(src, dst, len, sum)	\
67 	csum_partial_copy((src), (dst), (len), (sum))
68 
69 /*
70  *	This is a version of ip_compute_csum() optimized for IP headers,
71  *	which always checksum on 4 octet boundaries.
72  *
73  */
74 extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
75 
76 /*
77  *	Fold a partial checksum
78  */
79 static inline __sum16 csum_fold(__wsum csum)
80 {
81 	u32 sum = (__force u32)csum;
82 	sum = (sum & 0xffff) + (sum >> 16);
83 	sum = (sum & 0xffff) + (sum >> 16);
84 	return (__force __sum16)~sum;
85 }
86 
87 static inline __sum16
88 csum_tcpudp_magic(__be32 saddr, __be32 daddr, unsigned short len,
89 		  unsigned short proto, __wsum sum)
90 {
91 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
92 }
93 
94 /*
95  * this routine is used for miscellaneous IP-like checksums, mainly
96  * in icmp.c
97  */
98 extern __sum16 ip_compute_csum(const void *buff, int len);
99 
100 #endif /* _ASM_MICROBLAZE_CHECKSUM_H */
101