checksum.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _PARISC_CHECKSUM_H
  3. #define _PARISC_CHECKSUM_H
  4. #include <linux/in6.h>
  5. /*
  6. * computes the checksum of a memory block at buff, length len,
  7. * and adds in "sum" (32-bit)
  8. *
  9. * returns a 32-bit number suitable for feeding into itself
  10. * or csum_tcpudp_magic
  11. *
  12. * this function must be called with even lengths, except
  13. * for the last fragment, which may be odd
  14. *
  15. * it's best to have buff aligned on a 32-bit boundary
  16. */
  17. extern __wsum csum_partial(const void *, int, __wsum);
  18. /*
  19. * Optimized for IP headers, which always checksum on 4 octet boundaries.
  20. *
  21. * Written by Randolph Chung <[email protected]>, and then mucked with by
  22. * LaMont Jones <[email protected]>
  23. */
  24. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  25. {
  26. unsigned int sum;
  27. unsigned long t0, t1, t2;
  28. __asm__ __volatile__ (
  29. " ldws,ma 4(%1), %0\n"
  30. " addib,<= -4, %2, 2f\n"
  31. "\n"
  32. " ldws 4(%1), %4\n"
  33. " ldws 8(%1), %5\n"
  34. " add %0, %4, %0\n"
  35. " ldws,ma 12(%1), %3\n"
  36. " addc %0, %5, %0\n"
  37. " addc %0, %3, %0\n"
  38. "1: ldws,ma 4(%1), %3\n"
  39. " addib,< 0, %2, 1b\n"
  40. " addc %0, %3, %0\n"
  41. "\n"
  42. " extru %0, 31, 16, %4\n"
  43. " extru %0, 15, 16, %5\n"
  44. " addc %4, %5, %0\n"
  45. " extru %0, 15, 16, %5\n"
  46. " add %0, %5, %0\n"
  47. " subi -1, %0, %0\n"
  48. "2:\n"
  49. : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (t0), "=r" (t1), "=r" (t2)
  50. : "1" (iph), "2" (ihl)
  51. : "memory");
  52. return (__force __sum16)sum;
  53. }
  54. /*
  55. * Fold a partial checksum
  56. */
  57. static inline __sum16 csum_fold(__wsum csum)
  58. {
  59. u32 sum = (__force u32)csum;
  60. /* add the swapped two 16-bit halves of sum,
  61. a possible carry from adding the two 16-bit halves,
  62. will carry from the lower half into the upper half,
  63. giving us the correct sum in the upper half. */
  64. sum += (sum << 16) + (sum >> 16);
  65. return (__force __sum16)(~sum >> 16);
  66. }
  67. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  68. __u32 len, __u8 proto,
  69. __wsum sum)
  70. {
  71. __asm__(
  72. " add %1, %0, %0\n"
  73. " addc %2, %0, %0\n"
  74. " addc %3, %0, %0\n"
  75. " addc %%r0, %0, %0\n"
  76. : "=r" (sum)
  77. : "r" (daddr), "r"(saddr), "r"(proto+len), "0"(sum));
  78. return sum;
  79. }
  80. /*
  81. * computes the checksum of the TCP/UDP pseudo-header
  82. * returns a 16-bit checksum, already complemented
  83. */
  84. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  85. __u32 len, __u8 proto,
  86. __wsum sum)
  87. {
  88. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  89. }
  90. /*
  91. * this routine is used for miscellaneous IP-like checksums, mainly
  92. * in icmp.c
  93. */
  94. static inline __sum16 ip_compute_csum(const void *buf, int len)
  95. {
  96. return csum_fold (csum_partial(buf, len, 0));
  97. }
  98. #define _HAVE_ARCH_IPV6_CSUM
  99. static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  100. const struct in6_addr *daddr,
  101. __u32 len, __u8 proto,
  102. __wsum sum)
  103. {
  104. unsigned long t0, t1, t2, t3;
  105. len += proto; /* add 16-bit proto + len */
  106. __asm__ __volatile__ (
  107. #if BITS_PER_LONG > 32
  108. /*
  109. ** We can execute two loads and two adds per cycle on PA 8000.
  110. ** But add insn's get serialized waiting for the carry bit.
  111. ** Try to keep 4 registers with "live" values ahead of the ALU.
  112. */
  113. " ldd,ma 8(%1), %4\n" /* get 1st saddr word */
  114. " ldd,ma 8(%2), %5\n" /* get 1st daddr word */
  115. " add %4, %0, %0\n"
  116. " ldd,ma 8(%1), %6\n" /* 2nd saddr */
  117. " ldd,ma 8(%2), %7\n" /* 2nd daddr */
  118. " add,dc %5, %0, %0\n"
  119. " add,dc %6, %0, %0\n"
  120. " add,dc %7, %0, %0\n"
  121. " add,dc %3, %0, %0\n" /* fold in proto+len | carry bit */
  122. " extrd,u %0, 31, 32, %4\n"/* copy upper half down */
  123. " depdi 0, 31, 32, %0\n"/* clear upper half */
  124. " add %4, %0, %0\n" /* fold into 32-bits */
  125. " addc 0, %0, %0\n" /* add carry */
  126. #else
  127. /*
  128. ** For PA 1.x, the insn order doesn't matter as much.
  129. ** Insn stream is serialized on the carry bit here too.
  130. ** result from the previous operation (eg r0 + x)
  131. */
  132. " ldw,ma 4(%1), %4\n" /* get 1st saddr word */
  133. " ldw,ma 4(%2), %5\n" /* get 1st daddr word */
  134. " add %4, %0, %0\n"
  135. " ldw,ma 4(%1), %6\n" /* 2nd saddr */
  136. " addc %5, %0, %0\n"
  137. " ldw,ma 4(%2), %7\n" /* 2nd daddr */
  138. " addc %6, %0, %0\n"
  139. " ldw,ma 4(%1), %4\n" /* 3rd saddr */
  140. " addc %7, %0, %0\n"
  141. " ldw,ma 4(%2), %5\n" /* 3rd daddr */
  142. " addc %4, %0, %0\n"
  143. " ldw,ma 4(%1), %6\n" /* 4th saddr */
  144. " addc %5, %0, %0\n"
  145. " ldw,ma 4(%2), %7\n" /* 4th daddr */
  146. " addc %6, %0, %0\n"
  147. " addc %7, %0, %0\n"
  148. " addc %3, %0, %0\n" /* fold in proto+len, catch carry */
  149. #endif
  150. : "=r" (sum), "=r" (saddr), "=r" (daddr), "=r" (len),
  151. "=r" (t0), "=r" (t1), "=r" (t2), "=r" (t3)
  152. : "0" (sum), "1" (saddr), "2" (daddr), "3" (len)
  153. : "memory");
  154. return csum_fold(sum);
  155. }
  156. #endif