checksum.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, int len);
  20. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  21. extern __wsum csum_and_copy_from_user(const void __user *src, void *dst,
  22. int len);
  23. #define HAVE_CSUM_COPY_USER
  24. extern __wsum csum_and_copy_to_user(const void *src, void __user *dst,
  25. int len);
  26. #define _HAVE_ARCH_CSUM_AND_COPY
  27. #define csum_partial_copy_nocheck(src, dst, len) \
  28. csum_partial_copy_generic((src), (dst), (len))
  29. /*
  30. * turns a 32-bit partial checksum (e.g. from csum_partial) into a
  31. * 1's complement 16-bit checksum.
  32. */
  33. static inline __sum16 csum_fold(__wsum sum)
  34. {
  35. u32 tmp = (__force u32)sum;
  36. /*
  37. * swap the two 16-bit halves of sum
  38. * if there is a carry from adding the two 16-bit halves,
  39. * it will carry from the lower half into the upper half,
  40. * giving us the correct sum in the upper half.
  41. */
  42. return (__force __sum16)(~(tmp + rol32(tmp, 16)) >> 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 __always_inline __wsum csum_add(__wsum csum, __wsum addend)
  84. {
  85. #ifdef __powerpc64__
  86. u64 res = (__force u64)csum;
  87. res += (__force u64)addend;
  88. return (__force __wsum)((u32)res + (res >> 32));
  89. #else
  90. if (__builtin_constant_p(csum) && csum == 0)
  91. return addend;
  92. if (__builtin_constant_p(addend) && addend == 0)
  93. return csum;
  94. asm("addc %0,%0,%1;"
  95. "addze %0,%0;"
  96. : "+r" (csum) : "r" (addend) : "xer");
  97. return csum;
  98. #endif
  99. }
  100. #define HAVE_ARCH_CSUM_SHIFT
  101. static __always_inline __wsum csum_shift(__wsum sum, int offset)
  102. {
  103. /* rotate sum to align it with a 16b boundary */
  104. return (__force __wsum)rol32((__force u32)sum, (offset & 1) << 3);
  105. }
  106. /*
  107. * This is a version of ip_compute_csum() optimized for IP headers,
  108. * which always checksum on 4 octet boundaries. ihl is the number
  109. * of 32-bit words and is always >= 5.
  110. */
  111. static inline __wsum ip_fast_csum_nofold(const void *iph, unsigned int ihl)
  112. {
  113. const u32 *ptr = (const u32 *)iph + 1;
  114. #ifdef __powerpc64__
  115. unsigned int i;
  116. u64 s = *(const u32 *)iph;
  117. for (i = 0; i < ihl - 1; i++, ptr++)
  118. s += *ptr;
  119. return (__force __wsum)from64to32(s);
  120. #else
  121. __wsum sum, tmp;
  122. asm("mtctr %3;"
  123. "addc %0,%4,%5;"
  124. "1: lwzu %1, 4(%2);"
  125. "adde %0,%0,%1;"
  126. "bdnz 1b;"
  127. "addze %0,%0;"
  128. : "=r" (sum), "=r" (tmp), "+b" (ptr)
  129. : "r" (ihl - 2), "r" (*(const u32 *)iph), "r" (*ptr)
  130. : "ctr", "xer", "memory");
  131. return sum;
  132. #endif
  133. }
  134. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  135. {
  136. return csum_fold(ip_fast_csum_nofold(iph, ihl));
  137. }
  138. /*
  139. * computes the checksum of a memory block at buff, length len,
  140. * and adds in "sum" (32-bit)
  141. *
  142. * returns a 32-bit number suitable for feeding into itself
  143. * or csum_tcpudp_magic
  144. *
  145. * this function must be called with even lengths, except
  146. * for the last fragment, which may be odd
  147. *
  148. * it's best to have buff aligned on a 32-bit boundary
  149. */
  150. __wsum __csum_partial(const void *buff, int len, __wsum sum);
  151. static __always_inline __wsum csum_partial(const void *buff, int len, __wsum sum)
  152. {
  153. if (__builtin_constant_p(len) && len <= 16 && (len & 1) == 0) {
  154. if (len == 2)
  155. sum = csum_add(sum, (__force __wsum)*(const u16 *)buff);
  156. if (len >= 4)
  157. sum = csum_add(sum, (__force __wsum)*(const u32 *)buff);
  158. if (len == 6)
  159. sum = csum_add(sum, (__force __wsum)
  160. *(const u16 *)(buff + 4));
  161. if (len >= 8)
  162. sum = csum_add(sum, (__force __wsum)
  163. *(const u32 *)(buff + 4));
  164. if (len == 10)
  165. sum = csum_add(sum, (__force __wsum)
  166. *(const u16 *)(buff + 8));
  167. if (len >= 12)
  168. sum = csum_add(sum, (__force __wsum)
  169. *(const u32 *)(buff + 8));
  170. if (len == 14)
  171. sum = csum_add(sum, (__force __wsum)
  172. *(const u16 *)(buff + 12));
  173. if (len >= 16)
  174. sum = csum_add(sum, (__force __wsum)
  175. *(const u32 *)(buff + 12));
  176. } else if (__builtin_constant_p(len) && (len & 3) == 0) {
  177. sum = csum_add(sum, ip_fast_csum_nofold(buff, len >> 2));
  178. } else {
  179. sum = __csum_partial(buff, len, sum);
  180. }
  181. return sum;
  182. }
  183. /*
  184. * this routine is used for miscellaneous IP-like checksums, mainly
  185. * in icmp.c
  186. */
  187. static inline __sum16 ip_compute_csum(const void *buff, int len)
  188. {
  189. return csum_fold(csum_partial(buff, len, 0));
  190. }
  191. #define _HAVE_ARCH_IPV6_CSUM
  192. __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  193. const struct in6_addr *daddr,
  194. __u32 len, __u8 proto, __wsum sum);
  195. #endif /* __KERNEL__ */
  196. #endif