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