1d81734caSHemant Agrawal /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
26d6b4f49SShreyansh Jain *
36d6b4f49SShreyansh Jain * Copyright 2011 Freescale Semiconductor, Inc.
46d6b4f49SShreyansh Jain *
56d6b4f49SShreyansh Jain */
66d6b4f49SShreyansh Jain
76d6b4f49SShreyansh Jain #ifndef __FSL_FMAN_CRC64_H
86d6b4f49SShreyansh Jain #define __FSL_FMAN_CRC64_H
96d6b4f49SShreyansh Jain
106d6b4f49SShreyansh Jain #ifdef __cplusplus
116d6b4f49SShreyansh Jain extern "C" {
126d6b4f49SShreyansh Jain #endif
136d6b4f49SShreyansh Jain
146d6b4f49SShreyansh Jain /*
156d6b4f49SShreyansh Jain * This following definitions provide a software implementation of the CRC64
166d6b4f49SShreyansh Jain * algorithm implemented within Fman.
176d6b4f49SShreyansh Jain *
186d6b4f49SShreyansh Jain * The following example shows how to compute a CRC64 hash value based on
196d6b4f49SShreyansh Jain * SRC_IP, DST_IP and ESP_SPI values
206d6b4f49SShreyansh Jain *
216d6b4f49SShreyansh Jain * #define compute_hash(saddr,daddr,spi) \
226d6b4f49SShreyansh Jain * do { \
236d6b4f49SShreyansh Jain * uint64_t result; \
246d6b4f49SShreyansh Jain * result = fman_crc64_init(); \
256d6b4f49SShreyansh Jain * result = fman_crc64_compute_32bit(saddr, result); \
266d6b4f49SShreyansh Jain * result = fman_crc64_compute_32bit(daddr, result); \
276d6b4f49SShreyansh Jain * result = fman_crc64_compute_32bit(spi, result); \
286d6b4f49SShreyansh Jain * return (uint32_t) result & RC_HASH_MASK; \
296d6b4f49SShreyansh Jain * } while (0);
306d6b4f49SShreyansh Jain *
316d6b4f49SShreyansh Jain * If hashing over a different number of fields (or of different types) is
326d6b4f49SShreyansh Jain * required, this can be implemented using the following primitives.
336d6b4f49SShreyansh Jain */
346d6b4f49SShreyansh Jain
356d6b4f49SShreyansh Jain /* The following table provides the constants used by the Fman CRC64
366d6b4f49SShreyansh Jain * implementation. The table is instantiated within the DPAA fman driver.
376d6b4f49SShreyansh Jain * However if the application is not going to be linked against the DPAA fman
386d6b4f49SShreyansh Jain * driver but will use this Fman CRC64 implementation, then it will need to
396d6b4f49SShreyansh Jain * instantiate this table by using the DECLARE_FMAN_CRC64_TABLE() macro.
406d6b4f49SShreyansh Jain */
416d6b4f49SShreyansh Jain struct fman_crc64_t {
426d6b4f49SShreyansh Jain uint64_t initial;
436d6b4f49SShreyansh Jain uint64_t table[1 << 8];
446d6b4f49SShreyansh Jain };
45*520dd992SFerruh Yigit extern struct fman_crc64_t fman_crc64_ecma_182;
466d6b4f49SShreyansh Jain #define DECLARE_FMAN_CRC64_TABLE() \
47*520dd992SFerruh Yigit struct fman_crc64_t fman_crc64_ecma_182 = { \
486d6b4f49SShreyansh Jain 0xFFFFFFFFFFFFFFFFULL, \
496d6b4f49SShreyansh Jain { \
506d6b4f49SShreyansh Jain 0x0000000000000000ULL, 0xb32e4cbe03a75f6fULL, \
516d6b4f49SShreyansh Jain 0xf4843657a840a05bULL, 0x47aa7ae9abe7ff34ULL, \
526d6b4f49SShreyansh Jain 0x7bd0c384ff8f5e33ULL, 0xc8fe8f3afc28015cULL, \
536d6b4f49SShreyansh Jain 0x8f54f5d357cffe68ULL, 0x3c7ab96d5468a107ULL, \
546d6b4f49SShreyansh Jain 0xf7a18709ff1ebc66ULL, 0x448fcbb7fcb9e309ULL, \
556d6b4f49SShreyansh Jain 0x0325b15e575e1c3dULL, 0xb00bfde054f94352ULL, \
566d6b4f49SShreyansh Jain 0x8c71448d0091e255ULL, 0x3f5f08330336bd3aULL, \
576d6b4f49SShreyansh Jain 0x78f572daa8d1420eULL, 0xcbdb3e64ab761d61ULL, \
586d6b4f49SShreyansh Jain 0x7d9ba13851336649ULL, 0xceb5ed8652943926ULL, \
596d6b4f49SShreyansh Jain 0x891f976ff973c612ULL, 0x3a31dbd1fad4997dULL, \
606d6b4f49SShreyansh Jain 0x064b62bcaebc387aULL, 0xb5652e02ad1b6715ULL, \
616d6b4f49SShreyansh Jain 0xf2cf54eb06fc9821ULL, 0x41e11855055bc74eULL, \
626d6b4f49SShreyansh Jain 0x8a3a2631ae2dda2fULL, 0x39146a8fad8a8540ULL, \
636d6b4f49SShreyansh Jain 0x7ebe1066066d7a74ULL, 0xcd905cd805ca251bULL, \
646d6b4f49SShreyansh Jain 0xf1eae5b551a2841cULL, 0x42c4a90b5205db73ULL, \
656d6b4f49SShreyansh Jain 0x056ed3e2f9e22447ULL, 0xb6409f5cfa457b28ULL, \
666d6b4f49SShreyansh Jain 0xfb374270a266cc92ULL, 0x48190ecea1c193fdULL, \
676d6b4f49SShreyansh Jain 0x0fb374270a266cc9ULL, 0xbc9d3899098133a6ULL, \
686d6b4f49SShreyansh Jain 0x80e781f45de992a1ULL, 0x33c9cd4a5e4ecdceULL, \
696d6b4f49SShreyansh Jain 0x7463b7a3f5a932faULL, 0xc74dfb1df60e6d95ULL, \
706d6b4f49SShreyansh Jain 0x0c96c5795d7870f4ULL, 0xbfb889c75edf2f9bULL, \
716d6b4f49SShreyansh Jain 0xf812f32ef538d0afULL, 0x4b3cbf90f69f8fc0ULL, \
726d6b4f49SShreyansh Jain 0x774606fda2f72ec7ULL, 0xc4684a43a15071a8ULL, \
736d6b4f49SShreyansh Jain 0x83c230aa0ab78e9cULL, 0x30ec7c140910d1f3ULL, \
746d6b4f49SShreyansh Jain 0x86ace348f355aadbULL, 0x3582aff6f0f2f5b4ULL, \
756d6b4f49SShreyansh Jain 0x7228d51f5b150a80ULL, 0xc10699a158b255efULL, \
766d6b4f49SShreyansh Jain 0xfd7c20cc0cdaf4e8ULL, 0x4e526c720f7dab87ULL, \
776d6b4f49SShreyansh Jain 0x09f8169ba49a54b3ULL, 0xbad65a25a73d0bdcULL, \
786d6b4f49SShreyansh Jain 0x710d64410c4b16bdULL, 0xc22328ff0fec49d2ULL, \
796d6b4f49SShreyansh Jain 0x85895216a40bb6e6ULL, 0x36a71ea8a7ace989ULL, \
806d6b4f49SShreyansh Jain 0x0adda7c5f3c4488eULL, 0xb9f3eb7bf06317e1ULL, \
816d6b4f49SShreyansh Jain 0xfe5991925b84e8d5ULL, 0x4d77dd2c5823b7baULL, \
826d6b4f49SShreyansh Jain 0x64b62bcaebc387a1ULL, 0xd7986774e864d8ceULL, \
836d6b4f49SShreyansh Jain 0x90321d9d438327faULL, 0x231c512340247895ULL, \
846d6b4f49SShreyansh Jain 0x1f66e84e144cd992ULL, 0xac48a4f017eb86fdULL, \
856d6b4f49SShreyansh Jain 0xebe2de19bc0c79c9ULL, 0x58cc92a7bfab26a6ULL, \
866d6b4f49SShreyansh Jain 0x9317acc314dd3bc7ULL, 0x2039e07d177a64a8ULL, \
876d6b4f49SShreyansh Jain 0x67939a94bc9d9b9cULL, 0xd4bdd62abf3ac4f3ULL, \
886d6b4f49SShreyansh Jain 0xe8c76f47eb5265f4ULL, 0x5be923f9e8f53a9bULL, \
896d6b4f49SShreyansh Jain 0x1c4359104312c5afULL, 0xaf6d15ae40b59ac0ULL, \
906d6b4f49SShreyansh Jain 0x192d8af2baf0e1e8ULL, 0xaa03c64cb957be87ULL, \
916d6b4f49SShreyansh Jain 0xeda9bca512b041b3ULL, 0x5e87f01b11171edcULL, \
926d6b4f49SShreyansh Jain 0x62fd4976457fbfdbULL, 0xd1d305c846d8e0b4ULL, \
936d6b4f49SShreyansh Jain 0x96797f21ed3f1f80ULL, 0x2557339fee9840efULL, \
946d6b4f49SShreyansh Jain 0xee8c0dfb45ee5d8eULL, 0x5da24145464902e1ULL, \
956d6b4f49SShreyansh Jain 0x1a083bacedaefdd5ULL, 0xa9267712ee09a2baULL, \
966d6b4f49SShreyansh Jain 0x955cce7fba6103bdULL, 0x267282c1b9c65cd2ULL, \
976d6b4f49SShreyansh Jain 0x61d8f8281221a3e6ULL, 0xd2f6b4961186fc89ULL, \
986d6b4f49SShreyansh Jain 0x9f8169ba49a54b33ULL, 0x2caf25044a02145cULL, \
996d6b4f49SShreyansh Jain 0x6b055fede1e5eb68ULL, 0xd82b1353e242b407ULL, \
1006d6b4f49SShreyansh Jain 0xe451aa3eb62a1500ULL, 0x577fe680b58d4a6fULL, \
1016d6b4f49SShreyansh Jain 0x10d59c691e6ab55bULL, 0xa3fbd0d71dcdea34ULL, \
1026d6b4f49SShreyansh Jain 0x6820eeb3b6bbf755ULL, 0xdb0ea20db51ca83aULL, \
1036d6b4f49SShreyansh Jain 0x9ca4d8e41efb570eULL, 0x2f8a945a1d5c0861ULL, \
1046d6b4f49SShreyansh Jain 0x13f02d374934a966ULL, 0xa0de61894a93f609ULL, \
1056d6b4f49SShreyansh Jain 0xe7741b60e174093dULL, 0x545a57dee2d35652ULL, \
1066d6b4f49SShreyansh Jain 0xe21ac88218962d7aULL, 0x5134843c1b317215ULL, \
1076d6b4f49SShreyansh Jain 0x169efed5b0d68d21ULL, 0xa5b0b26bb371d24eULL, \
1086d6b4f49SShreyansh Jain 0x99ca0b06e7197349ULL, 0x2ae447b8e4be2c26ULL, \
1096d6b4f49SShreyansh Jain 0x6d4e3d514f59d312ULL, 0xde6071ef4cfe8c7dULL, \
1106d6b4f49SShreyansh Jain 0x15bb4f8be788911cULL, 0xa6950335e42fce73ULL, \
1116d6b4f49SShreyansh Jain 0xe13f79dc4fc83147ULL, 0x521135624c6f6e28ULL, \
1126d6b4f49SShreyansh Jain 0x6e6b8c0f1807cf2fULL, 0xdd45c0b11ba09040ULL, \
1136d6b4f49SShreyansh Jain 0x9aefba58b0476f74ULL, 0x29c1f6e6b3e0301bULL, \
1146d6b4f49SShreyansh Jain 0xc96c5795d7870f42ULL, 0x7a421b2bd420502dULL, \
1156d6b4f49SShreyansh Jain 0x3de861c27fc7af19ULL, 0x8ec62d7c7c60f076ULL, \
1166d6b4f49SShreyansh Jain 0xb2bc941128085171ULL, 0x0192d8af2baf0e1eULL, \
1176d6b4f49SShreyansh Jain 0x4638a2468048f12aULL, 0xf516eef883efae45ULL, \
1186d6b4f49SShreyansh Jain 0x3ecdd09c2899b324ULL, 0x8de39c222b3eec4bULL, \
1196d6b4f49SShreyansh Jain 0xca49e6cb80d9137fULL, 0x7967aa75837e4c10ULL, \
1206d6b4f49SShreyansh Jain 0x451d1318d716ed17ULL, 0xf6335fa6d4b1b278ULL, \
1216d6b4f49SShreyansh Jain 0xb199254f7f564d4cULL, 0x02b769f17cf11223ULL, \
1226d6b4f49SShreyansh Jain 0xb4f7f6ad86b4690bULL, 0x07d9ba1385133664ULL, \
1236d6b4f49SShreyansh Jain 0x4073c0fa2ef4c950ULL, 0xf35d8c442d53963fULL, \
1246d6b4f49SShreyansh Jain 0xcf273529793b3738ULL, 0x7c0979977a9c6857ULL, \
1256d6b4f49SShreyansh Jain 0x3ba3037ed17b9763ULL, 0x888d4fc0d2dcc80cULL, \
1266d6b4f49SShreyansh Jain 0x435671a479aad56dULL, 0xf0783d1a7a0d8a02ULL, \
1276d6b4f49SShreyansh Jain 0xb7d247f3d1ea7536ULL, 0x04fc0b4dd24d2a59ULL, \
1286d6b4f49SShreyansh Jain 0x3886b22086258b5eULL, 0x8ba8fe9e8582d431ULL, \
1296d6b4f49SShreyansh Jain 0xcc0284772e652b05ULL, 0x7f2cc8c92dc2746aULL, \
1306d6b4f49SShreyansh Jain 0x325b15e575e1c3d0ULL, 0x8175595b76469cbfULL, \
1316d6b4f49SShreyansh Jain 0xc6df23b2dda1638bULL, 0x75f16f0cde063ce4ULL, \
1326d6b4f49SShreyansh Jain 0x498bd6618a6e9de3ULL, 0xfaa59adf89c9c28cULL, \
1336d6b4f49SShreyansh Jain 0xbd0fe036222e3db8ULL, 0x0e21ac88218962d7ULL, \
1346d6b4f49SShreyansh Jain 0xc5fa92ec8aff7fb6ULL, 0x76d4de52895820d9ULL, \
1356d6b4f49SShreyansh Jain 0x317ea4bb22bfdfedULL, 0x8250e80521188082ULL, \
1366d6b4f49SShreyansh Jain 0xbe2a516875702185ULL, 0x0d041dd676d77eeaULL, \
1376d6b4f49SShreyansh Jain 0x4aae673fdd3081deULL, 0xf9802b81de97deb1ULL, \
1386d6b4f49SShreyansh Jain 0x4fc0b4dd24d2a599ULL, 0xfceef8632775faf6ULL, \
1396d6b4f49SShreyansh Jain 0xbb44828a8c9205c2ULL, 0x086ace348f355aadULL, \
1406d6b4f49SShreyansh Jain 0x34107759db5dfbaaULL, 0x873e3be7d8faa4c5ULL, \
1416d6b4f49SShreyansh Jain 0xc094410e731d5bf1ULL, 0x73ba0db070ba049eULL, \
1426d6b4f49SShreyansh Jain 0xb86133d4dbcc19ffULL, 0x0b4f7f6ad86b4690ULL, \
1436d6b4f49SShreyansh Jain 0x4ce50583738cb9a4ULL, 0xffcb493d702be6cbULL, \
1446d6b4f49SShreyansh Jain 0xc3b1f050244347ccULL, 0x709fbcee27e418a3ULL, \
1456d6b4f49SShreyansh Jain 0x3735c6078c03e797ULL, 0x841b8ab98fa4b8f8ULL, \
1466d6b4f49SShreyansh Jain 0xadda7c5f3c4488e3ULL, 0x1ef430e13fe3d78cULL, \
1476d6b4f49SShreyansh Jain 0x595e4a08940428b8ULL, 0xea7006b697a377d7ULL, \
1486d6b4f49SShreyansh Jain 0xd60abfdbc3cbd6d0ULL, 0x6524f365c06c89bfULL, \
1496d6b4f49SShreyansh Jain 0x228e898c6b8b768bULL, 0x91a0c532682c29e4ULL, \
1506d6b4f49SShreyansh Jain 0x5a7bfb56c35a3485ULL, 0xe955b7e8c0fd6beaULL, \
1516d6b4f49SShreyansh Jain 0xaeffcd016b1a94deULL, 0x1dd181bf68bdcbb1ULL, \
1526d6b4f49SShreyansh Jain 0x21ab38d23cd56ab6ULL, 0x9285746c3f7235d9ULL, \
1536d6b4f49SShreyansh Jain 0xd52f0e859495caedULL, 0x6601423b97329582ULL, \
1546d6b4f49SShreyansh Jain 0xd041dd676d77eeaaULL, 0x636f91d96ed0b1c5ULL, \
1556d6b4f49SShreyansh Jain 0x24c5eb30c5374ef1ULL, 0x97eba78ec690119eULL, \
1566d6b4f49SShreyansh Jain 0xab911ee392f8b099ULL, 0x18bf525d915feff6ULL, \
1576d6b4f49SShreyansh Jain 0x5f1528b43ab810c2ULL, 0xec3b640a391f4fadULL, \
1586d6b4f49SShreyansh Jain 0x27e05a6e926952ccULL, 0x94ce16d091ce0da3ULL, \
1596d6b4f49SShreyansh Jain 0xd3646c393a29f297ULL, 0x604a2087398eadf8ULL, \
1606d6b4f49SShreyansh Jain 0x5c3099ea6de60cffULL, 0xef1ed5546e415390ULL, \
1616d6b4f49SShreyansh Jain 0xa8b4afbdc5a6aca4ULL, 0x1b9ae303c601f3cbULL, \
1626d6b4f49SShreyansh Jain 0x56ed3e2f9e224471ULL, 0xe5c372919d851b1eULL, \
1636d6b4f49SShreyansh Jain 0xa26908783662e42aULL, 0x114744c635c5bb45ULL, \
1646d6b4f49SShreyansh Jain 0x2d3dfdab61ad1a42ULL, 0x9e13b115620a452dULL, \
1656d6b4f49SShreyansh Jain 0xd9b9cbfcc9edba19ULL, 0x6a978742ca4ae576ULL, \
1666d6b4f49SShreyansh Jain 0xa14cb926613cf817ULL, 0x1262f598629ba778ULL, \
1676d6b4f49SShreyansh Jain 0x55c88f71c97c584cULL, 0xe6e6c3cfcadb0723ULL, \
1686d6b4f49SShreyansh Jain 0xda9c7aa29eb3a624ULL, 0x69b2361c9d14f94bULL, \
1696d6b4f49SShreyansh Jain 0x2e184cf536f3067fULL, 0x9d36004b35545910ULL, \
1706d6b4f49SShreyansh Jain 0x2b769f17cf112238ULL, 0x9858d3a9ccb67d57ULL, \
1716d6b4f49SShreyansh Jain 0xdff2a94067518263ULL, 0x6cdce5fe64f6dd0cULL, \
1726d6b4f49SShreyansh Jain 0x50a65c93309e7c0bULL, 0xe388102d33392364ULL, \
1736d6b4f49SShreyansh Jain 0xa4226ac498dedc50ULL, 0x170c267a9b79833fULL, \
1746d6b4f49SShreyansh Jain 0xdcd7181e300f9e5eULL, 0x6ff954a033a8c131ULL, \
1756d6b4f49SShreyansh Jain 0x28532e49984f3e05ULL, 0x9b7d62f79be8616aULL, \
1766d6b4f49SShreyansh Jain 0xa707db9acf80c06dULL, 0x14299724cc279f02ULL, \
1776d6b4f49SShreyansh Jain 0x5383edcd67c06036ULL, 0xe0ada17364673f59ULL} \
1786d6b4f49SShreyansh Jain }
1796d6b4f49SShreyansh Jain
1806d6b4f49SShreyansh Jain /*
1816d6b4f49SShreyansh Jain * Return the initial CRC seed. Use the value returned from this API as the
1826d6b4f49SShreyansh Jain * "crc" parameter to the first call to add data.
1836d6b4f49SShreyansh Jain */
fman_crc64_init(void)1846d6b4f49SShreyansh Jain static inline uint64_t fman_crc64_init(void)
1856d6b4f49SShreyansh Jain {
186*520dd992SFerruh Yigit return fman_crc64_ecma_182.initial;
1876d6b4f49SShreyansh Jain }
1886d6b4f49SShreyansh Jain
1896d6b4f49SShreyansh Jain /* Updates the CRC with arbitrary data */
fman_crc64_update(uint64_t crc,void * data,unsigned int len)1906d6b4f49SShreyansh Jain static inline uint64_t fman_crc64_update(uint64_t crc,
1916d6b4f49SShreyansh Jain void *data, unsigned int len)
1926d6b4f49SShreyansh Jain {
1936d6b4f49SShreyansh Jain uint8_t *p = data;
1946d6b4f49SShreyansh Jain while (len--)
195*520dd992SFerruh Yigit crc = fman_crc64_ecma_182.table[(crc ^ *(p++)) & 0xff] ^
1966d6b4f49SShreyansh Jain (crc >> 8);
1976d6b4f49SShreyansh Jain return crc;
1986d6b4f49SShreyansh Jain }
1996d6b4f49SShreyansh Jain
2006d6b4f49SShreyansh Jain /* Shorthands for updating the CRC with 8/16/32 bits of data.
2016d6b4f49SShreyansh Jain * IMPORTANT NOTE: the typed "data" arguments should not be mistaken for
2026d6b4f49SShreyansh Jain * host-endian numerical values, the assumption is that these values contain
2036d6b4f49SShreyansh Jain * big-endian (ie. network byte order) data.
2046d6b4f49SShreyansh Jain */
fman_crc64_compute_32bit(uint32_t data,uint64_t crc)2056d6b4f49SShreyansh Jain static inline uint64_t fman_crc64_compute_32bit(uint32_t data, uint64_t crc)
2066d6b4f49SShreyansh Jain {
2076d6b4f49SShreyansh Jain return fman_crc64_update(crc, &data, sizeof(data));
2086d6b4f49SShreyansh Jain }
fman_crc64_compute_16bit(uint16_t data,uint64_t crc)2096d6b4f49SShreyansh Jain static inline uint64_t fman_crc64_compute_16bit(uint16_t data, uint64_t crc)
2106d6b4f49SShreyansh Jain {
2116d6b4f49SShreyansh Jain return fman_crc64_update(crc, &data, sizeof(data));
2126d6b4f49SShreyansh Jain }
fman_crc64_compute_8bit(uint8_t data,uint64_t crc)2136d6b4f49SShreyansh Jain static inline uint64_t fman_crc64_compute_8bit(uint8_t data, uint64_t crc)
2146d6b4f49SShreyansh Jain {
2156d6b4f49SShreyansh Jain return fman_crc64_update(crc, &data, sizeof(data));
2166d6b4f49SShreyansh Jain }
2176d6b4f49SShreyansh Jain
2186d6b4f49SShreyansh Jain /*
2196d6b4f49SShreyansh Jain * Finalise the CRC (using 2's complement)
2206d6b4f49SShreyansh Jain */
fman_crc64_finish(uint64_t seed)2216d6b4f49SShreyansh Jain static inline uint64_t fman_crc64_finish(uint64_t seed)
2226d6b4f49SShreyansh Jain {
2236d6b4f49SShreyansh Jain return ~seed;
2246d6b4f49SShreyansh Jain }
2256d6b4f49SShreyansh Jain
2266d6b4f49SShreyansh Jain #ifdef __cplusplus
2276d6b4f49SShreyansh Jain }
2286d6b4f49SShreyansh Jain #endif
2296d6b4f49SShreyansh Jain
2306d6b4f49SShreyansh Jain #endif /* __FSL_FMAN_CRC64_H */
231