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>
19 #include <variant/core.h>
22 * computes a partial checksum, e.g. for TCP/UDP fragments
26 * unsigned int csum_partial(const unsigned char *buf, int len,
32 * This function assumes 2- or 4-byte alignment. Other alignments will fail!
35 /* ONES_ADD converts twos-complement math to ones-complement. */
36 #define ONES_ADD(sum, val) \
38 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 */
174 ENDPROC(csum_partial)
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)
221 /* We optimize the following alignment tests for the 4-byte
222 aligned case. Two bbsi.l instructions might seem more optimal
223 (commented out below). However, both labels 5: and 3: are out
224 of the imm8 range, so the assembler relaxes them into
225 equivalent bbci.l, j combinations, which is actually
229 beqz a9, 1f /* branch if both are 4-byte aligned */
230 bbsi.l a10, 0, 5f /* branch if one address is odd */
231 j 3f /* one address is 2-byte aligned */
233 /* _bbsi.l a10, 0, 5f */ /* branch if odd address */
234 /* _bbsi.l a10, 1, 3f */ /* branch if 2-byte-aligned address */
237 /* src and dst are both 4-byte aligned */
238 srli a10, a4, 5 /* 32-byte chunks */
244 add a10, a10, a2 /* a10 = end of last 32-byte src chunk */
247 SRC( l32i a9, a2, 0 )
248 SRC( l32i a8, a2, 4 )
249 DST( s32i a9, a3, 0 )
250 DST( s32i a8, a3, 4 )
253 SRC( l32i a9, a2, 8 )
254 SRC( l32i a8, a2, 12 )
255 DST( s32i a9, a3, 8 )
256 DST( s32i a8, a3, 12 )
259 SRC( l32i a9, a2, 16 )
260 SRC( l32i a8, a2, 20 )
261 DST( s32i a9, a3, 16 )
262 DST( s32i a8, a3, 20 )
265 SRC( l32i a9, a2, 24 )
266 SRC( l32i a8, a2, 28 )
267 DST( s32i a9, a3, 24 )
268 DST( s32i a8, a3, 28 )
273 #if !XCHAL_HAVE_LOOPS
277 extui a10, a4, 2, 3 /* remaining 4-byte chunks */
278 extui a4, a4, 0, 2 /* reset len for general-case, 2-byte chunks */
284 add a10, a10, a2 /* a10 = end of last 4-byte src chunk */
287 SRC( l32i a9, a2, 0 )
288 DST( s32i a9, a3, 0 )
292 #if !XCHAL_HAVE_LOOPS
297 Control comes to here in two cases: (1) It may fall through
298 to here from the 4-byte alignment case to process, at most,
299 one 2-byte chunk. (2) It branches to here from above if
300 either src or dst is 2-byte aligned, and we process all bytes
301 here, except for perhaps a trailing odd byte. It's
302 inefficient, so align your addresses to 4-byte boundaries.
309 srli a10, a4, 1 /* 2-byte chunks */
315 add a10, a10, a2 /* a10 = end of last 2-byte src chunk */
318 SRC( l16ui a9, a2, 0 )
319 DST( s16i a9, a3, 0 )
323 #if !XCHAL_HAVE_LOOPS
327 /* This section processes a possible trailing odd byte. */
328 _bbci.l a4, 0, 8f /* 1-byte chunk */
329 SRC( l8ui a9, a2, 0 )
332 slli a9, a9, 8 /* shift byte to bits 8..15 */
340 /* Control branch to here when either src or dst is odd. We
341 process all bytes using 8-bit accesses. Grossly inefficient,
342 so don't feed us an odd address. */
344 srli a10, a4, 1 /* handle in pairs for 16-bit csum */
350 add a10, a10, a2 /* a10 = end of last odd-aligned, 2-byte src chunk */
353 SRC( l8ui a9, a2, 0 )
354 SRC( l8ui a8, a2, 1 )
358 slli a9, a9, 8 /* combine into a single 16-bit value */
359 #else /* for checksum computation */
366 #if !XCHAL_HAVE_LOOPS
370 j 4b /* process the possible trailing odd byte */
372 ENDPROC(csum_partial_copy_generic)
376 .section .fixup, "ax"
380 a11 = original len for exception handling
381 a12 = original dst for exception handling
386 s32i a2, a6, 0 /* src_err_ptr */
388 # clear the complete destination - computing the rest
395 add a11, a11, a12 /* a11 = ending address */
400 #if !XCHAL_HAVE_LOOPS
401 blt a12, a11, .Leloop
408 s32i a2, a7, 0 /* dst_err_ptr */