checksum.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
  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 __wsum csum_partial_copy(const void *src, void *dst, int len,
  14. __wsum sum);
  15. extern __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  16. int len, __wsum sum, int *csum_err);
  17. #define csum_partial_copy_nocheck(src, dst, len, sum) \
  18. csum_partial_copy((src), (dst), (len), (sum))
  19. extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
  20. extern __sum16 ip_compute_csum(const void *buff, int len);
  21. /*
  22. * Fold a partial checksum
  23. */
  24. static inline __sum16 csum_fold(__wsum sum)
  25. {
  26. __asm__ __volatile__(
  27. "add %0, %1, %0\n"
  28. "cmpltu r8, %0, %1\n"
  29. "srli %0, %0, 16\n"
  30. "add %0, %0, r8\n"
  31. "nor %0, %0, %0\n"
  32. : "=r" (sum)
  33. : "r" (sum << 16), "0" (sum)
  34. : "r8");
  35. return (__force __sum16) sum;
  36. }
  37. /*
  38. * computes the checksum of the TCP/UDP pseudo-header
  39. * returns a 16-bit checksum, already complemented
  40. */
  41. #define csum_tcpudp_nofold csum_tcpudp_nofold
  42. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  43. __u32 len, __u8 proto,
  44. __wsum sum)
  45. {
  46. __asm__ __volatile__(
  47. "add %0, %1, %0\n"
  48. "cmpltu r8, %0, %1\n"
  49. "add %0, %0, r8\n" /* add carry */
  50. "add %0, %2, %0\n"
  51. "cmpltu r8, %0, %2\n"
  52. "add %0, %0, r8\n" /* add carry */
  53. "add %0, %3, %0\n"
  54. "cmpltu r8, %0, %3\n"
  55. "add %0, %0, r8\n" /* add carry */
  56. : "=r" (sum), "=r" (saddr)
  57. : "r" (daddr), "r" ((len + proto) << 8),
  58. "0" (sum),
  59. "1" (saddr)
  60. : "r8");
  61. return sum;
  62. }
  63. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  64. __u32 len, __u8 proto,
  65. __wsum sum)
  66. {
  67. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  68. }
  69. #endif /* _ASM_NIOS_CHECKSUM_H */