blob: 9815ab1fc8aad92a49547d296b4da9dafb964f20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _PARISC_CHECKSUM_H
2#define _PARISC_CHECKSUM_H
3
4#include <linux/in6.h>
5
6/*
7 * computes the checksum of a memory block at buff, length len,
8 * and adds in "sum" (32-bit)
9 *
10 * returns a 32-bit number suitable for feeding into itself
11 * or csum_tcpudp_magic
12 *
13 * this function must be called with even lengths, except
14 * for the last fragment, which may be odd
15 *
16 * it's best to have buff aligned on a 32-bit boundary
17 */
Al Viro7814e4b2006-11-14 21:18:39 -080018extern __wsum csum_partial(const void *, int, __wsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
21 * The same as csum_partial, but copies from src while it checksums.
22 *
23 * Here even more important to align src and dst on a 32-bit (or even
24 * better 64-bit) boundary
25 */
Al Viro7814e4b2006-11-14 21:18:39 -080026extern __wsum csum_partial_copy_nocheck(const void *, void *, int, __wsum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * this is a new version of the above that records errors it finds in *errp,
30 * but continues and zeros the rest of the buffer.
31 */
Al Viro7814e4b2006-11-14 21:18:39 -080032extern __wsum csum_partial_copy_from_user(const void __user *src,
33 void *dst, int len, __wsum sum, int *errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
36 * Optimized for IP headers, which always checksum on 4 octet boundaries.
37 *
38 * Written by Randolph Chung <tausq@debian.org>, and then mucked with by
39 * LaMont Jones <lamont@debian.org>
40 */
Al Viro7814e4b2006-11-14 21:18:39 -080041static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
42{
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int sum;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 __asm__ __volatile__ (
46" ldws,ma 4(%1), %0\n"
47" addib,<= -4, %2, 2f\n"
48"\n"
49" ldws 4(%1), %%r20\n"
50" ldws 8(%1), %%r21\n"
51" add %0, %%r20, %0\n"
52" ldws,ma 12(%1), %%r19\n"
53" addc %0, %%r21, %0\n"
54" addc %0, %%r19, %0\n"
55"1: ldws,ma 4(%1), %%r19\n"
56" addib,< 0, %2, 1b\n"
57" addc %0, %%r19, %0\n"
58"\n"
59" extru %0, 31, 16, %%r20\n"
60" extru %0, 15, 16, %%r21\n"
61" addc %%r20, %%r21, %0\n"
62" extru %0, 15, 16, %%r21\n"
63" add %0, %%r21, %0\n"
64" subi -1, %0, %0\n"
65"2:\n"
66 : "=r" (sum), "=r" (iph), "=r" (ihl)
67 : "1" (iph), "2" (ihl)
Kyle McMartine374d172008-05-31 12:15:42 -040068 : "r19", "r20", "r21", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Al Viro7814e4b2006-11-14 21:18:39 -080070 return (__force __sum16)sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73/*
74 * Fold a partial checksum
75 */
Al Viro7814e4b2006-11-14 21:18:39 -080076static inline __sum16 csum_fold(__wsum csum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Al Viro7814e4b2006-11-14 21:18:39 -080078 u32 sum = (__force u32)csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* add the swapped two 16-bit halves of sum,
80 a possible carry from adding the two 16-bit halves,
81 will carry from the lower half into the upper half,
82 giving us the correct sum in the upper half. */
83 sum += (sum << 16) + (sum >> 16);
Al Viro7814e4b2006-11-14 21:18:39 -080084 return (__force __sum16)(~sum >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Al Viro7814e4b2006-11-14 21:18:39 -080087static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Alexander Duyck01cfbad2016-03-11 14:05:34 -080088 __u32 len, __u8 proto,
89 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 __asm__(
92 " add %1, %0, %0\n"
93 " addc %2, %0, %0\n"
94 " addc %3, %0, %0\n"
95 " addc %%r0, %0, %0\n"
96 : "=r" (sum)
Al Viro7814e4b2006-11-14 21:18:39 -080097 : "r" (daddr), "r"(saddr), "r"(proto+len), "0"(sum));
98 return sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/*
102 * computes the checksum of the TCP/UDP pseudo-header
103 * returns a 16-bit checksum, already complemented
104 */
Al Viro7814e4b2006-11-14 21:18:39 -0800105static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Alexander Duyck01cfbad2016-03-11 14:05:34 -0800106 __u32 len, __u8 proto,
107 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
110}
111
112/*
113 * this routine is used for miscellaneous IP-like checksums, mainly
114 * in icmp.c
115 */
Al Viro7814e4b2006-11-14 21:18:39 -0800116static inline __sum16 ip_compute_csum(const void *buf, int len)
117{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return csum_fold (csum_partial(buf, len, 0));
119}
120
121
122#define _HAVE_ARCH_IPV6_CSUM
Al Viro7814e4b2006-11-14 21:18:39 -0800123static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
124 const struct in6_addr *daddr,
125 __u32 len, unsigned short proto,
126 __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 __asm__ __volatile__ (
129
130#if BITS_PER_LONG > 32
131
132 /*
133 ** We can execute two loads and two adds per cycle on PA 8000.
134 ** But add insn's get serialized waiting for the carry bit.
135 ** Try to keep 4 registers with "live" values ahead of the ALU.
136 */
137
138" ldd,ma 8(%1), %%r19\n" /* get 1st saddr word */
139" ldd,ma 8(%2), %%r20\n" /* get 1st daddr word */
140" add %8, %3, %3\n"/* add 16-bit proto + len */
141" add %%r19, %0, %0\n"
142" ldd,ma 8(%1), %%r21\n" /* 2cd saddr */
143" ldd,ma 8(%2), %%r22\n" /* 2cd daddr */
144" add,dc %%r20, %0, %0\n"
145" add,dc %%r21, %0, %0\n"
146" add,dc %%r22, %0, %0\n"
147" add,dc %3, %0, %0\n" /* fold in proto+len | carry bit */
148" extrd,u %0, 31, 32, %%r19\n" /* copy upper half down */
149" depdi 0, 31, 32, %0\n" /* clear upper half */
150" add %%r19, %0, %0\n" /* fold into 32-bits */
151" addc 0, %0, %0\n" /* add carry */
152
153#else
154
155 /*
156 ** For PA 1.x, the insn order doesn't matter as much.
157 ** Insn stream is serialized on the carry bit here too.
158 ** result from the previous operation (eg r0 + x)
159 */
160
161" ldw,ma 4(%1), %%r19\n" /* get 1st saddr word */
162" ldw,ma 4(%2), %%r20\n" /* get 1st daddr word */
163" add %8, %3, %3\n" /* add 16-bit proto + len */
164" add %%r19, %0, %0\n"
165" ldw,ma 4(%1), %%r21\n" /* 2cd saddr */
166" addc %%r20, %0, %0\n"
167" ldw,ma 4(%2), %%r22\n" /* 2cd daddr */
168" addc %%r21, %0, %0\n"
169" ldw,ma 4(%1), %%r19\n" /* 3rd saddr */
170" addc %%r22, %0, %0\n"
171" ldw,ma 4(%2), %%r20\n" /* 3rd daddr */
172" addc %%r19, %0, %0\n"
173" ldw,ma 4(%1), %%r21\n" /* 4th saddr */
174" addc %%r20, %0, %0\n"
175" ldw,ma 4(%2), %%r22\n" /* 4th daddr */
176" addc %%r21, %0, %0\n"
177" addc %%r22, %0, %0\n"
178" addc %3, %0, %0\n" /* fold in proto+len, catch carry */
179
180#endif
181 : "=r" (sum), "=r" (saddr), "=r" (daddr), "=r" (len)
182 : "0" (sum), "1" (saddr), "2" (daddr), "3" (len), "r" (proto)
Kyle McMartin5fbf6632008-12-08 05:15:51 +0000183 : "r19", "r20", "r21", "r22", "memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return csum_fold(sum);
185}
186
187/*
188 * Copy and checksum to user
189 */
190#define HAVE_CSUM_COPY_USER
Al Viro7814e4b2006-11-14 21:18:39 -0800191static __inline__ __wsum csum_and_copy_to_user(const void *src,
192 void __user *dst,
193 int len, __wsum sum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int *err_ptr)
195{
196 /* code stolen from include/asm-mips64 */
197 sum = csum_partial(src, len, sum);
198
199 if (copy_to_user(dst, src, len)) {
200 *err_ptr = -EFAULT;
Al Viro7814e4b2006-11-14 21:18:39 -0800201 return (__force __wsum)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 return sum;
205}
206
207#endif
208