Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * cleaned up code to current version of sparse and added the slicing-by-8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * algorithm to the closely similar existing slicing-by-4 algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Nicer crc32 functions/docs submitted by linux@horizon.com.  Thanks!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Code was from the public domain, copyright abandoned.  Code was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * subsequently included in the kernel, thus was re-licensed under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * GNU GPL v2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Same crc32 function was used in 5 other places in the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * I made one version, and deleted the others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * There are various incantations of crc32().  Some use a seed of 0 or ~0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Some xor at the end with ~0.  The generic crc32() function takes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * seed as an argument, and doesn't xor at the end.  Then individual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * users can do whatever they need.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *   drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *   fs/jffs2 uses seed 0, doesn't xor with ~0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *   fs/partitions/efi.c uses seed ~0, xor's with ~0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * This source code is licensed under the GNU General Public License,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Version 2.  See the file COPYING for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /* see: Documentation/staging/crc32.rst for a description of algorithms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/crc32poly.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include "crc32defs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #if CRC_LE_BITS > 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) # define tole(x) ((__force u32) cpu_to_le32(x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) # define tole(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #if CRC_BE_BITS > 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) # define tobe(x) ((__force u32) cpu_to_be32(x))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) # define tobe(x) (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include "crc32table.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) MODULE_DESCRIPTION("Various CRC32 calculations");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /* implements slicing-by-4 or slicing-by-8 algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static inline u32 __pure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) # ifdef __LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #  define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #  define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		   t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #  define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		   t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #  define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #  define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		   t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #  define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		   t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	const u32 *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	size_t    rem_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) # ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) # if CRC_LE_BITS != 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u32 q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* Align it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (unlikely((long)buf & 3 && len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			DO_CRC(*buf++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		} while ((--len) && ((long)buf)&3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) # if CRC_LE_BITS == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	rem_len = len & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	len = len >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	rem_len = len & 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	len = len >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	b = (const u32 *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) # ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	--b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	for (i = 0; i < len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	for (--b; len; --len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		q = crc ^ *++b; /* use pre increment for speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) # if CRC_LE_BITS == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		crc = DO_CRC4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		crc = DO_CRC8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		q = *++b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		crc ^= DO_CRC4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	len = rem_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	/* And the last few bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		u8 *p = (u8 *)(b + 1) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) # ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			DO_CRC(*++p); /* use pre increment for speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			DO_CRC(*++p); /* use pre increment for speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		} while (--len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #undef DO_CRC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #undef DO_CRC4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #undef DO_CRC8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  *			CRC32/CRC32C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  *	 uses, or the previous crc32/crc32c value if computing incrementally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * @p: pointer to buffer over which CRC32/CRC32C is run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * @len: length of buffer @p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * @tab: little-endian Ethernet table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * @polynomial: CRC32/CRC32c LE polynomial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 					  size_t len, const u32 (*tab)[256],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 					  u32 polynomial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #if CRC_LE_BITS == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		crc ^= *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) # elif CRC_LE_BITS == 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		crc ^= *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		crc = (crc >> 2) ^ tab[0][crc & 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		crc = (crc >> 2) ^ tab[0][crc & 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		crc = (crc >> 2) ^ tab[0][crc & 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		crc = (crc >> 2) ^ tab[0][crc & 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) # elif CRC_LE_BITS == 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		crc ^= *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		crc = (crc >> 4) ^ tab[0][crc & 15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		crc = (crc >> 4) ^ tab[0][crc & 15];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) # elif CRC_LE_BITS == 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/* aka Sarwate algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		crc ^= *p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		crc = (crc >> 8) ^ tab[0][crc & 255];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	crc = (__force u32) __cpu_to_le32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	crc = crc32_body(crc, p, len, tab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	crc = __le32_to_cpu((__force __le32)crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #if CRC_LE_BITS == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return crc32_le_generic(crc, p, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			(const u32 (*)[256])crc32table_le, CRC32_POLY_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return crc32_le_generic(crc, p, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			(const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) EXPORT_SYMBOL(crc32_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) EXPORT_SYMBOL(__crc32c_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * This multiplies the polynomials x and y modulo the given modulus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * This follows the "little-endian" CRC convention that the lsbit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * represents the highest power of x, and the msbit represents x^0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	u32 product = x & 1 ? y : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	for (i = 0; i < 31; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		product = (product >> 1) ^ (product & 1 ? modulus : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		x >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		product ^= x & 1 ? y : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * @len: The number of bytes. @crc is multiplied by x^(8*@len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * @polynomial: The modulus used to reduce the result to 32 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * It's possible to parallelize CRC computations by computing a CRC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * over separate ranges of a buffer, then summing them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * This shifts the given CRC by 8*len bits (i.e. produces the same effect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  * as appending len bytes of zero to the data), in time proportional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * to log(len).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 						   u32 polynomial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	u32 power = polynomial;	/* CRC of x^32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	/* Shift up to 32 bits in the simple linear way */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	for (i = 0; i < 8 * (int)(len & 3); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	len >>= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		/* "power" is x^(2^i), modulo the polynomial */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (len & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			crc = gf2_multiply(crc, power, polynomial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		len >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		/* Square power, advancing to x^(2^(i+1)) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		power = gf2_multiply(power, power, polynomial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return crc32_generic_shift(crc, len, CRC32_POLY_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) EXPORT_SYMBOL(crc32_le_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) EXPORT_SYMBOL(__crc32c_le_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  *	other uses, or the previous crc32 value if computing incrementally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  * @p: pointer to buffer over which CRC32 is run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * @len: length of buffer @p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * @tab: big-endian Ethernet table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * @polynomial: CRC32 BE polynomial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 					  size_t len, const u32 (*tab)[256],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 					  u32 polynomial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) #if CRC_BE_BITS == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		crc ^= *p++ << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			crc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			    (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 					  0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) # elif CRC_BE_BITS == 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		crc ^= *p++ << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		crc = (crc << 2) ^ tab[0][crc >> 30];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		crc = (crc << 2) ^ tab[0][crc >> 30];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		crc = (crc << 2) ^ tab[0][crc >> 30];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		crc = (crc << 2) ^ tab[0][crc >> 30];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) # elif CRC_BE_BITS == 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		crc ^= *p++ << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		crc = (crc << 4) ^ tab[0][crc >> 28];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		crc = (crc << 4) ^ tab[0][crc >> 28];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) # elif CRC_BE_BITS == 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	while (len--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		crc ^= *p++ << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		crc = (crc << 8) ^ tab[0][crc >> 24];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	crc = (__force u32) __cpu_to_be32(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	crc = crc32_body(crc, p, len, tab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	crc = __be32_to_cpu((__force __be32)crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) #if CRC_BE_BITS == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	return crc32_be_generic(crc, p, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			(const u32 (*)[256])crc32table_be, CRC32_POLY_BE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) EXPORT_SYMBOL(crc32_be);