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 __wsum csum_finalize_sum(u64 temp64)
16 return (__force __wsum)((temp64 + ror64(temp64, 32)) >> 32);
19 static inline unsigned long update_csum_40b(unsigned long sum, const unsigned long m[5])
28 :"m" (m[0]), "m" (m[1]), "m" (m[2]),
29 "m" (m[3]), "m" (m[4]));
34 * Do a checksum on an arbitrary memory area.
35 * Returns a 32bit checksum.
37 * This isn't as time critical as it used to be because many NICs
38 * do hardware checksumming these days.
40 * Still, with CHECKSUM_COMPLETE this is called to compute
41 * checksums on IPv6 headers (40 bytes) and other small parts.
42 * it's best to have buff aligned on a 64-bit boundary
44 __wsum csum_partial(const void *buff, int len, __wsum sum)
46 u64 temp64 = (__force u64)sum;
48 /* Do two 40-byte chunks in parallel to get better ILP */
49 if (likely(len >= 80)) {
52 temp64 = update_csum_40b(temp64, buff);
53 temp64_2 = update_csum_40b(temp64_2, buff + 40);
60 :"+r" (temp64): "r" (temp64_2));
64 * len == 40 is the hot case due to IPv6 headers, so return
65 * early for that exact case without checking the tail bytes.
68 temp64 = update_csum_40b(temp64, buff);
71 return csum_finalize_sum(temp64);
76 asm("addq 0*8(%[src]),%[res]\n\t"
77 "adcq 1*8(%[src]),%[res]\n\t"
78 "adcq 2*8(%[src]),%[res]\n\t"
79 "adcq 3*8(%[src]),%[res]\n\t"
82 : [src] "r"(buff), "m"(*(const char(*)[32])buff));
86 asm("addq 0*8(%[src]),%[res]\n\t"
87 "adcq 1*8(%[src]),%[res]\n\t"
90 : [src] "r"(buff), "m"(*(const char(*)[16])buff));
94 asm("addq 0*8(%[src]),%[res]\n\t"
97 : [src] "r"(buff), "m"(*(const char(*)[8])buff));
101 unsigned int shift = (-len << 3) & 63;
104 trail = (load_unaligned_zeropad(buff) << shift) >> shift;
106 asm("addq %[trail],%[res]\n\t"
109 : [trail] "r"(trail));
111 return csum_finalize_sum(temp64);
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);