checksum.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Joern Rennecke <joern.rennecke@embecosm.com>: Jan 2012
  9. * -Insn Scheduling improvements to csum core routines.
  10. * = csum_fold( ) largely derived from ARM version.
  11. * = ip_fast_cum( ) to have module scheduling
  12. * -gcc 4.4.x broke networking. Alias analysis needed to be primed.
  13. * worked around by adding memory clobber to ip_fast_csum( )
  14. *
  15. * vineetg: May 2010
  16. * -Rewrote ip_fast_cscum( ) and csum_fold( ) with fast inline asm
  17. */
  18. #ifndef _ASM_ARC_CHECKSUM_H
  19. #define _ASM_ARC_CHECKSUM_H
  20. /*
  21. * Fold a partial checksum
  22. *
  23. * The 2 swords comprising the 32bit sum are added, any carry to 16th bit
  24. * added back and final sword result inverted.
  25. */
  26. static inline __sum16 csum_fold(__wsum s)
  27. {
  28. unsigned r = s << 16 | s >> 16; /* ror */
  29. s = ~s;
  30. s -= r;
  31. return s >> 16;
  32. }
  33. /*
  34. * This is a version of ip_compute_csum() optimized for IP headers,
  35. * which always checksum on 4 octet boundaries.
  36. */
  37. static inline __sum16
  38. ip_fast_csum(const void *iph, unsigned int ihl)
  39. {
  40. const void *ptr = iph;
  41. unsigned int tmp, tmp2, sum;
  42. __asm__(
  43. " ld.ab %0, [%3, 4] \n"
  44. " ld.ab %2, [%3, 4] \n"
  45. " sub %1, %4, 2 \n"
  46. " lsr.f lp_count, %1, 1 \n"
  47. " bcc 0f \n"
  48. " add.f %0, %0, %2 \n"
  49. " ld.ab %2, [%3, 4] \n"
  50. "0: lp 1f \n"
  51. " ld.ab %1, [%3, 4] \n"
  52. " adc.f %0, %0, %2 \n"
  53. " ld.ab %2, [%3, 4] \n"
  54. " adc.f %0, %0, %1 \n"
  55. "1: adc.f %0, %0, %2 \n"
  56. " add.cs %0,%0,1 \n"
  57. : "=&r"(sum), "=r"(tmp), "=&r"(tmp2), "+&r" (ptr)
  58. : "r"(ihl)
  59. : "cc", "lp_count", "memory");
  60. return csum_fold(sum);
  61. }
  62. /*
  63. * TCP pseudo Header is 12 bytes:
  64. * SA [4], DA [4], zeroes [1], Proto[1], TCP Seg(hdr+data) Len [2]
  65. */
  66. static inline __wsum
  67. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  68. unsigned short proto, __wsum sum)
  69. {
  70. __asm__ __volatile__(
  71. " add.f %0, %0, %1 \n"
  72. " adc.f %0, %0, %2 \n"
  73. " adc.f %0, %0, %3 \n"
  74. " adc.f %0, %0, %4 \n"
  75. " adc %0, %0, 0 \n"
  76. : "+&r"(sum)
  77. : "r"(saddr), "r"(daddr),
  78. #ifdef CONFIG_CPU_BIG_ENDIAN
  79. "r"(len),
  80. #else
  81. "r"(len << 8),
  82. #endif
  83. "r"(htons(proto))
  84. : "cc");
  85. return sum;
  86. }
  87. #define csum_fold csum_fold
  88. #define ip_fast_csum ip_fast_csum
  89. #define csum_tcpudp_nofold csum_tcpudp_nofold
  90. #include <asm-generic/checksum.h>
  91. #endif /* _ASM_ARC_CHECKSUM_H */