2 * Network Checksum & Copy routine
4 * Copyright (C) 1999, 2003-2004 Hewlett-Packard Co
5 * Stephane Eranian <eranian@hpl.hp.com>
7 * Most of the code has been imported from Linux/Alpha
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
14 #include <asm/uaccess.h>
16 static inline unsigned
17 short from64to16(unsigned long x
)
19 /* add up 32-bit words for 33 bits */
20 x
= (x
& 0xffffffff) + (x
>> 32);
21 /* add up 16-bit and 17-bit words for 17+c bits */
22 x
= (x
& 0xffff) + (x
>> 16);
23 /* add up 16-bit and 2-bit for 16+c bit */
24 x
= (x
& 0xffff) + (x
>> 16);
26 x
= (x
& 0xffff) + (x
>> 16);
31 unsigned long do_csum_c(const unsigned char * buff
, int len
, unsigned int psum
)
34 unsigned long result
= (unsigned long)psum
;
38 odd
= 1 & (unsigned long) buff
;
44 count
= len
>> 1; /* nr of 16-bit words.. */
46 if (2 & (unsigned long) buff
) {
47 result
+= *(unsigned short *) buff
;
52 count
>>= 1; /* nr of 32-bit words.. */
54 if (4 & (unsigned long) buff
) {
55 result
+= *(unsigned int *) buff
;
60 count
>>= 1; /* nr of 64-bit words.. */
62 unsigned long carry
= 0;
64 unsigned long w
= *(unsigned long *) buff
;
72 result
= (result
& 0xffffffff) + (result
>> 32);
75 result
+= *(unsigned int *) buff
;
80 result
+= *(unsigned short *) buff
;
87 result
= from64to16(result
);
90 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
96 extern unsigned long do_csum(const unsigned char *, long);
99 csum_partial_copy_from_user(const void __user
*src
, void *dst
,
100 int len
, __wsum psum
, int *errp
)
102 unsigned long result
;
105 if (__copy_from_user(dst
, src
, len
) != 0 && errp
)
108 result
= do_csum(dst
, len
);
110 /* add in old sum, and carry.. */
111 result
+= (__force u32
)psum
;
112 /* 32+c bits -> 32 bits */
113 result
= (result
& 0xffffffff) + (result
>> 32);
114 return (__force __wsum
)result
;
117 EXPORT_SYMBOL(csum_partial_copy_from_user
);
120 csum_partial_copy_nocheck(const void *src
, void *dst
, int len
, __wsum sum
)
122 return csum_partial_copy_from_user((__force
const void __user
*)src
,
123 dst
, len
, sum
, NULL
);
126 EXPORT_SYMBOL(csum_partial_copy_nocheck
);