/arch/powerpc/include/asm/checksum.h

http://github.com/mirrors/linux · C Header · 215 lines · 146 code · 23 blank · 46 comment · 28 complexity · a75bbd4ef3068ce9f7208aed8fe3c9d8 MD5 · raw file

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