1 /* checksum.h: FRV checksumming
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #ifndef _ASM_CHECKSUM_H
13 #define _ASM_CHECKSUM_H
15 #include <linux/in6.h>
18 * computes the checksum of a memory block at buff, length len,
19 * and adds in "sum" (32-bit)
21 * returns a 32-bit number suitable for feeding into itself
22 * or csum_tcpudp_magic
24 * this function must be called with even lengths, except
25 * for the last fragment, which may be odd
27 * it's best to have buff aligned on a 32-bit boundary
29 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
32 * the same as csum_partial, but copies from src while it
35 * here even more important to align src and dst on a 32-bit (or even
36 * better 64-bit) boundary
38 unsigned int csum_partial_copy(const char *src, char *dst, int len, int sum);
41 * the same as csum_partial_copy, but copies from user space.
43 * here even more important to align src and dst on a 32-bit (or even
44 * better 64-bit) boundary
46 extern unsigned int csum_partial_copy_from_user(const char __user *src, char *dst,
47 int len, int sum, int *csum_err);
49 #define csum_partial_copy_nocheck(src, dst, len, sum) \
50 csum_partial_copy((src), (dst), (len), (sum))
53 * This is a version of ip_compute_csum() optimized for IP headers,
54 * which always checksum on 4 octet boundaries.
58 unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
60 unsigned int tmp, inc, sum = 0;
62 asm(" addcc gr0,gr0,gr0,icc0\n" /* clear icc0.C */
65 " ldu.p @(%1,%3),%4 \n"
66 " subicc %2,#1,%2,icc1 \n"
67 " addxcc.p %4,%0,%0,icc0 \n"
70 /* fold the 33-bit result into 16-bits */
71 " addxcc gr0,%0,%0,icc0 \n"
78 : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (inc), "=&r"(tmp)
79 : "0" (sum), "1" (iph), "2" (ihl), "3" (4),
80 "m"(*(volatile struct { int _[100]; } *)iph)
88 * Fold a partial checksum
90 static inline unsigned int csum_fold(unsigned int sum)
94 asm(" srli %0,#16,%1 \n"
99 : "=r"(sum), "=&r"(tmp)
107 * computes the checksum of the TCP/UDP pseudo-header
108 * returns a 16-bit checksum, already complemented
110 static inline unsigned int
111 csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr, unsigned short len,
112 unsigned short proto, unsigned int sum)
114 asm(" addcc %1,%0,%0,icc0 \n"
115 " addxcc %2,%0,%0,icc0 \n"
116 " addxcc %3,%0,%0,icc0 \n"
117 " addxcc gr0,%0,%0,icc0 \n"
119 : "r" (daddr), "r" (saddr), "r" (len + proto), "0"(sum)
125 static inline unsigned short int
126 csum_tcpudp_magic(unsigned long saddr, unsigned long daddr, unsigned short len,
127 unsigned short proto, unsigned int sum)
129 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
133 * this routine is used for miscellaneous IP-like checksums, mainly
136 extern unsigned short ip_compute_csum(const unsigned char * buff, int len);
138 #define _HAVE_ARCH_IPV6_CSUM
139 static inline unsigned short int
140 csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
141 __u32 len, unsigned short proto, unsigned int sum)
143 unsigned long tmp, tmp2;
145 asm(" addcc %2,%0,%0,icc0 \n"
147 /* add up the source addr */
149 " addxcc %1,%0,%0,icc0 \n"
151 " addxcc %2,%0,%0,icc0 \n"
153 " addxcc %1,%0,%0,icc0 \n"
154 " ldi @(%3,12),%2 \n"
155 " addxcc %2,%0,%0,icc0 \n"
157 /* add up the dest addr */
159 " addxcc %1,%0,%0,icc0 \n"
161 " addxcc %2,%0,%0,icc0 \n"
163 " addxcc %1,%0,%0,icc0 \n"
164 " ldi @(%4,12),%2 \n"
165 " addxcc %2,%0,%0,icc0 \n"
167 /* fold the 33-bit result into 16-bits */
168 " addxcc gr0,%0,%0,icc0 \n"
175 : "=r" (sum), "=&r" (tmp), "=r" (tmp2)
176 : "r" (saddr), "r" (daddr), "0" (sum), "2" (len + proto)
183 #endif /* _ASM_CHECKSUM_H */