checksum.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/config.h>
  13. #include <linux/in6.h>
  14. #include <xtensa/config/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 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int 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 unsigned int csum_partial_copy_generic( const char *src, char *dst, int len, int sum,
  36. int *src_err_ptr, int *dst_err_ptr);
  37. /*
  38. * Note: when you get a NULL pointer exception here this means someone
  39. * passed in an incorrect kernel address to one of these functions.
  40. *
  41. * If you use these functions directly please don't forget the
  42. * verify_area().
  43. */
  44. static inline
  45. unsigned int csum_partial_copy_nocheck ( const char *src, char *dst,
  46. int len, int sum)
  47. {
  48. return csum_partial_copy_generic ( src, dst, len, sum, NULL, NULL);
  49. }
  50. static inline
  51. unsigned int csum_partial_copy_from_user ( const char *src, char *dst,
  52. int len, int sum, int *err_ptr)
  53. {
  54. return csum_partial_copy_generic ( src, dst, len, sum, err_ptr, NULL);
  55. }
  56. /*
  57. * These are the old (and unsafe) way of doing checksums, a warning message will be
  58. * printed if they are used and an exeption occurs.
  59. *
  60. * these functions should go away after some time.
  61. */
  62. #define csum_partial_copy_fromuser csum_partial_copy
  63. unsigned int csum_partial_copy( const char *src, char *dst, int len, int sum);
  64. /*
  65. * Fold a partial checksum
  66. */
  67. static __inline__ unsigned int csum_fold(unsigned int sum)
  68. {
  69. unsigned int __dummy;
  70. __asm__("extui %1, %0, 16, 16\n\t"
  71. "extui %0 ,%0, 0, 16\n\t"
  72. "add %0, %0, %1\n\t"
  73. "slli %1, %0, 16\n\t"
  74. "add %0, %0, %1\n\t"
  75. "extui %0, %0, 16, 16\n\t"
  76. "neg %0, %0\n\t"
  77. "addi %0, %0, -1\n\t"
  78. "extui %0, %0, 0, 16\n\t"
  79. : "=r" (sum), "=&r" (__dummy)
  80. : "0" (sum));
  81. return sum;
  82. }
  83. /*
  84. * This is a version of ip_compute_csum() optimized for IP headers,
  85. * which always checksum on 4 octet boundaries.
  86. */
  87. static __inline__ unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
  88. {
  89. unsigned int sum, tmp, endaddr;
  90. __asm__ __volatile__(
  91. "sub %0, %0, %0\n\t"
  92. #if XCHAL_HAVE_LOOPS
  93. "loopgtz %2, 2f\n\t"
  94. #else
  95. "beqz %2, 2f\n\t"
  96. "slli %4, %2, 2\n\t"
  97. "add %4, %4, %1\n\t"
  98. "0:\t"
  99. #endif
  100. "l32i %3, %1, 0\n\t"
  101. "add %0, %0, %3\n\t"
  102. "bgeu %0, %3, 1f\n\t"
  103. "addi %0, %0, 1\n\t"
  104. "1:\t"
  105. "addi %1, %1, 4\n\t"
  106. #if !XCHAL_HAVE_LOOPS
  107. "blt %1, %4, 0b\n\t"
  108. #endif
  109. "2:\t"
  110. /* Since the input registers which are loaded with iph and ihl
  111. are modified, we must also specify them as outputs, or gcc
  112. will assume they contain their original values. */
  113. : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (tmp), "=&r" (endaddr)
  114. : "1" (iph), "2" (ihl));
  115. return csum_fold(sum);
  116. }
  117. static __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
  118. unsigned long daddr,
  119. unsigned short len,
  120. unsigned short proto,
  121. unsigned int sum)
  122. {
  123. #ifdef __XTENSA_EL__
  124. unsigned long len_proto = (ntohs(len)<<16)+proto*256;
  125. #elif defined(__XTENSA_EB__)
  126. unsigned long len_proto = (proto<<16)+len;
  127. #else
  128. # error processor byte order undefined!
  129. #endif
  130. __asm__("add %0, %0, %1\n\t"
  131. "bgeu %0, %1, 1f\n\t"
  132. "addi %0, %0, 1\n\t"
  133. "1:\t"
  134. "add %0, %0, %2\n\t"
  135. "bgeu %0, %2, 1f\n\t"
  136. "addi %0, %0, 1\n\t"
  137. "1:\t"
  138. "add %0, %0, %3\n\t"
  139. "bgeu %0, %3, 1f\n\t"
  140. "addi %0, %0, 1\n\t"
  141. "1:\t"
  142. : "=r" (sum), "=r" (len_proto)
  143. : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum));
  144. return sum;
  145. }
  146. /*
  147. * computes the checksum of the TCP/UDP pseudo-header
  148. * returns a 16-bit checksum, already complemented
  149. */
  150. static __inline__ unsigned short int csum_tcpudp_magic(unsigned long saddr,
  151. unsigned long daddr,
  152. unsigned short len,
  153. unsigned short proto,
  154. unsigned int sum)
  155. {
  156. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  157. }
  158. /*
  159. * this routine is used for miscellaneous IP-like checksums, mainly
  160. * in icmp.c
  161. */
  162. static __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len)
  163. {
  164. return csum_fold (csum_partial(buff, len, 0));
  165. }
  166. #define _HAVE_ARCH_IPV6_CSUM
  167. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  168. struct in6_addr *daddr,
  169. __u32 len,
  170. unsigned short proto,
  171. unsigned int sum)
  172. {
  173. unsigned int __dummy;
  174. __asm__("l32i %1, %2, 0\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, 4\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, %2, 8\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, %2, 12\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, 0\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, 4\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. "l32i %1, %3, 8\n\t"
  205. "add %0, %0, %1\n\t"
  206. "bgeu %0, %1, 1f\n\t"
  207. "addi %0, %0, 1\n\t"
  208. "1:\t"
  209. "l32i %1, %3, 12\n\t"
  210. "add %0, %0, %1\n\t"
  211. "bgeu %0, %1, 1f\n\t"
  212. "addi %0, %0, 1\n\t"
  213. "1:\t"
  214. "add %0, %0, %4\n\t"
  215. "bgeu %0, %4, 1f\n\t"
  216. "addi %0, %0, 1\n\t"
  217. "1:\t"
  218. "add %0, %0, %5\n\t"
  219. "bgeu %0, %5, 1f\n\t"
  220. "addi %0, %0, 1\n\t"
  221. "1:\t"
  222. : "=r" (sum), "=&r" (__dummy)
  223. : "r" (saddr), "r" (daddr),
  224. "r" (htonl(len)), "r" (htonl(proto)), "0" (sum));
  225. return csum_fold(sum);
  226. }
  227. /*
  228. * Copy and checksum to user
  229. */
  230. #define HAVE_CSUM_COPY_USER
  231. static __inline__ unsigned int csum_and_copy_to_user (const char *src, char *dst,
  232. int len, int sum, int *err_ptr)
  233. {
  234. if (access_ok(VERIFY_WRITE, dst, len))
  235. return csum_partial_copy_generic(src, dst, len, sum, NULL, err_ptr);
  236. if (len)
  237. *err_ptr = -EFAULT;
  238. return -1; /* invalid checksum */
  239. }
  240. #endif