xref: /linux/drivers/block/zram/zcomp.h (revision 101fea09a860c7cd3ef53faefaab1f4a03132446)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef _ZCOMP_H_
4 #define _ZCOMP_H_
5 
6 #include <linux/mutex.h>
7 
8 struct scatterlist;
9 
10 #define ZCOMP_PARAM_NOT_SET	INT_MIN
11 
12 struct deflate_params {
13 	s32 winbits;
14 };
15 
16 /*
17  * Immutable driver (backend) parameters. The driver may attach private
18  * data to it (e.g. driver representation of the dictionary, etc.).
19  *
20  * This data is kept per-comp and is shared among execution contexts.
21  */
22 struct zcomp_params {
23 	void *dict;
24 	size_t dict_sz;
25 	s32 level;
26 	union {
27 		struct deflate_params deflate;
28 	};
29 
30 	void *drv_data;
31 };
32 
33 /*
34  * Run-time driver context - scratch buffers, etc. It is modified during
35  * request execution (compression/decompression), cannot be shared, so
36  * it's in per-CPU area.
37  */
38 struct zcomp_ctx {
39 	void *context;
40 };
41 
42 struct zcomp_strm {
43 	struct mutex lock;
44 	/* compression buffer */
45 	void *buffer;
46 	/* local copy of handle memory */
47 	void *local_copy;
48 	struct zcomp_ctx ctx;
49 };
50 
51 struct zcomp_req {
52 	const unsigned char *src;
53 	const size_t src_len;
54 
55 	unsigned char *dst;
56 	size_t dst_len;
57 };
58 
59 struct zcomp_ops {
60 	int (*compress)(struct zcomp_params *params, struct zcomp_ctx *ctx,
61 			struct zcomp_req *req);
62 	int (*decompress)(struct zcomp_params *params, struct zcomp_ctx *ctx,
63 			  struct zcomp_req *req);
64 
65 	int (*create_ctx)(struct zcomp_params *params, struct zcomp_ctx *ctx);
66 	void (*destroy_ctx)(struct zcomp_ctx *ctx);
67 
68 	int (*setup_params)(struct zcomp_params *params);
69 	void (*release_params)(struct zcomp_params *params);
70 
71 	const char *name;
72 };
73 
74 /* dynamic per-device compression frontend */
75 struct zcomp {
76 	struct zcomp_strm __percpu *stream;
77 	const struct zcomp_ops *ops;
78 	struct zcomp_params *params;
79 	struct hlist_node node;
80 };
81 
82 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node);
83 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node);
84 ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at);
85 const char *zcomp_lookup_backend_name(const char *comp);
86 
87 struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params);
88 void zcomp_destroy(struct zcomp *comp);
89 
90 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp);
91 void zcomp_stream_put(struct zcomp_strm *zstrm);
92 
93 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
94 		   const void *src, unsigned int *dst_len);
95 int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm,
96 		     struct scatterlist *sg, unsigned int src_len, void *dst);
97 
98 #endif /* _ZCOMP_H_ */
99