checksum_64.h 3.8 KB

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