checksum.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. __wsum csum_partial(const void *buff, int len, __wsum sum);
  23. /*
  24. * the same as csum_partial, but copies from src while it
  25. * checksums, and handles user-space pointer exceptions correctly, when needed.
  26. *
  27. * here even more important to align src and dst on a 32-bit (or even
  28. * better 64-bit) boundary
  29. */
  30. __wsum csum_partial_copy_generic(const void *src, void *dst, int len,
  31. __wsum sum, int *src_err_ptr,
  32. int *dst_err_ptr);
  33. /*
  34. * Note: when you get a NULL pointer exception here this means someone
  35. * passed in an incorrect kernel address to one of these functions.
  36. *
  37. * If you use these functions directly please don't forget the
  38. * access_ok().
  39. */
  40. static inline
  41. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  42. int len, __wsum sum)
  43. {
  44. return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
  45. }
  46. static inline
  47. __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  48. int len, __wsum sum, int *err_ptr)
  49. {
  50. return csum_partial_copy_generic((const void __force *)src, dst, len,
  51. sum, err_ptr, NULL);
  52. }
  53. /*
  54. * This is a version of ip_compute_csum() optimized for IP headers,
  55. * which always checksum on 4 octet boundaries.
  56. */
  57. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  58. {
  59. unsigned int sum, tmp;
  60. __asm__ __volatile__(
  61. " ld.w %0, %1++\n"
  62. " ld.w %3, %1++\n"
  63. " sub %2, 4\n"
  64. " add %0, %3\n"
  65. " ld.w %3, %1++\n"
  66. " adc %0, %0, %3\n"
  67. " ld.w %3, %1++\n"
  68. " adc %0, %0, %3\n"
  69. " acr %0\n"
  70. "1: ld.w %3, %1++\n"
  71. " add %0, %3\n"
  72. " acr %0\n"
  73. " sub %2, 1\n"
  74. " brne 1b\n"
  75. " lsl %3, %0, 16\n"
  76. " andl %0, 0\n"
  77. " mov %2, 0xffff\n"
  78. " add %0, %3\n"
  79. " adc %0, %0, %2\n"
  80. " com %0\n"
  81. " lsr %0, 16\n"
  82. : "=r"(sum), "=r"(iph), "=r"(ihl), "=r"(tmp)
  83. : "1"(iph), "2"(ihl)
  84. : "memory", "cc");
  85. return (__force __sum16)sum;
  86. }
  87. /*
  88. * Fold a partial checksum
  89. */
  90. static inline __sum16 csum_fold(__wsum sum)
  91. {
  92. unsigned int tmp;
  93. asm(" bfextu %1, %0, 0, 16\n"
  94. " lsr %0, 16\n"
  95. " add %0, %1\n"
  96. " bfextu %1, %0, 16, 16\n"
  97. " add %0, %1"
  98. : "=&r"(sum), "=&r"(tmp)
  99. : "0"(sum));
  100. return (__force __sum16)~sum;
  101. }
  102. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  103. unsigned short len,
  104. unsigned short proto,
  105. __wsum sum)
  106. {
  107. asm(" add %0, %1\n"
  108. " adc %0, %0, %2\n"
  109. " adc %0, %0, %3\n"
  110. " acr %0"
  111. : "=r"(sum)
  112. : "r"(daddr), "r"(saddr), "r"(len + proto),
  113. "0"(sum)
  114. : "cc");
  115. return sum;
  116. }
  117. /*
  118. * computes the checksum of the TCP/UDP pseudo-header
  119. * returns a 16-bit checksum, already complemented
  120. */
  121. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  122. unsigned short len,
  123. unsigned short proto,
  124. __wsum sum)
  125. {
  126. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  127. }
  128. /*
  129. * this routine is used for miscellaneous IP-like checksums, mainly
  130. * in icmp.c
  131. */
  132. static inline __sum16 ip_compute_csum(const void *buff, int len)
  133. {
  134. return csum_fold(csum_partial(buff, len, 0));
  135. }
  136. #endif /* __ASM_AVR32_CHECKSUM_H */