2 * include/asm-xtensa/checksum.h
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (C) 2001 - 2005 Tensilica Inc.
11 #ifndef _XTENSA_CHECKSUM_H
12 #define _XTENSA_CHECKSUM_H
14 #include <linux/in6.h>
15 #include <xtensa/config/core.h>
18 * computes the checksum of a memory block at buff, length len,
19 * and adds in "sum" (32-bit)
21 * returns a 32-bit number suitable for feeding into itself
22 * or csum_tcpudp_magic
24 * this function must be called with even lengths, except
25 * for the last fragment, which may be odd
27 * it's best to have buff aligned on a 32-bit boundary
29 asmlinkage
unsigned int csum_partial(const unsigned char * buff
, int len
, unsigned int sum
);
32 * the same as csum_partial, but copies from src while it
33 * checksums, and handles user-space pointer exceptions correctly, when needed.
35 * here even more important to align src and dst on a 32-bit (or even
36 * better 64-bit) boundary
39 asmlinkage
unsigned int csum_partial_copy_generic( const char *src
, char *dst
, int len
, int sum
,
40 int *src_err_ptr
, int *dst_err_ptr
);
43 * Note: when you get a NULL pointer exception here this means someone
44 * passed in an incorrect kernel address to one of these functions.
46 * If you use these functions directly please don't forget the
50 unsigned int csum_partial_copy_nocheck ( const char *src
, char *dst
,
53 return csum_partial_copy_generic ( src
, dst
, len
, sum
, NULL
, NULL
);
57 unsigned int csum_partial_copy_from_user ( const char *src
, char *dst
,
58 int len
, int sum
, int *err_ptr
)
60 return csum_partial_copy_generic ( src
, dst
, len
, sum
, err_ptr
, NULL
);
64 * These are the old (and unsafe) way of doing checksums, a warning message will be
65 * printed if they are used and an exeption occurs.
67 * these functions should go away after some time.
70 #define csum_partial_copy_fromuser csum_partial_copy
71 unsigned int csum_partial_copy( const char *src
, char *dst
, int len
, int sum
);
74 * Fold a partial checksum
77 static __inline__
unsigned int csum_fold(unsigned int sum
)
80 __asm__("extui %1, %0, 16, 16\n\t"
81 "extui %0 ,%0, 0, 16\n\t"
85 "extui %0, %0, 16, 16\n\t"
88 "extui %0, %0, 0, 16\n\t"
89 : "=r" (sum
), "=&r" (__dummy
)
95 * This is a version of ip_compute_csum() optimized for IP headers,
96 * which always checksum on 4 octet boundaries.
98 static __inline__
unsigned short ip_fast_csum(unsigned char * iph
, unsigned int ihl
)
100 unsigned int sum
, tmp
, endaddr
;
102 __asm__
__volatile__(
114 "bgeu %0, %3, 1f\n\t"
118 #if !XCHAL_HAVE_LOOPS
122 /* Since the input registers which are loaded with iph and ihl
123 are modified, we must also specify them as outputs, or gcc
124 will assume they contain their original values. */
125 : "=r" (sum
), "=r" (iph
), "=r" (ihl
), "=&r" (tmp
), "=&r" (endaddr
)
126 : "1" (iph
), "2" (ihl
));
128 return csum_fold(sum
);
131 static __inline__
unsigned long csum_tcpudp_nofold(unsigned long saddr
,
134 unsigned short proto
,
139 unsigned long len_proto
= (ntohs(len
)<<16)+proto
*256;
140 #elif defined(__XTENSA_EB__)
141 unsigned long len_proto
= (proto
<<16)+len
;
143 # error processor byte order undefined!
145 __asm__("add %0, %0, %1\n\t"
146 "bgeu %0, %1, 1f\n\t"
150 "bgeu %0, %2, 1f\n\t"
154 "bgeu %0, %3, 1f\n\t"
157 : "=r" (sum
), "=r" (len_proto
)
158 : "r" (daddr
), "r" (saddr
), "1" (len_proto
), "0" (sum
));
163 * computes the checksum of the TCP/UDP pseudo-header
164 * returns a 16-bit checksum, already complemented
166 static __inline__
unsigned short int csum_tcpudp_magic(unsigned long saddr
,
169 unsigned short proto
,
172 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
176 * this routine is used for miscellaneous IP-like checksums, mainly
180 static __inline__
unsigned short ip_compute_csum(unsigned char * buff
, int len
)
182 return csum_fold (csum_partial(buff
, len
, 0));
185 #define _HAVE_ARCH_IPV6_CSUM
186 static __inline__
unsigned short int csum_ipv6_magic(struct in6_addr
*saddr
,
187 struct in6_addr
*daddr
,
189 unsigned short proto
,
192 unsigned int __dummy
;
193 __asm__("l32i %1, %2, 0\n\t"
195 "bgeu %0, %1, 1f\n\t"
200 "bgeu %0, %1, 1f\n\t"
205 "bgeu %0, %1, 1f\n\t"
208 "l32i %1, %2, 12\n\t"
210 "bgeu %0, %1, 1f\n\t"
215 "bgeu %0, %1, 1f\n\t"
220 "bgeu %0, %1, 1f\n\t"
225 "bgeu %0, %1, 1f\n\t"
228 "l32i %1, %3, 12\n\t"
230 "bgeu %0, %1, 1f\n\t"
234 "bgeu %0, %4, 1f\n\t"
238 "bgeu %0, %5, 1f\n\t"
241 : "=r" (sum
), "=&r" (__dummy
)
242 : "r" (saddr
), "r" (daddr
),
243 "r" (htonl(len
)), "r" (htonl(proto
)), "0" (sum
));
245 return csum_fold(sum
);
249 * Copy and checksum to user
251 #define HAVE_CSUM_COPY_USER
252 static __inline__
unsigned int csum_and_copy_to_user (const char *src
, char *dst
,
253 int len
, int sum
, int *err_ptr
)
255 if (access_ok(VERIFY_WRITE
, dst
, len
))
256 return csum_partial_copy_generic(src
, dst
, len
, sum
, NULL
, err_ptr
);
261 return -1; /* invalid checksum */