checksum.h 5.1 KB

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