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>
17 * XXX Fixme: those 2 inlines are meant for debugging and will go away
19 static inline unsigned
20 short from64to16(unsigned long x
)
22 /* add up 32-bit words for 33 bits */
23 x
= (x
& 0xffffffff) + (x
>> 32);
24 /* add up 16-bit and 17-bit words for 17+c bits */
25 x
= (x
& 0xffff) + (x
>> 16);
26 /* add up 16-bit and 2-bit for 16+c bit */
27 x
= (x
& 0xffff) + (x
>> 16);
29 x
= (x
& 0xffff) + (x
>> 16);
34 unsigned long do_csum_c(const unsigned char * buff
, int len
, unsigned int psum
)
37 unsigned long result
= (unsigned long)psum
;
41 odd
= 1 & (unsigned long) buff
;
47 count
= len
>> 1; /* nr of 16-bit words.. */
49 if (2 & (unsigned long) buff
) {
50 result
+= *(unsigned short *) buff
;
55 count
>>= 1; /* nr of 32-bit words.. */
57 if (4 & (unsigned long) buff
) {
58 result
+= *(unsigned int *) buff
;
63 count
>>= 1; /* nr of 64-bit words.. */
65 unsigned long carry
= 0;
67 unsigned long w
= *(unsigned long *) buff
;
75 result
= (result
& 0xffffffff) + (result
>> 32);
78 result
+= *(unsigned int *) buff
;
83 result
+= *(unsigned short *) buff
;
90 result
= from64to16(result
);
93 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
102 * This is very ugly but temporary. THIS NEEDS SERIOUS ENHANCEMENTS.
103 * But it's very tricky to get right even in C.
105 extern unsigned long do_csum(const unsigned char *, long);
108 do_csum_partial_copy_from_user (const unsigned char __user
*src
, unsigned char *dst
,
109 int len
, unsigned int psum
, int *errp
)
111 unsigned long result
;
114 * for now we separate the copy from checksum for obvious
115 * alignment difficulties. Look at the Alpha code and you'll be
119 if (__copy_from_user(dst
, src
, len
) != 0 && errp
)
122 result
= do_csum(dst
, len
);
124 /* add in old sum, and carry.. */
126 /* 32+c bits -> 32 bits */
127 result
= (result
& 0xffffffff) + (result
>> 32);
132 csum_partial_copy_from_user (const unsigned char __user
*src
, unsigned char *dst
,
133 int len
, unsigned int sum
, int *errp
)
135 if (!access_ok(VERIFY_READ
, src
, len
)) {
141 return do_csum_partial_copy_from_user(src
, dst
, len
, sum
, errp
);
145 csum_partial_copy_nocheck(const unsigned char __user
*src
, unsigned char *dst
,
146 int len
, unsigned int sum
)
148 return do_csum_partial_copy_from_user(src
, dst
, len
, sum
, NULL
);
151 EXPORT_SYMBOL(csum_partial_copy_nocheck
);