xref: /openbmc/linux/include/crypto/acompress.h (revision d47a97bd)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Asynchronous Compression operations
4  *
5  * Copyright (c) 2016, Intel Corporation
6  * Authors: Weigang Li <weigang.li@intel.com>
7  *          Giovanni Cabiddu <giovanni.cabiddu@intel.com>
8  */
9 #ifndef _CRYPTO_ACOMP_H
10 #define _CRYPTO_ACOMP_H
11 #include <linux/crypto.h>
12 
13 #define CRYPTO_ACOMP_ALLOC_OUTPUT	0x00000001
14 #define CRYPTO_ACOMP_DST_MAX		131072
15 
16 /**
17  * struct acomp_req - asynchronous (de)compression request
18  *
19  * @base:	Common attributes for asynchronous crypto requests
20  * @src:	Source Data
21  * @dst:	Destination data
22  * @slen:	Size of the input buffer
23  * @dlen:	Size of the output buffer and number of bytes produced
24  * @flags:	Internal flags
25  * @__ctx:	Start of private context data
26  */
27 struct acomp_req {
28 	struct crypto_async_request base;
29 	struct scatterlist *src;
30 	struct scatterlist *dst;
31 	unsigned int slen;
32 	unsigned int dlen;
33 	u32 flags;
34 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
35 };
36 
37 /**
38  * struct crypto_acomp - user-instantiated objects which encapsulate
39  * algorithms and core processing logic
40  *
41  * @compress:		Function performs a compress operation
42  * @decompress:		Function performs a de-compress operation
43  * @dst_free:		Frees destination buffer if allocated inside the
44  *			algorithm
45  * @reqsize:		Context size for (de)compression requests
46  * @base:		Common crypto API algorithm data structure
47  */
48 struct crypto_acomp {
49 	int (*compress)(struct acomp_req *req);
50 	int (*decompress)(struct acomp_req *req);
51 	void (*dst_free)(struct scatterlist *dst);
52 	unsigned int reqsize;
53 	struct crypto_tfm base;
54 };
55 
56 /**
57  * struct acomp_alg - asynchronous compression algorithm
58  *
59  * @compress:	Function performs a compress operation
60  * @decompress:	Function performs a de-compress operation
61  * @dst_free:	Frees destination buffer if allocated inside the algorithm
62  * @init:	Initialize the cryptographic transformation object.
63  *		This function is used to initialize the cryptographic
64  *		transformation object. This function is called only once at
65  *		the instantiation time, right after the transformation context
66  *		was allocated. In case the cryptographic hardware has some
67  *		special requirements which need to be handled by software, this
68  *		function shall check for the precise requirement of the
69  *		transformation and put any software fallbacks in place.
70  * @exit:	Deinitialize the cryptographic transformation object. This is a
71  *		counterpart to @init, used to remove various changes set in
72  *		@init.
73  *
74  * @reqsize:	Context size for (de)compression requests
75  * @base:	Common crypto API algorithm data structure
76  */
77 struct acomp_alg {
78 	int (*compress)(struct acomp_req *req);
79 	int (*decompress)(struct acomp_req *req);
80 	void (*dst_free)(struct scatterlist *dst);
81 	int (*init)(struct crypto_acomp *tfm);
82 	void (*exit)(struct crypto_acomp *tfm);
83 	unsigned int reqsize;
84 	struct crypto_alg base;
85 };
86 
87 /**
88  * DOC: Asynchronous Compression API
89  *
90  * The Asynchronous Compression API is used with the algorithms of type
91  * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
92  */
93 
94 /**
95  * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
96  * @alg_name:	is the cra_name / name or cra_driver_name / driver name of the
97  *		compression algorithm e.g. "deflate"
98  * @type:	specifies the type of the algorithm
99  * @mask:	specifies the mask for the algorithm
100  *
101  * Allocate a handle for a compression algorithm. The returned struct
102  * crypto_acomp is the handle that is required for any subsequent
103  * API invocation for the compression operations.
104  *
105  * Return:	allocated handle in case of success; IS_ERR() is true in case
106  *		of an error, PTR_ERR() returns the error code.
107  */
108 struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
109 					u32 mask);
110 /**
111  * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
112  * @alg_name:	is the cra_name / name or cra_driver_name / driver name of the
113  *		compression algorithm e.g. "deflate"
114  * @type:	specifies the type of the algorithm
115  * @mask:	specifies the mask for the algorithm
116  * @node:	specifies the NUMA node the ZIP hardware belongs to
117  *
118  * Allocate a handle for a compression algorithm. Drivers should try to use
119  * (de)compressors on the specified NUMA node.
120  * The returned struct crypto_acomp is the handle that is required for any
121  * subsequent API invocation for the compression operations.
122  *
123  * Return:	allocated handle in case of success; IS_ERR() is true in case
124  *		of an error, PTR_ERR() returns the error code.
125  */
126 struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
127 					u32 mask, int node);
128 
129 static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
130 {
131 	return &tfm->base;
132 }
133 
134 static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
135 {
136 	return container_of(alg, struct acomp_alg, base);
137 }
138 
139 static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
140 {
141 	return container_of(tfm, struct crypto_acomp, base);
142 }
143 
144 static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
145 {
146 	return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
147 }
148 
149 static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
150 {
151 	return tfm->reqsize;
152 }
153 
154 static inline void acomp_request_set_tfm(struct acomp_req *req,
155 					 struct crypto_acomp *tfm)
156 {
157 	req->base.tfm = crypto_acomp_tfm(tfm);
158 }
159 
160 static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
161 {
162 	return __crypto_acomp_tfm(req->base.tfm);
163 }
164 
165 /**
166  * crypto_free_acomp() -- free ACOMPRESS tfm handle
167  *
168  * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
169  *
170  * If @tfm is a NULL or error pointer, this function does nothing.
171  */
172 static inline void crypto_free_acomp(struct crypto_acomp *tfm)
173 {
174 	crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
175 }
176 
177 static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
178 {
179 	type &= ~CRYPTO_ALG_TYPE_MASK;
180 	type |= CRYPTO_ALG_TYPE_ACOMPRESS;
181 	mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
182 
183 	return crypto_has_alg(alg_name, type, mask);
184 }
185 
186 /**
187  * acomp_request_alloc() -- allocates asynchronous (de)compression request
188  *
189  * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
190  *
191  * Return:	allocated handle in case of success or NULL in case of an error
192  */
193 struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
194 
195 /**
196  * acomp_request_free() -- zeroize and free asynchronous (de)compression
197  *			   request as well as the output buffer if allocated
198  *			   inside the algorithm
199  *
200  * @req:	request to free
201  */
202 void acomp_request_free(struct acomp_req *req);
203 
204 /**
205  * acomp_request_set_callback() -- Sets an asynchronous callback
206  *
207  * Callback will be called when an asynchronous operation on a given
208  * request is finished.
209  *
210  * @req:	request that the callback will be set for
211  * @flgs:	specify for instance if the operation may backlog
212  * @cmlp:	callback which will be called
213  * @data:	private data used by the caller
214  */
215 static inline void acomp_request_set_callback(struct acomp_req *req,
216 					      u32 flgs,
217 					      crypto_completion_t cmpl,
218 					      void *data)
219 {
220 	req->base.complete = cmpl;
221 	req->base.data = data;
222 	req->base.flags = flgs;
223 }
224 
225 /**
226  * acomp_request_set_params() -- Sets request parameters
227  *
228  * Sets parameters required by an acomp operation
229  *
230  * @req:	asynchronous compress request
231  * @src:	pointer to input buffer scatterlist
232  * @dst:	pointer to output buffer scatterlist. If this is NULL, the
233  *		acomp layer will allocate the output memory
234  * @slen:	size of the input buffer
235  * @dlen:	size of the output buffer. If dst is NULL, this can be used by
236  *		the user to specify the maximum amount of memory to allocate
237  */
238 static inline void acomp_request_set_params(struct acomp_req *req,
239 					    struct scatterlist *src,
240 					    struct scatterlist *dst,
241 					    unsigned int slen,
242 					    unsigned int dlen)
243 {
244 	req->src = src;
245 	req->dst = dst;
246 	req->slen = slen;
247 	req->dlen = dlen;
248 
249 	if (!req->dst)
250 		req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
251 }
252 
253 /**
254  * crypto_acomp_compress() -- Invoke asynchronous compress operation
255  *
256  * Function invokes the asynchronous compress operation
257  *
258  * @req:	asynchronous compress request
259  *
260  * Return:	zero on success; error code in case of error
261  */
262 static inline int crypto_acomp_compress(struct acomp_req *req)
263 {
264 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
265 	struct crypto_alg *alg = tfm->base.__crt_alg;
266 	unsigned int slen = req->slen;
267 	int ret;
268 
269 	crypto_stats_get(alg);
270 	ret = tfm->compress(req);
271 	crypto_stats_compress(slen, ret, alg);
272 	return ret;
273 }
274 
275 /**
276  * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
277  *
278  * Function invokes the asynchronous decompress operation
279  *
280  * @req:	asynchronous compress request
281  *
282  * Return:	zero on success; error code in case of error
283  */
284 static inline int crypto_acomp_decompress(struct acomp_req *req)
285 {
286 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
287 	struct crypto_alg *alg = tfm->base.__crt_alg;
288 	unsigned int slen = req->slen;
289 	int ret;
290 
291 	crypto_stats_get(alg);
292 	ret = tfm->decompress(req);
293 	crypto_stats_decompress(slen, ret, alg);
294 	return ret;
295 }
296 
297 #endif
298