checksum.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(unsigned long saddr, unsigned long daddr,
  52. unsigned short len, unsigned short proto,
  53. __wsum sum)
  54. {
  55. return (__force __sum16)~from64to16(
  56. (__force u64)saddr + (__force u64)daddr +
  57. (__force u64)sum + ((len + proto) << 8));
  58. }
  59. __wsum csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr,
  60. unsigned short len, unsigned short proto,
  61. __wsum sum)
  62. {
  63. u64 result;
  64. result = (__force u64)saddr + (__force u64)daddr +
  65. (__force u64)sum + ((len + proto) << 8);
  66. /* Fold down to 32-bits so we don't lose in the typedef-less
  67. network stack. */
  68. /* 64 to 33 */
  69. result = (result & 0xffffffffUL) + (result >> 32);
  70. /* 33 to 32 */
  71. result = (result & 0xffffffffUL) + (result >> 32);
  72. return (__force __wsum)result;
  73. }
  74. EXPORT_SYMBOL(csum_tcpudp_nofold);
  75. /*
  76. * Do a 64-bit checksum on an arbitrary memory area..
  77. *
  78. * This isn't a great routine, but it's not _horrible_ either. The
  79. * inner loop could be unrolled a bit further, and there are better
  80. * ways to do the carry, but this is reasonable.
  81. */
  82. /* optimized HEXAGON intrinsic version, with over read fixed */
  83. unsigned int do_csum(const void *voidptr, int len)
  84. {
  85. u64 sum0, sum1, x0, x1, *ptr8_o, *ptr8_e, *ptr8;
  86. int i, start, mid, end, mask;
  87. const char *ptr = voidptr;
  88. unsigned short *ptr2;
  89. unsigned int *ptr4;
  90. if (len <= 0)
  91. return 0;
  92. start = 0xF & (16-(((int) ptr) & 0xF)) ;
  93. mask = 0x7fffffffUL >> HEXAGON_R_cl0_R(len);
  94. start = start & mask ;
  95. mid = len - start;
  96. end = mid & 0xF;
  97. mid = mid>>4;
  98. sum0 = mid << 18;
  99. sum1 = 0;
  100. if (start & 1)
  101. sum0 += (u64) (ptr[0] << 8);
  102. ptr2 = (unsigned short *) &ptr[start & 1];
  103. if (start & 2)
  104. sum1 += (u64) ptr2[0];
  105. ptr4 = (unsigned int *) &ptr[start & 3];
  106. if (start & 4) {
  107. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
  108. VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
  109. VR_SELECT(0, 0, 1, 1));
  110. sum0 += VR_SELECT(0, 0, 1, 0);
  111. }
  112. ptr8 = (u64 *) &ptr[start & 7];
  113. if (start & 8) {
  114. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
  115. VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
  116. VR_SELECT(1, 1, 1, 1));
  117. sum1 += VR_CARRY(0, 0, 1, 0);
  118. }
  119. ptr8_o = (u64 *) (ptr + start);
  120. ptr8_e = (u64 *) (ptr + start + 8);
  121. if (mid) {
  122. x0 = *ptr8_e; ptr8_e += 2;
  123. x1 = *ptr8_o; ptr8_o += 2;
  124. if (mid > 1)
  125. for (i = 0; i < mid-1; i++) {
  126. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
  127. x0^VR_NEGATE(1, 1, 1, 1),
  128. VR_SELECT(1, 1, 1, 1));
  129. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
  130. x1^VR_NEGATE(1, 1, 1, 1),
  131. VR_SELECT(1, 1, 1, 1));
  132. x0 = *ptr8_e; ptr8_e += 2;
  133. x1 = *ptr8_o; ptr8_o += 2;
  134. }
  135. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0, x0^VR_NEGATE(1, 1, 1, 1),
  136. VR_SELECT(1, 1, 1, 1));
  137. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1, x1^VR_NEGATE(1, 1, 1, 1),
  138. VR_SELECT(1, 1, 1, 1));
  139. }
  140. ptr4 = (unsigned int *) &ptr[start + (mid * 16) + (end & 8)];
  141. if (end & 4) {
  142. sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
  143. VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
  144. VR_SELECT(0, 0, 1, 1));
  145. sum1 += VR_SELECT(0, 0, 1, 0);
  146. }
  147. ptr2 = (unsigned short *) &ptr[start + (mid * 16) + (end & 12)];
  148. if (end & 2)
  149. sum0 += (u64) ptr2[0];
  150. if (end & 1)
  151. sum1 += (u64) ptr[start + (mid * 16) + (end & 14)];
  152. ptr8 = (u64 *) &ptr[start + (mid * 16)];
  153. if (end & 8) {
  154. sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
  155. VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
  156. VR_SELECT(1, 1, 1, 1));
  157. sum0 += VR_CARRY(0, 0, 1, 0);
  158. }
  159. sum0 = HEXAGON_P_vrmpyh_PP((sum0+sum1)^VR_NEGATE(0, 0, 0, 1),
  160. VR_SELECT(0, 0, 1, 1));
  161. sum0 += VR_NEGATE(0, 0, 0, 1);
  162. sum0 = HEXAGON_P_vrmpyh_PP(sum0, VR_SELECT(0, 0, 1, 1));
  163. if (start & 1)
  164. sum0 = (sum0 << 8) | (0xFF & (sum0 >> 8));
  165. return 0xFFFF & sum0;
  166. }
  167. /*
  168. * copy from ds while checksumming, otherwise like csum_partial
  169. */
  170. __wsum
  171. csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
  172. {
  173. memcpy(dst, src, len);
  174. return csum_partial(dst, len, sum);
  175. }