Linux Audio

Check our new training course

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