Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: checksum.h,v 1.19 2002/02/09 19:49:31 davem Exp $ */ |
| 2 | #ifndef __SPARC64_CHECKSUM_H |
| 3 | #define __SPARC64_CHECKSUM_H |
| 4 | |
| 5 | /* checksum.h: IP/UDP/TCP checksum routines on the V9. |
| 6 | * |
| 7 | * Copyright(C) 1995 Linus Torvalds |
| 8 | * Copyright(C) 1995 Miguel de Icaza |
| 9 | * Copyright(C) 1996 David S. Miller |
| 10 | * Copyright(C) 1996 Eddie C. Dost |
| 11 | * Copyright(C) 1997 Jakub Jelinek |
| 12 | * |
| 13 | * derived from: |
| 14 | * Alpha checksum c-code |
| 15 | * ix86 inline assembly |
| 16 | * RFC1071 Computing the Internet Checksum |
| 17 | */ |
| 18 | |
| 19 | #include <linux/in6.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | |
| 22 | /* computes the checksum of a memory block at buff, length len, |
| 23 | * and adds in "sum" (32-bit) |
| 24 | * |
| 25 | * returns a 32-bit number suitable for feeding into itself |
| 26 | * or csum_tcpudp_magic |
| 27 | * |
| 28 | * this function must be called with even lengths, except |
| 29 | * for the last fragment, which may be odd |
| 30 | * |
| 31 | * it's best to have buff aligned on a 32-bit boundary |
| 32 | */ |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 33 | extern __wsum csum_partial(const void * buff, int len, __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* the same as csum_partial, but copies from user space while it |
| 36 | * checksums |
| 37 | * |
| 38 | * here even more important to align src and dst on a 32-bit (or even |
| 39 | * better 64-bit) boundary |
| 40 | */ |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 41 | extern __wsum csum_partial_copy_nocheck(const void *src, void *dst, |
| 42 | int len, __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 44 | extern long __csum_partial_copy_from_user(const void __user *src, |
| 45 | void *dst, int len, |
| 46 | __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 48 | static inline __wsum |
| 49 | csum_partial_copy_from_user(const void __user *src, |
| 50 | void *dst, int len, |
| 51 | __wsum sum, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | long ret = __csum_partial_copy_from_user(src, dst, len, sum); |
| 54 | if (ret < 0) |
| 55 | *err = -EFAULT; |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 56 | return (__force __wsum) ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Copy and checksum to user |
| 61 | */ |
| 62 | #define HAVE_CSUM_COPY_USER |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 63 | extern long __csum_partial_copy_to_user(const void *src, |
| 64 | void __user *dst, int len, |
| 65 | __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 67 | static inline __wsum |
| 68 | csum_and_copy_to_user(const void *src, |
| 69 | void __user *dst, int len, |
| 70 | __wsum sum, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | long ret = __csum_partial_copy_to_user(src, dst, len, sum); |
| 73 | if (ret < 0) |
| 74 | *err = -EFAULT; |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 75 | return (__force __wsum) ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* ihl is always 5 or greater, almost always is 5, and iph is word aligned |
| 79 | * the majority of the time. |
| 80 | */ |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 81 | extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* Fold a partial checksum without adding pseudo headers. */ |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 84 | static inline __sum16 csum_fold(__wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | unsigned int tmp; |
| 87 | |
| 88 | __asm__ __volatile__( |
| 89 | " addcc %0, %1, %1\n" |
| 90 | " srl %1, 16, %1\n" |
| 91 | " addc %1, %%g0, %1\n" |
| 92 | " xnor %%g0, %1, %0\n" |
| 93 | : "=&r" (sum), "=r" (tmp) |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 94 | : "0" (sum), "1" ((__force u32)sum<<16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | : "cc"); |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 96 | return (__force __sum16)sum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 99 | static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | unsigned int len, |
| 101 | unsigned short proto, |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 102 | __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | __asm__ __volatile__( |
| 105 | " addcc %1, %0, %0\n" |
| 106 | " addccc %2, %0, %0\n" |
| 107 | " addccc %3, %0, %0\n" |
| 108 | " addc %0, %%g0, %0\n" |
| 109 | : "=r" (sum), "=r" (saddr) |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 110 | : "r" (daddr), "r" (proto + len), "0" (sum), "1" (saddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | : "cc"); |
| 112 | return sum; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * computes the checksum of the TCP/UDP pseudo-header |
| 117 | * returns a 16-bit checksum, already complemented |
| 118 | */ |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 119 | static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | unsigned short len, |
| 121 | unsigned short proto, |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 122 | __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); |
| 125 | } |
| 126 | |
| 127 | #define _HAVE_ARCH_IPV6_CSUM |
| 128 | |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 129 | static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, |
| 130 | const struct in6_addr *daddr, |
| 131 | __u32 len, unsigned short proto, |
| 132 | __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | __asm__ __volatile__ ( |
| 135 | " addcc %3, %4, %%g7\n" |
| 136 | " addccc %5, %%g7, %%g7\n" |
| 137 | " lduw [%2 + 0x0c], %%g2\n" |
| 138 | " lduw [%2 + 0x08], %%g3\n" |
| 139 | " addccc %%g2, %%g7, %%g7\n" |
| 140 | " lduw [%2 + 0x04], %%g2\n" |
| 141 | " addccc %%g3, %%g7, %%g7\n" |
| 142 | " lduw [%2 + 0x00], %%g3\n" |
| 143 | " addccc %%g2, %%g7, %%g7\n" |
| 144 | " lduw [%1 + 0x0c], %%g2\n" |
| 145 | " addccc %%g3, %%g7, %%g7\n" |
| 146 | " lduw [%1 + 0x08], %%g3\n" |
| 147 | " addccc %%g2, %%g7, %%g7\n" |
| 148 | " lduw [%1 + 0x04], %%g2\n" |
| 149 | " addccc %%g3, %%g7, %%g7\n" |
| 150 | " lduw [%1 + 0x00], %%g3\n" |
| 151 | " addccc %%g2, %%g7, %%g7\n" |
| 152 | " addccc %%g3, %%g7, %0\n" |
| 153 | " addc 0, %0, %0\n" |
| 154 | : "=&r" (sum) |
| 155 | : "r" (saddr), "r" (daddr), "r"(htonl(len)), |
| 156 | "r"(htonl(proto)), "r"(sum) |
| 157 | : "g2", "g3", "g7", "cc"); |
| 158 | |
| 159 | return csum_fold(sum); |
| 160 | } |
| 161 | |
| 162 | /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */ |
Al Viro | d5c6393 | 2006-11-14 21:23:20 -0800 | [diff] [blame] | 163 | static inline __sum16 ip_compute_csum(const void *buff, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
| 165 | return csum_fold(csum_partial(buff, len, 0)); |
| 166 | } |
| 167 | |
| 168 | #endif /* !(__SPARC64_CHECKSUM_H) */ |