1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2016 ARM Ltd. 4 * Copyright (C) 2023 Loongson Technology Corporation Limited 5 */ 6 #ifndef __ASM_CHECKSUM_H 7 #define __ASM_CHECKSUM_H 8 9 #include <linux/bitops.h> 10 #include <linux/in6.h> 11 12 #ifdef CONFIG_64BIT 13 14 #define _HAVE_ARCH_IPV6_CSUM 15 __sum16 csum_ipv6_magic(const struct in6_addr *saddr, 16 const struct in6_addr *daddr, 17 __u32 len, __u8 proto, __wsum sum); 18 19 /* 20 * turns a 32-bit partial checksum (e.g. from csum_partial) into a 21 * 1's complement 16-bit checksum. 22 */ 23 static inline __sum16 csum_fold(__wsum sum) 24 { 25 u32 tmp = (__force u32)sum; 26 27 /* 28 * swap the two 16-bit halves of sum 29 * if there is a carry from adding the two 16-bit halves, 30 * it will carry from the lower half into the upper half, 31 * giving us the correct sum in the upper half. 32 */ 33 return (__force __sum16)(~(tmp + rol32(tmp, 16)) >> 16); 34 } 35 #define csum_fold csum_fold 36 37 /* 38 * This is a version of ip_compute_csum() optimized for IP headers, 39 * which always checksum on 4 octet boundaries. ihl is the number 40 * of 32-bit words and is always >= 5. 41 */ 42 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) 43 { 44 u64 sum; 45 __uint128_t tmp; 46 int n = ihl; /* we want it signed */ 47 48 tmp = *(const __uint128_t *)iph; 49 iph += 16; 50 n -= 4; 51 tmp += ((tmp >> 64) | (tmp << 64)); 52 sum = tmp >> 64; 53 do { 54 sum += *(const u32 *)iph; 55 iph += 4; 56 } while (--n > 0); 57 58 sum += ror64(sum, 32); 59 return csum_fold((__force __wsum)(sum >> 32)); 60 } 61 #define ip_fast_csum ip_fast_csum 62 63 extern unsigned int do_csum(const unsigned char *buff, int len); 64 #define do_csum do_csum 65 66 #endif 67 68 #include <asm-generic/checksum.h> 69 70 #endif /* __ASM_CHECKSUM_H */ 71