checksum.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef _H8300_CHECKSUM_H
  2. #define _H8300_CHECKSUM_H
  3. /*
  4. * computes the checksum of a memory block at buff, length len,
  5. * and adds in "sum" (32-bit)
  6. *
  7. * returns a 32-bit number suitable for feeding into itself
  8. * or csum_tcpudp_magic
  9. *
  10. * this function must be called with even lengths, except
  11. * for the last fragment, which may be odd
  12. *
  13. * it's best to have buff aligned on a 32-bit boundary
  14. */
  15. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
  16. /*
  17. * the same as csum_partial, but copies from src while it
  18. * checksums
  19. *
  20. * here even more important to align src and dst on a 32-bit (or even
  21. * better 64-bit) boundary
  22. */
  23. unsigned int csum_partial_copy(const char *src, char *dst, int len, int sum);
  24. /*
  25. * the same as csum_partial_copy, but copies from user space.
  26. *
  27. * here even more important to align src and dst on a 32-bit (or even
  28. * better 64-bit) boundary
  29. */
  30. extern unsigned int csum_partial_copy_from_user(const char *src, char *dst,
  31. int len, int sum, int *csum_err);
  32. #define csum_partial_copy_nocheck(src, dst, len, sum) \
  33. csum_partial_copy((src), (dst), (len), (sum))
  34. unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl);
  35. /*
  36. * Fold a partial checksum
  37. */
  38. static inline unsigned int csum_fold(unsigned int sum)
  39. {
  40. __asm__("mov.l %0,er0\n\t"
  41. "add.w e0,r0\n\t"
  42. "xor.w e0,e0\n\t"
  43. "rotxl.w e0\n\t"
  44. "add.w e0,r0\n\t"
  45. "sub.w e0,e0\n\t"
  46. "mov.l er0,%0"
  47. : "=r"(sum)
  48. : "0"(sum)
  49. : "er0");
  50. return ~sum;
  51. }
  52. /*
  53. * computes the checksum of the TCP/UDP pseudo-header
  54. * returns a 16-bit checksum, already complemented
  55. */
  56. static inline unsigned int
  57. csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr, unsigned short len,
  58. unsigned short proto, unsigned int sum)
  59. {
  60. __asm__ ("sub.l er0,er0\n\t"
  61. "add.l %2,%0\n\t"
  62. "addx #0,r0l\n\t"
  63. "add.l %3,%0\n\t"
  64. "addx #0,r0l\n\t"
  65. "add.l %4,%0\n\t"
  66. "addx #0,r0l\n\t"
  67. "add.l er0,%0\n\t"
  68. "bcc 1f\n\t"
  69. "inc.l #1,%0\n"
  70. "1:"
  71. : "=&r" (sum)
  72. : "0" (sum), "r" (daddr), "r" (saddr), "r" (len + proto)
  73. :"er0");
  74. return sum;
  75. }
  76. static inline unsigned short int
  77. csum_tcpudp_magic(unsigned long saddr, unsigned long daddr, unsigned short len,
  78. unsigned short proto, unsigned int sum)
  79. {
  80. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  81. }
  82. /*
  83. * this routine is used for miscellaneous IP-like checksums, mainly
  84. * in icmp.c
  85. */
  86. extern unsigned short ip_compute_csum(const unsigned char * buff, int len);
  87. #endif /* _H8300_CHECKSUM_H */