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