checksum.h 3.4 KB

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