checksum.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* checksum.h: FRV checksumming
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #ifndef _ASM_CHECKSUM_H
  12. #define _ASM_CHECKSUM_H
  13. #include <linux/in6.h>
  14. /*
  15. * computes the checksum of a memory block at buff, length len,
  16. * and adds in "sum" (32-bit)
  17. *
  18. * returns a 32-bit number suitable for feeding into itself
  19. * or csum_tcpudp_magic
  20. *
  21. * this function must be called with even lengths, except
  22. * for the last fragment, which may be odd
  23. *
  24. * it's best to have buff aligned on a 32-bit boundary
  25. */
  26. __wsum csum_partial(const void *buff, int len, __wsum sum);
  27. /*
  28. * the same as csum_partial, but copies from src while it
  29. * checksums
  30. *
  31. * here even more important to align src and dst on a 32-bit (or even
  32. * better 64-bit) boundary
  33. */
  34. __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum);
  35. /*
  36. * the same as csum_partial_copy, but copies from user space.
  37. *
  38. * here even more important to align src and dst on a 32-bit (or even
  39. * better 64-bit) boundary
  40. */
  41. extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  42. int len, __wsum sum, int *csum_err);
  43. /*
  44. * This is a version of ip_compute_csum() optimized for IP headers,
  45. * which always checksum on 4 octet boundaries.
  46. *
  47. */
  48. static inline
  49. __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  50. {
  51. unsigned int tmp, inc, sum = 0;
  52. asm(" addcc gr0,gr0,gr0,icc0\n" /* clear icc0.C */
  53. " subi %1,#4,%1 \n"
  54. "0: \n"
  55. " ldu.p @(%1,%3),%4 \n"
  56. " subicc %2,#1,%2,icc1 \n"
  57. " addxcc.p %4,%0,%0,icc0 \n"
  58. " bhi icc1,#2,0b \n"
  59. /* fold the 33-bit result into 16-bits */
  60. " addxcc gr0,%0,%0,icc0 \n"
  61. " srli %0,#16,%1 \n"
  62. " sethi #0,%0 \n"
  63. " add %1,%0,%0 \n"
  64. " srli %0,#16,%1 \n"
  65. " add %1,%0,%0 \n"
  66. : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (inc), "=&r"(tmp)
  67. : "0" (sum), "1" (iph), "2" (ihl), "3" (4),
  68. "m"(*(volatile struct { int _[100]; } *)iph)
  69. : "icc0", "icc1", "memory"
  70. );
  71. return (__force __sum16)~sum;
  72. }
  73. /*
  74. * Fold a partial checksum
  75. */
  76. static inline __sum16 csum_fold(__wsum sum)
  77. {
  78. unsigned int tmp;
  79. asm(" srli %0,#16,%1 \n"
  80. " sethi #0,%0 \n"
  81. " add %1,%0,%0 \n"
  82. " srli %0,#16,%1 \n"
  83. " add %1,%0,%0 \n"
  84. : "=r"(sum), "=&r"(tmp)
  85. : "0"(sum)
  86. );
  87. return (__force __sum16)~sum;
  88. }
  89. /*
  90. * computes the checksum of the TCP/UDP pseudo-header
  91. * returns a 16-bit checksum, already complemented
  92. */
  93. static inline __wsum
  94. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  95. unsigned short proto, __wsum sum)
  96. {
  97. asm(" addcc %1,%0,%0,icc0 \n"
  98. " addxcc %2,%0,%0,icc0 \n"
  99. " addxcc %3,%0,%0,icc0 \n"
  100. " addxcc gr0,%0,%0,icc0 \n"
  101. : "=r" (sum)
  102. : "r" (daddr), "r" (saddr), "r" (len + proto), "0"(sum)
  103. : "icc0"
  104. );
  105. return sum;
  106. }
  107. static inline __sum16
  108. csum_tcpudp_magic(__be32 saddr, __be32 daddr, unsigned short len,
  109. unsigned short proto, __wsum sum)
  110. {
  111. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  112. }
  113. /*
  114. * this routine is used for miscellaneous IP-like checksums, mainly
  115. * in icmp.c
  116. */
  117. extern __sum16 ip_compute_csum(const void *buff, int len);
  118. #define _HAVE_ARCH_IPV6_CSUM
  119. static inline __sum16
  120. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
  121. __u32 len, unsigned short proto, __wsum sum)
  122. {
  123. unsigned long tmp, tmp2;
  124. asm(" addcc %2,%0,%0,icc0 \n"
  125. /* add up the source addr */
  126. " ldi @(%3,0),%1 \n"
  127. " addxcc %1,%0,%0,icc0 \n"
  128. " ldi @(%3,4),%2 \n"
  129. " addxcc %2,%0,%0,icc0 \n"
  130. " ldi @(%3,8),%1 \n"
  131. " addxcc %1,%0,%0,icc0 \n"
  132. " ldi @(%3,12),%2 \n"
  133. " addxcc %2,%0,%0,icc0 \n"
  134. /* add up the dest addr */
  135. " ldi @(%4,0),%1 \n"
  136. " addxcc %1,%0,%0,icc0 \n"
  137. " ldi @(%4,4),%2 \n"
  138. " addxcc %2,%0,%0,icc0 \n"
  139. " ldi @(%4,8),%1 \n"
  140. " addxcc %1,%0,%0,icc0 \n"
  141. " ldi @(%4,12),%2 \n"
  142. " addxcc %2,%0,%0,icc0 \n"
  143. /* fold the 33-bit result into 16-bits */
  144. " addxcc gr0,%0,%0,icc0 \n"
  145. " srli %0,#16,%1 \n"
  146. " sethi #0,%0 \n"
  147. " add %1,%0,%0 \n"
  148. " srli %0,#16,%1 \n"
  149. " add %1,%0,%0 \n"
  150. : "=r" (sum), "=&r" (tmp), "=r" (tmp2)
  151. : "r" (saddr), "r" (daddr), "0" (sum), "2" (len + proto)
  152. : "icc0"
  153. );
  154. return (__force __sum16)~sum;
  155. }
  156. #endif /* _ASM_CHECKSUM_H */