checksum.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * include/asm-v850/checksum.h -- Checksum ops
  3. *
  4. * Copyright (C) 2001,2005 NEC Corporation
  5. * Copyright (C) 2001,2005 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #ifndef __V850_CHECKSUM_H__
  14. #define __V850_CHECKSUM_H__
  15. /*
  16. * computes the checksum of a memory block at buff, length len,
  17. * and adds in "sum" (32-bit)
  18. *
  19. * returns a 32-bit number suitable for feeding into itself
  20. * or csum_tcpudp_magic
  21. *
  22. * this function must be called with even lengths, except
  23. * for the last fragment, which may be odd
  24. *
  25. * it's best to have buff aligned on a 32-bit boundary
  26. */
  27. extern unsigned int csum_partial (const unsigned char * buff, int len,
  28. unsigned int sum);
  29. /*
  30. * the same as csum_partial, but copies from src while it
  31. * checksums
  32. *
  33. * here even more important to align src and dst on a 32-bit (or even
  34. * better 64-bit) boundary
  35. */
  36. extern unsigned csum_partial_copy (const unsigned char *src,
  37. unsigned char *dst, int len, unsigned sum);
  38. /*
  39. * the same as csum_partial_copy, but copies from user space.
  40. *
  41. * here even more important to align src and dst on a 32-bit (or even
  42. * better 64-bit) boundary
  43. */
  44. extern unsigned csum_partial_copy_from_user (const unsigned char *src,
  45. unsigned char *dst,
  46. int len, unsigned sum,
  47. int *csum_err);
  48. #define csum_partial_copy_nocheck(src, dst, len, sum) \
  49. csum_partial_copy ((src), (dst), (len), (sum))
  50. unsigned short ip_fast_csum (unsigned char *iph, unsigned int ihl);
  51. /*
  52. * Fold a partial checksum
  53. */
  54. static inline unsigned int csum_fold (unsigned long sum)
  55. {
  56. unsigned int result;
  57. /*
  58. %0 %1
  59. hsw %1, %0 H L L H
  60. add %1, %0 H L H+L+C H+L
  61. */
  62. asm ("hsw %1, %0; add %1, %0" : "=&r" (result) : "r" (sum));
  63. return (~result) >> 16;
  64. }
  65. /*
  66. * computes the checksum of the TCP/UDP pseudo-header
  67. * returns a 16-bit checksum, already complemented
  68. */
  69. static inline unsigned int
  70. csum_tcpudp_nofold (unsigned long saddr, unsigned long daddr,
  71. unsigned short len,
  72. unsigned short proto, unsigned int sum)
  73. {
  74. int __carry;
  75. __asm__ ("add %2, %0;"
  76. "setf c, %1;"
  77. "add %1, %0;"
  78. "add %3, %0;"
  79. "setf c, %1;"
  80. "add %1, %0;"
  81. "add %4, %0;"
  82. "setf c, %1;"
  83. "add %1, %0"
  84. : "=&r" (sum), "=&r" (__carry)
  85. : "r" (daddr), "r" (saddr),
  86. "r" (ntohs (len) + (proto << 8)),
  87. "0" (sum));
  88. return sum;
  89. }
  90. static inline unsigned short int
  91. csum_tcpudp_magic (unsigned long saddr, unsigned long daddr,
  92. unsigned short len,
  93. unsigned short proto, unsigned int sum)
  94. {
  95. return csum_fold (csum_tcpudp_nofold (saddr, daddr, len, proto, sum));
  96. }
  97. /*
  98. * this routine is used for miscellaneous IP-like checksums, mainly
  99. * in icmp.c
  100. */
  101. extern unsigned short ip_compute_csum (const unsigned char * buff, int len);
  102. #endif /* __V850_CHECKSUM_H__ */