checksum.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * include/asm-xtensa/checksum.h
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_CHECKSUM_H
  11. #define _XTENSA_CHECKSUM_H
  12. #include <linux/in6.h>
  13. #include <asm/uaccess.h>
  14. #include <variant/core.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. asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
  28. /*
  29. * the same as csum_partial, but copies from src while it
  30. * checksums, and handles user-space pointer exceptions correctly, when needed.
  31. *
  32. * here even more important to align src and dst on a 32-bit (or even
  33. * better 64-bit) boundary
  34. */
  35. asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,
  36. int len, __wsum sum,
  37. int *src_err_ptr, int *dst_err_ptr);
  38. /*
  39. * Note: when you get a NULL pointer exception here this means someone
  40. * passed in an incorrect kernel address to one of these functions.
  41. *
  42. * If you use these functions directly please don't forget the access_ok().
  43. */
  44. static inline
  45. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  46. int len, __wsum sum)
  47. {
  48. return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
  49. }
  50. static inline
  51. __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  52. int len, __wsum sum, int *err_ptr)
  53. {
  54. return csum_partial_copy_generic((__force const void *)src, dst,
  55. len, sum, err_ptr, NULL);
  56. }
  57. /*
  58. * Fold a partial checksum
  59. */
  60. static __inline__ __sum16 csum_fold(__wsum sum)
  61. {
  62. unsigned int __dummy;
  63. __asm__("extui %1, %0, 16, 16\n\t"
  64. "extui %0 ,%0, 0, 16\n\t"
  65. "add %0, %0, %1\n\t"
  66. "slli %1, %0, 16\n\t"
  67. "add %0, %0, %1\n\t"
  68. "extui %0, %0, 16, 16\n\t"
  69. "neg %0, %0\n\t"
  70. "addi %0, %0, -1\n\t"
  71. "extui %0, %0, 0, 16\n\t"
  72. : "=r" (sum), "=&r" (__dummy)
  73. : "0" (sum));
  74. return (__force __sum16)sum;
  75. }
  76. /*
  77. * This is a version of ip_compute_csum() optimized for IP headers,
  78. * which always checksum on 4 octet boundaries.
  79. */
  80. static __inline__ __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  81. {
  82. unsigned int sum, tmp, endaddr;
  83. __asm__ __volatile__(
  84. "sub %0, %0, %0\n\t"
  85. #if XCHAL_HAVE_LOOPS
  86. "loopgtz %2, 2f\n\t"
  87. #else
  88. "beqz %2, 2f\n\t"
  89. "slli %4, %2, 2\n\t"
  90. "add %4, %4, %1\n\t"
  91. "0:\t"
  92. #endif
  93. "l32i %3, %1, 0\n\t"
  94. "add %0, %0, %3\n\t"
  95. "bgeu %0, %3, 1f\n\t"
  96. "addi %0, %0, 1\n\t"
  97. "1:\t"
  98. "addi %1, %1, 4\n\t"
  99. #if !XCHAL_HAVE_LOOPS
  100. "blt %1, %4, 0b\n\t"
  101. #endif
  102. "2:\t"
  103. /* Since the input registers which are loaded with iph and ihl
  104. are modified, we must also specify them as outputs, or gcc
  105. will assume they contain their original values. */
  106. : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp),
  107. "=&r" (endaddr)
  108. : "1" (iph), "2" (ihl)
  109. : "memory");
  110. return csum_fold(sum);
  111. }
  112. static __inline__ __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  113. __u32 len, __u8 proto,
  114. __wsum sum)
  115. {
  116. #ifdef __XTENSA_EL__
  117. unsigned long len_proto = (len + proto) << 8;
  118. #elif defined(__XTENSA_EB__)
  119. unsigned long len_proto = len + proto;
  120. #else
  121. # error processor byte order undefined!
  122. #endif
  123. __asm__("add %0, %0, %1\n\t"
  124. "bgeu %0, %1, 1f\n\t"
  125. "addi %0, %0, 1\n\t"
  126. "1:\t"
  127. "add %0, %0, %2\n\t"
  128. "bgeu %0, %2, 1f\n\t"
  129. "addi %0, %0, 1\n\t"
  130. "1:\t"
  131. "add %0, %0, %3\n\t"
  132. "bgeu %0, %3, 1f\n\t"
  133. "addi %0, %0, 1\n\t"
  134. "1:\t"
  135. : "=r" (sum), "=r" (len_proto)
  136. : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
  137. return sum;
  138. }
  139. /*
  140. * computes the checksum of the TCP/UDP pseudo-header
  141. * returns a 16-bit checksum, already complemented
  142. */
  143. static __inline__ __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  144. __u32 len, __u8 proto,
  145. __wsum sum)
  146. {
  147. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  148. }
  149. /*
  150. * this routine is used for miscellaneous IP-like checksums, mainly
  151. * in icmp.c
  152. */
  153. static __inline__ __sum16 ip_compute_csum(const void *buff, int len)
  154. {
  155. return csum_fold (csum_partial(buff, len, 0));
  156. }
  157. #define _HAVE_ARCH_IPV6_CSUM
  158. static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  159. const struct in6_addr *daddr,
  160. __u32 len, __u8 proto,
  161. __wsum sum)
  162. {
  163. unsigned int __dummy;
  164. __asm__("l32i %1, %2, 0\n\t"
  165. "add %0, %0, %1\n\t"
  166. "bgeu %0, %1, 1f\n\t"
  167. "addi %0, %0, 1\n\t"
  168. "1:\t"
  169. "l32i %1, %2, 4\n\t"
  170. "add %0, %0, %1\n\t"
  171. "bgeu %0, %1, 1f\n\t"
  172. "addi %0, %0, 1\n\t"
  173. "1:\t"
  174. "l32i %1, %2, 8\n\t"
  175. "add %0, %0, %1\n\t"
  176. "bgeu %0, %1, 1f\n\t"
  177. "addi %0, %0, 1\n\t"
  178. "1:\t"
  179. "l32i %1, %2, 12\n\t"
  180. "add %0, %0, %1\n\t"
  181. "bgeu %0, %1, 1f\n\t"
  182. "addi %0, %0, 1\n\t"
  183. "1:\t"
  184. "l32i %1, %3, 0\n\t"
  185. "add %0, %0, %1\n\t"
  186. "bgeu %0, %1, 1f\n\t"
  187. "addi %0, %0, 1\n\t"
  188. "1:\t"
  189. "l32i %1, %3, 4\n\t"
  190. "add %0, %0, %1\n\t"
  191. "bgeu %0, %1, 1f\n\t"
  192. "addi %0, %0, 1\n\t"
  193. "1:\t"
  194. "l32i %1, %3, 8\n\t"
  195. "add %0, %0, %1\n\t"
  196. "bgeu %0, %1, 1f\n\t"
  197. "addi %0, %0, 1\n\t"
  198. "1:\t"
  199. "l32i %1, %3, 12\n\t"
  200. "add %0, %0, %1\n\t"
  201. "bgeu %0, %1, 1f\n\t"
  202. "addi %0, %0, 1\n\t"
  203. "1:\t"
  204. "add %0, %0, %4\n\t"
  205. "bgeu %0, %4, 1f\n\t"
  206. "addi %0, %0, 1\n\t"
  207. "1:\t"
  208. "add %0, %0, %5\n\t"
  209. "bgeu %0, %5, 1f\n\t"
  210. "addi %0, %0, 1\n\t"
  211. "1:\t"
  212. : "=r" (sum), "=&r" (__dummy)
  213. : "r" (saddr), "r" (daddr),
  214. "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
  215. : "memory");
  216. return csum_fold(sum);
  217. }
  218. /*
  219. * Copy and checksum to user
  220. */
  221. #define HAVE_CSUM_COPY_USER
  222. static __inline__ __wsum csum_and_copy_to_user(const void *src,
  223. void __user *dst, int len,
  224. __wsum sum, int *err_ptr)
  225. {
  226. if (access_ok(VERIFY_WRITE, dst, len))
  227. return csum_partial_copy_generic(src,dst,len,sum,NULL,err_ptr);
  228. if (len)
  229. *err_ptr = -EFAULT;
  230. return (__force __wsum)-1; /* invalid checksum */
  231. }
  232. #endif