blob: 9bfdc41629ecb8478135754cd89659f358fcc2c0 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_CHECKSUM_64_H
2#define _ASM_X86_CHECKSUM_64_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Joe Perches3d3c6e12008-03-23 01:01:50 -07004/*
5 * Checksums for x86-64
6 * Copyright 2002 by Andi Kleen, SuSE Labs
Dave Jones99122a32008-01-30 13:30:28 +01007 * with some code from asm-x86/checksum.h
Joe Perches3d3c6e12008-03-23 01:01:50 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/compiler.h>
11#include <asm/uaccess.h>
12#include <asm/byteorder.h>
13
Joe Perches3d3c6e12008-03-23 01:01:50 -070014/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * csum_fold - Fold and invert a 32bit checksum.
16 * sum: 32bit unfolded sum
Joe Perches3d3c6e12008-03-23 01:01:50 -070017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Fold a 32bit running checksum to 16bit and invert it. This is usually
19 * the last step before putting a checksum into a packet.
20 * Make sure not to mix with 64bit checksums.
21 */
Al Viroa4f89fb2006-11-14 21:20:08 -080022static inline __sum16 csum_fold(__wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Joe Perches3d3c6e12008-03-23 01:01:50 -070024 asm(" addl %1,%0\n"
25 " adcl $0xffff,%0"
26 : "=r" (sum)
27 : "r" ((__force u32)sum << 16),
28 "0" ((__force u32)sum & 0xffff0000));
Al Viroa4f89fb2006-11-14 21:20:08 -080029 return (__force __sum16)(~(__force u32)sum >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
32/*
33 * This is a version of ip_compute_csum() optimized for IP headers,
34 * which always checksum on 4 octet boundaries.
35 *
36 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
37 * Arnt Gulbrandsen.
38 */
39
40/**
41 * ip_fast_csum - Compute the IPv4 header checksum efficiently.
42 * iph: ipv4 header
43 * ihl: length of header / 4
Joe Perches3d3c6e12008-03-23 01:01:50 -070044 */
Al Viroa4f89fb2006-11-14 21:20:08 -080045static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 unsigned int sum;
48
Joe Perches3d3c6e12008-03-23 01:01:50 -070049 asm(" movl (%1), %0\n"
50 " subl $4, %2\n"
51 " jbe 2f\n"
52 " addl 4(%1), %0\n"
53 " adcl 8(%1), %0\n"
54 " adcl 12(%1), %0\n"
55 "1: adcl 16(%1), %0\n"
56 " lea 4(%1), %1\n"
57 " decl %2\n"
58 " jne 1b\n"
59 " adcl $0, %0\n"
60 " movl %0, %2\n"
61 " shrl $16, %0\n"
62 " addw %w2, %w0\n"
63 " adcl $0, %0\n"
64 " notl %0\n"
65 "2:"
Thomas Graf2c656492005-08-20 17:24:25 -070066 /* Since the input registers which are loaded with iph and ihl
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 are modified, we must also specify them as outputs, or gcc
68 will assume they contain their original values. */
Joe Perches3d3c6e12008-03-23 01:01:50 -070069 : "=r" (sum), "=r" (iph), "=r" (ihl)
70 : "1" (iph), "2" (ihl)
71 : "memory");
Al Viroa4f89fb2006-11-14 21:20:08 -080072 return (__force __sum16)sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Joe Perches3d3c6e12008-03-23 01:01:50 -070075/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
77 * @saddr: source address
78 * @daddr: destination address
79 * @len: length of packet
80 * @proto: ip protocol of packet
Joe Perches3d3c6e12008-03-23 01:01:50 -070081 * @sum: initial sum to be added in (32bit unfolded)
82 *
83 * Returns the pseudo header checksum the input data. Result is
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * 32bit unfolded.
85 */
Al Viroa4f89fb2006-11-14 21:20:08 -080086static inline __wsum
87csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
88 unsigned short proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 asm(" addl %1, %0\n"
91 " adcl %2, %0\n"
92 " adcl %3, %0\n"
93 " adcl $0, %0\n"
Joe Perches3d3c6e12008-03-23 01:01:50 -070094 : "=r" (sum)
Al Viroa4f89fb2006-11-14 21:20:08 -080095 : "g" (daddr), "g" (saddr),
96 "g" ((len + proto)<<8), "0" (sum));
Joe Perches3d3c6e12008-03-23 01:01:50 -070097 return sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100
Joe Perches3d3c6e12008-03-23 01:01:50 -0700101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * csum_tcpup_magic - Compute an IPv4 pseudo header checksum.
103 * @saddr: source address
104 * @daddr: destination address
105 * @len: length of packet
106 * @proto: ip protocol of packet
Joe Perches3d3c6e12008-03-23 01:01:50 -0700107 * @sum: initial sum to be added in (32bit unfolded)
108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Returns the 16bit pseudo header checksum the input data already
110 * complemented and ready to be filled in.
111 */
Joe Perches3d3c6e12008-03-23 01:01:50 -0700112static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
113 unsigned short len,
114 unsigned short proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Joe Perches3d3c6e12008-03-23 01:01:50 -0700116 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Joe Perches3d3c6e12008-03-23 01:01:50 -0700119/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * csum_partial - Compute an internet checksum.
121 * @buff: buffer to be checksummed
122 * @len: length of buffer.
123 * @sum: initial sum to be added in (32bit unfolded)
124 *
125 * Returns the 32bit unfolded internet checksum of the buffer.
126 * Before filling it in it needs to be csum_fold()'ed.
127 * buff should be aligned to a 64bit boundary if possible.
Joe Perches3d3c6e12008-03-23 01:01:50 -0700128 */
Al Viroa4f89fb2006-11-14 21:20:08 -0800129extern __wsum csum_partial(const void *buff, int len, __wsum sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1
132#define HAVE_CSUM_COPY_USER 1
133
134
135/* Do not call this directly. Use the wrappers below */
Al Viroa4f89fb2006-11-14 21:20:08 -0800136extern __wsum csum_partial_copy_generic(const void *src, const void *dst,
Joe Perches3d3c6e12008-03-23 01:01:50 -0700137 int len, __wsum sum,
138 int *src_err_ptr, int *dst_err_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140
Al Viroa4f89fb2006-11-14 21:20:08 -0800141extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
Joe Perches3d3c6e12008-03-23 01:01:50 -0700142 int len, __wsum isum, int *errp);
Al Viroa4f89fb2006-11-14 21:20:08 -0800143extern __wsum csum_partial_copy_to_user(const void *src, void __user *dst,
Joe Perches3d3c6e12008-03-23 01:01:50 -0700144 int len, __wsum isum, int *errp);
145extern __wsum csum_partial_copy_nocheck(const void *src, void *dst,
146 int len, __wsum sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* Old names. To be removed. */
149#define csum_and_copy_to_user csum_partial_copy_to_user
150#define csum_and_copy_from_user csum_partial_copy_from_user
151
Joe Perches3d3c6e12008-03-23 01:01:50 -0700152/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * ip_compute_csum - Compute an 16bit IP checksum.
154 * @buff: buffer address.
155 * @len: length of buffer.
156 *
157 * Returns the 16bit folded/inverted checksum of the passed buffer.
158 * Ready to fill in.
159 */
Al Viroa4f89fb2006-11-14 21:20:08 -0800160extern __sum16 ip_compute_csum(const void *buff, int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/**
163 * csum_ipv6_magic - Compute checksum of an IPv6 pseudo header.
164 * @saddr: source address
165 * @daddr: destination address
166 * @len: length of packet
167 * @proto: protocol of packet
168 * @sum: initial sum (32bit unfolded) to be added in
169 *
Joe Perches3d3c6e12008-03-23 01:01:50 -0700170 * Computes an IPv6 pseudo header checksum. This sum is added the checksum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * into UDP/TCP packets and contains some link layer information.
172 * Returns the unfolded 32bit checksum.
173 */
174
175struct in6_addr;
176
177#define _HAVE_ARCH_IPV6_CSUM 1
Al Viroa4f89fb2006-11-14 21:20:08 -0800178extern __sum16
179csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
180 __u32 len, unsigned short proto, __wsum sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static inline unsigned add32_with_carry(unsigned a, unsigned b)
183{
184 asm("addl %2,%0\n\t"
Joe Perches3d3c6e12008-03-23 01:01:50 -0700185 "adcl $0,%0"
186 : "=r" (a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 : "0" (a), "r" (b));
188 return a;
189}
190
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700191#endif /* _ASM_X86_CHECKSUM_64_H */