checksum.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef __ASM_AVR32_CHECKSUM_H
  9. #define __ASM_AVR32_CHECKSUM_H
  10. /*
  11. * computes the checksum of a memory block at buff, length len,
  12. * and adds in "sum" (32-bit)
  13. *
  14. * returns a 32-bit number suitable for feeding into itself
  15. * or csum_tcpudp_magic
  16. *
  17. * this function must be called with even lengths, except
  18. * for the last fragment, which may be odd
  19. *
  20. * it's best to have buff aligned on a 32-bit boundary
  21. */
  22. unsigned int csum_partial(const unsigned char * buff, int len,
  23. unsigned int sum);
  24. /*
  25. * the same as csum_partial, but copies from src while it
  26. * checksums, and handles user-space pointer exceptions correctly, when needed.
  27. *
  28. * here even more important to align src and dst on a 32-bit (or even
  29. * better 64-bit) boundary
  30. */
  31. unsigned int csum_partial_copy_generic(const char *src, char *dst, int len,
  32. int sum, int *src_err_ptr,
  33. int *dst_err_ptr);
  34. /*
  35. * Note: when you get a NULL pointer exception here this means someone
  36. * passed in an incorrect kernel address to one of these functions.
  37. *
  38. * If you use these functions directly please don't forget the
  39. * verify_area().
  40. */
  41. static inline
  42. unsigned int csum_partial_copy_nocheck(const char *src, char *dst,
  43. int len, int sum)
  44. {
  45. return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
  46. }
  47. static inline
  48. unsigned int csum_partial_copy_from_user (const char __user *src, char *dst,
  49. int len, int sum, int *err_ptr)
  50. {
  51. return csum_partial_copy_generic((const char __force *)src, dst, len,
  52. sum, err_ptr, NULL);
  53. }
  54. /*
  55. * This is a version of ip_compute_csum() optimized for IP headers,
  56. * which always checksum on 4 octet boundaries.
  57. */
  58. static inline unsigned short ip_fast_csum(unsigned char *iph,
  59. unsigned int ihl)
  60. {
  61. unsigned int sum, tmp;
  62. __asm__ __volatile__(
  63. " ld.w %0, %1++\n"
  64. " ld.w %3, %1++\n"
  65. " sub %2, 4\n"
  66. " add %0, %3\n"
  67. " ld.w %3, %1++\n"
  68. " adc %0, %0, %3\n"
  69. " ld.w %3, %1++\n"
  70. " adc %0, %0, %3\n"
  71. " acr %0\n"
  72. "1: ld.w %3, %1++\n"
  73. " add %0, %3\n"
  74. " acr %0\n"
  75. " sub %2, 1\n"
  76. " brne 1b\n"
  77. " lsl %3, %0, 16\n"
  78. " andl %0, 0\n"
  79. " mov %2, 0xffff\n"
  80. " add %0, %3\n"
  81. " adc %0, %0, %2\n"
  82. " com %0\n"
  83. " lsr %0, 16\n"
  84. : "=r"(sum), "=r"(iph), "=r"(ihl), "=r"(tmp)
  85. : "1"(iph), "2"(ihl)
  86. : "memory", "cc");
  87. return sum;
  88. }
  89. /*
  90. * Fold a partial checksum
  91. */
  92. static inline unsigned int csum_fold(unsigned int sum)
  93. {
  94. unsigned int tmp;
  95. asm(" bfextu %1, %0, 0, 16\n"
  96. " lsr %0, 16\n"
  97. " add %0, %1\n"
  98. " bfextu %1, %0, 16, 16\n"
  99. " add %0, %1"
  100. : "=&r"(sum), "=&r"(tmp)
  101. : "0"(sum));
  102. return ~sum;
  103. }
  104. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  105. unsigned long daddr,
  106. unsigned short len,
  107. unsigned short proto,
  108. unsigned int sum)
  109. {
  110. asm(" add %0, %1\n"
  111. " adc %0, %0, %2\n"
  112. " adc %0, %0, %3\n"
  113. " acr %0"
  114. : "=r"(sum)
  115. : "r"(daddr), "r"(saddr), "r"(ntohs(len) | (proto << 16)),
  116. "0"(sum)
  117. : "cc");
  118. return sum;
  119. }
  120. /*
  121. * computes the checksum of the TCP/UDP pseudo-header
  122. * returns a 16-bit checksum, already complemented
  123. */
  124. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  125. unsigned long daddr,
  126. unsigned short len,
  127. unsigned short proto,
  128. unsigned int sum)
  129. {
  130. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  131. }
  132. /*
  133. * this routine is used for miscellaneous IP-like checksums, mainly
  134. * in icmp.c
  135. */
  136. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  137. {
  138. return csum_fold(csum_partial(buff, len, 0));
  139. }
  140. #endif /* __ASM_AVR32_CHECKSUM_H */