checksum.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _M68K_CHECKSUM_H
  3. #define _M68K_CHECKSUM_H
  4. #include <linux/in6.h>
  5. #ifdef CONFIG_GENERIC_CSUM
  6. #include <asm-generic/checksum.h>
  7. #else
  8. /*
  9. * computes the checksum of a memory block at buff, length len,
  10. * and adds in "sum" (32-bit)
  11. *
  12. * returns a 32-bit number suitable for feeding into itself
  13. * or csum_tcpudp_magic
  14. *
  15. * this function must be called with even lengths, except
  16. * for the last fragment, which may be odd
  17. *
  18. * it's best to have buff aligned on a 32-bit boundary
  19. */
  20. __wsum csum_partial(const void *buff, int len, __wsum sum);
  21. /*
  22. * the same as csum_partial, but copies from src while it
  23. * checksums
  24. *
  25. * here even more important to align src and dst on a 32-bit (or even
  26. * better 64-bit) boundary
  27. */
  28. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  29. #define _HAVE_ARCH_CSUM_AND_COPY
  30. extern __wsum csum_and_copy_from_user(const void __user *src,
  31. void *dst,
  32. int len);
  33. extern __wsum csum_partial_copy_nocheck(const void *src,
  34. void *dst, int len);
  35. /*
  36. * This is a version of ip_fast_csum() optimized for IP headers,
  37. * which always checksum on 4 octet boundaries.
  38. */
  39. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  40. {
  41. unsigned int sum = 0;
  42. unsigned long tmp;
  43. __asm__ ("subqw #1,%2\n"
  44. "1:\t"
  45. "movel %1@+,%3\n\t"
  46. "addxl %3,%0\n\t"
  47. "dbra %2,1b\n\t"
  48. "movel %0,%3\n\t"
  49. "swap %3\n\t"
  50. "addxw %3,%0\n\t"
  51. "clrw %3\n\t"
  52. "addxw %3,%0\n\t"
  53. : "=d" (sum), "=&a" (iph), "=&d" (ihl), "=&d" (tmp)
  54. : "0" (sum), "1" (iph), "2" (ihl)
  55. : "memory");
  56. return (__force __sum16)~sum;
  57. }
  58. static inline __sum16 csum_fold(__wsum sum)
  59. {
  60. unsigned int tmp = (__force u32)sum;
  61. __asm__("swap %1\n\t"
  62. "addw %1, %0\n\t"
  63. "clrw %1\n\t"
  64. "addxw %1, %0"
  65. : "=&d" (sum), "=&d" (tmp)
  66. : "0" (sum), "1" (tmp));
  67. return (__force __sum16)~sum;
  68. }
  69. static inline __wsum
  70. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  71. unsigned short proto, __wsum sum)
  72. {
  73. __asm__ ("addl %2,%0\n\t"
  74. "addxl %3,%0\n\t"
  75. "addxl %4,%0\n\t"
  76. "clrl %1\n\t"
  77. "addxl %1,%0"
  78. : "=&d" (sum), "=d" (saddr)
  79. : "g" (daddr), "1" (saddr), "d" (len + proto),
  80. "0" (sum));
  81. return sum;
  82. }
  83. /*
  84. * computes the checksum of the TCP/UDP pseudo-header
  85. * returns a 16-bit checksum, already complemented
  86. */
  87. static inline __sum16
  88. csum_tcpudp_magic(__be32 saddr, __be32 daddr, unsigned short len,
  89. unsigned short proto, __wsum sum)
  90. {
  91. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  92. }
  93. /*
  94. * this routine is used for miscellaneous IP-like checksums, mainly
  95. * in icmp.c
  96. */
  97. static inline __sum16 ip_compute_csum(const void *buff, int len)
  98. {
  99. return csum_fold (csum_partial(buff, len, 0));
  100. }
  101. #define _HAVE_ARCH_IPV6_CSUM
  102. static __inline__ __sum16
  103. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
  104. __u32 len, __u8 proto, __wsum sum)
  105. {
  106. register unsigned long tmp;
  107. __asm__("addl %2@,%0\n\t"
  108. "movel %2@(4),%1\n\t"
  109. "addxl %1,%0\n\t"
  110. "movel %2@(8),%1\n\t"
  111. "addxl %1,%0\n\t"
  112. "movel %2@(12),%1\n\t"
  113. "addxl %1,%0\n\t"
  114. "movel %3@,%1\n\t"
  115. "addxl %1,%0\n\t"
  116. "movel %3@(4),%1\n\t"
  117. "addxl %1,%0\n\t"
  118. "movel %3@(8),%1\n\t"
  119. "addxl %1,%0\n\t"
  120. "movel %3@(12),%1\n\t"
  121. "addxl %1,%0\n\t"
  122. "addxl %4,%0\n\t"
  123. "clrl %1\n\t"
  124. "addxl %1,%0"
  125. : "=&d" (sum), "=&d" (tmp)
  126. : "a" (saddr), "a" (daddr), "d" (len + proto),
  127. "0" (sum));
  128. return csum_fold(sum);
  129. }
  130. #endif /* CONFIG_GENERIC_CSUM */
  131. #endif /* _M68K_CHECKSUM_H */