checksum.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* MN10300 Optimised checksumming wrappers
  2. *
  3. * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <asm/byteorder.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/checksum.h>
  17. #include "internal.h"
  18. static inline unsigned short from32to16(__wsum sum)
  19. {
  20. asm(" add %1,%0 \n"
  21. " addc 0xffff,%0 \n"
  22. : "=r" (sum)
  23. : "r" (sum << 16), "0" (sum & 0xffff0000)
  24. : "cc"
  25. );
  26. return sum >> 16;
  27. }
  28. __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  29. {
  30. return ~do_csum(iph, ihl * 4);
  31. }
  32. EXPORT_SYMBOL(ip_fast_csum);
  33. __wsum csum_partial(const void *buff, int len, __wsum sum)
  34. {
  35. __wsum result;
  36. result = do_csum(buff, len);
  37. result += sum;
  38. if (sum > result)
  39. result++;
  40. return result;
  41. }
  42. EXPORT_SYMBOL(csum_partial);
  43. __sum16 ip_compute_csum(const void *buff, int len)
  44. {
  45. return ~from32to16(do_csum(buff, len));
  46. }
  47. EXPORT_SYMBOL(ip_compute_csum);
  48. __wsum csum_partial_copy(const void *src, void *dst, int len, __wsum sum)
  49. {
  50. copy_from_user(dst, src, len);
  51. return csum_partial(dst, len, sum);
  52. }
  53. EXPORT_SYMBOL(csum_partial_copy);
  54. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  55. int len, __wsum sum)
  56. {
  57. sum = csum_partial(src, len, sum);
  58. memcpy(dst, src, len);
  59. return sum;
  60. }
  61. EXPORT_SYMBOL(csum_partial_copy_nocheck);
  62. __wsum csum_partial_copy_from_user(const void *src, void *dst,
  63. int len, __wsum sum,
  64. int *err_ptr)
  65. {
  66. int missing;
  67. missing = copy_from_user(dst, src, len);
  68. if (missing) {
  69. memset(dst + len - missing, 0, missing);
  70. *err_ptr = -EFAULT;
  71. }
  72. return csum_partial(dst, len, sum);
  73. }
  74. EXPORT_SYMBOL(csum_partial_copy_from_user);
  75. __wsum csum_and_copy_to_user(const void *src, void *dst,
  76. int len, __wsum sum,
  77. int *err_ptr)
  78. {
  79. int missing;
  80. missing = copy_to_user(dst, src, len);
  81. if (missing) {
  82. memset(dst + len - missing, 0, missing);
  83. *err_ptr = -EFAULT;
  84. }
  85. return csum_partial(src, len, sum);
  86. }
  87. EXPORT_SYMBOL(csum_and_copy_to_user);