Path: blob/master/arch/ia64/lib/csum_partial_copy.c
10817 views
/*1* Network Checksum & Copy routine2*3* Copyright (C) 1999, 2003-2004 Hewlett-Packard Co4* Stephane Eranian <[email protected]>5*6* Most of the code has been imported from Linux/Alpha7*/89#include <linux/module.h>10#include <linux/types.h>11#include <linux/string.h>1213#include <asm/uaccess.h>1415/*16* XXX Fixme: those 2 inlines are meant for debugging and will go away17*/18static inline unsigned19short from64to16(unsigned long x)20{21/* add up 32-bit words for 33 bits */22x = (x & 0xffffffff) + (x >> 32);23/* add up 16-bit and 17-bit words for 17+c bits */24x = (x & 0xffff) + (x >> 16);25/* add up 16-bit and 2-bit for 16+c bit */26x = (x & 0xffff) + (x >> 16);27/* add up carry.. */28x = (x & 0xffff) + (x >> 16);29return x;30}3132static inline33unsigned long do_csum_c(const unsigned char * buff, int len, unsigned int psum)34{35int odd, count;36unsigned long result = (unsigned long)psum;3738if (len <= 0)39goto out;40odd = 1 & (unsigned long) buff;41if (odd) {42result = *buff << 8;43len--;44buff++;45}46count = len >> 1; /* nr of 16-bit words.. */47if (count) {48if (2 & (unsigned long) buff) {49result += *(unsigned short *) buff;50count--;51len -= 2;52buff += 2;53}54count >>= 1; /* nr of 32-bit words.. */55if (count) {56if (4 & (unsigned long) buff) {57result += *(unsigned int *) buff;58count--;59len -= 4;60buff += 4;61}62count >>= 1; /* nr of 64-bit words.. */63if (count) {64unsigned long carry = 0;65do {66unsigned long w = *(unsigned long *) buff;67count--;68buff += 8;69result += carry;70result += w;71carry = (w > result);72} while (count);73result += carry;74result = (result & 0xffffffff) + (result >> 32);75}76if (len & 4) {77result += *(unsigned int *) buff;78buff += 4;79}80}81if (len & 2) {82result += *(unsigned short *) buff;83buff += 2;84}85}86if (len & 1)87result += *buff;8889result = from64to16(result);9091if (odd)92result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);9394out:95return result;96}9798/*99* XXX Fixme100*101* This is very ugly but temporary. THIS NEEDS SERIOUS ENHANCEMENTS.102* But it's very tricky to get right even in C.103*/104extern unsigned long do_csum(const unsigned char *, long);105106__wsum107csum_partial_copy_from_user(const void __user *src, void *dst,108int len, __wsum psum, int *errp)109{110unsigned long result;111112/* XXX Fixme113* for now we separate the copy from checksum for obvious114* alignment difficulties. Look at the Alpha code and you'll be115* scared.116*/117118if (__copy_from_user(dst, src, len) != 0 && errp)119*errp = -EFAULT;120121result = do_csum(dst, len);122123/* add in old sum, and carry.. */124result += (__force u32)psum;125/* 32+c bits -> 32 bits */126result = (result & 0xffffffff) + (result >> 32);127return (__force __wsum)result;128}129130EXPORT_SYMBOL(csum_partial_copy_from_user);131132__wsum133csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)134{135return csum_partial_copy_from_user((__force const void __user *)src,136dst, len, sum, NULL);137}138139EXPORT_SYMBOL(csum_partial_copy_nocheck);140141142