- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / arch / ia64 / lib / csum_partial_copy.c
blobd09f11e2149fc4d8be6590ceb3ca20d78518ca2a
1 /*
2 * Network Checksum & Copy routine
3 *
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
8 */
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);
27 /* add up carry.. */
28 x = (x & 0xffff) + (x >> 16);
29 return x;
32 static inline
33 unsigned long do_csum_c(const unsigned char * buff, int len, unsigned int psum)
35 int odd, count;
36 unsigned long result = (unsigned long)psum;
38 if (len <= 0)
39 goto out;
40 odd = 1 & (unsigned long) buff;
41 if (odd) {
42 result = *buff << 8;
43 len--;
44 buff++;
46 count = len >> 1; /* nr of 16-bit words.. */
47 if (count) {
48 if (2 & (unsigned long) buff) {
49 result += *(unsigned short *) buff;
50 count--;
51 len -= 2;
52 buff += 2;
54 count >>= 1; /* nr of 32-bit words.. */
55 if (count) {
56 if (4 & (unsigned long) buff) {
57 result += *(unsigned int *) buff;
58 count--;
59 len -= 4;
60 buff += 4;
62 count >>= 1; /* nr of 64-bit words.. */
63 if (count) {
64 unsigned long carry = 0;
65 do {
66 unsigned long w = *(unsigned long *) buff;
67 count--;
68 buff += 8;
69 result += carry;
70 result += w;
71 carry = (w > result);
72 } while (count);
73 result += carry;
74 result = (result & 0xffffffff) + (result >> 32);
76 if (len & 4) {
77 result += *(unsigned int *) buff;
78 buff += 4;
81 if (len & 2) {
82 result += *(unsigned short *) buff;
83 buff += 2;
86 if (len & 1)
87 result += *buff;
89 result = from64to16(result);
91 if (odd)
92 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
94 out:
95 return result;
99 * XXX Fixme
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);
106 static unsigned 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;
112 int cplen = len;
113 int r = 0;
115 /* XXX Fixme
116 * for now we separate the copy from checksum for obvious
117 * alignment difficulties. Look at the Alpha code and you'll be
118 * scared.
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.. */
128 result += psum;
129 /* 32+c bits -> 32 bits */
130 result = (result & 0xffffffff) + (result >> 32);
131 return result;
134 unsigned int
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)) {
139 *errp = -EFAULT;
140 memset(dst, 0, len);
141 return sum;
144 return do_csum_partial_copy_from_user(src, dst, len, sum, errp);
147 unsigned int
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);
153 unsigned int
154 csum_partial_copy (const char *src, char *dst, int len, unsigned int sum)
156 unsigned int ret;
157 int error = 0;
159 ret = do_csum_partial_copy_from_user(src, dst, len, sum, &error);
160 if (error)
161 printk("csum_partial_copy_old(): tell mingo to convert me!\n");
163 return ret;