checksum.h 4.9 KB

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