checksum.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * include/asm-v850/checksum.h -- Checksum ops
  3. *
  4. * Copyright (C) 2001 NEC Corporation
  5. * Copyright (C) 2001 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 char *src, char *dst, int len,
  37. 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 char *src, char *dst,
  45. int len, unsigned sum,
  46. int *csum_err);
  47. #define csum_partial_copy_nocheck(src, dst, len, sum) \
  48. csum_partial_copy ((src), (dst), (len), (sum))
  49. unsigned short ip_fast_csum (unsigned char *iph, unsigned int ihl);
  50. /*
  51. * Fold a partial checksum
  52. */
  53. static inline unsigned int csum_fold (unsigned long sum)
  54. {
  55. unsigned int result;
  56. /*
  57. %0 %1
  58. hsw %1, %0 H L L H
  59. add %1, %0 H L H+L+C H+L
  60. */
  61. asm ("hsw %1, %0; add %1, %0" : "=&r" (result) : "r" (sum));
  62. return (~result) >> 16;
  63. }
  64. /*
  65. * computes the checksum of the TCP/UDP pseudo-header
  66. * returns a 16-bit checksum, already complemented
  67. */
  68. static inline unsigned int
  69. csum_tcpudp_nofold (unsigned long saddr, unsigned long daddr,
  70. unsigned short len,
  71. unsigned short proto, unsigned int sum)
  72. {
  73. int __carry;
  74. __asm__ ("add %2, %0;"
  75. "setf c, %1;"
  76. "add %1, %0;"
  77. "add %3, %0;"
  78. "setf c, %1;"
  79. "add %1, %0;"
  80. "add %4, %0;"
  81. "setf c, %1;"
  82. "add %1, %0"
  83. : "=&r" (sum), "=&r" (__carry)
  84. : "r" (daddr), "r" (saddr),
  85. "r" (ntohs (len) + (proto << 8)),
  86. "0" (sum));
  87. return sum;
  88. }
  89. static inline unsigned short int
  90. csum_tcpudp_magic (unsigned long saddr, unsigned long daddr,
  91. unsigned short len,
  92. unsigned short proto, unsigned int sum)
  93. {
  94. return csum_fold (csum_tcpudp_nofold (saddr, daddr, len, proto, sum));
  95. }
  96. /*
  97. * this routine is used for miscellaneous IP-like checksums, mainly
  98. * in icmp.c
  99. */
  100. extern unsigned short ip_compute_csum (const unsigned char * buff, int len);
  101. #endif /* __V850_CHECKSUM_H__ */