xref: /linux/drivers/net/ethernet/netronome/nfp/nfpcore/crc32.h (revision 307797159ac25fe5a2048bf5c6a5718298edca57)
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #ifndef NFP_CRC32_H
35 #define NFP_CRC32_H
36 
37 #include <linux/kernel.h>
38 #include <linux/crc32.h>
39 
40 /**
41  * crc32_posix_end() - Finalize POSIX CRC32 working state
42  * @crc:	Current CRC32 working state
43  * @total_len:	Total length of data that was CRC32'd
44  *
45  * Return: Final POSIX CRC32 value
46  */
47 static inline u32 crc32_posix_end(u32 crc, size_t total_len)
48 {
49 	/* Extend with the length of the string. */
50 	while (total_len != 0) {
51 		u8 c = total_len & 0xff;
52 
53 		crc = crc32_be(crc, &c, 1);
54 		total_len >>= 8;
55 	}
56 
57 	return ~crc;
58 }
59 
60 static inline u32 crc32_posix(const void *buff, size_t len)
61 {
62 	return crc32_posix_end(crc32_be(0, buff, len), len);
63 }
64 
65 #endif /* NFP_CRC32_H */
66