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/config.h>
15 #include <linux/in6.h>
16 #include <xtensa/config/core.h>
19 * computes the checksum of a memory block at buff, length len,
20 * and adds in "sum" (32-bit)
22 * returns a 32-bit number suitable for feeding into itself
23 * or csum_tcpudp_magic
25 * this function must be called with even lengths, except
26 * for the last fragment, which may be odd
28 * it's best to have buff aligned on a 32-bit boundary
30 asmlinkage
unsigned int csum_partial(const unsigned char * buff
, int len
, unsigned int sum
);
33 * the same as csum_partial, but copies from src while it
34 * checksums, and handles user-space pointer exceptions correctly, when needed.
36 * here even more important to align src and dst on a 32-bit (or even
37 * better 64-bit) boundary
40 asmlinkage
unsigned int csum_partial_copy_generic( const char *src
, char *dst
, int len
, int sum
,
41 int *src_err_ptr
, int *dst_err_ptr
);
44 * Note: when you get a NULL pointer exception here this means someone
45 * passed in an incorrect kernel address to one of these functions.
47 * If you use these functions directly please don't forget the
51 unsigned int csum_partial_copy_nocheck ( const char *src
, char *dst
,
54 return csum_partial_copy_generic ( src
, dst
, len
, sum
, NULL
, NULL
);
58 unsigned int csum_partial_copy_from_user ( const char *src
, char *dst
,
59 int len
, int sum
, int *err_ptr
)
61 return csum_partial_copy_generic ( src
, dst
, len
, sum
, err_ptr
, NULL
);
65 * These are the old (and unsafe) way of doing checksums, a warning message will be
66 * printed if they are used and an exeption occurs.
68 * these functions should go away after some time.
71 #define csum_partial_copy_fromuser csum_partial_copy
72 unsigned int csum_partial_copy( const char *src
, char *dst
, int len
, int sum
);
75 * Fold a partial checksum
78 static __inline__
unsigned int csum_fold(unsigned int sum
)
81 __asm__("extui %1, %0, 16, 16\n\t"
82 "extui %0 ,%0, 0, 16\n\t"
86 "extui %0, %0, 16, 16\n\t"
89 "extui %0, %0, 0, 16\n\t"
90 : "=r" (sum
), "=&r" (__dummy
)
96 * This is a version of ip_compute_csum() optimized for IP headers,
97 * which always checksum on 4 octet boundaries.
99 static __inline__
unsigned short ip_fast_csum(unsigned char * iph
, unsigned int ihl
)
101 unsigned int sum
, tmp
, endaddr
;
103 __asm__
__volatile__(
115 "bgeu %0, %3, 1f\n\t"
119 #if !XCHAL_HAVE_LOOPS
123 /* Since the input registers which are loaded with iph and ihl
124 are modified, we must also specify them as outputs, or gcc
125 will assume they contain their original values. */
126 : "=r" (sum
), "=r" (iph
), "=r" (ihl
), "=&r" (tmp
), "=&r" (endaddr
)
127 : "1" (iph
), "2" (ihl
));
129 return csum_fold(sum
);
132 static __inline__
unsigned long csum_tcpudp_nofold(unsigned long saddr
,
135 unsigned short proto
,
140 unsigned long len_proto
= (ntohs(len
)<<16)+proto
*256;
141 #elif defined(__XTENSA_EB__)
142 unsigned long len_proto
= (proto
<<16)+len
;
144 # error processor byte order undefined!
146 __asm__("add %0, %0, %1\n\t"
147 "bgeu %0, %1, 1f\n\t"
151 "bgeu %0, %2, 1f\n\t"
155 "bgeu %0, %3, 1f\n\t"
158 : "=r" (sum
), "=r" (len_proto
)
159 : "r" (daddr
), "r" (saddr
), "1" (len_proto
), "0" (sum
));
164 * computes the checksum of the TCP/UDP pseudo-header
165 * returns a 16-bit checksum, already complemented
167 static __inline__
unsigned short int csum_tcpudp_magic(unsigned long saddr
,
170 unsigned short proto
,
173 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
177 * this routine is used for miscellaneous IP-like checksums, mainly
181 static __inline__
unsigned short ip_compute_csum(unsigned char * buff
, int len
)
183 return csum_fold (csum_partial(buff
, len
, 0));
186 #define _HAVE_ARCH_IPV6_CSUM
187 static __inline__
unsigned short int csum_ipv6_magic(struct in6_addr
*saddr
,
188 struct in6_addr
*daddr
,
190 unsigned short proto
,
193 unsigned int __dummy
;
194 __asm__("l32i %1, %2, 0\n\t"
196 "bgeu %0, %1, 1f\n\t"
201 "bgeu %0, %1, 1f\n\t"
206 "bgeu %0, %1, 1f\n\t"
209 "l32i %1, %2, 12\n\t"
211 "bgeu %0, %1, 1f\n\t"
216 "bgeu %0, %1, 1f\n\t"
221 "bgeu %0, %1, 1f\n\t"
226 "bgeu %0, %1, 1f\n\t"
229 "l32i %1, %3, 12\n\t"
231 "bgeu %0, %1, 1f\n\t"
235 "bgeu %0, %4, 1f\n\t"
239 "bgeu %0, %5, 1f\n\t"
242 : "=r" (sum
), "=&r" (__dummy
)
243 : "r" (saddr
), "r" (daddr
),
244 "r" (htonl(len
)), "r" (htonl(proto
)), "0" (sum
));
246 return csum_fold(sum
);
250 * Copy and checksum to user
252 #define HAVE_CSUM_COPY_USER
253 static __inline__
unsigned int csum_and_copy_to_user (const char *src
, char *dst
,
254 int len
, int sum
, int *err_ptr
)
256 if (access_ok(VERIFY_WRITE
, dst
, len
))
257 return csum_partial_copy_generic(src
, dst
, len
, sum
, NULL
, err_ptr
);
262 return -1; /* invalid checksum */