Path: blob/master/arch/s390/include/asm/checksum.h
10819 views
#ifndef _S390_CHECKSUM_H1#define _S390_CHECKSUM_H23/*4* include/asm-s390/checksum.h5* S390 fast network checksum routines6* see also arch/S390/lib/checksum.c7*8* S390 version9* Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation10* Author(s): Ulrich Hild (first version)11* Martin Schwidefsky (heavily optimized CKSM version)12* D.J. Barrow (third attempt)13*/1415#include <asm/uaccess.h>1617/*18* computes the checksum of a memory block at buff, length len,19* and adds in "sum" (32-bit)20*21* returns a 32-bit number suitable for feeding into itself22* or csum_tcpudp_magic23*24* this function must be called with even lengths, except25* for the last fragment, which may be odd26*27* it's best to have buff aligned on a 32-bit boundary28*/29static inline __wsum30csum_partial(const void *buff, int len, __wsum sum)31{32register unsigned long reg2 asm("2") = (unsigned long) buff;33register unsigned long reg3 asm("3") = (unsigned long) len;3435asm volatile(36"0: cksm %0,%1\n" /* do checksum on longs */37" jo 0b\n"38: "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");39return sum;40}4142/*43* the same as csum_partial_copy, but copies from user space.44*45* here even more important to align src and dst on a 32-bit (or even46* better 64-bit) boundary47*48* Copy from userspace and compute checksum. If we catch an exception49* then zero the rest of the buffer.50*/51static inline __wsum52csum_partial_copy_from_user(const void __user *src, void *dst,53int len, __wsum sum,54int *err_ptr)55{56int missing;5758missing = copy_from_user(dst, src, len);59if (missing) {60memset(dst + len - missing, 0, missing);61*err_ptr = -EFAULT;62}6364return csum_partial(dst, len, sum);65}666768static inline __wsum69csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum)70{71memcpy(dst,src,len);72return csum_partial(dst, len, sum);73}7475/*76* Fold a partial checksum without adding pseudo headers77*/78static inline __sum16 csum_fold(__wsum sum)79{80u32 csum = (__force u32) sum;8182csum += (csum >> 16) + (csum << 16);83csum >>= 16;84return (__force __sum16) ~csum;85}8687/*88* This is a version of ip_compute_csum() optimized for IP headers,89* which always checksum on 4 octet boundaries.90*91*/92static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)93{94return csum_fold(csum_partial(iph, ihl*4, 0));95}9697/*98* computes the checksum of the TCP/UDP pseudo-header99* returns a 32-bit checksum100*/101static inline __wsum102csum_tcpudp_nofold(__be32 saddr, __be32 daddr,103unsigned short len, unsigned short proto,104__wsum sum)105{106__u32 csum = (__force __u32)sum;107108csum += (__force __u32)saddr;109if (csum < (__force __u32)saddr)110csum++;111112csum += (__force __u32)daddr;113if (csum < (__force __u32)daddr)114csum++;115116csum += len + proto;117if (csum < len + proto)118csum++;119120return (__force __wsum)csum;121}122123/*124* computes the checksum of the TCP/UDP pseudo-header125* returns a 16-bit checksum, already complemented126*/127128static inline __sum16129csum_tcpudp_magic(__be32 saddr, __be32 daddr,130unsigned short len, unsigned short proto,131__wsum sum)132{133return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));134}135136/*137* this routine is used for miscellaneous IP-like checksums, mainly138* in icmp.c139*/140141static inline __sum16 ip_compute_csum(const void *buff, int len)142{143return csum_fold(csum_partial(buff, len, 0));144}145146#endif /* _S390_CHECKSUM_H */147148149150151