MIPS: checksum: Split the 'copy_user' symbol
[firefly-linux-kernel-4.4.55.git] / arch / mips / include / asm / checksum.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995, 96, 97, 98, 99, 2001 by Ralf Baechle
7  * Copyright (C) 1999 Silicon Graphics, Inc.
8  * Copyright (C) 2001 Thiemo Seufer.
9  * Copyright (C) 2002 Maciej W. Rozycki
10  * Copyright (C) 2014 Imagination Technologies Ltd.
11  */
12 #ifndef _ASM_CHECKSUM_H
13 #define _ASM_CHECKSUM_H
14
15 #include <linux/in6.h>
16
17 #include <asm/uaccess.h>
18
19 /*
20  * computes the checksum of a memory block at buff, length len,
21  * and adds in "sum" (32-bit)
22  *
23  * returns a 32-bit number suitable for feeding into itself
24  * or csum_tcpudp_magic
25  *
26  * this function must be called with even lengths, except
27  * for the last fragment, which may be odd
28  *
29  * it's best to have buff aligned on a 32-bit boundary
30  */
31 __wsum csum_partial(const void *buff, int len, __wsum sum);
32
33 __wsum __csum_partial_copy_kernel(const void *src, void *dst,
34                                   int len, __wsum sum, int *err_ptr);
35
36 __wsum __csum_partial_copy_from_user(const void *src, void *dst,
37                                      int len, __wsum sum, int *err_ptr);
38 __wsum __csum_partial_copy_to_user(const void *src, void *dst,
39                                    int len, __wsum sum, int *err_ptr);
40
41 /*
42  * this is a new version of the above that records errors it finds in *errp,
43  * but continues and zeros the rest of the buffer.
44  */
45 static inline
46 __wsum csum_partial_copy_from_user(const void __user *src, void *dst, int len,
47                                    __wsum sum, int *err_ptr)
48 {
49         might_fault();
50         return __csum_partial_copy_from_user((__force void *)src, dst,
51                                              len, sum, err_ptr);
52 }
53
54 /*
55  * Copy and checksum to user
56  */
57 #define HAVE_CSUM_COPY_USER
58 static inline
59 __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len,
60                              __wsum sum, int *err_ptr)
61 {
62         might_fault();
63         if (access_ok(VERIFY_WRITE, dst, len))
64                 return __csum_partial_copy_to_user(src, (__force void *)dst,
65                                                    len, sum, err_ptr);
66         if (len)
67                 *err_ptr = -EFAULT;
68
69         return (__force __wsum)-1; /* invalid checksum */
70 }
71
72 /*
73  * the same as csum_partial, but copies from user space (but on MIPS
74  * we have just one address space, so this is identical to the above)
75  */
76 __wsum csum_partial_copy_nocheck(const void *src, void *dst,
77                                        int len, __wsum sum);
78
79 /*
80  *      Fold a partial checksum without adding pseudo headers
81  */
82 static inline __sum16 csum_fold(__wsum sum)
83 {
84         __asm__(
85         "       .set    push            # csum_fold\n"
86         "       .set    noat            \n"
87         "       sll     $1, %0, 16      \n"
88         "       addu    %0, $1          \n"
89         "       sltu    $1, %0, $1      \n"
90         "       srl     %0, %0, 16      \n"
91         "       addu    %0, $1          \n"
92         "       xori    %0, 0xffff      \n"
93         "       .set    pop"
94         : "=r" (sum)
95         : "0" (sum));
96
97         return (__force __sum16)sum;
98 }
99
100 /*
101  *      This is a version of ip_compute_csum() optimized for IP headers,
102  *      which always checksum on 4 octet boundaries.
103  *
104  *      By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
105  *      Arnt Gulbrandsen.
106  */
107 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
108 {
109         const unsigned int *word = iph;
110         const unsigned int *stop = word + ihl;
111         unsigned int csum;
112         int carry;
113
114         csum = word[0];
115         csum += word[1];
116         carry = (csum < word[1]);
117         csum += carry;
118
119         csum += word[2];
120         carry = (csum < word[2]);
121         csum += carry;
122
123         csum += word[3];
124         carry = (csum < word[3]);
125         csum += carry;
126
127         word += 4;
128         do {
129                 csum += *word;
130                 carry = (csum < *word);
131                 csum += carry;
132                 word++;
133         } while (word != stop);
134
135         return csum_fold(csum);
136 }
137
138 static inline __wsum csum_tcpudp_nofold(__be32 saddr,
139         __be32 daddr, unsigned short len, unsigned short proto,
140         __wsum sum)
141 {
142         __asm__(
143         "       .set    push            # csum_tcpudp_nofold\n"
144         "       .set    noat            \n"
145 #ifdef CONFIG_32BIT
146         "       addu    %0, %2          \n"
147         "       sltu    $1, %0, %2      \n"
148         "       addu    %0, $1          \n"
149
150         "       addu    %0, %3          \n"
151         "       sltu    $1, %0, %3      \n"
152         "       addu    %0, $1          \n"
153
154         "       addu    %0, %4          \n"
155         "       sltu    $1, %0, %4      \n"
156         "       addu    %0, $1          \n"
157 #endif
158 #ifdef CONFIG_64BIT
159         "       daddu   %0, %2          \n"
160         "       daddu   %0, %3          \n"
161         "       daddu   %0, %4          \n"
162         "       dsll32  $1, %0, 0       \n"
163         "       daddu   %0, $1          \n"
164         "       dsra32  %0, %0, 0       \n"
165 #endif
166         "       .set    pop"
167         : "=r" (sum)
168         : "0" ((__force unsigned long)daddr),
169           "r" ((__force unsigned long)saddr),
170 #ifdef __MIPSEL__
171           "r" ((proto + len) << 8),
172 #else
173           "r" (proto + len),
174 #endif
175           "r" ((__force unsigned long)sum));
176
177         return sum;
178 }
179
180 /*
181  * computes the checksum of the TCP/UDP pseudo-header
182  * returns a 16-bit checksum, already complemented
183  */
184 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
185                                                    unsigned short len,
186                                                    unsigned short proto,
187                                                    __wsum sum)
188 {
189         return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
190 }
191
192 /*
193  * this routine is used for miscellaneous IP-like checksums, mainly
194  * in icmp.c
195  */
196 static inline __sum16 ip_compute_csum(const void *buff, int len)
197 {
198         return csum_fold(csum_partial(buff, len, 0));
199 }
200
201 #define _HAVE_ARCH_IPV6_CSUM
202 static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
203                                           const struct in6_addr *daddr,
204                                           __u32 len, unsigned short proto,
205                                           __wsum sum)
206 {
207         __asm__(
208         "       .set    push            # csum_ipv6_magic\n"
209         "       .set    noreorder       \n"
210         "       .set    noat            \n"
211         "       addu    %0, %5          # proto (long in network byte order)\n"
212         "       sltu    $1, %0, %5      \n"
213         "       addu    %0, $1          \n"
214
215         "       addu    %0, %6          # csum\n"
216         "       sltu    $1, %0, %6      \n"
217         "       lw      %1, 0(%2)       # four words source address\n"
218         "       addu    %0, $1          \n"
219         "       addu    %0, %1          \n"
220         "       sltu    $1, %0, %1      \n"
221
222         "       lw      %1, 4(%2)       \n"
223         "       addu    %0, $1          \n"
224         "       addu    %0, %1          \n"
225         "       sltu    $1, %0, %1      \n"
226
227         "       lw      %1, 8(%2)       \n"
228         "       addu    %0, $1          \n"
229         "       addu    %0, %1          \n"
230         "       sltu    $1, %0, %1      \n"
231
232         "       lw      %1, 12(%2)      \n"
233         "       addu    %0, $1          \n"
234         "       addu    %0, %1          \n"
235         "       sltu    $1, %0, %1      \n"
236
237         "       lw      %1, 0(%3)       \n"
238         "       addu    %0, $1          \n"
239         "       addu    %0, %1          \n"
240         "       sltu    $1, %0, %1      \n"
241
242         "       lw      %1, 4(%3)       \n"
243         "       addu    %0, $1          \n"
244         "       addu    %0, %1          \n"
245         "       sltu    $1, %0, %1      \n"
246
247         "       lw      %1, 8(%3)       \n"
248         "       addu    %0, $1          \n"
249         "       addu    %0, %1          \n"
250         "       sltu    $1, %0, %1      \n"
251
252         "       lw      %1, 12(%3)      \n"
253         "       addu    %0, $1          \n"
254         "       addu    %0, %1          \n"
255         "       sltu    $1, %0, %1      \n"
256
257         "       addu    %0, $1          # Add final carry\n"
258         "       .set    pop"
259         : "=r" (sum), "=r" (proto)
260         : "r" (saddr), "r" (daddr),
261           "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
262
263         return csum_fold(sum);
264 }
265
266 #endif /* _ASM_CHECKSUM_H */