10bcfca56SEric Biggers // SPDX-License-Identifier: GPL-2.0 20bcfca56SEric Biggers /* 30bcfca56SEric Biggers * Normal 64-bit CRC calculation. 40bcfca56SEric Biggers * 50bcfca56SEric Biggers * This is a basic crc64 implementation following ECMA-182 specification, 60bcfca56SEric Biggers * which can be found from, 70bcfca56SEric Biggers * https://www.ecma-international.org/publications/standards/Ecma-182.htm 80bcfca56SEric Biggers * 90bcfca56SEric Biggers * Dr. Ross N. Williams has a great document to introduce the idea of CRC 100bcfca56SEric Biggers * algorithm, here the CRC64 code is also inspired by the table-driven 110bcfca56SEric Biggers * algorithm and detail example from this paper. This paper can be found 120bcfca56SEric Biggers * from, 130bcfca56SEric Biggers * http://www.ross.net/crc/download/crc_v3.txt 140bcfca56SEric Biggers * 150bcfca56SEric Biggers * crc64table[256] is the lookup table of a table-driven 64-bit CRC 160bcfca56SEric Biggers * calculation, which is generated by gen_crc64table.c in kernel build 170bcfca56SEric Biggers * time. The polynomial of crc64 arithmetic is from ECMA-182 specification 180bcfca56SEric Biggers * as well, which is defined as, 190bcfca56SEric Biggers * 200bcfca56SEric Biggers * x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 + 210bcfca56SEric Biggers * x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 + 220bcfca56SEric Biggers * x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 + 230bcfca56SEric Biggers * x^7 + x^4 + x + 1 240bcfca56SEric Biggers * 250bcfca56SEric Biggers * crc64nvmetable[256] uses the CRC64 polynomial from the NVME NVM Command Set 260bcfca56SEric Biggers * Specification and uses least-significant-bit first bit order: 270bcfca56SEric Biggers * 280bcfca56SEric Biggers * x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 + 290bcfca56SEric Biggers * x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 + 300bcfca56SEric Biggers * x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 1 310bcfca56SEric Biggers * 320bcfca56SEric Biggers * Copyright 2018 SUSE Linux. 330bcfca56SEric Biggers * Author: Coly Li <colyli@suse.de> 340bcfca56SEric Biggers */ 350bcfca56SEric Biggers 36*1a822ea5SEric Biggers #include <linux/crc64.h> 37*1a822ea5SEric Biggers #include <linux/export.h> 380bcfca56SEric Biggers #include <linux/module.h> 390bcfca56SEric Biggers #include <linux/types.h> 40*1a822ea5SEric Biggers 410bcfca56SEric Biggers #include "crc64table.h" 420bcfca56SEric Biggers 430bcfca56SEric Biggers static inline u64 __maybe_unused 440bcfca56SEric Biggers crc64_be_generic(u64 crc, const u8 *p, size_t len) 450bcfca56SEric Biggers { 460bcfca56SEric Biggers while (len--) 470bcfca56SEric Biggers crc = (crc << 8) ^ crc64table[(crc >> 56) ^ *p++]; 480bcfca56SEric Biggers return crc; 490bcfca56SEric Biggers } 500bcfca56SEric Biggers 510bcfca56SEric Biggers static inline u64 __maybe_unused 520bcfca56SEric Biggers crc64_nvme_generic(u64 crc, const u8 *p, size_t len) 530bcfca56SEric Biggers { 540bcfca56SEric Biggers while (len--) 550bcfca56SEric Biggers crc = (crc >> 8) ^ crc64nvmetable[(crc & 0xff) ^ *p++]; 560bcfca56SEric Biggers return crc; 570bcfca56SEric Biggers } 580bcfca56SEric Biggers 590bcfca56SEric Biggers #ifdef CONFIG_CRC64_ARCH 600bcfca56SEric Biggers #include "crc64.h" /* $(SRCARCH)/crc64.h */ 610bcfca56SEric Biggers #else 620bcfca56SEric Biggers #define crc64_be_arch crc64_be_generic 630bcfca56SEric Biggers #define crc64_nvme_arch crc64_nvme_generic 640bcfca56SEric Biggers #endif 650bcfca56SEric Biggers 660bcfca56SEric Biggers u64 crc64_be(u64 crc, const void *p, size_t len) 670bcfca56SEric Biggers { 680bcfca56SEric Biggers return crc64_be_arch(crc, p, len); 690bcfca56SEric Biggers } 700bcfca56SEric Biggers EXPORT_SYMBOL_GPL(crc64_be); 710bcfca56SEric Biggers 720bcfca56SEric Biggers u64 crc64_nvme(u64 crc, const void *p, size_t len) 730bcfca56SEric Biggers { 740bcfca56SEric Biggers return ~crc64_nvme_arch(~crc, p, len); 750bcfca56SEric Biggers } 760bcfca56SEric Biggers EXPORT_SYMBOL_GPL(crc64_nvme); 770bcfca56SEric Biggers 780bcfca56SEric Biggers #ifdef crc64_mod_init_arch 790bcfca56SEric Biggers static int __init crc64_mod_init(void) 800bcfca56SEric Biggers { 810bcfca56SEric Biggers crc64_mod_init_arch(); 820bcfca56SEric Biggers return 0; 830bcfca56SEric Biggers } 840bcfca56SEric Biggers subsys_initcall(crc64_mod_init); 850bcfca56SEric Biggers 860bcfca56SEric Biggers static void __exit crc64_mod_exit(void) 870bcfca56SEric Biggers { 880bcfca56SEric Biggers } 890bcfca56SEric Biggers module_exit(crc64_mod_exit); 900bcfca56SEric Biggers #endif 910bcfca56SEric Biggers 920bcfca56SEric Biggers MODULE_DESCRIPTION("CRC64 library functions"); 930bcfca56SEric Biggers MODULE_LICENSE("GPL"); 94