checksum_64.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef __SPARC64_CHECKSUM_H
  2. #define __SPARC64_CHECKSUM_H
  3. /* checksum.h: IP/UDP/TCP checksum routines on the V9.
  4. *
  5. * Copyright(C) 1995 Linus Torvalds
  6. * Copyright(C) 1995 Miguel de Icaza
  7. * Copyright(C) 1996 David S. Miller
  8. * Copyright(C) 1996 Eddie C. Dost
  9. * Copyright(C) 1997 Jakub Jelinek
  10. *
  11. * derived from:
  12. * Alpha checksum c-code
  13. * ix86 inline assembly
  14. * RFC1071 Computing the Internet Checksum
  15. */
  16. #include <linux/in6.h>
  17. #include <asm/uaccess.h>
  18. /* computes the checksum of a memory block at buff, length len,
  19. * and adds in "sum" (32-bit)
  20. *
  21. * returns a 32-bit number suitable for feeding into itself
  22. * or csum_tcpudp_magic
  23. *
  24. * this function must be called with even lengths, except
  25. * for the last fragment, which may be odd
  26. *
  27. * it's best to have buff aligned on a 32-bit boundary
  28. */
  29. extern __wsum csum_partial(const void * buff, int len, __wsum sum);
  30. /* the same as csum_partial, but copies from user space while it
  31. * checksums
  32. *
  33. * here even more important to align src and dst on a 32-bit (or even
  34. * better 64-bit) boundary
  35. */
  36. extern __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  37. int len, __wsum sum);
  38. extern long __csum_partial_copy_from_user(const void __user *src,
  39. void *dst, int len,
  40. __wsum sum);
  41. static inline __wsum
  42. csum_partial_copy_from_user(const void __user *src,
  43. void *dst, int len,
  44. __wsum sum, int *err)
  45. {
  46. long ret = __csum_partial_copy_from_user(src, dst, len, sum);
  47. if (ret < 0)
  48. *err = -EFAULT;
  49. return (__force __wsum) ret;
  50. }
  51. /*
  52. * Copy and checksum to user
  53. */
  54. #define HAVE_CSUM_COPY_USER
  55. extern long __csum_partial_copy_to_user(const void *src,
  56. void __user *dst, int len,
  57. __wsum sum);
  58. static inline __wsum
  59. csum_and_copy_to_user(const void *src,
  60. void __user *dst, int len,
  61. __wsum sum, int *err)
  62. {
  63. long ret = __csum_partial_copy_to_user(src, dst, len, sum);
  64. if (ret < 0)
  65. *err = -EFAULT;
  66. return (__force __wsum) ret;
  67. }
  68. /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
  69. * the majority of the time.
  70. */
  71. extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
  72. /* Fold a partial checksum without adding pseudo headers. */
  73. static inline __sum16 csum_fold(__wsum sum)
  74. {
  75. unsigned int tmp;
  76. __asm__ __volatile__(
  77. " addcc %0, %1, %1\n"
  78. " srl %1, 16, %1\n"
  79. " addc %1, %%g0, %1\n"
  80. " xnor %%g0, %1, %0\n"
  81. : "=&r" (sum), "=r" (tmp)
  82. : "0" (sum), "1" ((__force u32)sum<<16)
  83. : "cc");
  84. return (__force __sum16)sum;
  85. }
  86. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  87. unsigned int len,
  88. unsigned short proto,
  89. __wsum sum)
  90. {
  91. __asm__ __volatile__(
  92. " addcc %1, %0, %0\n"
  93. " addccc %2, %0, %0\n"
  94. " addccc %3, %0, %0\n"
  95. " addc %0, %%g0, %0\n"
  96. : "=r" (sum), "=r" (saddr)
  97. : "r" (daddr), "r" (proto + len), "0" (sum), "1" (saddr)
  98. : "cc");
  99. return sum;
  100. }
  101. /*
  102. * computes the checksum of the TCP/UDP pseudo-header
  103. * returns a 16-bit checksum, already complemented
  104. */
  105. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  106. unsigned short len,
  107. unsigned short proto,
  108. __wsum sum)
  109. {
  110. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  111. }
  112. #define _HAVE_ARCH_IPV6_CSUM
  113. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  114. const struct in6_addr *daddr,
  115. __u32 len, unsigned short proto,
  116. __wsum sum)
  117. {
  118. __asm__ __volatile__ (
  119. " addcc %3, %4, %%g7\n"
  120. " addccc %5, %%g7, %%g7\n"
  121. " lduw [%2 + 0x0c], %%g2\n"
  122. " lduw [%2 + 0x08], %%g3\n"
  123. " addccc %%g2, %%g7, %%g7\n"
  124. " lduw [%2 + 0x04], %%g2\n"
  125. " addccc %%g3, %%g7, %%g7\n"
  126. " lduw [%2 + 0x00], %%g3\n"
  127. " addccc %%g2, %%g7, %%g7\n"
  128. " lduw [%1 + 0x0c], %%g2\n"
  129. " addccc %%g3, %%g7, %%g7\n"
  130. " lduw [%1 + 0x08], %%g3\n"
  131. " addccc %%g2, %%g7, %%g7\n"
  132. " lduw [%1 + 0x04], %%g2\n"
  133. " addccc %%g3, %%g7, %%g7\n"
  134. " lduw [%1 + 0x00], %%g3\n"
  135. " addccc %%g2, %%g7, %%g7\n"
  136. " addccc %%g3, %%g7, %0\n"
  137. " addc 0, %0, %0\n"
  138. : "=&r" (sum)
  139. : "r" (saddr), "r" (daddr), "r"(htonl(len)),
  140. "r"(htonl(proto)), "r"(sum)
  141. : "g2", "g3", "g7", "cc");
  142. return csum_fold(sum);
  143. }
  144. /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
  145. static inline __sum16 ip_compute_csum(const void *buff, int len)
  146. {
  147. return csum_fold(csum_partial(buff, len, 0));
  148. }
  149. #endif /* !(__SPARC64_CHECKSUM_H) */