checksum.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. s += proto + len;
  64. return (__force __wsum) from64to32(s);
  65. #else
  66. __asm__("\n\
  67. addc %0,%0,%1 \n\
  68. adde %0,%0,%2 \n\
  69. adde %0,%0,%3 \n\
  70. addze %0,%0 \n\
  71. "
  72. : "=r" (sum)
  73. : "r" (daddr), "r"(saddr), "r"(proto + len), "0"(sum));
  74. return sum;
  75. #endif
  76. }
  77. /*
  78. * computes the checksum of the TCP/UDP pseudo-header
  79. * returns a 16-bit checksum, already complemented
  80. */
  81. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  82. __u8 proto, __wsum sum)
  83. {
  84. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  85. }
  86. #define HAVE_ARCH_CSUM_ADD
  87. static inline __wsum csum_add(__wsum csum, __wsum addend)
  88. {
  89. #ifdef __powerpc64__
  90. u64 res = (__force u64)csum;
  91. #endif
  92. if (__builtin_constant_p(csum) && csum == 0)
  93. return addend;
  94. if (__builtin_constant_p(addend) && addend == 0)
  95. return csum;
  96. #ifdef __powerpc64__
  97. res += (__force u64)addend;
  98. return (__force __wsum) from64to32(res);
  99. #else
  100. asm("addc %0,%0,%1;"
  101. "addze %0,%0;"
  102. : "+r" (csum) : "r" (addend) : "xer");
  103. return csum;
  104. #endif
  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 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. #endif
  192. #endif /* __KERNEL__ */
  193. #endif