2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * IP/TCP/UDP checksumming routines
8 * Xtensa version: Copyright (C) 2001 Tensilica, Inc. by Kevin Chea
9 * Optimized by Joe Taylor
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <asm/errno.h>
18 #include <linux/linkage.h>
20 #include <xtensa/config/core.h>
23 * computes a partial checksum, e.g. for TCP/UDP fragments
27 * unsigned int csum_partial(const unsigned char *buf, int len,
33 * This function assumes 2- or 4-byte alignment. Other alignments will fail!
36 /* ONES_ADD converts twos-complement math to ones-complement. */
37 #define ONES_ADD(sum, val) \
39 bgeu sum, val, 99f ; \
46 * Experiments with Ethernet and SLIP connections show that buf
47 * is aligned on either a 2-byte or 4-byte boundary.
51 bnez a5, 8f /* branch if 2-byte aligned */
52 /* Fall-through on common case, 4-byte alignment */
54 srli a5, a3, 5 /* 32-byte chunks */
60 add a5, a5, a2 /* a5 = end of last 32-byte chunk */
84 extui a5, a3, 2, 3 /* remaining 4-byte chunks */
90 add a5, a5, a2 /* a5 = end of last 4-byte chunk */
100 _bbci.l a3, 1, 5f /* remaining 2-byte chunk */
105 _bbci.l a3, 0, 7f /* remaining 1-byte chunk */
108 slli a6, a6, 8 /* load byte into bits 8..15 */
115 /* uncommon case, buf is 2-byte aligned */
117 beqz a3, 7b /* branch if len == 0 */
118 beqi a3, 1, 6b /* branch if len == 1 */
121 bnez a5, 8f /* branch if 1-byte aligned */
123 l16ui a6, a2, 0 /* common case, len >= 2 */
125 addi a2, a2, 2 /* adjust buf */
126 addi a3, a3, -2 /* adjust len */
127 j 1b /* now buf is 4-byte aligned */
129 /* case: odd-byte aligned, len > 1
130 * This case is dog slow, so don't give us an odd address.
131 * (I don't think this ever happens, but just in case.)
134 srli a5, a3, 2 /* 4-byte chunks */
140 add a5, a5, a2 /* a5 = end of last 4-byte chunk */
143 l8ui a6, a2, 0 /* bits 24..31 */
144 l16ui a7, a2, 1 /* bits 8..23 */
145 l8ui a8, a2, 3 /* bits 0.. 8 */
156 #if !XCHAL_HAVE_LOOPS
160 _bbci.l a3, 1, 3f /* remaining 2-byte chunk, still odd addr */
172 j 5b /* branch to handle the remaining byte */
177 * Copy from ds while checksumming, otherwise like csum_partial
179 * The macros SRC and DST specify the type of access for the instruction.
180 * thus we can call a custom exception handler for each access type.
185 .section __ex_table, "a"; \
186 .long 9999b, 6001f ; \
191 .section __ex_table, "a"; \
192 .long 9999b, 6002f ; \
196 unsigned int csum_partial_copy_generic (const char *src, char *dst, int len,
197 int sum, int *src_err_ptr, int *dst_err_ptr)
207 a11 = original len for exception handling
208 a12 = original dst for exception handling
210 This function is optimized for 4-byte aligned addresses. Other
211 alignments work, but not nearly as efficiently.
214 ENTRY(csum_partial_copy_generic)
220 /* We optimize the following alignment tests for the 4-byte
221 aligned case. Two bbsi.l instructions might seem more optimal
222 (commented out below). However, both labels 5: and 3: are out
223 of the imm8 range, so the assembler relaxes them into
224 equivalent bbci.l, j combinations, which is actually
228 beqz a9, 1f /* branch if both are 4-byte aligned */
229 bbsi.l a10, 0, 5f /* branch if one address is odd */
230 j 3f /* one address is 2-byte aligned */
232 /* _bbsi.l a10, 0, 5f */ /* branch if odd address */
233 /* _bbsi.l a10, 1, 3f */ /* branch if 2-byte-aligned address */
236 /* src and dst are both 4-byte aligned */
237 srli a10, a4, 5 /* 32-byte chunks */
243 add a10, a10, a2 /* a10 = end of last 32-byte src chunk */
246 SRC( l32i a9, a2, 0 )
247 SRC( l32i a8, a2, 4 )
248 DST( s32i a9, a3, 0 )
249 DST( s32i a8, a3, 4 )
252 SRC( l32i a9, a2, 8 )
253 SRC( l32i a8, a2, 12 )
254 DST( s32i a9, a3, 8 )
255 DST( s32i a8, a3, 12 )
258 SRC( l32i a9, a2, 16 )
259 SRC( l32i a8, a2, 20 )
260 DST( s32i a9, a3, 16 )
261 DST( s32i a8, a3, 20 )
264 SRC( l32i a9, a2, 24 )
265 SRC( l32i a8, a2, 28 )
266 DST( s32i a9, a3, 24 )
267 DST( s32i a8, a3, 28 )
272 #if !XCHAL_HAVE_LOOPS
276 extui a10, a4, 2, 3 /* remaining 4-byte chunks */
277 extui a4, a4, 0, 2 /* reset len for general-case, 2-byte chunks */
283 add a10, a10, a2 /* a10 = end of last 4-byte src chunk */
286 SRC( l32i a9, a2, 0 )
287 DST( s32i a9, a3, 0 )
291 #if !XCHAL_HAVE_LOOPS
296 Control comes to here in two cases: (1) It may fall through
297 to here from the 4-byte alignment case to process, at most,
298 one 2-byte chunk. (2) It branches to here from above if
299 either src or dst is 2-byte aligned, and we process all bytes
300 here, except for perhaps a trailing odd byte. It's
301 inefficient, so align your addresses to 4-byte boundaries.
308 srli a10, a4, 1 /* 2-byte chunks */
314 add a10, a10, a2 /* a10 = end of last 2-byte src chunk */
317 SRC( l16ui a9, a2, 0 )
318 DST( s16i a9, a3, 0 )
322 #if !XCHAL_HAVE_LOOPS
326 /* This section processes a possible trailing odd byte. */
327 _bbci.l a4, 0, 8f /* 1-byte chunk */
328 SRC( l8ui a9, a2, 0 )
331 slli a9, a9, 8 /* shift byte to bits 8..15 */
339 /* Control branch to here when either src or dst is odd. We
340 process all bytes using 8-bit accesses. Grossly inefficient,
341 so don't feed us an odd address. */
343 srli a10, a4, 1 /* handle in pairs for 16-bit csum */
349 add a10, a10, a2 /* a10 = end of last odd-aligned, 2-byte src chunk */
352 SRC( l8ui a9, a2, 0 )
353 SRC( l8ui a8, a2, 1 )
357 slli a9, a9, 8 /* combine into a single 16-bit value */
358 #else /* for checksum computation */
365 #if !XCHAL_HAVE_LOOPS
369 j 4b /* process the possible trailing odd byte */
373 .section .fixup, "ax"
377 a11 = original len for exception handling
378 a12 = original dst for exception handling
383 s32i a2, a6, 0 /* src_err_ptr */
385 # clear the complete destination - computing the rest
392 add a11, a11, a12 /* a11 = ending address */
397 #if !XCHAL_HAVE_LOOPS
398 blt a12, a11, .Leloop
405 s32i a2, a7, 0 /* dst_err_ptr */