checksum.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifdef __KERNEL__
  2. #ifndef _ASM_M32R_CHECKSUM_H
  3. #define _ASM_M32R_CHECKSUM_H
  4. /*
  5. * include/asm-m32r/checksum.h
  6. *
  7. * IP/TCP/UDP checksum routines
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. *
  13. * Some code taken from mips and parisc architecture.
  14. *
  15. * Copyright (C) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata
  16. * Copyright (C) 2004 Hirokazu Takata <takata at linux-m32r.org>
  17. */
  18. #include <linux/in6.h>
  19. /*
  20. * computes the checksum of a memory block at buff, length len,
  21. * and adds in "sum" (32-bit)
  22. *
  23. * returns a 32-bit number suitable for feeding into itself
  24. * or csum_tcpudp_magic
  25. *
  26. * this function must be called with even lengths, except
  27. * for the last fragment, which may be odd
  28. *
  29. * it's best to have buff aligned on a 32-bit boundary
  30. */
  31. asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
  32. /*
  33. * The same as csum_partial, but copies from src while it checksums.
  34. *
  35. * Here even more important to align src and dst on a 32-bit (or even
  36. * better 64-bit) boundary
  37. */
  38. extern __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  39. int len, __wsum sum);
  40. /*
  41. * This is a new version of the above that records errors it finds in *errp,
  42. * but continues and zeros thre rest of the buffer.
  43. */
  44. extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  45. int len, __wsum sum,
  46. int *err_ptr);
  47. /*
  48. * Fold a partial checksum
  49. */
  50. static inline __sum16 csum_fold(__wsum sum)
  51. {
  52. unsigned long tmpreg;
  53. __asm__(
  54. " sll3 %1, %0, #16 \n"
  55. " cmp %0, %0 \n"
  56. " addx %0, %1 \n"
  57. " ldi %1, #0 \n"
  58. " srli %0, #16 \n"
  59. " addx %0, %1 \n"
  60. " xor3 %0, %0, #0x0000ffff \n"
  61. : "=r" (sum), "=&r" (tmpreg)
  62. : "0" (sum)
  63. : "cbit"
  64. );
  65. return (__force __sum16)sum;
  66. }
  67. /*
  68. * This is a version of ip_compute_csum() optimized for IP headers,
  69. * which always checksum on 4 octet boundaries.
  70. */
  71. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  72. {
  73. unsigned long tmpreg0, tmpreg1;
  74. __wsum sum;
  75. __asm__ __volatile__(
  76. " ld %0, @%1+ \n"
  77. " addi %2, #-4 \n"
  78. "# bgez %2, 2f \n"
  79. " cmp %0, %0 \n"
  80. " ld %3, @%1+ \n"
  81. " ld %4, @%1+ \n"
  82. " addx %0, %3 \n"
  83. " ld %3, @%1+ \n"
  84. " addx %0, %4 \n"
  85. " addx %0, %3 \n"
  86. " .fillinsn\n"
  87. "1: \n"
  88. " ld %4, @%1+ \n"
  89. " addi %2, #-1 \n"
  90. " addx %0, %4 \n"
  91. " bgtz %2, 1b \n"
  92. "\n"
  93. " ldi %3, #0 \n"
  94. " addx %0, %3 \n"
  95. " .fillinsn\n"
  96. "2: \n"
  97. /* Since the input registers which are loaded with iph and ihl
  98. are modified, we must also specify them as outputs, or gcc
  99. will assume they contain their original values. */
  100. : "=&r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmpreg0), "=&r" (tmpreg1)
  101. : "1" (iph), "2" (ihl)
  102. : "cbit", "memory");
  103. return csum_fold(sum);
  104. }
  105. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  106. unsigned short len,
  107. unsigned short proto,
  108. __wsum sum)
  109. {
  110. #if defined(__LITTLE_ENDIAN)
  111. unsigned long len_proto = (proto + len) << 8;
  112. #else
  113. unsigned long len_proto = proto + len;
  114. #endif
  115. unsigned long tmpreg;
  116. __asm__(
  117. " cmp %0, %0 \n"
  118. " addx %0, %2 \n"
  119. " addx %0, %3 \n"
  120. " addx %0, %4 \n"
  121. " ldi %1, #0 \n"
  122. " addx %0, %1 \n"
  123. : "=r" (sum), "=&r" (tmpreg)
  124. : "r" (daddr), "r" (saddr), "r" (len_proto), "0" (sum)
  125. : "cbit"
  126. );
  127. return sum;
  128. }
  129. /*
  130. * computes the checksum of the TCP/UDP pseudo-header
  131. * returns a 16-bit checksum, already complemented
  132. */
  133. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  134. unsigned short len,
  135. unsigned short proto,
  136. __wsum sum)
  137. {
  138. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  139. }
  140. /*
  141. * this routine is used for miscellaneous IP-like checksums, mainly
  142. * in icmp.c
  143. */
  144. static inline __sum16 ip_compute_csum(const void *buff, int len)
  145. {
  146. return csum_fold (csum_partial(buff, len, 0));
  147. }
  148. #define _HAVE_ARCH_IPV6_CSUM
  149. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  150. const struct in6_addr *daddr,
  151. __u32 len, unsigned short proto,
  152. __wsum sum)
  153. {
  154. unsigned long tmpreg0, tmpreg1, tmpreg2, tmpreg3;
  155. __asm__(
  156. " ld %1, @(%5) \n"
  157. " ld %2, @(4,%5) \n"
  158. " ld %3, @(8,%5) \n"
  159. " ld %4, @(12,%5) \n"
  160. " add %0, %1 \n"
  161. " addx %0, %2 \n"
  162. " addx %0, %3 \n"
  163. " addx %0, %4 \n"
  164. " ld %1, @(%6) \n"
  165. " ld %2, @(4,%6) \n"
  166. " ld %3, @(8,%6) \n"
  167. " ld %4, @(12,%6) \n"
  168. " addx %0, %1 \n"
  169. " addx %0, %2 \n"
  170. " addx %0, %3 \n"
  171. " addx %0, %4 \n"
  172. " addx %0, %7 \n"
  173. " addx %0, %8 \n"
  174. " ldi %1, #0 \n"
  175. " addx %0, %1 \n"
  176. : "=&r" (sum), "=&r" (tmpreg0), "=&r" (tmpreg1),
  177. "=&r" (tmpreg2), "=&r" (tmpreg3)
  178. : "r" (saddr), "r" (daddr),
  179. "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
  180. : "cbit"
  181. );
  182. return csum_fold(sum);
  183. }
  184. #endif /* _ASM_M32R_CHECKSUM_H */
  185. #endif /* __KERNEL__ */