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
12 #include <linux/atomic.h>
13 #include <linux/container_of.h>
14 #include <linux/crypto.h>
16 #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001
17 #define CRYPTO_ACOMP_DST_MAX 131072
20 * struct acomp_req - asynchronous (de)compression request
22 * @base: Common attributes for asynchronous crypto requests
24 * @dst: Destination data
25 * @slen: Size of the input buffer
26 * @dlen: Size of the output buffer and number of bytes produced
27 * @flags: Internal flags
28 * @__ctx: Start of private context data
31 struct crypto_async_request base
;
32 struct scatterlist
*src
;
33 struct scatterlist
*dst
;
37 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
41 * struct crypto_acomp - user-instantiated objects which encapsulate
42 * algorithms and core processing logic
44 * @compress: Function performs a compress operation
45 * @decompress: Function performs a de-compress operation
46 * @dst_free: Frees destination buffer if allocated inside the
48 * @reqsize: Context size for (de)compression requests
49 * @base: Common crypto API algorithm data structure
52 int (*compress
)(struct acomp_req
*req
);
53 int (*decompress
)(struct acomp_req
*req
);
54 void (*dst_free
)(struct scatterlist
*dst
);
56 struct crypto_tfm base
;
59 #define COMP_ALG_COMMON { \
60 struct crypto_alg base; \
62 struct comp_alg_common COMP_ALG_COMMON
;
65 * DOC: Asynchronous Compression API
67 * The Asynchronous Compression API is used with the algorithms of type
68 * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
72 * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
73 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
74 * compression algorithm e.g. "deflate"
75 * @type: specifies the type of the algorithm
76 * @mask: specifies the mask for the algorithm
78 * Allocate a handle for a compression algorithm. The returned struct
79 * crypto_acomp is the handle that is required for any subsequent
80 * API invocation for the compression operations.
82 * Return: allocated handle in case of success; IS_ERR() is true in case
83 * of an error, PTR_ERR() returns the error code.
85 struct crypto_acomp
*crypto_alloc_acomp(const char *alg_name
, u32 type
,
88 * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
89 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
90 * compression algorithm e.g. "deflate"
91 * @type: specifies the type of the algorithm
92 * @mask: specifies the mask for the algorithm
93 * @node: specifies the NUMA node the ZIP hardware belongs to
95 * Allocate a handle for a compression algorithm. Drivers should try to use
96 * (de)compressors on the specified NUMA node.
97 * The returned struct crypto_acomp is the handle that is required for any
98 * subsequent API invocation for the compression operations.
100 * Return: allocated handle in case of success; IS_ERR() is true in case
101 * of an error, PTR_ERR() returns the error code.
103 struct crypto_acomp
*crypto_alloc_acomp_node(const char *alg_name
, u32 type
,
106 static inline struct crypto_tfm
*crypto_acomp_tfm(struct crypto_acomp
*tfm
)
111 static inline struct comp_alg_common
*__crypto_comp_alg_common(
112 struct crypto_alg
*alg
)
114 return container_of(alg
, struct comp_alg_common
, base
);
117 static inline struct crypto_acomp
*__crypto_acomp_tfm(struct crypto_tfm
*tfm
)
119 return container_of(tfm
, struct crypto_acomp
, base
);
122 static inline struct comp_alg_common
*crypto_comp_alg_common(
123 struct crypto_acomp
*tfm
)
125 return __crypto_comp_alg_common(crypto_acomp_tfm(tfm
)->__crt_alg
);
128 static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp
*tfm
)
133 static inline void acomp_request_set_tfm(struct acomp_req
*req
,
134 struct crypto_acomp
*tfm
)
136 req
->base
.tfm
= crypto_acomp_tfm(tfm
);
139 static inline bool acomp_is_async(struct crypto_acomp
*tfm
)
141 return crypto_comp_alg_common(tfm
)->base
.cra_flags
&
145 static inline struct crypto_acomp
*crypto_acomp_reqtfm(struct acomp_req
*req
)
147 return __crypto_acomp_tfm(req
->base
.tfm
);
151 * crypto_free_acomp() -- free ACOMPRESS tfm handle
153 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
155 * If @tfm is a NULL or error pointer, this function does nothing.
157 static inline void crypto_free_acomp(struct crypto_acomp
*tfm
)
159 crypto_destroy_tfm(tfm
, crypto_acomp_tfm(tfm
));
162 static inline int crypto_has_acomp(const char *alg_name
, u32 type
, u32 mask
)
164 type
&= ~CRYPTO_ALG_TYPE_MASK
;
165 type
|= CRYPTO_ALG_TYPE_ACOMPRESS
;
166 mask
|= CRYPTO_ALG_TYPE_ACOMPRESS_MASK
;
168 return crypto_has_alg(alg_name
, type
, mask
);
172 * acomp_request_alloc() -- allocates asynchronous (de)compression request
174 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
176 * Return: allocated handle in case of success or NULL in case of an error
178 struct acomp_req
*acomp_request_alloc(struct crypto_acomp
*tfm
);
181 * acomp_request_free() -- zeroize and free asynchronous (de)compression
182 * request as well as the output buffer if allocated
183 * inside the algorithm
185 * @req: request to free
187 void acomp_request_free(struct acomp_req
*req
);
190 * acomp_request_set_callback() -- Sets an asynchronous callback
192 * Callback will be called when an asynchronous operation on a given
193 * request is finished.
195 * @req: request that the callback will be set for
196 * @flgs: specify for instance if the operation may backlog
197 * @cmlp: callback which will be called
198 * @data: private data used by the caller
200 static inline void acomp_request_set_callback(struct acomp_req
*req
,
202 crypto_completion_t cmpl
,
205 req
->base
.complete
= cmpl
;
206 req
->base
.data
= data
;
207 req
->base
.flags
&= CRYPTO_ACOMP_ALLOC_OUTPUT
;
208 req
->base
.flags
|= flgs
& ~CRYPTO_ACOMP_ALLOC_OUTPUT
;
212 * acomp_request_set_params() -- Sets request parameters
214 * Sets parameters required by an acomp operation
216 * @req: asynchronous compress request
217 * @src: pointer to input buffer scatterlist
218 * @dst: pointer to output buffer scatterlist. If this is NULL, the
219 * acomp layer will allocate the output memory
220 * @slen: size of the input buffer
221 * @dlen: size of the output buffer. If dst is NULL, this can be used by
222 * the user to specify the maximum amount of memory to allocate
224 static inline void acomp_request_set_params(struct acomp_req
*req
,
225 struct scatterlist
*src
,
226 struct scatterlist
*dst
,
235 req
->flags
&= ~CRYPTO_ACOMP_ALLOC_OUTPUT
;
237 req
->flags
|= CRYPTO_ACOMP_ALLOC_OUTPUT
;
241 * crypto_acomp_compress() -- Invoke asynchronous compress operation
243 * Function invokes the asynchronous compress operation
245 * @req: asynchronous compress request
247 * Return: zero on success; error code in case of error
249 static inline int crypto_acomp_compress(struct acomp_req
*req
)
251 return crypto_acomp_reqtfm(req
)->compress(req
);
255 * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
257 * Function invokes the asynchronous decompress operation
259 * @req: asynchronous compress request
261 * Return: zero on success; error code in case of error
263 static inline int crypto_acomp_decompress(struct acomp_req
*req
)
265 return crypto_acomp_reqtfm(req
)->decompress(req
);