Path: blob/master/arch/avr32/include/asm/checksum.h
10819 views
/*1* Copyright (C) 2004-2006 Atmel Corporation2*3* This program is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License version 2 as5* published by the Free Software Foundation.6*/7#ifndef __ASM_AVR32_CHECKSUM_H8#define __ASM_AVR32_CHECKSUM_H910/*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 itself15* or csum_tcpudp_magic16*17* this function must be called with even lengths, except18* for the last fragment, which may be odd19*20* it's best to have buff aligned on a 32-bit boundary21*/22__wsum csum_partial(const void *buff, int len, __wsum sum);2324/*25* the same as csum_partial, but copies from src while it26* 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 even29* better 64-bit) boundary30*/31__wsum csum_partial_copy_generic(const void *src, void *dst, int len,32__wsum sum, int *src_err_ptr,33int *dst_err_ptr);3435/*36* Note: when you get a NULL pointer exception here this means someone37* passed in an incorrect kernel address to one of these functions.38*39* If you use these functions directly please don't forget the40* access_ok().41*/42static inline43__wsum csum_partial_copy_nocheck(const void *src, void *dst,44int len, __wsum sum)45{46return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);47}4849static inline50__wsum csum_partial_copy_from_user(const void __user *src, void *dst,51int len, __wsum sum, int *err_ptr)52{53return csum_partial_copy_generic((const void __force *)src, dst, len,54sum, err_ptr, NULL);55}5657/*58* This is a version of ip_compute_csum() optimized for IP headers,59* which always checksum on 4 octet boundaries.60*/61static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)62{63unsigned int sum, tmp;6465__asm__ __volatile__(66" ld.w %0, %1++\n"67" ld.w %3, %1++\n"68" sub %2, 4\n"69" add %0, %3\n"70" ld.w %3, %1++\n"71" adc %0, %0, %3\n"72" ld.w %3, %1++\n"73" adc %0, %0, %3\n"74" acr %0\n"75"1: ld.w %3, %1++\n"76" add %0, %3\n"77" acr %0\n"78" sub %2, 1\n"79" brne 1b\n"80" lsl %3, %0, 16\n"81" andl %0, 0\n"82" mov %2, 0xffff\n"83" add %0, %3\n"84" adc %0, %0, %2\n"85" com %0\n"86" lsr %0, 16\n"87: "=r"(sum), "=r"(iph), "=r"(ihl), "=r"(tmp)88: "1"(iph), "2"(ihl)89: "memory", "cc");90return (__force __sum16)sum;91}9293/*94* Fold a partial checksum95*/9697static inline __sum16 csum_fold(__wsum sum)98{99unsigned int tmp;100101asm(" bfextu %1, %0, 0, 16\n"102" lsr %0, 16\n"103" add %0, %1\n"104" bfextu %1, %0, 16, 16\n"105" add %0, %1"106: "=&r"(sum), "=&r"(tmp)107: "0"(sum));108109return (__force __sum16)~sum;110}111112static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,113unsigned short len,114unsigned short proto,115__wsum sum)116{117asm(" add %0, %1\n"118" adc %0, %0, %2\n"119" adc %0, %0, %3\n"120" acr %0"121: "=r"(sum)122: "r"(daddr), "r"(saddr), "r"(len + proto),123"0"(sum)124: "cc");125126return sum;127}128129/*130* computes the checksum of the TCP/UDP pseudo-header131* returns a 16-bit checksum, already complemented132*/133static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,134unsigned short len,135unsigned short proto,136__wsum sum)137{138return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));139}140141/*142* this routine is used for miscellaneous IP-like checksums, mainly143* in icmp.c144*/145146static inline __sum16 ip_compute_csum(const void *buff, int len)147{148return csum_fold(csum_partial(buff, len, 0));149}150151#endif /* __ASM_AVR32_CHECKSUM_H */152153154