1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PARISC_CHECKSUM_H
3 #define _PARISC_CHECKSUM_H
8 * computes the checksum of a memory block at buff, length len,
9 * and adds in "sum" (32-bit)
11 * returns a 32-bit number suitable for feeding into itself
12 * or csum_tcpudp_magic
14 * this function must be called with even lengths, except
15 * for the last fragment, which may be odd
17 * it's best to have buff aligned on a 32-bit boundary
19 extern __wsum
csum_partial(const void *, int, __wsum
);
22 * The same as csum_partial, but copies from src while it checksums.
24 * Here even more important to align src and dst on a 32-bit (or even
25 * better 64-bit) boundary
27 extern __wsum
csum_partial_copy_nocheck(const void *, void *, int, __wsum
);
30 * Optimized for IP headers, which always checksum on 4 octet boundaries.
32 * Written by Randolph Chung <tausq@debian.org>, and then mucked with by
33 * LaMont Jones <lamont@debian.org>
35 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
38 unsigned long t0
, t1
, t2
;
40 __asm__
__volatile__ (
41 " ldws,ma 4(%1), %0\n"
42 " addib,<= -4, %2, 2f\n"
47 " ldws,ma 12(%1), %3\n"
50 "1: ldws,ma 4(%1), %3\n"
51 " addib,< 0, %2, 1b\n"
54 " extru %0, 31, 16, %4\n"
55 " extru %0, 15, 16, %5\n"
57 " extru %0, 15, 16, %5\n"
61 : "=r" (sum
), "=r" (iph
), "=r" (ihl
), "=r" (t0
), "=r" (t1
), "=r" (t2
)
62 : "1" (iph
), "2" (ihl
)
65 return (__force __sum16
)sum
;
69 * Fold a partial checksum
71 static inline __sum16
csum_fold(__wsum csum
)
73 u32 sum
= (__force u32
)csum
;
74 /* add the swapped two 16-bit halves of sum,
75 a possible carry from adding the two 16-bit halves,
76 will carry from the lower half into the upper half,
77 giving us the correct sum in the upper half. */
78 sum
+= (sum
<< 16) + (sum
>> 16);
79 return (__force __sum16
)(~sum
>> 16);
82 static inline __wsum
csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
,
83 __u32 len
, __u8 proto
,
90 " addc %%r0, %0, %0\n"
92 : "r" (daddr
), "r"(saddr
), "r"(proto
+len
), "0"(sum
));
97 * computes the checksum of the TCP/UDP pseudo-header
98 * returns a 16-bit checksum, already complemented
100 static inline __sum16
csum_tcpudp_magic(__be32 saddr
, __be32 daddr
,
101 __u32 len
, __u8 proto
,
104 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
108 * this routine is used for miscellaneous IP-like checksums, mainly
111 static inline __sum16
ip_compute_csum(const void *buf
, int len
)
113 return csum_fold (csum_partial(buf
, len
, 0));
117 #define _HAVE_ARCH_IPV6_CSUM
118 static __inline__ __sum16
csum_ipv6_magic(const struct in6_addr
*saddr
,
119 const struct in6_addr
*daddr
,
120 __u32 len
, __u8 proto
,
123 unsigned long t0
, t1
, t2
, t3
;
125 len
+= proto
; /* add 16-bit proto + len */
127 __asm__
__volatile__ (
129 #if BITS_PER_LONG > 32
132 ** We can execute two loads and two adds per cycle on PA 8000.
133 ** But add insn's get serialized waiting for the carry bit.
134 ** Try to keep 4 registers with "live" values ahead of the ALU.
137 " ldd,ma 8(%1), %4\n" /* get 1st saddr word */
138 " ldd,ma 8(%2), %5\n" /* get 1st daddr word */
140 " ldd,ma 8(%1), %6\n" /* 2nd saddr */
141 " ldd,ma 8(%2), %7\n" /* 2nd daddr */
142 " add,dc %5, %0, %0\n"
143 " add,dc %6, %0, %0\n"
144 " add,dc %7, %0, %0\n"
145 " add,dc %3, %0, %0\n" /* fold in proto+len | carry bit */
146 " extrd,u %0, 31, 32, %4\n"/* copy upper half down */
147 " depdi 0, 31, 32, %0\n"/* clear upper half */
148 " add %4, %0, %0\n" /* fold into 32-bits */
149 " addc 0, %0, %0\n" /* add carry */
154 ** For PA 1.x, the insn order doesn't matter as much.
155 ** Insn stream is serialized on the carry bit here too.
156 ** result from the previous operation (eg r0 + x)
158 " ldw,ma 4(%1), %4\n" /* get 1st saddr word */
159 " ldw,ma 4(%2), %5\n" /* get 1st daddr word */
161 " ldw,ma 4(%1), %6\n" /* 2nd saddr */
163 " ldw,ma 4(%2), %7\n" /* 2nd daddr */
165 " ldw,ma 4(%1), %4\n" /* 3rd saddr */
167 " ldw,ma 4(%2), %5\n" /* 3rd daddr */
169 " ldw,ma 4(%1), %6\n" /* 4th saddr */
171 " ldw,ma 4(%2), %7\n" /* 4th daddr */
174 " addc %3, %0, %0\n" /* fold in proto+len, catch carry */
177 : "=r" (sum
), "=r" (saddr
), "=r" (daddr
), "=r" (len
),
178 "=r" (t0
), "=r" (t1
), "=r" (t2
), "=r" (t3
)
179 : "0" (sum
), "1" (saddr
), "2" (daddr
), "3" (len
)
181 return csum_fold(sum
);
185 * Copy and checksum to user
187 #define HAVE_CSUM_COPY_USER
188 static __inline__ __wsum
csum_and_copy_to_user(const void *src
,
193 /* code stolen from include/asm-mips64 */
194 sum
= csum_partial(src
, len
, sum
);
196 if (copy_to_user(dst
, src
, len
)) {
198 return (__force __wsum
)-1;