1/* $Id: checksum.h,v 1.1.1.1 2008/10/15 03:29:17 james26_jang Exp $ */ 2#ifndef __SPARC_CHECKSUM_H 3#define __SPARC_CHECKSUM_H 4 5/* checksum.h: IP/UDP/TCP checksum routines on the Sparc. 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#include <asm/cprefix.h> 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 */ 34extern unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum); 35 36/* the same as csum_partial, but copies from fs:src while it 37 * checksums 38 * 39 * here even more important to align src and dst on a 32-bit (or even 40 * better 64-bit) boundary 41 */ 42 43#define csum_partial_copy(src, dst, len, sum) \ 44 csum_partial_copy_nocheck(src,dst,len,sum) 45#define csum_partial_copy_fromuser(s, d, l, w) \ 46 csum_partial_copy((char *) (s), (d), (l), (w)) 47 48extern unsigned int __csum_partial_copy_sparc_generic (const char *, char *); 49 50extern __inline__ unsigned int 51csum_partial_copy_nocheck (const char *src, char *dst, int len, 52 unsigned int sum) 53{ 54 register unsigned int ret asm("o0") = (unsigned int)src; 55 register char *d asm("o1") = dst; 56 register int l asm("g1") = len; 57 58 __asm__ __volatile__ ( 59 "call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "\n\t" 60 " mov %4, %%g7\n" 61 : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (sum) : 62 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7"); 63 return ret; 64} 65 66extern __inline__ unsigned int 67csum_partial_copy_from_user(const char *src, char *dst, int len, 68 unsigned int sum, int *err) 69 { 70 if (!access_ok (VERIFY_READ, src, len)) { 71 *err = -EFAULT; 72 memset (dst, 0, len); 73 return sum; 74 } else { 75 register unsigned int ret asm("o0") = (unsigned int)src; 76 register char *d asm("o1") = dst; 77 register int l asm("g1") = len; 78 register unsigned int s asm("g7") = sum; 79 80 __asm__ __volatile__ ( 81 ".section __ex_table,#alloc\n\t" 82 ".align 4\n\t" 83 ".word 1f,2\n\t" 84 ".previous\n" 85 "1:\n\t" 86 "call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "\n\t" 87 " st %5, [%%sp + 64]\n" 88 : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (s), "r" (err) : 89 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7"); 90 return ret; 91 } 92 } 93 94extern __inline__ unsigned int 95csum_partial_copy_to_user(const char *src, char *dst, int len, 96 unsigned int sum, int *err) 97{ 98 if (!access_ok (VERIFY_WRITE, dst, len)) { 99 *err = -EFAULT; 100 return sum; 101 } else { 102 register unsigned int ret asm("o0") = (unsigned int)src; 103 register char *d asm("o1") = dst; 104 register int l asm("g1") = len; 105 register unsigned int s asm("g7") = sum; 106 107 __asm__ __volatile__ ( 108 ".section __ex_table,#alloc\n\t" 109 ".align 4\n\t" 110 ".word 1f,1\n\t" 111 ".previous\n" 112 "1:\n\t" 113 "call " C_LABEL_STR(__csum_partial_copy_sparc_generic) "\n\t" 114 " st %5, [%%sp + 64]\n" 115 : "=r" (ret) : "0" (ret), "r" (d), "r" (l), "r" (s), "r" (err) : 116 "o1", "o2", "o3", "o4", "o5", "o7", "g1", "g2", "g3", "g4", "g5", "g7"); 117 return ret; 118 } 119} 120 121#define HAVE_CSUM_COPY_USER 122#define csum_and_copy_to_user csum_partial_copy_to_user 123 124/* ihl is always 5 or greater, almost always is 5, and iph is word aligned 125 * the majority of the time. 126 */ 127extern __inline__ unsigned short ip_fast_csum(__const__ unsigned char *iph, 128 unsigned int ihl) 129{ 130 unsigned short sum; 131 132 /* Note: We must read %2 before we touch %0 for the first time, 133 * because GCC can legitimately use the same register for 134 * both operands. 135 */ 136 __asm__ __volatile__("sub\t%2, 4, %%g4\n\t" 137 "ld\t[%1 + 0x00], %0\n\t" 138 "ld\t[%1 + 0x04], %%g2\n\t" 139 "ld\t[%1 + 0x08], %%g3\n\t" 140 "addcc\t%%g2, %0, %0\n\t" 141 "addxcc\t%%g3, %0, %0\n\t" 142 "ld\t[%1 + 0x0c], %%g2\n\t" 143 "ld\t[%1 + 0x10], %%g3\n\t" 144 "addxcc\t%%g2, %0, %0\n\t" 145 "addx\t%0, %%g0, %0\n" 146 "1:\taddcc\t%%g3, %0, %0\n\t" 147 "add\t%1, 4, %1\n\t" 148 "addxcc\t%0, %%g0, %0\n\t" 149 "subcc\t%%g4, 1, %%g4\n\t" 150 "be,a\t2f\n\t" 151 "sll\t%0, 16, %%g2\n\t" 152 "b\t1b\n\t" 153 "ld\t[%1 + 0x10], %%g3\n" 154 "2:\taddcc\t%0, %%g2, %%g2\n\t" 155 "srl\t%%g2, 16, %0\n\t" 156 "addx\t%0, %%g0, %0\n\t" 157 "xnor\t%%g0, %0, %0" 158 : "=r" (sum), "=&r" (iph) 159 : "r" (ihl), "1" (iph) 160 : "g2", "g3", "g4", "cc"); 161 return sum; 162} 163 164/* Fold a partial checksum without adding pseudo headers. */ 165extern __inline__ unsigned int csum_fold(unsigned int sum) 166{ 167 unsigned int tmp; 168 169 __asm__ __volatile__("addcc\t%0, %1, %1\n\t" 170 "srl\t%1, 16, %1\n\t" 171 "addx\t%1, %%g0, %1\n\t" 172 "xnor\t%%g0, %1, %0" 173 : "=&r" (sum), "=r" (tmp) 174 : "0" (sum), "1" (sum<<16) 175 : "cc"); 176 return sum; 177} 178 179extern __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr, 180 unsigned long daddr, 181 unsigned int len, 182 unsigned short proto, 183 unsigned int sum) 184{ 185 __asm__ __volatile__("addcc\t%1, %0, %0\n\t" 186 "addxcc\t%2, %0, %0\n\t" 187 "addxcc\t%3, %0, %0\n\t" 188 "addx\t%0, %%g0, %0\n\t" 189 : "=r" (sum), "=r" (saddr) 190 : "r" (daddr), "r" ((proto<<16)+len), "0" (sum), 191 "1" (saddr) 192 : "cc"); 193 return sum; 194} 195 196/* 197 * computes the checksum of the TCP/UDP pseudo-header 198 * returns a 16-bit checksum, already complemented 199 */ 200static inline unsigned short int csum_tcpudp_magic(unsigned long saddr, 201 unsigned long daddr, 202 unsigned short len, 203 unsigned short proto, 204 unsigned int sum) 205{ 206 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); 207} 208 209#define _HAVE_ARCH_IPV6_CSUM 210 211static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr, 212 struct in6_addr *daddr, 213 __u32 len, 214 unsigned short proto, 215 unsigned int sum) 216{ 217 __asm__ __volatile__ ( 218 "addcc %3, %4, %%g4\n\t" 219 "addxcc %5, %%g4, %%g4\n\t" 220 "ld [%2 + 0x0c], %%g2\n\t" 221 "ld [%2 + 0x08], %%g3\n\t" 222 "addxcc %%g2, %%g4, %%g4\n\t" 223 "ld [%2 + 0x04], %%g2\n\t" 224 "addxcc %%g3, %%g4, %%g4\n\t" 225 "ld [%2 + 0x00], %%g3\n\t" 226 "addxcc %%g2, %%g4, %%g4\n\t" 227 "ld [%1 + 0x0c], %%g2\n\t" 228 "addxcc %%g3, %%g4, %%g4\n\t" 229 "ld [%1 + 0x08], %%g3\n\t" 230 "addxcc %%g2, %%g4, %%g4\n\t" 231 "ld [%1 + 0x04], %%g2\n\t" 232 "addxcc %%g3, %%g4, %%g4\n\t" 233 "ld [%1 + 0x00], %%g3\n\t" 234 "addxcc %%g2, %%g4, %%g4\n\t" 235 "addxcc %%g3, %%g4, %0\n\t" 236 "addx 0, %0, %0\n" 237 : "=&r" (sum) 238 : "r" (saddr), "r" (daddr), 239 "r"(htonl(len)), "r"(htonl(proto)), "r"(sum) 240 : "g2", "g3", "g4", "cc"); 241 242 return csum_fold(sum); 243} 244 245/* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */ 246extern __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len) 247{ 248 return csum_fold(csum_partial(buff, len, 0)); 249} 250 251#endif /* !(__SPARC_CHECKSUM_H) */ 252