checksum.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #ifndef _ASM_POWERPC_CHECKSUM_H
  2. #define _ASM_POWERPC_CHECKSUM_H
  3. #ifdef __KERNEL__
  4. /*
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #ifdef CONFIG_GENERIC_CSUM
  11. #include <asm-generic/checksum.h>
  12. #else
  13. /*
  14. * Computes the checksum of a memory block at src, length len,
  15. * and adds in "sum" (32-bit), while copying the block to dst.
  16. * If an access exception occurs on src or dst, it stores -EFAULT
  17. * to *src_err or *dst_err respectively (if that pointer is not
  18. * NULL), and, for an error on src, zeroes the rest of dst.
  19. *
  20. * Like csum_partial, this must be called with even lengths,
  21. * except for the last fragment.
  22. */
  23. extern __wsum csum_partial_copy_generic(const void *src, void *dst,
  24. int len, __wsum sum,
  25. int *src_err, int *dst_err);
  26. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  27. extern __wsum csum_and_copy_from_user(const void __user *src, void *dst,
  28. int len, __wsum sum, int *err_ptr);
  29. #define HAVE_CSUM_COPY_USER
  30. extern __wsum csum_and_copy_to_user(const void *src, void __user *dst,
  31. int len, __wsum sum, int *err_ptr);
  32. #define csum_partial_copy_nocheck(src, dst, len, sum) \
  33. csum_partial_copy_generic((src), (dst), (len), (sum), NULL, NULL)
  34. /*
  35. * turns a 32-bit partial checksum (e.g. from csum_partial) into a
  36. * 1's complement 16-bit checksum.
  37. */
  38. static inline __sum16 csum_fold(__wsum sum)
  39. {
  40. unsigned int tmp;
  41. /* swap the two 16-bit halves of sum */
  42. __asm__("rlwinm %0,%1,16,0,31" : "=r" (tmp) : "r" (sum));
  43. /* if there is a carry from adding the two 16-bit halves,
  44. it will carry from the lower half into the upper half,
  45. giving us the correct sum in the upper half. */
  46. return (__force __sum16)(~((__force u32)sum + tmp) >> 16);
  47. }
  48. static inline u32 from64to32(u64 x)
  49. {
  50. /* add up 32-bit and 32-bit for 32+c bit */
  51. x = (x & 0xffffffff) + (x >> 32);
  52. /* add up carry.. */
  53. x = (x & 0xffffffff) + (x >> 32);
  54. return (u32)x;
  55. }
  56. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  57. __u8 proto, __wsum sum)
  58. {
  59. #ifdef __powerpc64__
  60. u64 s = (__force u32)sum;
  61. s += (__force u32)saddr;
  62. s += (__force u32)daddr;
  63. #ifdef __BIG_ENDIAN__
  64. s += proto + len;
  65. #else
  66. s += (proto + len) << 8;
  67. #endif
  68. return (__force __wsum) from64to32(s);
  69. #else
  70. __asm__("\n\
  71. addc %0,%0,%1 \n\
  72. adde %0,%0,%2 \n\
  73. adde %0,%0,%3 \n\
  74. addze %0,%0 \n\
  75. "
  76. : "=r" (sum)
  77. : "r" (daddr), "r"(saddr), "r"(proto + len), "0"(sum));
  78. return sum;
  79. #endif
  80. }
  81. /*
  82. * computes the checksum of the TCP/UDP pseudo-header
  83. * returns a 16-bit checksum, already complemented
  84. */
  85. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  86. __u8 proto, __wsum sum)
  87. {
  88. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  89. }
  90. #define HAVE_ARCH_CSUM_ADD
  91. static inline __wsum csum_add(__wsum csum, __wsum addend)
  92. {
  93. #ifdef __powerpc64__
  94. u64 res = (__force u64)csum;
  95. #endif
  96. if (__builtin_constant_p(csum) && csum == 0)
  97. return addend;
  98. if (__builtin_constant_p(addend) && addend == 0)
  99. return csum;
  100. #ifdef __powerpc64__
  101. res += (__force u64)addend;
  102. return (__force __wsum)((u32)res + (res >> 32));
  103. #else
  104. asm("addc %0,%0,%1;"
  105. "addze %0,%0;"
  106. : "+r" (csum) : "r" (addend) : "xer");
  107. return csum;
  108. #endif
  109. }
  110. /*
  111. * This is a version of ip_compute_csum() optimized for IP headers,
  112. * which always checksum on 4 octet boundaries. ihl is the number
  113. * of 32-bit words and is always >= 5.
  114. */
  115. static inline __wsum ip_fast_csum_nofold(const void *iph, unsigned int ihl)
  116. {
  117. const u32 *ptr = (const u32 *)iph + 1;
  118. #ifdef __powerpc64__
  119. unsigned int i;
  120. u64 s = *(const u32 *)iph;
  121. for (i = 0; i < ihl - 1; i++, ptr++)
  122. s += *ptr;
  123. return (__force __wsum)from64to32(s);
  124. #else
  125. __wsum sum, tmp;
  126. asm("mtctr %3;"
  127. "addc %0,%4,%5;"
  128. "1: lwzu %1, 4(%2);"
  129. "adde %0,%0,%1;"
  130. "bdnz 1b;"
  131. "addze %0,%0;"
  132. : "=r" (sum), "=r" (tmp), "+b" (ptr)
  133. : "r" (ihl - 2), "r" (*(const u32 *)iph), "r" (*ptr)
  134. : "ctr", "xer", "memory");
  135. return sum;
  136. #endif
  137. }
  138. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  139. {
  140. return csum_fold(ip_fast_csum_nofold(iph, ihl));
  141. }
  142. /*
  143. * computes the checksum of a memory block at buff, length len,
  144. * and adds in "sum" (32-bit)
  145. *
  146. * returns a 32-bit number suitable for feeding into itself
  147. * or csum_tcpudp_magic
  148. *
  149. * this function must be called with even lengths, except
  150. * for the last fragment, which may be odd
  151. *
  152. * it's best to have buff aligned on a 32-bit boundary
  153. */
  154. __wsum __csum_partial(const void *buff, int len, __wsum sum);
  155. static inline __wsum csum_partial(const void *buff, int len, __wsum sum)
  156. {
  157. if (__builtin_constant_p(len) && len <= 16 && (len & 1) == 0) {
  158. if (len == 2)
  159. sum = csum_add(sum, (__force __wsum)*(const u16 *)buff);
  160. if (len >= 4)
  161. sum = csum_add(sum, (__force __wsum)*(const u32 *)buff);
  162. if (len == 6)
  163. sum = csum_add(sum, (__force __wsum)
  164. *(const u16 *)(buff + 4));
  165. if (len >= 8)
  166. sum = csum_add(sum, (__force __wsum)
  167. *(const u32 *)(buff + 4));
  168. if (len == 10)
  169. sum = csum_add(sum, (__force __wsum)
  170. *(const u16 *)(buff + 8));
  171. if (len >= 12)
  172. sum = csum_add(sum, (__force __wsum)
  173. *(const u32 *)(buff + 8));
  174. if (len == 14)
  175. sum = csum_add(sum, (__force __wsum)
  176. *(const u16 *)(buff + 12));
  177. if (len >= 16)
  178. sum = csum_add(sum, (__force __wsum)
  179. *(const u32 *)(buff + 12));
  180. } else if (__builtin_constant_p(len) && (len & 3) == 0) {
  181. sum = csum_add(sum, ip_fast_csum_nofold(buff, len >> 2));
  182. } else {
  183. sum = __csum_partial(buff, len, sum);
  184. }
  185. return sum;
  186. }
  187. /*
  188. * this routine is used for miscellaneous IP-like checksums, mainly
  189. * in icmp.c
  190. */
  191. static inline __sum16 ip_compute_csum(const void *buff, int len)
  192. {
  193. return csum_fold(csum_partial(buff, len, 0));
  194. }
  195. #endif
  196. #endif /* __KERNEL__ */
  197. #endif