more changes on original files
[linux-2.4.git] / include / asm-parisc / checksum.h
1 #ifndef _PARISC_CHECKSUM_H
2 #define _PARISC_CHECKSUM_H
3
4 /*
5  * computes the checksum of a memory block at buff, length len,
6  * and adds in "sum" (32-bit)
7  *
8  * returns a 32-bit number suitable for feeding into itself
9  * or csum_tcpudp_magic
10  *
11  * this function must be called with even lengths, except
12  * for the last fragment, which may be odd
13  *
14  * it's best to have buff aligned on a 32-bit boundary
15  */
16 extern unsigned int csum_partial(const unsigned char *, int, unsigned int);
17
18 /*
19  * the same as csum_partial, but copies from src while it
20  * checksums
21  *
22  * here even more important to align src and dst on a 32-bit (or even
23  * better 64-bit) boundary
24  */
25 extern unsigned int csum_partial_copy(const char *, char *, int, unsigned int);
26
27 /*
28  * the same as csum_partial, but copies from user space
29  *
30  * this is obsolete and will go away.
31  */
32 #define csum_partial_copy_fromuser csum_partial_copy
33
34 /*
35  * this is a new version of the above that records errors it finds in *errp,
36  * but continues and zeros the rest of the buffer.
37  */
38 unsigned int csum_partial_copy_from_user(const char *src, char *dst, int len, unsigned int sum, int *errp);
39
40 /*
41  *      Note: when you get a NULL pointer exception here this means someone
42  *      passed in an incorrect kernel address to one of these functions. 
43  *      
44  *      If you use these functions directly please don't forget the 
45  *      verify_area().
46  */
47 extern __inline__
48 unsigned int csum_partial_copy_nocheck (const char *src, char *dst,
49                                         int len, int sum)
50 {
51         return csum_partial_copy (src, dst, len, sum);
52 }
53
54 /*
55  *      Optimized for IP headers, which always checksum on 4 octet boundaries.
56  */
57 static inline unsigned short ip_fast_csum(unsigned char * iph,
58                                           unsigned int ihl) {
59         unsigned int sum;
60
61
62         __asm__ __volatile__ (
63 "       ldws,ma         4(%1), %0\n"
64 "       addib,<=        -4, %2, 2f\n"
65 "\n"
66 "       ldws            4(%1), %%r20\n"
67 "       ldws            8(%1), %%r21\n"
68 "       add             %0, %%r20, %0\n"
69 "       ldws,ma         12(%1), %%r19\n"
70 "       addc            %0, %%r21, %0\n"
71 "       addc            %0, %%r19, %0\n"
72 "1:     ldws,ma         4(%1), %%r19\n"
73 "       addib,<         0, %2, 1b\n"
74 "       addc            %0, %%r19, %0\n"
75 "\n"
76 "       extru           %0, 31, 16, %%r20\n"
77 "       extru           %0, 15, 16, %%r21\n"
78 "       addc            %%r20, %%r21, %0\n"
79 "       extru           %0, 15, 16, %%r21\n"
80 "       add             %0, %%r21, %0\n"
81 "       subi            -1, %0, %0\n"
82 "2:\n"
83         : "=r" (sum), "=r" (iph), "=r" (ihl)
84         : "1" (iph), "2" (ihl)
85         : "r19", "r20", "r21" );
86
87         return(sum);
88 }
89
90 /*
91  *      Fold a partial checksum
92  */
93 static inline unsigned int csum_fold(unsigned int sum)
94 {
95         /* add the swapped two 16-bit halves of sum,
96            a possible carry from adding the two 16-bit halves,
97            will carry from the lower half into the upper half,
98            giving us the correct sum in the upper half. */
99         sum += (sum << 16) + (sum >> 16);
100         return (~sum) >> 16;
101 }
102  
103 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
104                                                unsigned long daddr,
105                                                unsigned short len,
106                                                unsigned short proto,
107                                                unsigned int sum) 
108 {
109         __asm__(
110         "       add  %1, %0, %0\n"
111         "       addc %2, %0, %0\n"
112         "       addc %3, %0, %0\n"
113         "       addc %%r0, %0, %0\n"
114                 : "=r" (sum)
115                 : "r" (daddr), "r"(saddr), "r"((proto<<16)+len), "0"(sum));
116     return sum;
117 }
118
119 /*
120  * computes the checksum of the TCP/UDP pseudo-header
121  * returns a 16-bit checksum, already complemented
122  */
123 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
124                                                    unsigned long daddr,
125                                                    unsigned short len,
126                                                    unsigned short proto,
127                                                    unsigned int sum) 
128 {
129         return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
130 }
131
132 /*
133  * this routine is used for miscellaneous IP-like checksums, mainly
134  * in icmp.c
135  */
136 static inline unsigned short ip_compute_csum(unsigned char * buf, int len) {
137          return csum_fold (csum_partial(buf, len, 0));
138 }
139
140
141 #define _HAVE_ARCH_IPV6_CSUM
142 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
143                                                      struct in6_addr *daddr,
144                                                      __u16 len,
145                                                      unsigned short proto,
146                                                      unsigned int sum) 
147 {
148         __asm__ __volatile__ (
149
150 #if BITS_PER_LONG > 32
151
152         /*
153         ** We can execute two loads and two adds per cycle on PA 8000.
154         ** But add insn's get serialized waiting for the carry bit.
155         ** Try to keep 4 registers with "live" values ahead of the ALU.
156         */
157
158 "       ldd,ma          8(%1), %%r19\n" /* get 1st saddr word */
159 "       ldd,ma          8(%2), %%r20\n" /* get 1st daddr word */
160 "       add             %8, %3, %3\n"/* add 16-bit proto + len */
161 "       add             %%r19, %0, %0\n"
162 "       ldd,ma          8(%1), %%r21\n" /* 2cd saddr */
163 "       ldd,ma          8(%2), %%r22\n" /* 2cd daddr */
164 "       add,dc          %%r20, %0, %0\n"
165 "       add,dc          %%r21, %0, %0\n"
166 "       add,dc          %%r22, %0, %0\n"
167 "       add,dc          %3, %0, %0\n"  /* fold in proto+len | carry bit */
168 "       extrd,u         %0, 31, 32, %%r19\n"    /* copy upper half down */
169 "       depdi           0, 31, 32, %0\n"        /* clear upper half */
170 "       add             %%r19, %0, %0\n"        /* fold into 32-bits */
171 "       addc            0, %0, %0\n"            /* add carry */
172
173 #else
174
175         /*
176         ** For PA 1.x, the insn order doesn't matter as much.
177         ** Insn stream is serialized on the carry bit here too.
178         ** result from the previous operation (eg r0 + x)
179         */
180
181 "       ldw,ma          4(%1), %%r19\n" /* get 1st saddr word */
182 "       ldw,ma          4(%2), %%r20\n" /* get 1st daddr word */
183 "       add             %8, %3, %3\n"   /* add 16-bit proto + len */
184 "       add             %%r19, %0, %0\n"
185 "       ldw,ma          4(%1), %%r21\n" /* 2cd saddr */
186 "       addc            %%r20, %0, %0\n"
187 "       ldw,ma          4(%2), %%r22\n" /* 2cd daddr */
188 "       addc            %%r21, %0, %0\n"
189 "       ldw,ma          4(%1), %%r19\n" /* 3rd saddr */
190 "       addc            %%r22, %0, %0\n"
191 "       ldw,ma          4(%2), %%r20\n" /* 3rd daddr */
192 "       addc            %%r19, %0, %0\n"
193 "       ldw,ma          4(%1), %%r21\n" /* 4th saddr */
194 "       addc            %%r20, %0, %0\n"
195 "       ldw,ma          4(%2), %%r22\n" /* 4th daddr */
196 "       addc            %%r21, %0, %0\n"
197 "       addc            %%r22, %0, %0\n"
198 "       addc            %3, %0, %0\n"   /* fold in proto+len, catch carry */
199
200 #endif
201         : "=r" (sum), "=r" (saddr), "=r" (daddr), "=r" (len)
202         : "0" (sum), "1" (saddr), "2" (daddr), "3" (len), "r" (proto)
203         : "r19", "r20", "r21", "r22");
204         return csum_fold(sum);
205 }
206
207 /* 
208  *      Copy and checksum to user
209  */
210 #define HAVE_CSUM_COPY_USER
211 static __inline__ unsigned int csum_and_copy_to_user (const char *src, char *dst,
212                                     int len, int sum, int *err_ptr)
213 {
214         /* code stolen from include/asm-mips64 */
215         sum = csum_partial(src, len, sum);
216          
217         if (copy_to_user(dst, src, len)) {
218                 *err_ptr = -EFAULT;
219                 return -1;
220         }
221
222         return sum;
223 }
224
225 #endif
226