Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __SPARC64_CHECKSUM_H
0003 #define __SPARC64_CHECKSUM_H
0004 
0005 /*  checksum.h:  IP/UDP/TCP checksum routines on the V9.
0006  *
0007  *  Copyright(C) 1995 Linus Torvalds
0008  *  Copyright(C) 1995 Miguel de Icaza
0009  *  Copyright(C) 1996 David S. Miller
0010  *  Copyright(C) 1996 Eddie C. Dost
0011  *  Copyright(C) 1997 Jakub Jelinek
0012  *
0013  * derived from:
0014  *  Alpha checksum c-code
0015  *      ix86 inline assembly
0016  *      RFC1071 Computing the Internet Checksum
0017  */
0018 
0019 #include <linux/in6.h>
0020 #include <linux/uaccess.h>
0021 
0022 /* computes the checksum of a memory block at buff, length len,
0023  * and adds in "sum" (32-bit)
0024  *
0025  * returns a 32-bit number suitable for feeding into itself
0026  * or csum_tcpudp_magic
0027  *
0028  * this function must be called with even lengths, except
0029  * for the last fragment, which may be odd
0030  *
0031  * it's best to have buff aligned on a 32-bit boundary
0032  */
0033 __wsum csum_partial(const void * buff, int len, __wsum sum);
0034 
0035 /* the same as csum_partial, but copies from user space while it
0036  * checksums
0037  *
0038  * here even more important to align src and dst on a 32-bit (or even
0039  * better 64-bit) boundary
0040  */
0041 __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len);
0042 __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len);
0043 __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len);
0044 
0045 /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
0046  * the majority of the time.
0047  */
0048 __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
0049 
0050 /* Fold a partial checksum without adding pseudo headers. */
0051 static inline __sum16 csum_fold(__wsum sum)
0052 {
0053     unsigned int tmp;
0054 
0055     __asm__ __volatile__(
0056 "   addcc       %0, %1, %1\n"
0057 "   srl     %1, 16, %1\n"
0058 "   addc        %1, %%g0, %1\n"
0059 "   xnor        %%g0, %1, %0\n"
0060     : "=&r" (sum), "=r" (tmp)
0061     : "0" (sum), "1" ((__force u32)sum<<16)
0062     : "cc");
0063     return (__force __sum16)sum;
0064 }
0065 
0066 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
0067                     __u32 len, __u8 proto,
0068                     __wsum sum)
0069 {
0070     __asm__ __volatile__(
0071 "   addcc       %1, %0, %0\n"
0072 "   addccc      %2, %0, %0\n"
0073 "   addccc      %3, %0, %0\n"
0074 "   addc        %0, %%g0, %0\n"
0075     : "=r" (sum), "=r" (saddr)
0076     : "r" (daddr), "r" (proto + len), "0" (sum), "1" (saddr)
0077     : "cc");
0078     return sum;
0079 }
0080 
0081 /*
0082  * computes the checksum of the TCP/UDP pseudo-header
0083  * returns a 16-bit checksum, already complemented
0084  */
0085 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
0086                     __u32 len, __u8 proto,
0087                     __wsum sum)
0088 {
0089     return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
0090 }
0091 
0092 #define _HAVE_ARCH_IPV6_CSUM
0093 
0094 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
0095                       const struct in6_addr *daddr,
0096                       __u32 len, __u8 proto, __wsum sum)
0097 {
0098     __asm__ __volatile__ (
0099 "   addcc       %3, %4, %%g7\n"
0100 "   addccc      %5, %%g7, %%g7\n"
0101 "   lduw        [%2 + 0x0c], %%g2\n"
0102 "   lduw        [%2 + 0x08], %%g3\n"
0103 "   addccc      %%g2, %%g7, %%g7\n"
0104 "   lduw        [%2 + 0x04], %%g2\n"
0105 "   addccc      %%g3, %%g7, %%g7\n"
0106 "   lduw        [%2 + 0x00], %%g3\n"
0107 "   addccc      %%g2, %%g7, %%g7\n"
0108 "   lduw        [%1 + 0x0c], %%g2\n"
0109 "   addccc      %%g3, %%g7, %%g7\n"
0110 "   lduw        [%1 + 0x08], %%g3\n"
0111 "   addccc      %%g2, %%g7, %%g7\n"
0112 "   lduw        [%1 + 0x04], %%g2\n"
0113 "   addccc      %%g3, %%g7, %%g7\n"
0114 "   lduw        [%1 + 0x00], %%g3\n"
0115 "   addccc      %%g2, %%g7, %%g7\n"
0116 "   addccc      %%g3, %%g7, %0\n"
0117 "   addc        0, %0, %0\n"
0118     : "=&r" (sum)
0119     : "r" (saddr), "r" (daddr), "r"(htonl(len)),
0120       "r"(htonl(proto)), "r"(sum)
0121     : "g2", "g3", "g7", "cc");
0122 
0123     return csum_fold(sum);
0124 }
0125 
0126 /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
0127 static inline __sum16 ip_compute_csum(const void *buff, int len)
0128 {
0129     return csum_fold(csum_partial(buff, len, 0));
0130 }
0131 
0132 #define HAVE_ARCH_CSUM_ADD
0133 static inline __wsum csum_add(__wsum csum, __wsum addend)
0134 {
0135     __asm__ __volatile__(
0136         "addcc   %0, %1, %0\n"
0137         "addx    %0, %%g0, %0"
0138         : "=r" (csum)
0139         : "r" (addend), "0" (csum));
0140 
0141     return csum;
0142 }
0143 
0144 #endif /* !(__SPARC64_CHECKSUM_H) */