xref: /linux/lib/crc/crc64-main.c (revision 0bcfca56406dc6342e30fafe41a2f34cdde029b4)
1*0bcfca56SEric Biggers // SPDX-License-Identifier: GPL-2.0
2*0bcfca56SEric Biggers /*
3*0bcfca56SEric Biggers  * Normal 64-bit CRC calculation.
4*0bcfca56SEric Biggers  *
5*0bcfca56SEric Biggers  * This is a basic crc64 implementation following ECMA-182 specification,
6*0bcfca56SEric Biggers  * which can be found from,
7*0bcfca56SEric Biggers  * https://www.ecma-international.org/publications/standards/Ecma-182.htm
8*0bcfca56SEric Biggers  *
9*0bcfca56SEric Biggers  * Dr. Ross N. Williams has a great document to introduce the idea of CRC
10*0bcfca56SEric Biggers  * algorithm, here the CRC64 code is also inspired by the table-driven
11*0bcfca56SEric Biggers  * algorithm and detail example from this paper. This paper can be found
12*0bcfca56SEric Biggers  * from,
13*0bcfca56SEric Biggers  * http://www.ross.net/crc/download/crc_v3.txt
14*0bcfca56SEric Biggers  *
15*0bcfca56SEric Biggers  * crc64table[256] is the lookup table of a table-driven 64-bit CRC
16*0bcfca56SEric Biggers  * calculation, which is generated by gen_crc64table.c in kernel build
17*0bcfca56SEric Biggers  * time. The polynomial of crc64 arithmetic is from ECMA-182 specification
18*0bcfca56SEric Biggers  * as well, which is defined as,
19*0bcfca56SEric Biggers  *
20*0bcfca56SEric Biggers  * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
21*0bcfca56SEric Biggers  * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
22*0bcfca56SEric Biggers  * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
23*0bcfca56SEric Biggers  * x^7 + x^4 + x + 1
24*0bcfca56SEric Biggers  *
25*0bcfca56SEric Biggers  * crc64nvmetable[256] uses the CRC64 polynomial from the NVME NVM Command Set
26*0bcfca56SEric Biggers  * Specification and uses least-significant-bit first bit order:
27*0bcfca56SEric Biggers  *
28*0bcfca56SEric Biggers  * x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 +
29*0bcfca56SEric Biggers  * x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 +
30*0bcfca56SEric Biggers  * x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1
31*0bcfca56SEric Biggers  *
32*0bcfca56SEric Biggers  * Copyright 2018 SUSE Linux.
33*0bcfca56SEric Biggers  *   Author: Coly Li <colyli@suse.de>
34*0bcfca56SEric Biggers  */
35*0bcfca56SEric Biggers 
36*0bcfca56SEric Biggers #include <linux/module.h>
37*0bcfca56SEric Biggers #include <linux/types.h>
38*0bcfca56SEric Biggers #include <linux/crc64.h>
39*0bcfca56SEric Biggers #include "crc64table.h"
40*0bcfca56SEric Biggers 
41*0bcfca56SEric Biggers static inline u64 __maybe_unused
42*0bcfca56SEric Biggers crc64_be_generic(u64 crc, const u8 *p, size_t len)
43*0bcfca56SEric Biggers {
44*0bcfca56SEric Biggers 	while (len--)
45*0bcfca56SEric Biggers 		crc = (crc << 8) ^ crc64table[(crc >> 56) ^ *p++];
46*0bcfca56SEric Biggers 	return crc;
47*0bcfca56SEric Biggers }
48*0bcfca56SEric Biggers 
49*0bcfca56SEric Biggers static inline u64 __maybe_unused
50*0bcfca56SEric Biggers crc64_nvme_generic(u64 crc, const u8 *p, size_t len)
51*0bcfca56SEric Biggers {
52*0bcfca56SEric Biggers 	while (len--)
53*0bcfca56SEric Biggers 		crc = (crc >> 8) ^ crc64nvmetable[(crc & 0xff) ^ *p++];
54*0bcfca56SEric Biggers 	return crc;
55*0bcfca56SEric Biggers }
56*0bcfca56SEric Biggers 
57*0bcfca56SEric Biggers #ifdef CONFIG_CRC64_ARCH
58*0bcfca56SEric Biggers #include "crc64.h" /* $(SRCARCH)/crc64.h */
59*0bcfca56SEric Biggers #else
60*0bcfca56SEric Biggers #define crc64_be_arch crc64_be_generic
61*0bcfca56SEric Biggers #define crc64_nvme_arch crc64_nvme_generic
62*0bcfca56SEric Biggers #endif
63*0bcfca56SEric Biggers 
64*0bcfca56SEric Biggers u64 crc64_be(u64 crc, const void *p, size_t len)
65*0bcfca56SEric Biggers {
66*0bcfca56SEric Biggers 	return crc64_be_arch(crc, p, len);
67*0bcfca56SEric Biggers }
68*0bcfca56SEric Biggers EXPORT_SYMBOL_GPL(crc64_be);
69*0bcfca56SEric Biggers 
70*0bcfca56SEric Biggers u64 crc64_nvme(u64 crc, const void *p, size_t len)
71*0bcfca56SEric Biggers {
72*0bcfca56SEric Biggers 	return ~crc64_nvme_arch(~crc, p, len);
73*0bcfca56SEric Biggers }
74*0bcfca56SEric Biggers EXPORT_SYMBOL_GPL(crc64_nvme);
75*0bcfca56SEric Biggers 
76*0bcfca56SEric Biggers #ifdef crc64_mod_init_arch
77*0bcfca56SEric Biggers static int __init crc64_mod_init(void)
78*0bcfca56SEric Biggers {
79*0bcfca56SEric Biggers 	crc64_mod_init_arch();
80*0bcfca56SEric Biggers 	return 0;
81*0bcfca56SEric Biggers }
82*0bcfca56SEric Biggers subsys_initcall(crc64_mod_init);
83*0bcfca56SEric Biggers 
84*0bcfca56SEric Biggers static void __exit crc64_mod_exit(void)
85*0bcfca56SEric Biggers {
86*0bcfca56SEric Biggers }
87*0bcfca56SEric Biggers module_exit(crc64_mod_exit);
88*0bcfca56SEric Biggers #endif
89*0bcfca56SEric Biggers 
90*0bcfca56SEric Biggers MODULE_DESCRIPTION("CRC64 library functions");
91*0bcfca56SEric Biggers MODULE_LICENSE("GPL");
92