checksum.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2010 Tobias Klauser <[email protected]>
  3. * Copyright (C) 2004 Microtronix Datacom Ltd.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. */
  9. #ifndef _ASM_NIOS_CHECKSUM_H
  10. #define _ASM_NIOS_CHECKSUM_H
  11. /* Take these from lib/checksum.c */
  12. extern __wsum csum_partial(const void *buff, int len, __wsum sum);
  13. extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
  14. extern __sum16 ip_compute_csum(const void *buff, int len);
  15. /*
  16. * Fold a partial checksum
  17. */
  18. static inline __sum16 csum_fold(__wsum sum)
  19. {
  20. __asm__ __volatile__(
  21. "add %0, %1, %0\n"
  22. "cmpltu r8, %0, %1\n"
  23. "srli %0, %0, 16\n"
  24. "add %0, %0, r8\n"
  25. "nor %0, %0, %0\n"
  26. : "=r" (sum)
  27. : "r" (sum << 16), "0" (sum)
  28. : "r8");
  29. return (__force __sum16) sum;
  30. }
  31. /*
  32. * computes the checksum of the TCP/UDP pseudo-header
  33. * returns a 16-bit checksum, already complemented
  34. */
  35. #define csum_tcpudp_nofold csum_tcpudp_nofold
  36. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  37. __u32 len, __u8 proto,
  38. __wsum sum)
  39. {
  40. __asm__ __volatile__(
  41. "add %0, %1, %0\n"
  42. "cmpltu r8, %0, %1\n"
  43. "add %0, %0, r8\n" /* add carry */
  44. "add %0, %2, %0\n"
  45. "cmpltu r8, %0, %2\n"
  46. "add %0, %0, r8\n" /* add carry */
  47. "add %0, %3, %0\n"
  48. "cmpltu r8, %0, %3\n"
  49. "add %0, %0, r8\n" /* add carry */
  50. : "=r" (sum), "=r" (saddr)
  51. : "r" (daddr), "r" ((len + proto) << 8),
  52. "0" (sum),
  53. "1" (saddr)
  54. : "r8");
  55. return sum;
  56. }
  57. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  58. __u32 len, __u8 proto,
  59. __wsum sum)
  60. {
  61. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  62. }
  63. #endif /* _ASM_NIOS_CHECKSUM_H */