checksum.h 6.4 KB

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