checksum.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Checksum functions for Hexagon
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. /* This was derived from arch/alpha/lib/checksum.c */
  21. #include <linux/module.h>
  22. #include <linux/string.h>
  23. #include <asm/byteorder.h>
  24. #include <net/checksum.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/intrinsics.h>
  27. /* Vector value operations */
  28. #define SIGN(x, y) ((0x8000ULL*x)<<y)
  29. #define CARRY(x, y) ((0x0002ULL*x)<<y)
  30. #define SELECT(x, y) ((0x0001ULL*x)<<y)
  31. #define VR_NEGATE(a, b, c, d) (SIGN(a, 48) + SIGN(b, 32) + SIGN(c, 16) \
  32. + SIGN(d, 0))
  33. #define VR_CARRY(a, b, c, d) (CARRY(a, 48) + CARRY(b, 32) + CARRY(c, 16) \
  34. + CARRY(d, 0))
  35. #define VR_SELECT(a, b, c, d) (SELECT(a, 48) + SELECT(b, 32) + SELECT(c, 16) \
  36. + SELECT(d, 0))
  37. /* optimized HEXAGON V3 intrinsic version */
  38. static inline unsigned short from64to16(u64 x)
  39. {
  40. u64 sum;
  41. sum = HEXAGON_P_vrmpyh_PP(x^VR_NEGATE(1, 1, 1, 1),
  42. VR_SELECT(1, 1, 1, 1));
  43. sum += VR_CARRY(0, 0, 1, 0);
  44. sum = HEXAGON_P_vrmpyh_PP(sum, VR_SELECT(0, 0, 1, 1));
  45. return 0xFFFF & sum;
  46. }
  47. /*
  48. * computes the checksum of the TCP/UDP pseudo-header
  49. * returns a 16-bit checksum, already complemented.
  50. */
  51. __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  52. __u32 len, __u8 proto, __wsum sum)
  53. {
  54. return (__force __sum16)~from64to16(
  55. (__force u64)saddr + (__force u64)daddr +
  56. (__force u64)sum + ((len + proto) << 8));
  57. }
  58. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  59. __u32 len, __u8 proto, __wsum sum)
  60. {
  61. u64 result;
  62. result = (__force u64)saddr + (__force u64)daddr +
  63. (__force u64)sum + ((len + proto) << 8);
  64. /* Fold down to 32-bits so we don't lose in the typedef-less
  65. network stack. */
  66. /* 64 to 33 */
  67. result = (result & 0xffffffffUL) + (result >> 32);
  68. /* 33 to 32 */
  69. result = (result & 0xffffffffUL) + (result >> 32);
  70. return (__force __wsum)result;
  71. }
  72. EXPORT_SYMBOL(csum_tcpudp_nofold);
  73. /*
  74. * Do a 64-bit checksum on an arbitrary memory area..
  75. *
  76. * This isn't a great routine, but it's not _horrible_ either. The
  77. * inner loop could be unrolled a bit further, and there are better
  78. * ways to do the carry, but this is reasonable.
  79. */
  80. /* optimized HEXAGON intrinsic version, with over read fixed */
  81. unsigned int do_csum(const void *voidptr, int len)
  82. {
  83. u64 sum0, sum1, x0, x1, *ptr8_o, *ptr8_e, *ptr8;
  84. int i, start, mid, end, mask;
  85. const char *ptr = voidptr;
  86. unsigned short *ptr2;
  87. unsigned int *ptr4;
  88. if (len <= 0)
  89. return 0;
  90. start = 0xF & (16-(((int) ptr) & 0xF)) ;
  91. mask = 0x7fffffffUL >> HEXAGON_R_cl0_R(len);
  92. start = start & mask ;
  93. mid = len - start;
  94. end = mid & 0xF;
  95. mid = mid>>4;
  96. sum0 = mid << 18;
  97. sum1 = 0;
  98. if (start & 1)
  99. sum0 += (u64) (ptr[0] << 8);
  100. ptr2 = (unsigned short *) &ptr[start & 1];
  101. if (start & 2)
  102. sum1 += (u64) ptr2[0];
  103. ptr4 = (unsigned int *) &ptr[start & 3];
  104. if (start & 4) {
  105. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
  106. VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
  107. VR_SELECT(0, 0, 1, 1));
  108. sum0 += VR_SELECT(0, 0, 1, 0);
  109. }
  110. ptr8 = (u64 *) &ptr[start & 7];
  111. if (start & 8) {
  112. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
  113. VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
  114. VR_SELECT(1, 1, 1, 1));
  115. sum1 += VR_CARRY(0, 0, 1, 0);
  116. }
  117. ptr8_o = (u64 *) (ptr + start);
  118. ptr8_e = (u64 *) (ptr + start + 8);
  119. if (mid) {
  120. x0 = *ptr8_e; ptr8_e += 2;
  121. x1 = *ptr8_o; ptr8_o += 2;
  122. if (mid > 1)
  123. for (i = 0; i < mid-1; i++) {
  124. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
  125. x0^VR_NEGATE(1, 1, 1, 1),
  126. VR_SELECT(1, 1, 1, 1));
  127. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
  128. x1^VR_NEGATE(1, 1, 1, 1),
  129. VR_SELECT(1, 1, 1, 1));
  130. x0 = *ptr8_e; ptr8_e += 2;
  131. x1 = *ptr8_o; ptr8_o += 2;
  132. }
  133. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0, x0^VR_NEGATE(1, 1, 1, 1),
  134. VR_SELECT(1, 1, 1, 1));
  135. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1, x1^VR_NEGATE(1, 1, 1, 1),
  136. VR_SELECT(1, 1, 1, 1));
  137. }
  138. ptr4 = (unsigned int *) &ptr[start + (mid * 16) + (end & 8)];
  139. if (end & 4) {
  140. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
  141. VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
  142. VR_SELECT(0, 0, 1, 1));
  143. sum1 += VR_SELECT(0, 0, 1, 0);
  144. }
  145. ptr2 = (unsigned short *) &ptr[start + (mid * 16) + (end & 12)];
  146. if (end & 2)
  147. sum0 += (u64) ptr2[0];
  148. if (end & 1)
  149. sum1 += (u64) ptr[start + (mid * 16) + (end & 14)];
  150. ptr8 = (u64 *) &ptr[start + (mid * 16)];
  151. if (end & 8) {
  152. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
  153. VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
  154. VR_SELECT(1, 1, 1, 1));
  155. sum0 += VR_CARRY(0, 0, 1, 0);
  156. }
  157. sum0 = HEXAGON_P_vrmpyh_PP((sum0+sum1)^VR_NEGATE(0, 0, 0, 1),
  158. VR_SELECT(0, 0, 1, 1));
  159. sum0 += VR_NEGATE(0, 0, 0, 1);
  160. sum0 = HEXAGON_P_vrmpyh_PP(sum0, VR_SELECT(0, 0, 1, 1));
  161. if (start & 1)
  162. sum0 = (sum0 << 8) | (0xFF & (sum0 >> 8));
  163. return 0xFFFF & sum0;
  164. }
  165. /*
  166. * copy from ds while checksumming, otherwise like csum_partial
  167. */
  168. __wsum
  169. csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
  170. {
  171. memcpy(dst, src, len);
  172. return csum_partial(dst, len, sum);
  173. }