Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _S390_CHECKSUM_H |
| 2 | #define _S390_CHECKSUM_H |
| 3 | |
| 4 | /* |
| 5 | * include/asm-s390/checksum.h |
| 6 | * S390 fast network checksum routines |
| 7 | * see also arch/S390/lib/checksum.c |
| 8 | * |
| 9 | * S390 version |
| 10 | * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 11 | * Author(s): Ulrich Hild (first version) |
| 12 | * Martin Schwidefsky (heavily optimized CKSM version) |
| 13 | * D.J. Barrow (third attempt) |
| 14 | */ |
| 15 | |
| 16 | #include <asm/uaccess.h> |
| 17 | |
| 18 | /* |
| 19 | * computes the checksum of a memory block at buff, length len, |
| 20 | * and adds in "sum" (32-bit) |
| 21 | * |
| 22 | * returns a 32-bit number suitable for feeding into itself |
| 23 | * or csum_tcpudp_magic |
| 24 | * |
| 25 | * this function must be called with even lengths, except |
| 26 | * for the last fragment, which may be odd |
| 27 | * |
| 28 | * it's best to have buff aligned on a 32-bit boundary |
| 29 | */ |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 30 | static inline __wsum |
| 31 | csum_partial(const void *buff, int len, __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 33 | register unsigned long reg2 asm("2") = (unsigned long) buff; |
| 34 | register unsigned long reg3 asm("3") = (unsigned long) len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 36 | asm volatile( |
| 37 | "0: cksm %0,%1\n" /* do checksum on longs */ |
| 38 | " jo 0b\n" |
| 39 | : "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return sum; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * the same as csum_partial_copy, but copies from user space. |
| 45 | * |
| 46 | * here even more important to align src and dst on a 32-bit (or even |
| 47 | * better 64-bit) boundary |
| 48 | * |
| 49 | * Copy from userspace and compute checksum. If we catch an exception |
| 50 | * then zero the rest of the buffer. |
| 51 | */ |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 52 | static inline __wsum |
| 53 | csum_partial_copy_from_user(const void __user *src, void *dst, |
| 54 | int len, __wsum sum, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | int *err_ptr) |
| 56 | { |
| 57 | int missing; |
| 58 | |
| 59 | missing = copy_from_user(dst, src, len); |
| 60 | if (missing) { |
| 61 | memset(dst + len - missing, 0, missing); |
| 62 | *err_ptr = -EFAULT; |
| 63 | } |
| 64 | |
| 65 | return csum_partial(dst, len, sum); |
| 66 | } |
| 67 | |
| 68 | |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 69 | static inline __wsum |
| 70 | csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | memcpy(dst,src,len); |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 73 | return csum_partial(dst, len, sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Fold a partial checksum without adding pseudo headers |
| 78 | */ |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 79 | static inline __sum16 csum_fold(__wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Heiko Carstens | 04efc3b | 2009-09-11 10:28:32 +0200 | [diff] [blame^] | 81 | u32 csum = (__force u32) sum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Heiko Carstens | 04efc3b | 2009-09-11 10:28:32 +0200 | [diff] [blame^] | 83 | csum += (csum >> 16) + (csum << 16); |
| 84 | csum >>= 16; |
| 85 | return (__force __sum16) ~csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * This is a version of ip_compute_csum() optimized for IP headers, |
| 90 | * which always checksum on 4 octet boundaries. |
| 91 | * |
| 92 | */ |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 93 | static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 95 | return csum_fold(csum_partial(iph, ihl*4, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
| 99 | * computes the checksum of the TCP/UDP pseudo-header |
| 100 | * returns a 32-bit checksum |
| 101 | */ |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 102 | static inline __wsum |
| 103 | csum_tcpudp_nofold(__be32 saddr, __be32 daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | unsigned short len, unsigned short proto, |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 105 | __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Heiko Carstens | afbc1e9 | 2007-03-26 20:42:39 +0200 | [diff] [blame] | 107 | __u32 csum = (__force __u32)sum; |
| 108 | |
| 109 | csum += (__force __u32)saddr; |
| 110 | if (csum < (__force __u32)saddr) |
| 111 | csum++; |
| 112 | |
| 113 | csum += (__force __u32)daddr; |
| 114 | if (csum < (__force __u32)daddr) |
| 115 | csum++; |
| 116 | |
| 117 | csum += len + proto; |
| 118 | if (csum < len + proto) |
| 119 | csum++; |
| 120 | |
| 121 | return (__force __wsum)csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * computes the checksum of the TCP/UDP pseudo-header |
| 126 | * returns a 16-bit checksum, already complemented |
| 127 | */ |
| 128 | |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 129 | static inline __sum16 |
| 130 | csum_tcpudp_magic(__be32 saddr, __be32 daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | unsigned short len, unsigned short proto, |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 132 | __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * this routine is used for miscellaneous IP-like checksums, mainly |
| 139 | * in icmp.c |
| 140 | */ |
| 141 | |
Al Viro | f994aae | 2006-11-14 21:22:18 -0800 | [diff] [blame] | 142 | static inline __sum16 ip_compute_csum(const void *buff, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | return csum_fold(csum_partial(buff, len, 0)); |
| 145 | } |
| 146 | |
| 147 | #endif /* _S390_CHECKSUM_H */ |
| 148 | |
| 149 | |