checksum.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 unsigned int csum_partial(const unsigned char *buff,
  32. int len, unsigned int sum);
  33. /*
  34. * The same as csum_partial, but copies from src while it checksums.
  35. *
  36. * Here even more important to align src and dst on a 32-bit (or even
  37. * better 64-bit) boundary
  38. */
  39. extern unsigned int csum_partial_copy_nocheck(const unsigned char *src,
  40. unsigned char *dst,
  41. int len, unsigned int sum);
  42. /*
  43. * This is a new version of the above that records errors it finds in *errp,
  44. * but continues and zeros thre rest of the buffer.
  45. */
  46. extern unsigned int csum_partial_copy_from_user(const unsigned char __user *src,
  47. unsigned char *dst,
  48. int len, unsigned int sum,
  49. int *err_ptr);
  50. /*
  51. * Fold a partial checksum
  52. */
  53. static inline unsigned int csum_fold(unsigned int sum)
  54. {
  55. unsigned long tmpreg;
  56. __asm__(
  57. " sll3 %1, %0, #16 \n"
  58. " cmp %0, %0 \n"
  59. " addx %0, %1 \n"
  60. " ldi %1, #0 \n"
  61. " srli %0, #16 \n"
  62. " addx %0, %1 \n"
  63. " xor3 %0, %0, #0x0000ffff \n"
  64. : "=r" (sum), "=&r" (tmpreg)
  65. : "0" (sum)
  66. : "cbit"
  67. );
  68. return sum;
  69. }
  70. /*
  71. * This is a version of ip_compute_csum() optimized for IP headers,
  72. * which always checksum on 4 octet boundaries.
  73. */
  74. static inline unsigned short ip_fast_csum(unsigned char * iph,
  75. unsigned int ihl) {
  76. unsigned long sum, tmpreg0, tmpreg1;
  77. __asm__ __volatile__(
  78. " ld %0, @%1+ \n"
  79. " addi %2, #-4 \n"
  80. "# bgez %2, 2f \n"
  81. " cmp %0, %0 \n"
  82. " ld %3, @%1+ \n"
  83. " ld %4, @%1+ \n"
  84. " addx %0, %3 \n"
  85. " ld %3, @%1+ \n"
  86. " addx %0, %4 \n"
  87. " addx %0, %3 \n"
  88. " .fillinsn\n"
  89. "1: \n"
  90. " ld %4, @%1+ \n"
  91. " addi %2, #-1 \n"
  92. " addx %0, %4 \n"
  93. " bgtz %2, 1b \n"
  94. "\n"
  95. " ldi %3, #0 \n"
  96. " addx %0, %3 \n"
  97. " .fillinsn\n"
  98. "2: \n"
  99. /* Since the input registers which are loaded with iph and ihl
  100. are modified, we must also specify them as outputs, or gcc
  101. will assume they contain their original values. */
  102. : "=&r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmpreg0), "=&r" (tmpreg1)
  103. : "1" (iph), "2" (ihl)
  104. : "cbit", "memory");
  105. return csum_fold(sum);
  106. }
  107. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  108. unsigned long daddr,
  109. unsigned short len,
  110. unsigned short proto,
  111. unsigned int sum)
  112. {
  113. #if defined(__LITTLE_ENDIAN)
  114. unsigned long len_proto = (ntohs(len)<<16)+proto*256;
  115. #else
  116. unsigned long len_proto = (proto<<16)+len;
  117. #endif
  118. unsigned long tmpreg;
  119. __asm__(
  120. " cmp %0, %0 \n"
  121. " addx %0, %2 \n"
  122. " addx %0, %3 \n"
  123. " addx %0, %4 \n"
  124. " ldi %1, #0 \n"
  125. " addx %0, %1 \n"
  126. : "=r" (sum), "=&r" (tmpreg)
  127. : "r" (daddr), "r" (saddr), "r" (len_proto), "0" (sum)
  128. : "cbit"
  129. );
  130. return sum;
  131. }
  132. /*
  133. * computes the checksum of the TCP/UDP pseudo-header
  134. * returns a 16-bit checksum, already complemented
  135. */
  136. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  137. unsigned long daddr,
  138. unsigned short len,
  139. unsigned short proto,
  140. unsigned int sum)
  141. {
  142. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  143. }
  144. /*
  145. * this routine is used for miscellaneous IP-like checksums, mainly
  146. * in icmp.c
  147. */
  148. static inline unsigned short ip_compute_csum(unsigned char * buff, int len) {
  149. return csum_fold (csum_partial(buff, len, 0));
  150. }
  151. #define _HAVE_ARCH_IPV6_CSUM
  152. static inline unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  153. struct in6_addr *daddr,
  154. __u16 len,
  155. unsigned short proto,
  156. unsigned int sum)
  157. {
  158. unsigned long tmpreg0, tmpreg1, tmpreg2, tmpreg3;
  159. __asm__(
  160. " ld %1, @(%5) \n"
  161. " ld %2, @(4,%5) \n"
  162. " ld %3, @(8,%5) \n"
  163. " ld %4, @(12,%5) \n"
  164. " add %0, %1 \n"
  165. " addx %0, %2 \n"
  166. " addx %0, %3 \n"
  167. " addx %0, %4 \n"
  168. " ld %1, @(%6) \n"
  169. " ld %2, @(4,%6) \n"
  170. " ld %3, @(8,%6) \n"
  171. " ld %4, @(12,%6) \n"
  172. " addx %0, %1 \n"
  173. " addx %0, %2 \n"
  174. " addx %0, %3 \n"
  175. " addx %0, %4 \n"
  176. " addx %0, %7 \n"
  177. " addx %0, %8 \n"
  178. " ldi %1, #0 \n"
  179. " addx %0, %1 \n"
  180. : "=&r" (sum), "=&r" (tmpreg0), "=&r" (tmpreg1),
  181. "=&r" (tmpreg2), "=&r" (tmpreg3)
  182. : "r" (saddr), "r" (daddr),
  183. "r" (htonl((__u32) (len))), "r" (htonl(proto)), "0" (sum)
  184. : "cbit"
  185. );
  186. return csum_fold(sum);
  187. }
  188. #endif /* _ASM_M32R_CHECKSUM_H */
  189. #endif /* __KERNEL__ */