2 * Network Checksum & Copy routine
4 * Copyright (C) 1999 Hewlett-Packard Co
5 * Copyright (C) 1999 Stephane Eranian <eranian@hpl.hp.com>
7 * Most of the code has been imported from Linux/Alpha
10 #include <linux/types.h>
11 #include <linux/string.h>
13 #include <asm/uaccess.h>
16 * XXX Fixme: those 2 inlines are meant for debugging and will go away
18 static inline unsigned
19 short from64to16(unsigned long x
)
21 /* add up 32-bit words for 33 bits */
22 x
= (x
& 0xffffffff) + (x
>> 32);
23 /* add up 16-bit and 17-bit words for 17+c bits */
24 x
= (x
& 0xffff) + (x
>> 16);
25 /* add up 16-bit and 2-bit for 16+c bit */
26 x
= (x
& 0xffff) + (x
>> 16);
28 x
= (x
& 0xffff) + (x
>> 16);
33 unsigned long do_csum_c(const unsigned char * buff
, int len
, unsigned int psum
)
36 unsigned long result
= (unsigned long)psum
;
40 odd
= 1 & (unsigned long) buff
;
46 count
= len
>> 1; /* nr of 16-bit words.. */
48 if (2 & (unsigned long) buff
) {
49 result
+= *(unsigned short *) buff
;
54 count
>>= 1; /* nr of 32-bit words.. */
56 if (4 & (unsigned long) buff
) {
57 result
+= *(unsigned int *) buff
;
62 count
>>= 1; /* nr of 64-bit words.. */
64 unsigned long carry
= 0;
66 unsigned long w
= *(unsigned long *) buff
;
74 result
= (result
& 0xffffffff) + (result
>> 32);
77 result
+= *(unsigned int *) buff
;
82 result
+= *(unsigned short *) buff
;
89 result
= from64to16(result
);
92 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
101 * This is very ugly but temporary. THIS NEEDS SERIOUS ENHANCEMENTS.
102 * But it's very tricky to get right even in C.
104 extern unsigned long do_csum(const unsigned char *, int);
107 do_csum_partial_copy_from_user (const char *src
, char *dst
, int len
,
108 unsigned int psum
, int *errp
)
110 const unsigned char *psrc
= src
;
111 unsigned long result
;
116 * for now we separate the copy from checksum for obvious
117 * alignment difficulties. Look at the Alpha code and you'll be
121 while ( cplen
-- ) r
|=__get_user(*dst
++,psrc
++);
123 if ( r
&& errp
) *errp
= r
;
125 result
= do_csum(src
, len
);
127 /* add in old sum, and carry.. */
129 /* 32+c bits -> 32 bits */
130 result
= (result
& 0xffffffff) + (result
>> 32);
135 csum_partial_copy_from_user(const char *src
, char *dst
, int len
,
136 unsigned int sum
, int *errp
)
138 if (!access_ok(src
, len
, VERIFY_READ
)) {
144 return do_csum_partial_copy_from_user(src
, dst
, len
, sum
, errp
);
148 csum_partial_copy_nocheck(const char *src
, char *dst
, int len
, unsigned int sum
)
150 return do_csum_partial_copy_from_user(src
, dst
, len
, sum
, NULL
);
154 csum_partial_copy (const char *src
, char *dst
, int len
, unsigned int sum
)
159 ret
= do_csum_partial_copy_from_user(src
, dst
, len
, sum
, &error
);
161 printk("csum_partial_copy_old(): tell mingo to convert me!\n");