/*1* INET An implementation of the TCP/IP protocol suite for the LINUX2* operating system. INET is implemented using the BSD Socket3* interface as the means of communication with the user level.4*5* IP/TCP/UDP checksumming routines6*7* Xtensa version: Copyright (C) 2001 Tensilica, Inc. by Kevin Chea8* Optimized by Joe Taylor9*10* This program is free software; you can redistribute it and/or11* modify it under the terms of the GNU General Public License12* as published by the Free Software Foundation; either version13* 2 of the License, or (at your option) any later version.14*/1516#include <asm/errno.h>17#include <linux/linkage.h>18#include <variant/core.h>1920/*21* computes a partial checksum, e.g. for TCP/UDP fragments22*/2324/*25* unsigned int csum_partial(const unsigned char *buf, int len,26* unsigned int sum);27* a2 = buf28* a3 = len29* a4 = sum30*31* This function assumes 2- or 4-byte alignment. Other alignments will fail!32*/3334/* ONES_ADD converts twos-complement math to ones-complement. */35#define ONES_ADD(sum, val) \36add sum, sum, val ; \37bgeu sum, val, 99f ; \38addi sum, sum, 1 ; \3999: ;4041.text42ENTRY(csum_partial)43/*44* Experiments with Ethernet and SLIP connections show that buf45* is aligned on either a 2-byte or 4-byte boundary.46*/47entry sp, 3248extui a5, a2, 0, 249bnez a5, 8f /* branch if 2-byte aligned */50/* Fall-through on common case, 4-byte alignment */511:52srli a5, a3, 5 /* 32-byte chunks */53#if XCHAL_HAVE_LOOPS54loopgtz a5, 2f55#else56beqz a5, 2f57slli a5, a5, 558add a5, a5, a2 /* a5 = end of last 32-byte chunk */59.Loop1:60#endif61l32i a6, a2, 062l32i a7, a2, 463ONES_ADD(a4, a6)64ONES_ADD(a4, a7)65l32i a6, a2, 866l32i a7, a2, 1267ONES_ADD(a4, a6)68ONES_ADD(a4, a7)69l32i a6, a2, 1670l32i a7, a2, 2071ONES_ADD(a4, a6)72ONES_ADD(a4, a7)73l32i a6, a2, 2474l32i a7, a2, 2875ONES_ADD(a4, a6)76ONES_ADD(a4, a7)77addi a2, a2, 4*878#if !XCHAL_HAVE_LOOPS79blt a2, a5, .Loop180#endif812:82extui a5, a3, 2, 3 /* remaining 4-byte chunks */83#if XCHAL_HAVE_LOOPS84loopgtz a5, 3f85#else86beqz a5, 3f87slli a5, a5, 288add a5, a5, a2 /* a5 = end of last 4-byte chunk */89.Loop2:90#endif91l32i a6, a2, 092ONES_ADD(a4, a6)93addi a2, a2, 494#if !XCHAL_HAVE_LOOPS95blt a2, a5, .Loop296#endif973:98_bbci.l a3, 1, 5f /* remaining 2-byte chunk */99l16ui a6, a2, 0100ONES_ADD(a4, a6)101addi a2, a2, 21025:103_bbci.l a3, 0, 7f /* remaining 1-byte chunk */1046: l8ui a6, a2, 0105#ifdef __XTENSA_EB__106slli a6, a6, 8 /* load byte into bits 8..15 */107#endif108ONES_ADD(a4, a6)1097:110mov a2, a4111retw112113/* uncommon case, buf is 2-byte aligned */1148:115beqz a3, 7b /* branch if len == 0 */116beqi a3, 1, 6b /* branch if len == 1 */117118extui a5, a2, 0, 1119bnez a5, 8f /* branch if 1-byte aligned */120121l16ui a6, a2, 0 /* common case, len >= 2 */122ONES_ADD(a4, a6)123addi a2, a2, 2 /* adjust buf */124addi a3, a3, -2 /* adjust len */125j 1b /* now buf is 4-byte aligned */126127/* case: odd-byte aligned, len > 1128* This case is dog slow, so don't give us an odd address.129* (I don't think this ever happens, but just in case.)130*/1318:132srli a5, a3, 2 /* 4-byte chunks */133#if XCHAL_HAVE_LOOPS134loopgtz a5, 2f135#else136beqz a5, 2f137slli a5, a5, 2138add a5, a5, a2 /* a5 = end of last 4-byte chunk */139.Loop3:140#endif141l8ui a6, a2, 0 /* bits 24..31 */142l16ui a7, a2, 1 /* bits 8..23 */143l8ui a8, a2, 3 /* bits 0.. 8 */144#ifdef __XTENSA_EB__145slli a6, a6, 24146#else147slli a8, a8, 24148#endif149slli a7, a7, 8150or a7, a7, a6151or a7, a7, a8152ONES_ADD(a4, a7)153addi a2, a2, 4154#if !XCHAL_HAVE_LOOPS155blt a2, a5, .Loop3156#endif1572:158_bbci.l a3, 1, 3f /* remaining 2-byte chunk, still odd addr */159l8ui a6, a2, 0160l8ui a7, a2, 1161#ifdef __XTENSA_EB__162slli a6, a6, 8163#else164slli a7, a7, 8165#endif166or a7, a7, a6167ONES_ADD(a4, a7)168addi a2, a2, 21693:170j 5b /* branch to handle the remaining byte */171172173174/*175* Copy from ds while checksumming, otherwise like csum_partial176*177* The macros SRC and DST specify the type of access for the instruction.178* thus we can call a custom exception handler for each access type.179*/180181#define SRC(y...) \1829999: y; \183.section __ex_table, "a"; \184.long 9999b, 6001f ; \185.previous186187#define DST(y...) \1889999: y; \189.section __ex_table, "a"; \190.long 9999b, 6002f ; \191.previous192193/*194unsigned int csum_partial_copy_generic (const char *src, char *dst, int len,195int sum, int *src_err_ptr, int *dst_err_ptr)196a2 = src197a3 = dst198a4 = len199a5 = sum200a6 = src_err_ptr201a7 = dst_err_ptr202a8 = temp203a9 = temp204a10 = temp205a11 = original len for exception handling206a12 = original dst for exception handling207208This function is optimized for 4-byte aligned addresses. Other209alignments work, but not nearly as efficiently.210*/211212ENTRY(csum_partial_copy_generic)213entry sp, 32214mov a12, a3215mov a11, a4216or a10, a2, a3217218/* We optimize the following alignment tests for the 4-byte219aligned case. Two bbsi.l instructions might seem more optimal220(commented out below). However, both labels 5: and 3: are out221of the imm8 range, so the assembler relaxes them into222equivalent bbci.l, j combinations, which is actually223slower. */224225extui a9, a10, 0, 2226beqz a9, 1f /* branch if both are 4-byte aligned */227bbsi.l a10, 0, 5f /* branch if one address is odd */228j 3f /* one address is 2-byte aligned */229230/* _bbsi.l a10, 0, 5f */ /* branch if odd address */231/* _bbsi.l a10, 1, 3f */ /* branch if 2-byte-aligned address */2322331:234/* src and dst are both 4-byte aligned */235srli a10, a4, 5 /* 32-byte chunks */236#if XCHAL_HAVE_LOOPS237loopgtz a10, 2f238#else239beqz a10, 2f240slli a10, a10, 5241add a10, a10, a2 /* a10 = end of last 32-byte src chunk */242.Loop5:243#endif244SRC( l32i a9, a2, 0 )245SRC( l32i a8, a2, 4 )246DST( s32i a9, a3, 0 )247DST( s32i a8, a3, 4 )248ONES_ADD(a5, a9)249ONES_ADD(a5, a8)250SRC( l32i a9, a2, 8 )251SRC( l32i a8, a2, 12 )252DST( s32i a9, a3, 8 )253DST( s32i a8, a3, 12 )254ONES_ADD(a5, a9)255ONES_ADD(a5, a8)256SRC( l32i a9, a2, 16 )257SRC( l32i a8, a2, 20 )258DST( s32i a9, a3, 16 )259DST( s32i a8, a3, 20 )260ONES_ADD(a5, a9)261ONES_ADD(a5, a8)262SRC( l32i a9, a2, 24 )263SRC( l32i a8, a2, 28 )264DST( s32i a9, a3, 24 )265DST( s32i a8, a3, 28 )266ONES_ADD(a5, a9)267ONES_ADD(a5, a8)268addi a2, a2, 32269addi a3, a3, 32270#if !XCHAL_HAVE_LOOPS271blt a2, a10, .Loop5272#endif2732:274extui a10, a4, 2, 3 /* remaining 4-byte chunks */275extui a4, a4, 0, 2 /* reset len for general-case, 2-byte chunks */276#if XCHAL_HAVE_LOOPS277loopgtz a10, 3f278#else279beqz a10, 3f280slli a10, a10, 2281add a10, a10, a2 /* a10 = end of last 4-byte src chunk */282.Loop6:283#endif284SRC( l32i a9, a2, 0 )285DST( s32i a9, a3, 0 )286ONES_ADD(a5, a9)287addi a2, a2, 4288addi a3, a3, 4289#if !XCHAL_HAVE_LOOPS290blt a2, a10, .Loop6291#endif2923:293/*294Control comes to here in two cases: (1) It may fall through295to here from the 4-byte alignment case to process, at most,296one 2-byte chunk. (2) It branches to here from above if297either src or dst is 2-byte aligned, and we process all bytes298here, except for perhaps a trailing odd byte. It's299inefficient, so align your addresses to 4-byte boundaries.300301a2 = src302a3 = dst303a4 = len304a5 = sum305*/306srli a10, a4, 1 /* 2-byte chunks */307#if XCHAL_HAVE_LOOPS308loopgtz a10, 4f309#else310beqz a10, 4f311slli a10, a10, 1312add a10, a10, a2 /* a10 = end of last 2-byte src chunk */313.Loop7:314#endif315SRC( l16ui a9, a2, 0 )316DST( s16i a9, a3, 0 )317ONES_ADD(a5, a9)318addi a2, a2, 2319addi a3, a3, 2320#if !XCHAL_HAVE_LOOPS321blt a2, a10, .Loop7322#endif3234:324/* This section processes a possible trailing odd byte. */325_bbci.l a4, 0, 8f /* 1-byte chunk */326SRC( l8ui a9, a2, 0 )327DST( s8i a9, a3, 0 )328#ifdef __XTENSA_EB__329slli a9, a9, 8 /* shift byte to bits 8..15 */330#endif331ONES_ADD(a5, a9)3328:333mov a2, a5334retw3353365:337/* Control branch to here when either src or dst is odd. We338process all bytes using 8-bit accesses. Grossly inefficient,339so don't feed us an odd address. */340341srli a10, a4, 1 /* handle in pairs for 16-bit csum */342#if XCHAL_HAVE_LOOPS343loopgtz a10, 6f344#else345beqz a10, 6f346slli a10, a10, 1347add a10, a10, a2 /* a10 = end of last odd-aligned, 2-byte src chunk */348.Loop8:349#endif350SRC( l8ui a9, a2, 0 )351SRC( l8ui a8, a2, 1 )352DST( s8i a9, a3, 0 )353DST( s8i a8, a3, 1 )354#ifdef __XTENSA_EB__355slli a9, a9, 8 /* combine into a single 16-bit value */356#else /* for checksum computation */357slli a8, a8, 8358#endif359or a9, a9, a8360ONES_ADD(a5, a9)361addi a2, a2, 2362addi a3, a3, 2363#if !XCHAL_HAVE_LOOPS364blt a2, a10, .Loop8365#endif3666:367j 4b /* process the possible trailing odd byte */368369370# Exception handler:371.section .fixup, "ax"372/*373a6 = src_err_ptr374a7 = dst_err_ptr375a11 = original len for exception handling376a12 = original dst for exception handling377*/3783796001:380_movi a2, -EFAULT381s32i a2, a6, 0 /* src_err_ptr */382383# clear the complete destination - computing the rest384# is too much work385movi a2, 0386#if XCHAL_HAVE_LOOPS387loopgtz a11, 2f388#else389beqz a11, 2f390add a11, a11, a12 /* a11 = ending address */391.Leloop:392#endif393s8i a2, a12, 0394addi a12, a12, 1395#if !XCHAL_HAVE_LOOPS396blt a12, a11, .Leloop397#endif3982:399retw4004016002:402movi a2, -EFAULT403s32i a2, a7, 0 /* dst_err_ptr */404movi a2, 0405retw406407.previous408409410411