1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * MIPS specific IP/TCP/UDP checksumming routines
10 * Lots of code moved from tcp.c and ip.c; see those files
13 #include <linux/module.h>
14 #include <linux/types.h>
16 #include <net/checksum.h>
17 #include <asm/byteorder.h>
18 #include <asm/string.h>
19 #include <linux/uaccess.h>
22 __asm__ __volatile__ ( \
24 " addc %0, %%r0, %0\n" \
28 static inline unsigned short from32to16(unsigned int x)
30 /* 32 bits --> 16 bits + carry */
31 x = (x & 0xffff) + (x >> 16);
32 /* 16 bits + carry --> 16 bits including carry */
33 x = (x & 0xffff) + (x >> 16);
34 return (unsigned short)x;
37 static inline unsigned int do_csum(const unsigned char * buff, int len)
40 unsigned int result = 0;
44 odd = 1 & (unsigned long) buff;
46 result = be16_to_cpu(*buff);
50 count = len >> 1; /* nr of 16-bit words.. */
52 if (2 & (unsigned long) buff) {
53 result += *(unsigned short *) buff;
58 count >>= 1; /* nr of 32-bit words.. */
61 unsigned int r1, r2, r3, r4;
62 r1 = *(unsigned int *)(buff + 0);
63 r2 = *(unsigned int *)(buff + 4);
64 r3 = *(unsigned int *)(buff + 8);
65 r4 = *(unsigned int *)(buff + 12);
74 unsigned int w = *(unsigned int *) buff;
79 result = (result & 0xffff) + (result >> 16);
82 result += *(unsigned short *) buff;
87 result += le16_to_cpu(*buff);
88 result = from32to16(result);
90 result = swab16(result);
96 * computes a partial checksum, e.g. for TCP/UDP fragments
101 __wsum csum_partial(const void *buff, int len, __wsum sum)
103 unsigned int result = do_csum(buff, len);
105 return (__force __wsum)from32to16(result);
108 EXPORT_SYMBOL(csum_partial);