1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Asynchronous Compression operations
5 * Copyright (c) 2016, Intel Corporation
6 * Authors: Weigang Li <weigang.li@intel.com>
7 * Giovanni Cabiddu <giovanni.cabiddu@intel.com>
9 #ifndef _CRYPTO_ACOMP_H
10 #define _CRYPTO_ACOMP_H
11 #include <linux/crypto.h>
13 #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001
16 * struct acomp_req - asynchronous (de)compression request
18 * @base: Common attributes for asynchronous crypto requests
20 * @dst: Destination data
21 * @slen: Size of the input buffer
22 * @dlen: Size of the output buffer and number of bytes produced
23 * @flags: Internal flags
24 * @__ctx: Start of private context data
27 struct crypto_async_request base;
28 struct scatterlist *src;
29 struct scatterlist *dst;
33 void *__ctx[] CRYPTO_MINALIGN_ATTR;
37 * struct crypto_acomp - user-instantiated objects which encapsulate
38 * algorithms and core processing logic
40 * @compress: Function performs a compress operation
41 * @decompress: Function performs a de-compress operation
42 * @dst_free: Frees destination buffer if allocated inside the
44 * @reqsize: Context size for (de)compression requests
45 * @base: Common crypto API algorithm data structure
48 int (*compress)(struct acomp_req *req);
49 int (*decompress)(struct acomp_req *req);
50 void (*dst_free)(struct scatterlist *dst);
52 struct crypto_tfm base;
56 * struct acomp_alg - asynchronous compression algorithm
58 * @compress: Function performs a compress operation
59 * @decompress: Function performs a de-compress operation
60 * @dst_free: Frees destination buffer if allocated inside the algorithm
61 * @init: Initialize the cryptographic transformation object.
62 * This function is used to initialize the cryptographic
63 * transformation object. This function is called only once at
64 * the instantiation time, right after the transformation context
65 * was allocated. In case the cryptographic hardware has some
66 * special requirements which need to be handled by software, this
67 * function shall check for the precise requirement of the
68 * transformation and put any software fallbacks in place.
69 * @exit: Deinitialize the cryptographic transformation object. This is a
70 * counterpart to @init, used to remove various changes set in
73 * @reqsize: Context size for (de)compression requests
74 * @base: Common crypto API algorithm data structure
77 int (*compress)(struct acomp_req *req);
78 int (*decompress)(struct acomp_req *req);
79 void (*dst_free)(struct scatterlist *dst);
80 int (*init)(struct crypto_acomp *tfm);
81 void (*exit)(struct crypto_acomp *tfm);
83 struct crypto_alg base;
87 * DOC: Asynchronous Compression API
89 * The Asynchronous Compression API is used with the algorithms of type
90 * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
94 * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
95 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
96 * compression algorithm e.g. "deflate"
97 * @type: specifies the type of the algorithm
98 * @mask: specifies the mask for the algorithm
100 * Allocate a handle for a compression algorithm. The returned struct
101 * crypto_acomp is the handle that is required for any subsequent
102 * API invocation for the compression operations.
104 * Return: allocated handle in case of success; IS_ERR() is true in case
105 * of an error, PTR_ERR() returns the error code.
107 struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
110 * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
111 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
112 * compression algorithm e.g. "deflate"
113 * @type: specifies the type of the algorithm
114 * @mask: specifies the mask for the algorithm
115 * @node: specifies the NUMA node the ZIP hardware belongs to
117 * Allocate a handle for a compression algorithm. Drivers should try to use
118 * (de)compressors on the specified NUMA node.
119 * The returned struct crypto_acomp is the handle that is required for any
120 * subsequent API invocation for the compression operations.
122 * Return: allocated handle in case of success; IS_ERR() is true in case
123 * of an error, PTR_ERR() returns the error code.
125 struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
128 static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
133 static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
135 return container_of(alg, struct acomp_alg, base);
138 static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
140 return container_of(tfm, struct crypto_acomp, base);
143 static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
145 return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
148 static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
153 static inline void acomp_request_set_tfm(struct acomp_req *req,
154 struct crypto_acomp *tfm)
156 req->base.tfm = crypto_acomp_tfm(tfm);
159 static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
161 return __crypto_acomp_tfm(req->base.tfm);
165 * crypto_free_acomp() -- free ACOMPRESS tfm handle
167 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
169 static inline void crypto_free_acomp(struct crypto_acomp *tfm)
171 crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
174 static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
176 type &= ~CRYPTO_ALG_TYPE_MASK;
177 type |= CRYPTO_ALG_TYPE_ACOMPRESS;
178 mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
180 return crypto_has_alg(alg_name, type, mask);
184 * acomp_request_alloc() -- allocates asynchronous (de)compression request
186 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
188 * Return: allocated handle in case of success or NULL in case of an error
190 struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
193 * acomp_request_free() -- zeroize and free asynchronous (de)compression
194 * request as well as the output buffer if allocated
195 * inside the algorithm
197 * @req: request to free
199 void acomp_request_free(struct acomp_req *req);
202 * acomp_request_set_callback() -- Sets an asynchronous callback
204 * Callback will be called when an asynchronous operation on a given
205 * request is finished.
207 * @req: request that the callback will be set for
208 * @flgs: specify for instance if the operation may backlog
209 * @cmlp: callback which will be called
210 * @data: private data used by the caller
212 static inline void acomp_request_set_callback(struct acomp_req *req,
214 crypto_completion_t cmpl,
217 req->base.complete = cmpl;
218 req->base.data = data;
219 req->base.flags = flgs;
223 * acomp_request_set_params() -- Sets request parameters
225 * Sets parameters required by an acomp operation
227 * @req: asynchronous compress request
228 * @src: pointer to input buffer scatterlist
229 * @dst: pointer to output buffer scatterlist. If this is NULL, the
230 * acomp layer will allocate the output memory
231 * @slen: size of the input buffer
232 * @dlen: size of the output buffer. If dst is NULL, this can be used by
233 * the user to specify the maximum amount of memory to allocate
235 static inline void acomp_request_set_params(struct acomp_req *req,
236 struct scatterlist *src,
237 struct scatterlist *dst,
247 req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
251 * crypto_acomp_compress() -- Invoke asynchronous compress operation
253 * Function invokes the asynchronous compress operation
255 * @req: asynchronous compress request
257 * Return: zero on success; error code in case of error
259 static inline int crypto_acomp_compress(struct acomp_req *req)
261 struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
262 struct crypto_alg *alg = tfm->base.__crt_alg;
263 unsigned int slen = req->slen;
266 crypto_stats_get(alg);
267 ret = tfm->compress(req);
268 crypto_stats_compress(slen, ret, alg);
273 * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
275 * Function invokes the asynchronous decompress operation
277 * @req: asynchronous compress request
279 * Return: zero on success; error code in case of error
281 static inline int crypto_acomp_decompress(struct acomp_req *req)
283 struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
284 struct crypto_alg *alg = tfm->base.__crt_alg;
285 unsigned int slen = req->slen;
288 crypto_stats_get(alg);
289 ret = tfm->decompress(req);
290 crypto_stats_decompress(slen, ret, alg);