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