| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifdef __KERNEL__ | 
|  | 2 | #ifndef _ASM_M32R_CHECKSUM_H | 
|  | 3 | #define _ASM_M32R_CHECKSUM_H | 
|  | 4 |  | 
|  | 5 | /* | 
|  | 6 | * include/asm-m32r/checksum.h | 
|  | 7 | * | 
|  | 8 | * IP/TCP/UDP checksum routines | 
|  | 9 | * | 
|  | 10 | * This file is subject to the terms and conditions of the GNU General Public | 
|  | 11 | * License.  See the file "COPYING" in the main directory of this archive | 
|  | 12 | * for more details. | 
|  | 13 | * | 
|  | 14 | * Some code taken from mips and parisc architecture. | 
|  | 15 | * | 
|  | 16 | *    Copyright (C) 2001, 2002  Hiroyuki Kondo, Hirokazu Takata | 
|  | 17 | *    Copyright (C) 2004  Hirokazu Takata <takata at linux-m32r.org> | 
|  | 18 | */ | 
|  | 19 |  | 
|  | 20 | #include <linux/in6.h> | 
|  | 21 |  | 
|  | 22 | /* | 
|  | 23 | * computes the checksum of a memory block at buff, length len, | 
|  | 24 | * and adds in "sum" (32-bit) | 
|  | 25 | * | 
|  | 26 | * returns a 32-bit number suitable for feeding into itself | 
|  | 27 | * or csum_tcpudp_magic | 
|  | 28 | * | 
|  | 29 | * this function must be called with even lengths, except | 
|  | 30 | * for the last fragment, which may be odd | 
|  | 31 | * | 
|  | 32 | * it's best to have buff aligned on a 32-bit boundary | 
|  | 33 | */ | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 34 | asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | /* | 
|  | 37 | * The same as csum_partial, but copies from src while it checksums. | 
|  | 38 | * | 
|  | 39 | * Here even more important to align src and dst on a 32-bit (or even | 
|  | 40 | * better 64-bit) boundary | 
|  | 41 | */ | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 42 | extern __wsum csum_partial_copy_nocheck(const void *src, void *dst, | 
|  | 43 | int len, __wsum sum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | /* | 
|  | 46 | * This is a new version of the above that records errors it finds in *errp, | 
|  | 47 | * but continues and zeros thre rest of the buffer. | 
|  | 48 | */ | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 49 | extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst, | 
|  | 50 | int len, __wsum sum, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int *err_ptr); | 
|  | 52 |  | 
|  | 53 | /* | 
|  | 54 | *	Fold a partial checksum | 
|  | 55 | */ | 
|  | 56 |  | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 57 | static inline __sum16 csum_fold(__wsum sum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { | 
|  | 59 | unsigned long tmpreg; | 
|  | 60 | __asm__( | 
|  | 61 | "	sll3	%1, %0, #16 \n" | 
|  | 62 | "	cmp	%0, %0 \n" | 
|  | 63 | "	addx	%0, %1 \n" | 
|  | 64 | "	ldi	%1, #0 \n" | 
|  | 65 | "	srli	%0, #16 \n" | 
|  | 66 | "	addx	%0, %1 \n" | 
|  | 67 | "	xor3	%0, %0, #0x0000ffff \n" | 
|  | 68 | : "=r" (sum), "=&r" (tmpreg) | 
|  | 69 | : "0"  (sum) | 
|  | 70 | : "cbit" | 
|  | 71 | ); | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 72 | return (__force __sum16)sum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
|  | 75 | /* | 
|  | 76 | * This is a version of ip_compute_csum() optimized for IP headers, | 
|  | 77 | * which always checksum on 4 octet boundaries. | 
|  | 78 | */ | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 79 | static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) | 
|  | 80 | { | 
|  | 81 | unsigned long tmpreg0, tmpreg1; | 
|  | 82 | __wsum sum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | __asm__ __volatile__( | 
|  | 85 | "	ld	%0, @%1+ \n" | 
|  | 86 | "	addi	%2, #-4 \n" | 
|  | 87 | "#	bgez	%2, 2f \n" | 
|  | 88 | "	cmp	%0, %0 \n" | 
|  | 89 | "	ld	%3, @%1+ \n" | 
|  | 90 | "	ld	%4, @%1+ \n" | 
|  | 91 | "	addx	%0, %3 \n" | 
|  | 92 | "	ld	%3, @%1+ \n" | 
|  | 93 | "	addx	%0, %4 \n" | 
|  | 94 | "	addx	%0, %3 \n" | 
|  | 95 | "	.fillinsn\n" | 
|  | 96 | "1: \n" | 
|  | 97 | "	ld	%4, @%1+ \n" | 
|  | 98 | "	addi	%2, #-1 \n" | 
|  | 99 | "	addx	%0, %4 \n" | 
|  | 100 | "	bgtz	%2, 1b \n" | 
|  | 101 | "\n" | 
|  | 102 | "	ldi	%3, #0 \n" | 
|  | 103 | "	addx	%0, %3 \n" | 
|  | 104 | "	.fillinsn\n" | 
|  | 105 | "2: \n" | 
| Thomas Graf | 2c65649 | 2005-08-20 17:24:25 -0700 | [diff] [blame] | 106 | /* Since the input registers which are loaded with iph and ihl | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | are modified, we must also specify them as outputs, or gcc | 
|  | 108 | will assume they contain their original values. */ | 
|  | 109 | : "=&r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmpreg0), "=&r" (tmpreg1) | 
|  | 110 | : "1" (iph), "2" (ihl) | 
|  | 111 | : "cbit", "memory"); | 
|  | 112 |  | 
|  | 113 | return csum_fold(sum); | 
|  | 114 | } | 
|  | 115 |  | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 116 | static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | unsigned short len, | 
|  | 118 | unsigned short proto, | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 119 | __wsum sum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { | 
|  | 121 | #if defined(__LITTLE_ENDIAN) | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 122 | unsigned long len_proto = (proto + len) << 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | #else | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 124 | unsigned long len_proto = proto + len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #endif | 
|  | 126 | unsigned long tmpreg; | 
|  | 127 |  | 
|  | 128 | __asm__( | 
|  | 129 | "	cmp	%0, %0 \n" | 
|  | 130 | "	addx	%0, %2 \n" | 
|  | 131 | "	addx	%0, %3 \n" | 
|  | 132 | "	addx	%0, %4 \n" | 
|  | 133 | "	ldi	%1, #0 \n" | 
|  | 134 | "	addx	%0, %1 \n" | 
|  | 135 | : "=r" (sum), "=&r" (tmpreg) | 
|  | 136 | : "r" (daddr), "r" (saddr), "r" (len_proto), "0" (sum) | 
|  | 137 | : "cbit" | 
|  | 138 | ); | 
|  | 139 |  | 
|  | 140 | return sum; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | /* | 
|  | 144 | * computes the checksum of the TCP/UDP pseudo-header | 
|  | 145 | * returns a 16-bit checksum, already complemented | 
|  | 146 | */ | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 147 | static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | unsigned short len, | 
|  | 149 | unsigned short proto, | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 150 | __wsum sum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { | 
|  | 152 | return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | /* | 
|  | 156 | * this routine is used for miscellaneous IP-like checksums, mainly | 
|  | 157 | * in icmp.c | 
|  | 158 | */ | 
|  | 159 |  | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 160 | static inline __sum16 ip_compute_csum(const void *buff, int len) | 
|  | 161 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return csum_fold (csum_partial(buff, len, 0)); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | #define _HAVE_ARCH_IPV6_CSUM | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 166 | static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, | 
|  | 167 | const struct in6_addr *daddr, | 
|  | 168 | __u32 len, unsigned short proto, | 
|  | 169 | __wsum sum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { | 
|  | 171 | unsigned long tmpreg0, tmpreg1, tmpreg2, tmpreg3; | 
|  | 172 | __asm__( | 
|  | 173 | "	ld	%1, @(%5) \n" | 
|  | 174 | "	ld	%2, @(4,%5) \n" | 
|  | 175 | "	ld	%3, @(8,%5) \n" | 
|  | 176 | "	ld	%4, @(12,%5) \n" | 
|  | 177 | "	add	%0, %1 \n" | 
|  | 178 | "	addx	%0, %2 \n" | 
|  | 179 | "	addx	%0, %3 \n" | 
|  | 180 | "	addx	%0, %4 \n" | 
|  | 181 | "	ld	%1, @(%6) \n" | 
|  | 182 | "	ld	%2, @(4,%6) \n" | 
|  | 183 | "	ld	%3, @(8,%6) \n" | 
|  | 184 | "	ld	%4, @(12,%6) \n" | 
|  | 185 | "	addx	%0, %1 \n" | 
|  | 186 | "	addx	%0, %2 \n" | 
|  | 187 | "	addx	%0, %3 \n" | 
|  | 188 | "	addx	%0, %4 \n" | 
|  | 189 | "	addx	%0, %7 \n" | 
|  | 190 | "	addx	%0, %8 \n" | 
|  | 191 | "	ldi	%1, #0 \n" | 
|  | 192 | "	addx	%0, %1 \n" | 
|  | 193 | : "=&r" (sum), "=&r" (tmpreg0), "=&r" (tmpreg1), | 
|  | 194 | "=&r" (tmpreg2), "=&r" (tmpreg3) | 
|  | 195 | : "r" (saddr), "r" (daddr), | 
| Al Viro | 85d20de | 2006-11-14 21:16:55 -0800 | [diff] [blame] | 196 | "r" (htonl(len)), "r" (htonl(proto)), "0" (sum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | : "cbit" | 
|  | 198 | ); | 
|  | 199 |  | 
|  | 200 | return csum_fold(sum); | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | #endif /* _ASM_M32R_CHECKSUM_H */ | 
|  | 204 | #endif /* __KERNEL__ */ |