Linux Audio

Check our new training course

Loading...
v6.8
  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
 12#include <linux/atomic.h>
 13#include <linux/container_of.h>
 14#include <linux/crypto.h>
 15
 16#define CRYPTO_ACOMP_ALLOC_OUTPUT	0x00000001
 17#define CRYPTO_ACOMP_DST_MAX		131072
 18
 19/**
 20 * struct acomp_req - asynchronous (de)compression request
 21 *
 22 * @base:	Common attributes for asynchronous crypto requests
 23 * @src:	Source Data
 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
 29 */
 30struct acomp_req {
 31	struct crypto_async_request base;
 32	struct scatterlist *src;
 33	struct scatterlist *dst;
 34	unsigned int slen;
 35	unsigned int dlen;
 36	u32 flags;
 37	void *__ctx[] CRYPTO_MINALIGN_ATTR;
 38};
 39
 40/**
 41 * struct crypto_acomp - user-instantiated objects which encapsulate
 42 * algorithms and core processing logic
 43 *
 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
 47 *			algorithm
 48 * @reqsize:		Context size for (de)compression requests
 49 * @base:		Common crypto API algorithm data structure
 50 */
 51struct crypto_acomp {
 52	int (*compress)(struct acomp_req *req);
 53	int (*decompress)(struct acomp_req *req);
 54	void (*dst_free)(struct scatterlist *dst);
 55	unsigned int reqsize;
 56	struct crypto_tfm base;
 57};
 58
 59/*
 60 * struct crypto_istat_compress - statistics for compress algorithm
 61 * @compress_cnt:	number of compress requests
 62 * @compress_tlen:	total data size handled by compress requests
 63 * @decompress_cnt:	number of decompress requests
 64 * @decompress_tlen:	total data size handled by decompress requests
 65 * @err_cnt:		number of error for compress requests
 66 */
 67struct crypto_istat_compress {
 68	atomic64_t compress_cnt;
 69	atomic64_t compress_tlen;
 70	atomic64_t decompress_cnt;
 71	atomic64_t decompress_tlen;
 72	atomic64_t err_cnt;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 73};
 74
 75#ifdef CONFIG_CRYPTO_STATS
 76#define COMP_ALG_COMMON_STATS struct crypto_istat_compress stat;
 77#else
 78#define COMP_ALG_COMMON_STATS
 79#endif
 80
 81#define COMP_ALG_COMMON {			\
 82	COMP_ALG_COMMON_STATS			\
 83						\
 84	struct crypto_alg base;			\
 85}
 86struct comp_alg_common COMP_ALG_COMMON;
 87
 88/**
 89 * DOC: Asynchronous Compression API
 90 *
 91 * The Asynchronous Compression API is used with the algorithms of type
 92 * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
 93 */
 94
 95/**
 96 * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
 97 * @alg_name:	is the cra_name / name or cra_driver_name / driver name of the
 98 *		compression algorithm e.g. "deflate"
 99 * @type:	specifies the type of the algorithm
100 * @mask:	specifies the mask for the algorithm
101 *
102 * Allocate a handle for a compression algorithm. The returned struct
103 * crypto_acomp is the handle that is required for any subsequent
104 * API invocation for the compression operations.
105 *
106 * Return:	allocated handle in case of success; IS_ERR() is true in case
107 *		of an error, PTR_ERR() returns the error code.
108 */
109struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
110					u32 mask);
111/**
112 * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
113 * @alg_name:	is the cra_name / name or cra_driver_name / driver name of the
114 *		compression algorithm e.g. "deflate"
115 * @type:	specifies the type of the algorithm
116 * @mask:	specifies the mask for the algorithm
117 * @node:	specifies the NUMA node the ZIP hardware belongs to
118 *
119 * Allocate a handle for a compression algorithm. Drivers should try to use
120 * (de)compressors on the specified NUMA node.
121 * The returned struct crypto_acomp is the handle that is required for any
122 * subsequent API invocation for the compression operations.
123 *
124 * Return:	allocated handle in case of success; IS_ERR() is true in case
125 *		of an error, PTR_ERR() returns the error code.
126 */
127struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
128					u32 mask, int node);
129
130static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
131{
132	return &tfm->base;
133}
134
135static inline struct comp_alg_common *__crypto_comp_alg_common(
136	struct crypto_alg *alg)
137{
138	return container_of(alg, struct comp_alg_common, base);
139}
140
141static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
142{
143	return container_of(tfm, struct crypto_acomp, base);
144}
145
146static inline struct comp_alg_common *crypto_comp_alg_common(
147	struct crypto_acomp *tfm)
148{
149	return __crypto_comp_alg_common(crypto_acomp_tfm(tfm)->__crt_alg);
150}
151
152static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
153{
154	return tfm->reqsize;
155}
156
157static inline void acomp_request_set_tfm(struct acomp_req *req,
158					 struct crypto_acomp *tfm)
159{
160	req->base.tfm = crypto_acomp_tfm(tfm);
161}
162
163static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
164{
165	return __crypto_acomp_tfm(req->base.tfm);
166}
167
168/**
169 * crypto_free_acomp() -- free ACOMPRESS tfm handle
170 *
171 * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
172 *
173 * If @tfm is a NULL or error pointer, this function does nothing.
174 */
175static inline void crypto_free_acomp(struct crypto_acomp *tfm)
176{
177	crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
178}
179
180static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
181{
182	type &= ~CRYPTO_ALG_TYPE_MASK;
183	type |= CRYPTO_ALG_TYPE_ACOMPRESS;
184	mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
185
186	return crypto_has_alg(alg_name, type, mask);
187}
188
189/**
190 * acomp_request_alloc() -- allocates asynchronous (de)compression request
191 *
192 * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
193 *
194 * Return:	allocated handle in case of success or NULL in case of an error
195 */
196struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
197
198/**
199 * acomp_request_free() -- zeroize and free asynchronous (de)compression
200 *			   request as well as the output buffer if allocated
201 *			   inside the algorithm
202 *
203 * @req:	request to free
204 */
205void acomp_request_free(struct acomp_req *req);
206
207/**
208 * acomp_request_set_callback() -- Sets an asynchronous callback
209 *
210 * Callback will be called when an asynchronous operation on a given
211 * request is finished.
212 *
213 * @req:	request that the callback will be set for
214 * @flgs:	specify for instance if the operation may backlog
215 * @cmlp:	callback which will be called
216 * @data:	private data used by the caller
217 */
218static inline void acomp_request_set_callback(struct acomp_req *req,
219					      u32 flgs,
220					      crypto_completion_t cmpl,
221					      void *data)
222{
223	req->base.complete = cmpl;
224	req->base.data = data;
225	req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT;
226	req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT;
227}
228
229/**
230 * acomp_request_set_params() -- Sets request parameters
231 *
232 * Sets parameters required by an acomp operation
233 *
234 * @req:	asynchronous compress request
235 * @src:	pointer to input buffer scatterlist
236 * @dst:	pointer to output buffer scatterlist. If this is NULL, the
237 *		acomp layer will allocate the output memory
238 * @slen:	size of the input buffer
239 * @dlen:	size of the output buffer. If dst is NULL, this can be used by
240 *		the user to specify the maximum amount of memory to allocate
241 */
242static inline void acomp_request_set_params(struct acomp_req *req,
243					    struct scatterlist *src,
244					    struct scatterlist *dst,
245					    unsigned int slen,
246					    unsigned int dlen)
247{
248	req->src = src;
249	req->dst = dst;
250	req->slen = slen;
251	req->dlen = dlen;
252
253	req->flags &= ~CRYPTO_ACOMP_ALLOC_OUTPUT;
254	if (!req->dst)
255		req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
256}
257
258static inline struct crypto_istat_compress *comp_get_stat(
259	struct comp_alg_common *alg)
260{
261#ifdef CONFIG_CRYPTO_STATS
262	return &alg->stat;
263#else
264	return NULL;
265#endif
266}
267
268static inline int crypto_comp_errstat(struct comp_alg_common *alg, int err)
269{
270	if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
271		return err;
272
273	if (err && err != -EINPROGRESS && err != -EBUSY)
274		atomic64_inc(&comp_get_stat(alg)->err_cnt);
275
276	return err;
277}
278
279/**
280 * crypto_acomp_compress() -- Invoke asynchronous compress operation
281 *
282 * Function invokes the asynchronous compress operation
283 *
284 * @req:	asynchronous compress request
285 *
286 * Return:	zero on success; error code in case of error
287 */
288static inline int crypto_acomp_compress(struct acomp_req *req)
289{
290	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
291	struct comp_alg_common *alg;
292
293	alg = crypto_comp_alg_common(tfm);
294
295	if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
296		struct crypto_istat_compress *istat = comp_get_stat(alg);
297
298		atomic64_inc(&istat->compress_cnt);
299		atomic64_add(req->slen, &istat->compress_tlen);
300	}
301
302	return crypto_comp_errstat(alg, tfm->compress(req));
303}
304
305/**
306 * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
307 *
308 * Function invokes the asynchronous decompress operation
309 *
310 * @req:	asynchronous compress request
311 *
312 * Return:	zero on success; error code in case of error
313 */
314static inline int crypto_acomp_decompress(struct acomp_req *req)
315{
316	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
317	struct comp_alg_common *alg;
318
319	alg = crypto_comp_alg_common(tfm);
320
321	if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
322		struct crypto_istat_compress *istat = comp_get_stat(alg);
323
324		atomic64_inc(&istat->decompress_cnt);
325		atomic64_add(req->slen, &istat->decompress_tlen);
326	}
327
328	return crypto_comp_errstat(alg, tfm->decompress(req));
329}
330
331#endif
v4.17
 
  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