1 // SPDX-License-Identifier: GPL-2.0
3 * arch/x86_64/lib/csum-partial.c
5 * This file contains network checksum routines that are better done
6 * in an architecture-specific manner due to speed.
9 #include <linux/compiler.h>
10 #include <linux/export.h>
11 #include <asm/checksum.h>
12 #include <asm/word-at-a-time.h>
14 static inline unsigned short from32to16(unsigned a)
16 unsigned short b = a >> 16;
17 asm("addw %w2,%w0\n\t"
25 * Do a checksum on an arbitrary memory area.
26 * Returns a 32bit checksum.
28 * This isn't as time critical as it used to be because many NICs
29 * do hardware checksumming these days.
31 * Still, with CHECKSUM_COMPLETE this is called to compute
32 * checksums on IPv6 headers (40 bytes) and other small parts.
33 * it's best to have buff aligned on a 64-bit boundary
35 __wsum csum_partial(const void *buff, int len, __wsum sum)
37 u64 temp64 = (__force u64)sum;
40 odd = 1 & (unsigned long) buff;
42 if (unlikely(len == 0))
44 temp64 = ror32((__force u32)sum, 8);
45 temp64 += (*(unsigned char *)buff << 8);
50 while (unlikely(len >= 64)) {
51 asm("addq 0*8(%[src]),%[res]\n\t"
52 "adcq 1*8(%[src]),%[res]\n\t"
53 "adcq 2*8(%[src]),%[res]\n\t"
54 "adcq 3*8(%[src]),%[res]\n\t"
55 "adcq 4*8(%[src]),%[res]\n\t"
56 "adcq 5*8(%[src]),%[res]\n\t"
57 "adcq 6*8(%[src]),%[res]\n\t"
58 "adcq 7*8(%[src]),%[res]\n\t"
68 asm("addq 0*8(%[src]),%[res]\n\t"
69 "adcq 1*8(%[src]),%[res]\n\t"
70 "adcq 2*8(%[src]),%[res]\n\t"
71 "adcq 3*8(%[src]),%[res]\n\t"
79 asm("addq 0*8(%[src]),%[res]\n\t"
80 "adcq 1*8(%[src]),%[res]\n\t"
88 asm("addq 0*8(%[src]),%[res]\n\t"
96 unsigned int shift = (8 - (len & 7)) * 8;
99 trail = (load_unaligned_zeropad(buff) << shift) >> shift;
101 asm("addq %[trail],%[res]\n\t"
103 : [res] "+r" (temp64)
104 : [trail] "r" (trail));
106 result = add32_with_carry(temp64 >> 32, temp64 & 0xffffffff);
108 result = from32to16(result);
109 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
111 return (__force __wsum)result;
113 EXPORT_SYMBOL(csum_partial);
116 * this routine is used for miscellaneous IP-like checksums, mainly
119 __sum16 ip_compute_csum(const void *buff, int len)
121 return csum_fold(csum_partial(buff,len,0));
123 EXPORT_SYMBOL(ip_compute_csum);