fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / lib / checksum.S
1 /*
2  * BK Id: SCCS/s.checksum.S 1.11 08/13/02 21:52:53 paulus
3  */
4 /*
5  * This file contains assembly-language implementations
6  * of IP-style 1's complement checksum routines.
7  *      
8  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
9  *
10  *  This program is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version
13  *  2 of the License, or (at your option) any later version.
14  *
15  * Severely hacked about by Paul Mackerras (paulus@cs.anu.edu.au).
16  */
17
18 #include <linux/sys.h>
19 #include <asm/processor.h>
20 #include <asm/errno.h>
21 #include <asm/ppc_asm.h>
22
23         .text
24
25 /*
26  * ip_fast_csum(buf, len) -- Optimized for IP header
27  * len is in words and is always >= 5.
28  */
29 _GLOBAL(ip_fast_csum)
30         lwz     r0,0(r3)
31         lwzu    r5,4(r3)
32         addic.  r4,r4,-2
33         addc    r0,r0,r5
34         mtctr   r4
35         blelr-
36 1:      lwzu    r4,4(r3)
37         adde    r0,r0,r4
38         bdnz    1b
39         addze   r0,r0           /* add in final carry */
40         rlwinm  r3,r0,16,0,31   /* fold two halves together */
41         add     r3,r0,r3
42         not     r3,r3
43         srwi    r3,r3,16
44         blr
45
46 /*
47  * Compute checksum of TCP or UDP pseudo-header:
48  *   csum_tcpudp_magic(saddr, daddr, len, proto, sum)
49  */     
50 _GLOBAL(csum_tcpudp_magic)
51         rlwimi  r5,r6,16,0,15   /* put proto in upper half of len */
52         addc    r0,r3,r4        /* add 4 32-bit words together */
53         adde    r0,r0,r5
54         adde    r0,r0,r7
55         addze   r0,r0           /* add in final carry */
56         rlwinm  r3,r0,16,0,31   /* fold two halves together */
57         add     r3,r0,r3
58         not     r3,r3
59         srwi    r3,r3,16
60         blr
61
62 /*
63  * computes the checksum of a memory block at buff, length len,
64  * and adds in "sum" (32-bit)
65  *
66  * csum_partial(buff, len, sum)
67  */
68 _GLOBAL(csum_partial)
69         addic   r0,r5,0
70         subi    r3,r3,4
71         srwi.   r6,r4,2
72         beq     3f              /* if we're doing < 4 bytes */
73         andi.   r5,r3,2         /* Align buffer to longword boundary */
74         beq+    1f
75         lhz     r5,4(r3)        /* do 2 bytes to get aligned */
76         addi    r3,r3,2
77         subi    r4,r4,2
78         addc    r0,r0,r5
79         srwi.   r6,r4,2         /* # words to do */
80         beq     3f
81 1:      mtctr   r6
82 2:      lwzu    r5,4(r3)        /* the bdnz has zero overhead, so it should */
83         adde    r0,r0,r5        /* be unnecessary to unroll this loop */
84         bdnz    2b
85         andi.   r4,r4,3
86 3:      cmpi    0,r4,2
87         blt+    4f
88         lhz     r5,4(r3)
89         addi    r3,r3,2
90         subi    r4,r4,2
91         adde    r0,r0,r5
92 4:      cmpi    0,r4,1
93         bne+    5f
94         lbz     r5,4(r3)
95         slwi    r5,r5,8         /* Upper byte of word */
96         adde    r0,r0,r5
97 5:      addze   r3,r0           /* add in final carry */
98         blr
99
100 /*
101  * Computes the checksum of a memory block at src, length len,
102  * and adds in "sum" (32-bit), while copying the block to dst.
103  * If an access exception occurs on src or dst, it stores -EFAULT
104  * to *src_err or *dst_err respectively, and (for an error on
105  * src) zeroes the rest of dst.
106  *
107  * csum_partial_copy_generic(src, dst, len, sum, src_err, dst_err)
108  */
109 _GLOBAL(csum_partial_copy_generic)
110         addic   r0,r6,0
111         subi    r3,r3,4
112         subi    r4,r4,4
113         srwi.   r6,r5,2
114         beq     3f              /* if we're doing < 4 bytes */
115         andi.   r9,r4,2         /* Align dst to longword boundary */
116         beq+    1f
117 81:     lhz     r6,4(r3)        /* do 2 bytes to get aligned */
118         addi    r3,r3,2
119         subi    r5,r5,2
120 91:     sth     r6,4(r4)
121         addi    r4,r4,2
122         addc    r0,r0,r6
123         srwi.   r6,r5,2         /* # words to do */
124         beq     3f
125 1:      srwi.   r6,r5,4         /* # groups of 4 words to do */
126         beq     10f
127         mtctr   r6
128 71:     lwz     r6,4(r3)
129 72:     lwz     r9,8(r3)
130 73:     lwz     r10,12(r3)
131 74:     lwzu    r11,16(r3)
132         adde    r0,r0,r6
133 75:     stw     r6,4(r4)
134         adde    r0,r0,r9
135 76:     stw     r9,8(r4)
136         adde    r0,r0,r10
137 77:     stw     r10,12(r4)
138         adde    r0,r0,r11
139 78:     stwu    r11,16(r4)
140         bdnz    71b
141 10:     rlwinm. r6,r5,30,30,31  /* # words left to do */
142         beq     13f
143         mtctr   r6
144 82:     lwzu    r9,4(r3)
145 92:     stwu    r9,4(r4)
146         adde    r0,r0,r9
147         bdnz    82b
148 13:     andi.   r5,r5,3
149 3:      cmpi    0,r5,2
150         blt+    4f
151 83:     lhz     r6,4(r3)
152         addi    r3,r3,2
153         subi    r5,r5,2
154 93:     sth     r6,4(r4)
155         addi    r4,r4,2
156         adde    r0,r0,r6
157 4:      cmpi    0,r5,1
158         bne+    5f
159 84:     lbz     r6,4(r3)
160 94:     stb     r6,4(r4)
161         slwi    r6,r6,8         /* Upper byte of word */
162         adde    r0,r0,r6
163 5:      addze   r3,r0           /* add in final carry */
164         blr
165
166 /* These shouldn't go in the fixup section, since that would
167    cause the ex_table addresses to get out of order. */
168
169 src_error_4:
170         mfctr   r6              /* update # bytes remaining from ctr */
171         rlwimi  r5,r6,4,0,27
172         b       79f
173 src_error_1:
174         li      r6,0
175         subi    r5,r5,2
176 95:     sth     r6,4(r4)
177         addi    r4,r4,2
178 79:     srwi.   r6,r5,2
179         beq     3f
180         mtctr   r6
181 src_error_2:
182         li      r6,0
183 96:     stwu    r6,4(r4)
184         bdnz    96b
185 3:      andi.   r5,r5,3
186         beq     src_error
187 src_error_3:
188         li      r6,0
189         mtctr   r5
190         addi    r4,r4,3
191 97:     stbu    r6,1(r4)
192         bdnz    97b
193 src_error:
194         cmpi    0,r7,0
195         beq     1f
196         li      r6,-EFAULT
197         stw     r6,0(r7)
198 1:      addze   r3,r0
199         blr
200
201 dst_error:
202         cmpi    0,r8,0
203         beq     1f
204         li      r6,-EFAULT
205         stw     r6,0(r8)
206 1:      addze   r3,r0
207         blr
208
209 .section __ex_table,"a"
210         .long   81b,src_error_1
211         .long   91b,dst_error
212         .long   71b,src_error_4
213         .long   72b,src_error_4
214         .long   73b,src_error_4
215         .long   74b,src_error_4
216         .long   75b,dst_error
217         .long   76b,dst_error
218         .long   77b,dst_error
219         .long   78b,dst_error
220         .long   82b,src_error_2
221         .long   92b,dst_error
222         .long   83b,src_error_3
223         .long   93b,dst_error
224         .long   84b,src_error_3
225         .long   94b,dst_error
226         .long   95b,dst_error
227         .long   96b,dst_error
228         .long   97b,dst_error