checksum.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  49. __u8 proto, __wsum sum)
  50. {
  51. #ifdef __powerpc64__
  52. unsigned long s = (__force u32)sum;
  53. s += (__force u32)saddr;
  54. s += (__force u32)daddr;
  55. s += proto + len;
  56. s += (s >> 32);
  57. return (__force __wsum) s;
  58. #else
  59. __asm__("\n\
  60. addc %0,%0,%1 \n\
  61. adde %0,%0,%2 \n\
  62. adde %0,%0,%3 \n\
  63. addze %0,%0 \n\
  64. "
  65. : "=r" (sum)
  66. : "r" (daddr), "r"(saddr), "r"(proto + len), "0"(sum));
  67. return sum;
  68. #endif
  69. }
  70. /*
  71. * computes the checksum of the TCP/UDP pseudo-header
  72. * returns a 16-bit checksum, already complemented
  73. */
  74. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  75. __u8 proto, __wsum sum)
  76. {
  77. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  78. }
  79. #define HAVE_ARCH_CSUM_ADD
  80. static inline __wsum csum_add(__wsum csum, __wsum addend)
  81. {
  82. #ifdef __powerpc64__
  83. u64 res = (__force u64)csum;
  84. #endif
  85. if (__builtin_constant_p(csum) && csum == 0)
  86. return addend;
  87. if (__builtin_constant_p(addend) && addend == 0)
  88. return csum;
  89. #ifdef __powerpc64__
  90. res += (__force u64)addend;
  91. return (__force __wsum)((u32)res + (res >> 32));
  92. #else
  93. asm("addc %0,%0,%1;"
  94. "addze %0,%0;"
  95. : "+r" (csum) : "r" (addend) : "xer");
  96. return csum;
  97. #endif
  98. }
  99. /*
  100. * This is a version of ip_compute_csum() optimized for IP headers,
  101. * which always checksum on 4 octet boundaries. ihl is the number
  102. * of 32-bit words and is always >= 5.
  103. */
  104. static inline __wsum ip_fast_csum_nofold(const void *iph, unsigned int ihl)
  105. {
  106. const u32 *ptr = (const u32 *)iph + 1;
  107. #ifdef __powerpc64__
  108. unsigned int i;
  109. u64 s = *(const u32 *)iph;
  110. for (i = 0; i < ihl - 1; i++, ptr++)
  111. s += *ptr;
  112. s += (s >> 32);
  113. return (__force __wsum)s;
  114. #else
  115. __wsum sum, tmp;
  116. asm("mtctr %3;"
  117. "addc %0,%4,%5;"
  118. "1: lwzu %1, 4(%2);"
  119. "adde %0,%0,%1;"
  120. "bdnz 1b;"
  121. "addze %0,%0;"
  122. : "=r" (sum), "=r" (tmp), "+b" (ptr)
  123. : "r" (ihl - 2), "r" (*(const u32 *)iph), "r" (*ptr)
  124. : "ctr", "xer", "memory");
  125. return sum;
  126. #endif
  127. }
  128. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  129. {
  130. return csum_fold(ip_fast_csum_nofold(iph, ihl));
  131. }
  132. /*
  133. * computes the checksum of a memory block at buff, length len,
  134. * and adds in "sum" (32-bit)
  135. *
  136. * returns a 32-bit number suitable for feeding into itself
  137. * or csum_tcpudp_magic
  138. *
  139. * this function must be called with even lengths, except
  140. * for the last fragment, which may be odd
  141. *
  142. * it's best to have buff aligned on a 32-bit boundary
  143. */
  144. __wsum __csum_partial(const void *buff, int len, __wsum sum);
  145. static inline __wsum csum_partial(const void *buff, int len, __wsum sum)
  146. {
  147. if (__builtin_constant_p(len) && len <= 16 && (len & 1) == 0) {
  148. if (len == 2)
  149. sum = csum_add(sum, (__force __wsum)*(const u16 *)buff);
  150. if (len >= 4)
  151. sum = csum_add(sum, (__force __wsum)*(const u32 *)buff);
  152. if (len == 6)
  153. sum = csum_add(sum, (__force __wsum)
  154. *(const u16 *)(buff + 4));
  155. if (len >= 8)
  156. sum = csum_add(sum, (__force __wsum)
  157. *(const u32 *)(buff + 4));
  158. if (len == 10)
  159. sum = csum_add(sum, (__force __wsum)
  160. *(const u16 *)(buff + 8));
  161. if (len >= 12)
  162. sum = csum_add(sum, (__force __wsum)
  163. *(const u32 *)(buff + 8));
  164. if (len == 14)
  165. sum = csum_add(sum, (__force __wsum)
  166. *(const u16 *)(buff + 12));
  167. if (len >= 16)
  168. sum = csum_add(sum, (__force __wsum)
  169. *(const u32 *)(buff + 12));
  170. } else if (__builtin_constant_p(len) && (len & 3) == 0) {
  171. sum = csum_add(sum, ip_fast_csum_nofold(buff, len >> 2));
  172. } else {
  173. sum = __csum_partial(buff, len, sum);
  174. }
  175. return sum;
  176. }
  177. /*
  178. * this routine is used for miscellaneous IP-like checksums, mainly
  179. * in icmp.c
  180. */
  181. static inline __sum16 ip_compute_csum(const void *buff, int len)
  182. {
  183. return csum_fold(csum_partial(buff, len, 0));
  184. }
  185. #endif
  186. #endif /* __KERNEL__ */
  187. #endif