Path: blob/master/arch/xtensa/include/asm/checksum.h
15126 views
/*1* include/asm-xtensa/checksum.h2*3* This file is subject to the terms and conditions of the GNU General Public4* License. See the file "COPYING" in the main directory of this archive5* for more details.6*7* Copyright (C) 2001 - 2005 Tensilica Inc.8*/910#ifndef _XTENSA_CHECKSUM_H11#define _XTENSA_CHECKSUM_H1213#include <linux/in6.h>14#include <variant/core.h>1516/*17* computes the checksum of a memory block at buff, length len,18* and adds in "sum" (32-bit)19*20* returns a 32-bit number suitable for feeding into itself21* or csum_tcpudp_magic22*23* this function must be called with even lengths, except24* for the last fragment, which may be odd25*26* it's best to have buff aligned on a 32-bit boundary27*/28asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);2930/*31* the same as csum_partial, but copies from src while it32* checksums, and handles user-space pointer exceptions correctly, when needed.33*34* here even more important to align src and dst on a 32-bit (or even35* better 64-bit) boundary36*/3738asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst, int len, __wsum sum,39int *src_err_ptr, int *dst_err_ptr);4041/*42* Note: when you get a NULL pointer exception here this means someone43* passed in an incorrect kernel address to one of these functions.44*45* If you use these functions directly please don't forget the access_ok().46*/47static inline48__wsum csum_partial_copy_nocheck(const void *src, void *dst,49int len, __wsum sum)50{51return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);52}5354static inline55__wsum csum_partial_copy_from_user(const void __user *src, void *dst,56int len, __wsum sum, int *err_ptr)57{58return csum_partial_copy_generic((__force const void *)src, dst,59len, sum, err_ptr, NULL);60}6162/*63* Fold a partial checksum64*/6566static __inline__ __sum16 csum_fold(__wsum sum)67{68unsigned int __dummy;69__asm__("extui %1, %0, 16, 16\n\t"70"extui %0 ,%0, 0, 16\n\t"71"add %0, %0, %1\n\t"72"slli %1, %0, 16\n\t"73"add %0, %0, %1\n\t"74"extui %0, %0, 16, 16\n\t"75"neg %0, %0\n\t"76"addi %0, %0, -1\n\t"77"extui %0, %0, 0, 16\n\t"78: "=r" (sum), "=&r" (__dummy)79: "0" (sum));80return (__force __sum16)sum;81}8283/*84* This is a version of ip_compute_csum() optimized for IP headers,85* which always checksum on 4 octet boundaries.86*/87static __inline__ __sum16 ip_fast_csum(const void *iph, unsigned int ihl)88{89unsigned int sum, tmp, endaddr;9091__asm__ __volatile__(92"sub %0, %0, %0\n\t"93#if XCHAL_HAVE_LOOPS94"loopgtz %2, 2f\n\t"95#else96"beqz %2, 2f\n\t"97"slli %4, %2, 2\n\t"98"add %4, %4, %1\n\t"99"0:\t"100#endif101"l32i %3, %1, 0\n\t"102"add %0, %0, %3\n\t"103"bgeu %0, %3, 1f\n\t"104"addi %0, %0, 1\n\t"105"1:\t"106"addi %1, %1, 4\n\t"107#if !XCHAL_HAVE_LOOPS108"blt %1, %4, 0b\n\t"109#endif110"2:\t"111/* Since the input registers which are loaded with iph and ihl112are modified, we must also specify them as outputs, or gcc113will assume they contain their original values. */114: "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp), "=&r" (endaddr)115: "1" (iph), "2" (ihl)116: "memory");117118return csum_fold(sum);119}120121static __inline__ __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,122unsigned short len,123unsigned short proto,124__wsum sum)125{126127#ifdef __XTENSA_EL__128unsigned long len_proto = (len + proto) << 8;129#elif defined(__XTENSA_EB__)130unsigned long len_proto = len + proto;131#else132# error processor byte order undefined!133#endif134__asm__("add %0, %0, %1\n\t"135"bgeu %0, %1, 1f\n\t"136"addi %0, %0, 1\n\t"137"1:\t"138"add %0, %0, %2\n\t"139"bgeu %0, %2, 1f\n\t"140"addi %0, %0, 1\n\t"141"1:\t"142"add %0, %0, %3\n\t"143"bgeu %0, %3, 1f\n\t"144"addi %0, %0, 1\n\t"145"1:\t"146: "=r" (sum), "=r" (len_proto)147: "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));148return sum;149}150151/*152* computes the checksum of the TCP/UDP pseudo-header153* returns a 16-bit checksum, already complemented154*/155static __inline__ __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,156unsigned short len,157unsigned short proto,158__wsum sum)159{160return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));161}162163/*164* this routine is used for miscellaneous IP-like checksums, mainly165* in icmp.c166*/167168static __inline__ __sum16 ip_compute_csum(const void *buff, int len)169{170return csum_fold (csum_partial(buff, len, 0));171}172173#define _HAVE_ARCH_IPV6_CSUM174static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,175const struct in6_addr *daddr,176__u32 len, unsigned short proto,177__wsum sum)178{179unsigned int __dummy;180__asm__("l32i %1, %2, 0\n\t"181"add %0, %0, %1\n\t"182"bgeu %0, %1, 1f\n\t"183"addi %0, %0, 1\n\t"184"1:\t"185"l32i %1, %2, 4\n\t"186"add %0, %0, %1\n\t"187"bgeu %0, %1, 1f\n\t"188"addi %0, %0, 1\n\t"189"1:\t"190"l32i %1, %2, 8\n\t"191"add %0, %0, %1\n\t"192"bgeu %0, %1, 1f\n\t"193"addi %0, %0, 1\n\t"194"1:\t"195"l32i %1, %2, 12\n\t"196"add %0, %0, %1\n\t"197"bgeu %0, %1, 1f\n\t"198"addi %0, %0, 1\n\t"199"1:\t"200"l32i %1, %3, 0\n\t"201"add %0, %0, %1\n\t"202"bgeu %0, %1, 1f\n\t"203"addi %0, %0, 1\n\t"204"1:\t"205"l32i %1, %3, 4\n\t"206"add %0, %0, %1\n\t"207"bgeu %0, %1, 1f\n\t"208"addi %0, %0, 1\n\t"209"1:\t"210"l32i %1, %3, 8\n\t"211"add %0, %0, %1\n\t"212"bgeu %0, %1, 1f\n\t"213"addi %0, %0, 1\n\t"214"1:\t"215"l32i %1, %3, 12\n\t"216"add %0, %0, %1\n\t"217"bgeu %0, %1, 1f\n\t"218"addi %0, %0, 1\n\t"219"1:\t"220"add %0, %0, %4\n\t"221"bgeu %0, %4, 1f\n\t"222"addi %0, %0, 1\n\t"223"1:\t"224"add %0, %0, %5\n\t"225"bgeu %0, %5, 1f\n\t"226"addi %0, %0, 1\n\t"227"1:\t"228: "=r" (sum), "=&r" (__dummy)229: "r" (saddr), "r" (daddr),230"r" (htonl(len)), "r" (htonl(proto)), "0" (sum)231: "memory");232233return csum_fold(sum);234}235236/*237* Copy and checksum to user238*/239#define HAVE_CSUM_COPY_USER240static __inline__ __wsum csum_and_copy_to_user(const void *src, void __user *dst,241int len, __wsum sum, int *err_ptr)242{243if (access_ok(VERIFY_WRITE, dst, len))244return csum_partial_copy_generic(src, dst, len, sum, NULL, err_ptr);245246if (len)247*err_ptr = -EFAULT;248249return (__force __wsum)-1; /* invalid checksum */250}251#endif252253254