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) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * 842 Software Compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2015 Dan Streetman, IBM Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * See 842.h for details of the 842 compressed format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define MODULE_NAME "842_compress"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "842.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "842_debugfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define SW842_HASHTABLE8_BITS	(10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define SW842_HASHTABLE4_BITS	(11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define SW842_HASHTABLE2_BITS	(10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* By default, we allow compressing input buffers of any length, but we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * use the non-standard "short data" template so the decompressor can correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * reproduce the uncompressed data buffer at the right length.  However the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * hardware 842 compressor will not recognize the "short data" template, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * will fail to decompress any compressed buffer containing it (I have no idea
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * why anyone would want to use software to compress and hardware to decompress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * but that's beside the point).  This parameter forces the compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * function to simply reject any input buffer that isn't a multiple of 8 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * long, instead of using the "short data" template, so that all compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * buffers produced by this function will be decompressable by the 842 hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * decompressor.  Unless you have a specific need for that, leave this disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * so that any length buffer can be compressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static bool sw842_strict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) module_param_named(strict, sw842_strict, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static u8 comp_ops[OPS_MAX][5] = { /* params size in bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	{ I8, N0, N0, N0, 0x19 }, /* 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	{ I4, I4, N0, N0, 0x18 }, /* 18 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	{ I4, I2, I2, N0, 0x17 }, /* 25 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	{ I2, I2, I4, N0, 0x13 }, /* 25 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	{ I2, I2, I2, I2, 0x12 }, /* 32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	{ I4, I2, D2, N0, 0x16 }, /* 33 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	{ I4, D2, I2, N0, 0x15 }, /* 33 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	{ I2, D2, I4, N0, 0x0e }, /* 33 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	{ D2, I2, I4, N0, 0x09 }, /* 33 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{ I2, I2, I2, D2, 0x11 }, /* 40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	{ I2, I2, D2, I2, 0x10 }, /* 40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	{ I2, D2, I2, I2, 0x0d }, /* 40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	{ D2, I2, I2, I2, 0x08 }, /* 40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	{ I4, D4, N0, N0, 0x14 }, /* 41 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	{ D4, I4, N0, N0, 0x04 }, /* 41 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	{ I2, I2, D4, N0, 0x0f }, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	{ I2, D2, I2, D2, 0x0c }, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	{ I2, D4, I2, N0, 0x0b }, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	{ D2, I2, I2, D2, 0x07 }, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	{ D2, I2, D2, I2, 0x06 }, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	{ D4, I2, I2, N0, 0x03 }, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	{ I2, D2, D4, N0, 0x0a }, /* 56 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	{ D2, I2, D4, N0, 0x05 }, /* 56 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	{ D4, I2, D2, N0, 0x02 }, /* 56 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	{ D4, D2, I2, N0, 0x01 }, /* 56 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	{ D8, N0, N0, N0, 0x00 }, /* 64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) struct sw842_hlist_node8 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct hlist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u64 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	u8 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) struct sw842_hlist_node4 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct hlist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	u32 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u16 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) struct sw842_hlist_node2 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct hlist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	u16 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define INDEX_NOT_FOUND		(-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define INDEX_NOT_CHECKED	(-2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) struct sw842_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u8 *in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	u8 *instart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	u64 ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	u8 *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u64 olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u8 bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u64 data8[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u32 data4[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u16 data2[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int index8[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int index4[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	int index2[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	DECLARE_HASHTABLE(htable8, SW842_HASHTABLE8_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	DECLARE_HASHTABLE(htable4, SW842_HASHTABLE4_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	DECLARE_HASHTABLE(htable2, SW842_HASHTABLE2_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct sw842_hlist_node8 node8[1 << I8_BITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct sw842_hlist_node4 node4[1 << I4_BITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct sw842_hlist_node2 node2[1 << I2_BITS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define get_input_data(p, o, b)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	be##b##_to_cpu(get_unaligned((__be##b *)((p)->in + (o))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define init_hashtable_nodes(p, b)	do {			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int _i;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	hash_init((p)->htable##b);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	for (_i = 0; _i < ARRAY_SIZE((p)->node##b); _i++) {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		(p)->node##b[_i].index = _i;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		(p)->node##b[_i].data = 0;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		INIT_HLIST_NODE(&(p)->node##b[_i].node);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define find_index(p, b, n)	({					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct sw842_hlist_node##b *_n;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	p->index##b[n] = INDEX_NOT_FOUND;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	hash_for_each_possible(p->htable##b, _n, node, p->data##b[n]) {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (p->data##b[n] == _n->data) {			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			p->index##b[n] = _n->index;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			break;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	p->index##b[n] >= 0;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define check_index(p, b, n)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	((p)->index##b[n] == INDEX_NOT_CHECKED	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 ? find_index(p, b, n)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 : (p)->index##b[n] >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #define replace_hash(p, b, i, d)	do {				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct sw842_hlist_node##b *_n = &(p)->node##b[(i)+(d)];	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	hash_del(&_n->node);						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	_n->data = (p)->data##b[d];					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	pr_debug("add hash index%x %x pos %x data %lx\n", b,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		 (unsigned int)_n->index,				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		 (unsigned int)((p)->in - (p)->instart),		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		 (unsigned long)_n->data);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	hash_add((p)->htable##b, &_n->node, _n->data);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static u8 bmask[8] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int add_bits(struct sw842_param *p, u64 d, u8 n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int __split_add_bits(struct sw842_param *p, u64 d, u8 n, u8 s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (n <= s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = add_bits(p, d >> s, n - s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return add_bits(p, d & GENMASK_ULL(s - 1, 0), s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int add_bits(struct sw842_param *p, u64 d, u8 n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int b = p->bit, bits = b + n, s = round_up(bits, 8) - bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	u64 o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	u8 *out = p->out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	pr_debug("add %u bits %lx\n", (unsigned char)n, (unsigned long)d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (n > 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* split this up if writing to > 8 bytes (i.e. n == 64 && p->bit > 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * or if we're at the end of the output buffer and would write past end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (bits > 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return __split_add_bits(p, d, n, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	else if (p->olen < 8 && bits > 32 && bits <= 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		return __split_add_bits(p, d, n, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	else if (p->olen < 4 && bits > 16 && bits <= 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return __split_add_bits(p, d, n, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (DIV_ROUND_UP(bits, 8) > p->olen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	o = *out & bmask[b];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	d <<= s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (bits <= 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		*out = o | d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	else if (bits <= 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		put_unaligned(cpu_to_be16(o << 8 | d), (__be16 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	else if (bits <= 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		put_unaligned(cpu_to_be32(o << 24 | d << 8), (__be32 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	else if (bits <= 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		put_unaligned(cpu_to_be32(o << 24 | d), (__be32 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	else if (bits <= 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		put_unaligned(cpu_to_be64(o << 56 | d << 24), (__be64 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	else if (bits <= 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		put_unaligned(cpu_to_be64(o << 56 | d << 16), (__be64 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	else if (bits <= 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		put_unaligned(cpu_to_be64(o << 56 | d << 8), (__be64 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		put_unaligned(cpu_to_be64(o << 56 | d), (__be64 *)out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	p->bit += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (p->bit > 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		p->out += p->bit / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		p->olen -= p->bit / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		p->bit %= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int add_template(struct sw842_param *p, u8 c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	int ret, i, b = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	u8 *t = comp_ops[c];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	bool inv = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (c >= OPS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	pr_debug("template %x\n", t[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	ret = add_bits(p, t[4], OP_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		pr_debug("op %x\n", t[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		switch (t[i] & OP_AMOUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		case OP_AMOUNT_8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			if (b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			else if (t[i] & OP_ACTION_INDEX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 				ret = add_bits(p, p->index8[0], I8_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			else if (t[i] & OP_ACTION_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				ret = add_bits(p, p->data8[0], 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		case OP_AMOUNT_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			if (b == 2 && t[i] & OP_ACTION_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				ret = add_bits(p, get_input_data(p, 2, 32), 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			else if (b != 0 && b != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			else if (t[i] & OP_ACTION_INDEX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				ret = add_bits(p, p->index4[b >> 2], I4_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			else if (t[i] & OP_ACTION_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 				ret = add_bits(p, p->data4[b >> 2], 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		case OP_AMOUNT_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			if (b != 0 && b != 2 && b != 4 && b != 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			if (t[i] & OP_ACTION_INDEX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				ret = add_bits(p, p->index2[b >> 1], I2_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			else if (t[i] & OP_ACTION_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 				ret = add_bits(p, p->data2[b >> 1], 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 				inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		case OP_AMOUNT_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			inv = (b != 8) || !(t[i] & OP_ACTION_NOOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			inv = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (inv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			pr_err("Invalid templ %x op %d : %x %x %x %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			       c, i, t[0], t[1], t[2], t[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		b += t[i] & OP_AMOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (b != 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		pr_err("Invalid template %x len %x : %x %x %x %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		       c, b, t[0], t[1], t[2], t[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		atomic_inc(&template_count[t[4]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int add_repeat_template(struct sw842_param *p, u8 r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/* repeat param is 0-based */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (!r || --r > REPEAT_BITS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	ret = add_bits(p, OP_REPEAT, OP_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ret = add_bits(p, r, REPEAT_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		atomic_inc(&template_repeat_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int add_short_data_template(struct sw842_param *p, u8 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (!b || b > SHORT_DATA_BITS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ret = add_bits(p, OP_SHORT_DATA, OP_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	ret = add_bits(p, b, SHORT_DATA_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	for (i = 0; i < b; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		ret = add_bits(p, p->in[i], 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		atomic_inc(&template_short_data_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static int add_zeros_template(struct sw842_param *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	int ret = add_bits(p, OP_ZEROS, OP_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		atomic_inc(&template_zeros_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int add_end_template(struct sw842_param *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	int ret = add_bits(p, OP_END, OP_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		atomic_inc(&template_end_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static bool check_template(struct sw842_param *p, u8 c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	u8 *t = comp_ops[c];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	int i, match, b = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (c >= OPS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (t[i] & OP_ACTION_INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			if (t[i] & OP_AMOUNT_2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				match = check_index(p, 2, b >> 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			else if (t[i] & OP_AMOUNT_4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 				match = check_index(p, 4, b >> 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			else if (t[i] & OP_AMOUNT_8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				match = check_index(p, 8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		b += t[i] & OP_AMOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static void get_next_data(struct sw842_param *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	p->data8[0] = get_input_data(p, 0, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	p->data4[0] = get_input_data(p, 0, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	p->data4[1] = get_input_data(p, 4, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	p->data2[0] = get_input_data(p, 0, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	p->data2[1] = get_input_data(p, 2, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	p->data2[2] = get_input_data(p, 4, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	p->data2[3] = get_input_data(p, 6, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /* update the hashtable entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * only call this after finding/adding the current template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  * the dataN fields for the current 8 byte block must be already updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static void update_hashtables(struct sw842_param *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	u64 pos = p->in - p->instart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	u64 n8 = (pos >> 3) % (1 << I8_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	u64 n4 = (pos >> 2) % (1 << I4_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	u64 n2 = (pos >> 1) % (1 << I2_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	replace_hash(p, 8, n8, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	replace_hash(p, 4, n4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	replace_hash(p, 4, n4, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	replace_hash(p, 2, n2, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	replace_hash(p, 2, n2, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	replace_hash(p, 2, n2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	replace_hash(p, 2, n2, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /* find the next template to use, and add it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * the p->dataN fields must already be set for the current 8 byte block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static int process_next(struct sw842_param *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	p->index8[0] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	p->index4[0] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	p->index4[1] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	p->index2[0] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	p->index2[1] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	p->index2[2] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	p->index2[3] = INDEX_NOT_CHECKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	/* check up to OPS_MAX - 1; last op is our fallback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	for (i = 0; i < OPS_MAX - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		if (check_template(p, i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	ret = add_template(p, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)  * sw842_compress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)  * Compress the uncompressed buffer of length @ilen at @in to the output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)  * @out, using no more than @olen bytes, using the 842 compression format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)  * Returns: 0 on success, error on failure.  The @olen parameter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)  * will contain the number of output bytes written on success, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)  * 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) int sw842_compress(const u8 *in, unsigned int ilen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		   u8 *out, unsigned int *olen, void *wmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	struct sw842_param *p = (struct sw842_param *)wmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	u64 last, next, pad, total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	u8 repeat_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	u32 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	BUILD_BUG_ON(sizeof(*p) > SW842_MEM_COMPRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	init_hashtable_nodes(p, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	init_hashtable_nodes(p, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	init_hashtable_nodes(p, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	p->in = (u8 *)in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	p->instart = p->in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	p->ilen = ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	p->out = out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	p->olen = *olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	p->bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	total = p->olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	*olen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	/* if using strict mode, we can only compress a multiple of 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (sw842_strict && (ilen % 8)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		pr_err("Using strict mode, can't compress len %d\n", ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	/* let's compress at least 8 bytes, mkay? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	if (unlikely(ilen < 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		goto skip_comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	/* make initial 'last' different so we don't match the first time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	last = ~get_unaligned((u64 *)p->in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	while (p->ilen > 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		next = get_unaligned((u64 *)p->in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		/* must get the next data, as we need to update the hashtable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		 * entries with the new data every time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		get_next_data(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		/* we don't care about endianness in last or next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		 * we're just comparing 8 bytes to another 8 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		 * they're both the same endianness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		if (next == last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			/* repeat count bits are 0-based, so we stop at +1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			if (++repeat_count <= REPEAT_BITS_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 				goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		if (repeat_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			ret = add_repeat_template(p, repeat_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			repeat_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			if (next == last) /* reached max repeat bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 				goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		if (next == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			ret = add_zeros_template(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			ret = process_next(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		last = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		update_hashtables(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		p->in += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		p->ilen -= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	if (repeat_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		ret = add_repeat_template(p, repeat_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) skip_comp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (p->ilen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		ret = add_short_data_template(p, p->ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		p->in += p->ilen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		p->ilen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	ret = add_end_template(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	 * crc(0:31) is appended to target data starting with the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	 * bit after End of stream template.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	 * nx842 calculates CRC for data in big-endian format. So doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	 * same here so that sw842 decompression can be used for both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	 * compressed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	crc = crc32_be(0, in, ilen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	ret = add_bits(p, crc, CRC_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (p->bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		p->out++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		p->olen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		p->bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	/* pad compressed length to multiple of 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	pad = (8 - ((total - p->olen) % 8)) % 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	if (pad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		if (pad > p->olen) /* we were so close! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 			return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 		memset(p->out, 0, pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		p->out += pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		p->olen -= pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	if (unlikely((total - p->olen) > UINT_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	*olen = total - p->olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) EXPORT_SYMBOL_GPL(sw842_compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) static int __init sw842_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		sw842_debugfs_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) module_init(sw842_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) static void __exit sw842_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if (sw842_template_counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		sw842_debugfs_remove();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) module_exit(sw842_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) MODULE_DESCRIPTION("Software 842 Compressor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");