// SPDX-License-Identifier: GPL-2.01/*2* Normal 64-bit CRC calculation.3*4* This is a basic crc64 implementation following ECMA-182 specification,5* which can be found from,6* https://www.ecma-international.org/publications/standards/Ecma-182.htm7*8* Dr. Ross N. Williams has a great document to introduce the idea of CRC9* algorithm, here the CRC64 code is also inspired by the table-driven10* algorithm and detail example from this paper. This paper can be found11* from,12* http://www.ross.net/crc/download/crc_v3.txt13*14* crc64table[256] is the lookup table of a table-driven 64-bit CRC15* calculation, which is generated by gen_crc64table.c in kernel build16* time. The polynomial of crc64 arithmetic is from ECMA-182 specification17* as well, which is defined as,18*19* x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +20* x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +21* x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +22* x^7 + x^4 + x + 123*24* crc64nvmetable[256] uses the CRC64 polynomial from the NVME NVM Command Set25* Specification and uses least-significant-bit first bit order:26*27* x^64 + x^63 + x^61 + x^59 + x^58 + x^56 + x^55 + x^52 + x^49 + x^48 + x^47 +28* x^46 + x^44 + x^41 + x^37 + x^36 + x^34 + x^32 + x^31 + x^28 + x^26 + x^23 +29* x^22 + x^19 + x^16 + x^13 + x^12 + x^10 + x^9 + x^6 + x^4 + x^3 + 130*31* Copyright 2018 SUSE Linux.32* Author: Coly Li <[email protected]>33*/3435#include <linux/crc64.h>36#include <linux/export.h>37#include <linux/module.h>38#include <linux/types.h>3940#include "crc64table.h"4142static inline u64 __maybe_unused43crc64_be_generic(u64 crc, const u8 *p, size_t len)44{45while (len--)46crc = (crc << 8) ^ crc64table[(crc >> 56) ^ *p++];47return crc;48}4950static inline u64 __maybe_unused51crc64_nvme_generic(u64 crc, const u8 *p, size_t len)52{53while (len--)54crc = (crc >> 8) ^ crc64nvmetable[(crc & 0xff) ^ *p++];55return crc;56}5758#ifdef CONFIG_CRC64_ARCH59#include "crc64.h" /* $(SRCARCH)/crc64.h */60#else61#define crc64_be_arch crc64_be_generic62#define crc64_nvme_arch crc64_nvme_generic63#endif6465u64 crc64_be(u64 crc, const void *p, size_t len)66{67return crc64_be_arch(crc, p, len);68}69EXPORT_SYMBOL_GPL(crc64_be);7071u64 crc64_nvme(u64 crc, const void *p, size_t len)72{73return ~crc64_nvme_arch(~crc, p, len);74}75EXPORT_SYMBOL_GPL(crc64_nvme);7677#ifdef crc64_mod_init_arch78static int __init crc64_mod_init(void)79{80crc64_mod_init_arch();81return 0;82}83subsys_initcall(crc64_mod_init);8485static void __exit crc64_mod_exit(void)86{87}88module_exit(crc64_mod_exit);89#endif9091MODULE_DESCRIPTION("CRC64 library functions");92MODULE_LICENSE("GPL");939495