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 | */ |
| 30 | static inline unsigned int |
| 31 | csum_partial(const unsigned char * buff, int len, unsigned int sum) |
| 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 | */ |
| 52 | static inline unsigned int |
| 53 | csum_partial_copy_from_user(const char __user *src, char *dst, |
| 54 | int len, unsigned int sum, |
| 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 | |
| 69 | static inline unsigned int |
| 70 | csum_partial_copy_nocheck (const char *src, char *dst, int len, unsigned int sum) |
| 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 | */ |
| 79 | static inline unsigned short |
| 80 | csum_fold(unsigned int sum) |
| 81 | { |
| 82 | #ifndef __s390x__ |
| 83 | register_pair rp; |
| 84 | |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 85 | asm volatile( |
| 86 | " slr %N1,%N1\n" /* %0 = H L */ |
| 87 | " lr %1,%0\n" /* %0 = H L, %1 = H L 0 0 */ |
| 88 | " srdl %1,16\n" /* %0 = H L, %1 = 0 H L 0 */ |
| 89 | " alr %1,%N1\n" /* %0 = H L, %1 = L H L 0 */ |
| 90 | " alr %0,%1\n" /* %0 = H+L+C L+H */ |
| 91 | " srl %0,16\n" /* %0 = H+L+C */ |
| 92 | : "+&d" (sum), "=d" (rp) : : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | #else /* __s390x__ */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 94 | asm volatile( |
| 95 | " sr 3,3\n" /* %0 = H*65536 + L */ |
| 96 | " lr 2,%0\n" /* %0 = H L, 2/3 = H L / 0 0 */ |
| 97 | " srdl 2,16\n" /* %0 = H L, 2/3 = 0 H / L 0 */ |
| 98 | " alr 2,3\n" /* %0 = H L, 2/3 = L H / L 0 */ |
| 99 | " alr %0,2\n" /* %0 = H+L+C L+H */ |
| 100 | " srl %0,16\n" /* %0 = H+L+C */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | : "+&d" (sum) : : "cc", "2", "3"); |
| 102 | #endif /* __s390x__ */ |
| 103 | return ((unsigned short) ~sum); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * This is a version of ip_compute_csum() optimized for IP headers, |
| 108 | * which always checksum on 4 octet boundaries. |
| 109 | * |
| 110 | */ |
| 111 | static inline unsigned short |
| 112 | ip_fast_csum(unsigned char *iph, unsigned int ihl) |
| 113 | { |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 114 | return csum_fold(csum_partial(iph, ihl*4, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * computes the checksum of the TCP/UDP pseudo-header |
| 119 | * returns a 32-bit checksum |
| 120 | */ |
| 121 | static inline unsigned int |
| 122 | csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr, |
| 123 | unsigned short len, unsigned short proto, |
| 124 | unsigned int sum) |
| 125 | { |
| 126 | #ifndef __s390x__ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 127 | asm volatile( |
| 128 | " alr %0,%1\n" /* sum += saddr */ |
| 129 | " brc 12,0f\n" |
| 130 | " ahi %0,1\n" /* add carry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | "0:" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 132 | : "+&d" (sum) : "d" (saddr) : "cc"); |
| 133 | asm volatile( |
| 134 | " alr %0,%1\n" /* sum += daddr */ |
| 135 | " brc 12,1f\n" |
| 136 | " ahi %0,1\n" /* add carry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | "1:" |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 138 | : "+&d" (sum) : "d" (daddr) : "cc"); |
| 139 | asm volatile( |
| 140 | " alr %0,%1\n" /* sum += (len<<16) + (proto<<8) */ |
| 141 | " brc 12,2f\n" |
| 142 | " ahi %0,1\n" /* add carry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | "2:" |
| 144 | : "+&d" (sum) |
| 145 | : "d" (((unsigned int) len<<16) + (unsigned int) proto) |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 146 | : "cc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | #else /* __s390x__ */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 148 | asm volatile( |
| 149 | " lgfr %0,%0\n" |
| 150 | " algr %0,%1\n" /* sum += saddr */ |
| 151 | " brc 12,0f\n" |
| 152 | " aghi %0,1\n" /* add carry */ |
| 153 | "0: algr %0,%2\n" /* sum += daddr */ |
| 154 | " brc 12,1f\n" |
| 155 | " aghi %0,1\n" /* add carry */ |
| 156 | "1: algfr %0,%3\n" /* sum += (len<<16) + proto */ |
| 157 | " brc 12,2f\n" |
| 158 | " aghi %0,1\n" /* add carry */ |
| 159 | "2: srlg 0,%0,32\n" |
| 160 | " alr %0,0\n" /* fold to 32 bits */ |
| 161 | " brc 12,3f\n" |
| 162 | " ahi %0,1\n" /* add carry */ |
| 163 | "3: llgfr %0,%0" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | : "+&d" (sum) |
| 165 | : "d" (saddr), "d" (daddr), |
| 166 | "d" (((unsigned int) len<<16) + (unsigned int) proto) |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 167 | : "cc", "0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | #endif /* __s390x__ */ |
| 169 | return sum; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * computes the checksum of the TCP/UDP pseudo-header |
| 174 | * returns a 16-bit checksum, already complemented |
| 175 | */ |
| 176 | |
| 177 | static inline unsigned short int |
| 178 | csum_tcpudp_magic(unsigned long saddr, unsigned long daddr, |
| 179 | unsigned short len, unsigned short proto, |
| 180 | unsigned int sum) |
| 181 | { |
| 182 | return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * this routine is used for miscellaneous IP-like checksums, mainly |
| 187 | * in icmp.c |
| 188 | */ |
| 189 | |
| 190 | static inline unsigned short |
| 191 | ip_compute_csum(unsigned char * buff, int len) |
| 192 | { |
| 193 | return csum_fold(csum_partial(buff, len, 0)); |
| 194 | } |
| 195 | |
| 196 | #endif /* _S390_CHECKSUM_H */ |
| 197 | |
| 198 | |