checksum_64.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. __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. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  37. int len, __wsum sum);
  38. 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. 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. __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. __u32 len, __u8 proto,
  88. __wsum sum)
  89. {
  90. __asm__ __volatile__(
  91. " addcc %1, %0, %0\n"
  92. " addccc %2, %0, %0\n"
  93. " addccc %3, %0, %0\n"
  94. " addc %0, %%g0, %0\n"
  95. : "=r" (sum), "=r" (saddr)
  96. : "r" (daddr), "r" (proto + len), "0" (sum), "1" (saddr)
  97. : "cc");
  98. return sum;
  99. }
  100. /*
  101. * computes the checksum of the TCP/UDP pseudo-header
  102. * returns a 16-bit checksum, already complemented
  103. */
  104. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  105. __u32 len, __u8 proto,
  106. __wsum sum)
  107. {
  108. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  109. }
  110. #define _HAVE_ARCH_IPV6_CSUM
  111. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  112. const struct in6_addr *daddr,
  113. __u32 len, __u8 proto, __wsum sum)
  114. {
  115. __asm__ __volatile__ (
  116. " addcc %3, %4, %%g7\n"
  117. " addccc %5, %%g7, %%g7\n"
  118. " lduw [%2 + 0x0c], %%g2\n"
  119. " lduw [%2 + 0x08], %%g3\n"
  120. " addccc %%g2, %%g7, %%g7\n"
  121. " lduw [%2 + 0x04], %%g2\n"
  122. " addccc %%g3, %%g7, %%g7\n"
  123. " lduw [%2 + 0x00], %%g3\n"
  124. " addccc %%g2, %%g7, %%g7\n"
  125. " lduw [%1 + 0x0c], %%g2\n"
  126. " addccc %%g3, %%g7, %%g7\n"
  127. " lduw [%1 + 0x08], %%g3\n"
  128. " addccc %%g2, %%g7, %%g7\n"
  129. " lduw [%1 + 0x04], %%g2\n"
  130. " addccc %%g3, %%g7, %%g7\n"
  131. " lduw [%1 + 0x00], %%g3\n"
  132. " addccc %%g2, %%g7, %%g7\n"
  133. " addccc %%g3, %%g7, %0\n"
  134. " addc 0, %0, %0\n"
  135. : "=&r" (sum)
  136. : "r" (saddr), "r" (daddr), "r"(htonl(len)),
  137. "r"(htonl(proto)), "r"(sum)
  138. : "g2", "g3", "g7", "cc");
  139. return csum_fold(sum);
  140. }
  141. /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
  142. static inline __sum16 ip_compute_csum(const void *buff, int len)
  143. {
  144. return csum_fold(csum_partial(buff, len, 0));
  145. }
  146. #define HAVE_ARCH_CSUM_ADD
  147. static inline __wsum csum_add(__wsum csum, __wsum addend)
  148. {
  149. __asm__ __volatile__(
  150. "addcc %0, %1, %0\n"
  151. "addx %0, %%g0, %0"
  152. : "=r" (csum)
  153. : "r" (addend), "0" (csum));
  154. return csum;
  155. }
  156. #endif /* !(__SPARC64_CHECKSUM_H) */