xref: /openbmc/linux/include/crypto/acompress.h (revision 4c9edf17c0b44655c565b59a956161a2ee125cca)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
22ebda74fSGiovanni Cabiddu /*
32ebda74fSGiovanni Cabiddu  * Asynchronous Compression operations
42ebda74fSGiovanni Cabiddu  *
52ebda74fSGiovanni Cabiddu  * Copyright (c) 2016, Intel Corporation
62ebda74fSGiovanni Cabiddu  * Authors: Weigang Li <weigang.li@intel.com>
72ebda74fSGiovanni Cabiddu  *          Giovanni Cabiddu <giovanni.cabiddu@intel.com>
82ebda74fSGiovanni Cabiddu  */
92ebda74fSGiovanni Cabiddu #ifndef _CRYPTO_ACOMP_H
102ebda74fSGiovanni Cabiddu #define _CRYPTO_ACOMP_H
112ebda74fSGiovanni Cabiddu #include <linux/crypto.h>
122ebda74fSGiovanni Cabiddu 
132ebda74fSGiovanni Cabiddu #define CRYPTO_ACOMP_ALLOC_OUTPUT	0x00000001
145fc8041eSGiovanni Cabiddu #define CRYPTO_ACOMP_DST_MAX		131072
152ebda74fSGiovanni Cabiddu 
162ebda74fSGiovanni Cabiddu /**
172ebda74fSGiovanni Cabiddu  * struct acomp_req - asynchronous (de)compression request
182ebda74fSGiovanni Cabiddu  *
192ebda74fSGiovanni Cabiddu  * @base:	Common attributes for asynchronous crypto requests
202ebda74fSGiovanni Cabiddu  * @src:	Source Data
212ebda74fSGiovanni Cabiddu  * @dst:	Destination data
222ebda74fSGiovanni Cabiddu  * @slen:	Size of the input buffer
232ebda74fSGiovanni Cabiddu  * @dlen:	Size of the output buffer and number of bytes produced
242ebda74fSGiovanni Cabiddu  * @flags:	Internal flags
252ebda74fSGiovanni Cabiddu  * @__ctx:	Start of private context data
262ebda74fSGiovanni Cabiddu  */
272ebda74fSGiovanni Cabiddu struct acomp_req {
282ebda74fSGiovanni Cabiddu 	struct crypto_async_request base;
292ebda74fSGiovanni Cabiddu 	struct scatterlist *src;
302ebda74fSGiovanni Cabiddu 	struct scatterlist *dst;
312ebda74fSGiovanni Cabiddu 	unsigned int slen;
322ebda74fSGiovanni Cabiddu 	unsigned int dlen;
332ebda74fSGiovanni Cabiddu 	u32 flags;
342ebda74fSGiovanni Cabiddu 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
352ebda74fSGiovanni Cabiddu };
362ebda74fSGiovanni Cabiddu 
372ebda74fSGiovanni Cabiddu /**
382ebda74fSGiovanni Cabiddu  * struct crypto_acomp - user-instantiated objects which encapsulate
392ebda74fSGiovanni Cabiddu  * algorithms and core processing logic
402ebda74fSGiovanni Cabiddu  *
411ab53a77SGiovanni Cabiddu  * @compress:		Function performs a compress operation
421ab53a77SGiovanni Cabiddu  * @decompress:		Function performs a de-compress operation
431ab53a77SGiovanni Cabiddu  * @dst_free:		Frees destination buffer if allocated inside the
441ab53a77SGiovanni Cabiddu  *			algorithm
451ab53a77SGiovanni Cabiddu  * @reqsize:		Context size for (de)compression requests
462ebda74fSGiovanni Cabiddu  * @base:		Common crypto API algorithm data structure
472ebda74fSGiovanni Cabiddu  */
482ebda74fSGiovanni Cabiddu struct crypto_acomp {
491ab53a77SGiovanni Cabiddu 	int (*compress)(struct acomp_req *req);
501ab53a77SGiovanni Cabiddu 	int (*decompress)(struct acomp_req *req);
511ab53a77SGiovanni Cabiddu 	void (*dst_free)(struct scatterlist *dst);
521ab53a77SGiovanni Cabiddu 	unsigned int reqsize;
532ebda74fSGiovanni Cabiddu 	struct crypto_tfm base;
542ebda74fSGiovanni Cabiddu };
552ebda74fSGiovanni Cabiddu 
562ebda74fSGiovanni Cabiddu /**
572ebda74fSGiovanni Cabiddu  * struct acomp_alg - asynchronous compression algorithm
582ebda74fSGiovanni Cabiddu  *
592ebda74fSGiovanni Cabiddu  * @compress:	Function performs a compress operation
602ebda74fSGiovanni Cabiddu  * @decompress:	Function performs a de-compress operation
612ebda74fSGiovanni Cabiddu  * @dst_free:	Frees destination buffer if allocated inside the algorithm
622ebda74fSGiovanni Cabiddu  * @init:	Initialize the cryptographic transformation object.
632ebda74fSGiovanni Cabiddu  *		This function is used to initialize the cryptographic
642ebda74fSGiovanni Cabiddu  *		transformation object. This function is called only once at
652ebda74fSGiovanni Cabiddu  *		the instantiation time, right after the transformation context
662ebda74fSGiovanni Cabiddu  *		was allocated. In case the cryptographic hardware has some
672ebda74fSGiovanni Cabiddu  *		special requirements which need to be handled by software, this
682ebda74fSGiovanni Cabiddu  *		function shall check for the precise requirement of the
692ebda74fSGiovanni Cabiddu  *		transformation and put any software fallbacks in place.
702ebda74fSGiovanni Cabiddu  * @exit:	Deinitialize the cryptographic transformation object. This is a
712ebda74fSGiovanni Cabiddu  *		counterpart to @init, used to remove various changes set in
722ebda74fSGiovanni Cabiddu  *		@init.
732ebda74fSGiovanni Cabiddu  *
742ebda74fSGiovanni Cabiddu  * @reqsize:	Context size for (de)compression requests
752ebda74fSGiovanni Cabiddu  * @base:	Common crypto API algorithm data structure
762ebda74fSGiovanni Cabiddu  */
772ebda74fSGiovanni Cabiddu struct acomp_alg {
782ebda74fSGiovanni Cabiddu 	int (*compress)(struct acomp_req *req);
792ebda74fSGiovanni Cabiddu 	int (*decompress)(struct acomp_req *req);
802ebda74fSGiovanni Cabiddu 	void (*dst_free)(struct scatterlist *dst);
812ebda74fSGiovanni Cabiddu 	int (*init)(struct crypto_acomp *tfm);
822ebda74fSGiovanni Cabiddu 	void (*exit)(struct crypto_acomp *tfm);
832ebda74fSGiovanni Cabiddu 	unsigned int reqsize;
842ebda74fSGiovanni Cabiddu 	struct crypto_alg base;
852ebda74fSGiovanni Cabiddu };
862ebda74fSGiovanni Cabiddu 
872ebda74fSGiovanni Cabiddu /**
882ebda74fSGiovanni Cabiddu  * DOC: Asynchronous Compression API
892ebda74fSGiovanni Cabiddu  *
902ebda74fSGiovanni Cabiddu  * The Asynchronous Compression API is used with the algorithms of type
912ebda74fSGiovanni Cabiddu  * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
922ebda74fSGiovanni Cabiddu  */
932ebda74fSGiovanni Cabiddu 
942ebda74fSGiovanni Cabiddu /**
952ebda74fSGiovanni Cabiddu  * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
962ebda74fSGiovanni Cabiddu  * @alg_name:	is the cra_name / name or cra_driver_name / driver name of the
972ebda74fSGiovanni Cabiddu  *		compression algorithm e.g. "deflate"
982ebda74fSGiovanni Cabiddu  * @type:	specifies the type of the algorithm
992ebda74fSGiovanni Cabiddu  * @mask:	specifies the mask for the algorithm
1002ebda74fSGiovanni Cabiddu  *
1012ebda74fSGiovanni Cabiddu  * Allocate a handle for a compression algorithm. The returned struct
1022ebda74fSGiovanni Cabiddu  * crypto_acomp is the handle that is required for any subsequent
1032ebda74fSGiovanni Cabiddu  * API invocation for the compression operations.
1042ebda74fSGiovanni Cabiddu  *
1052ebda74fSGiovanni Cabiddu  * Return:	allocated handle in case of success; IS_ERR() is true in case
1062ebda74fSGiovanni Cabiddu  *		of an error, PTR_ERR() returns the error code.
1072ebda74fSGiovanni Cabiddu  */
1082ebda74fSGiovanni Cabiddu struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
1092ebda74fSGiovanni Cabiddu 					u32 mask);
1107bc13b5bSBarry Song /**
1117bc13b5bSBarry Song  * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
1127bc13b5bSBarry Song  * @alg_name:	is the cra_name / name or cra_driver_name / driver name of the
1137bc13b5bSBarry Song  *		compression algorithm e.g. "deflate"
1147bc13b5bSBarry Song  * @type:	specifies the type of the algorithm
1157bc13b5bSBarry Song  * @mask:	specifies the mask for the algorithm
1167bc13b5bSBarry Song  * @node:	specifies the NUMA node the ZIP hardware belongs to
1177bc13b5bSBarry Song  *
1187bc13b5bSBarry Song  * Allocate a handle for a compression algorithm. Drivers should try to use
1197bc13b5bSBarry Song  * (de)compressors on the specified NUMA node.
1207bc13b5bSBarry Song  * The returned struct crypto_acomp is the handle that is required for any
1217bc13b5bSBarry Song  * subsequent API invocation for the compression operations.
1227bc13b5bSBarry Song  *
1237bc13b5bSBarry Song  * Return:	allocated handle in case of success; IS_ERR() is true in case
1247bc13b5bSBarry Song  *		of an error, PTR_ERR() returns the error code.
1257bc13b5bSBarry Song  */
1267bc13b5bSBarry Song struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
1277bc13b5bSBarry Song 					u32 mask, int node);
1282ebda74fSGiovanni Cabiddu 
1292ebda74fSGiovanni Cabiddu static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
1302ebda74fSGiovanni Cabiddu {
1312ebda74fSGiovanni Cabiddu 	return &tfm->base;
1322ebda74fSGiovanni Cabiddu }
1332ebda74fSGiovanni Cabiddu 
1342ebda74fSGiovanni Cabiddu static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
1352ebda74fSGiovanni Cabiddu {
1362ebda74fSGiovanni Cabiddu 	return container_of(alg, struct acomp_alg, base);
1372ebda74fSGiovanni Cabiddu }
1382ebda74fSGiovanni Cabiddu 
1392ebda74fSGiovanni Cabiddu static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
1402ebda74fSGiovanni Cabiddu {
1412ebda74fSGiovanni Cabiddu 	return container_of(tfm, struct crypto_acomp, base);
1422ebda74fSGiovanni Cabiddu }
1432ebda74fSGiovanni Cabiddu 
1442ebda74fSGiovanni Cabiddu static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
1452ebda74fSGiovanni Cabiddu {
1462ebda74fSGiovanni Cabiddu 	return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
1472ebda74fSGiovanni Cabiddu }
1482ebda74fSGiovanni Cabiddu 
1492ebda74fSGiovanni Cabiddu static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
1502ebda74fSGiovanni Cabiddu {
1511ab53a77SGiovanni Cabiddu 	return tfm->reqsize;
1522ebda74fSGiovanni Cabiddu }
1532ebda74fSGiovanni Cabiddu 
1542ebda74fSGiovanni Cabiddu static inline void acomp_request_set_tfm(struct acomp_req *req,
1552ebda74fSGiovanni Cabiddu 					 struct crypto_acomp *tfm)
1562ebda74fSGiovanni Cabiddu {
1572ebda74fSGiovanni Cabiddu 	req->base.tfm = crypto_acomp_tfm(tfm);
1582ebda74fSGiovanni Cabiddu }
1592ebda74fSGiovanni Cabiddu 
1602ebda74fSGiovanni Cabiddu static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
1612ebda74fSGiovanni Cabiddu {
1622ebda74fSGiovanni Cabiddu 	return __crypto_acomp_tfm(req->base.tfm);
1632ebda74fSGiovanni Cabiddu }
1642ebda74fSGiovanni Cabiddu 
1652ebda74fSGiovanni Cabiddu /**
1662ebda74fSGiovanni Cabiddu  * crypto_free_acomp() -- free ACOMPRESS tfm handle
1672ebda74fSGiovanni Cabiddu  *
1682ebda74fSGiovanni Cabiddu  * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
16983681f2bSArd Biesheuvel  *
17083681f2bSArd Biesheuvel  * If @tfm is a NULL or error pointer, this function does nothing.
1712ebda74fSGiovanni Cabiddu  */
1722ebda74fSGiovanni Cabiddu static inline void crypto_free_acomp(struct crypto_acomp *tfm)
1732ebda74fSGiovanni Cabiddu {
1742ebda74fSGiovanni Cabiddu 	crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
1752ebda74fSGiovanni Cabiddu }
1762ebda74fSGiovanni Cabiddu 
1772ebda74fSGiovanni Cabiddu static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
1782ebda74fSGiovanni Cabiddu {
1792ebda74fSGiovanni Cabiddu 	type &= ~CRYPTO_ALG_TYPE_MASK;
1802ebda74fSGiovanni Cabiddu 	type |= CRYPTO_ALG_TYPE_ACOMPRESS;
181c5492269SBarry Song 	mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
1822ebda74fSGiovanni Cabiddu 
1832ebda74fSGiovanni Cabiddu 	return crypto_has_alg(alg_name, type, mask);
1842ebda74fSGiovanni Cabiddu }
1852ebda74fSGiovanni Cabiddu 
1862ebda74fSGiovanni Cabiddu /**
1872ebda74fSGiovanni Cabiddu  * acomp_request_alloc() -- allocates asynchronous (de)compression request
1882ebda74fSGiovanni Cabiddu  *
1892ebda74fSGiovanni Cabiddu  * @tfm:	ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
1902ebda74fSGiovanni Cabiddu  *
1912ebda74fSGiovanni Cabiddu  * Return:	allocated handle in case of success or NULL in case of an error
1922ebda74fSGiovanni Cabiddu  */
1931ab53a77SGiovanni Cabiddu struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
1942ebda74fSGiovanni Cabiddu 
1952ebda74fSGiovanni Cabiddu /**
1962ebda74fSGiovanni Cabiddu  * acomp_request_free() -- zeroize and free asynchronous (de)compression
1972ebda74fSGiovanni Cabiddu  *			   request as well as the output buffer if allocated
1982ebda74fSGiovanni Cabiddu  *			   inside the algorithm
1992ebda74fSGiovanni Cabiddu  *
2002ebda74fSGiovanni Cabiddu  * @req:	request to free
2012ebda74fSGiovanni Cabiddu  */
2021ab53a77SGiovanni Cabiddu void acomp_request_free(struct acomp_req *req);
2032ebda74fSGiovanni Cabiddu 
2042ebda74fSGiovanni Cabiddu /**
2052ebda74fSGiovanni Cabiddu  * acomp_request_set_callback() -- Sets an asynchronous callback
2062ebda74fSGiovanni Cabiddu  *
2072ebda74fSGiovanni Cabiddu  * Callback will be called when an asynchronous operation on a given
2082ebda74fSGiovanni Cabiddu  * request is finished.
2092ebda74fSGiovanni Cabiddu  *
2102ebda74fSGiovanni Cabiddu  * @req:	request that the callback will be set for
2112ebda74fSGiovanni Cabiddu  * @flgs:	specify for instance if the operation may backlog
2122ebda74fSGiovanni Cabiddu  * @cmlp:	callback which will be called
2132ebda74fSGiovanni Cabiddu  * @data:	private data used by the caller
2142ebda74fSGiovanni Cabiddu  */
2152ebda74fSGiovanni Cabiddu static inline void acomp_request_set_callback(struct acomp_req *req,
2162ebda74fSGiovanni Cabiddu 					      u32 flgs,
2172ebda74fSGiovanni Cabiddu 					      crypto_completion_t cmpl,
2182ebda74fSGiovanni Cabiddu 					      void *data)
2192ebda74fSGiovanni Cabiddu {
2202ebda74fSGiovanni Cabiddu 	req->base.complete = cmpl;
2212ebda74fSGiovanni Cabiddu 	req->base.data = data;
222*4c9edf17SHerbert Xu 	req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT;
223*4c9edf17SHerbert Xu 	req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT;
2242ebda74fSGiovanni Cabiddu }
2252ebda74fSGiovanni Cabiddu 
2262ebda74fSGiovanni Cabiddu /**
2272ebda74fSGiovanni Cabiddu  * acomp_request_set_params() -- Sets request parameters
2282ebda74fSGiovanni Cabiddu  *
2292ebda74fSGiovanni Cabiddu  * Sets parameters required by an acomp operation
2302ebda74fSGiovanni Cabiddu  *
2312ebda74fSGiovanni Cabiddu  * @req:	asynchronous compress request
2322ebda74fSGiovanni Cabiddu  * @src:	pointer to input buffer scatterlist
2332ebda74fSGiovanni Cabiddu  * @dst:	pointer to output buffer scatterlist. If this is NULL, the
2342ebda74fSGiovanni Cabiddu  *		acomp layer will allocate the output memory
2352ebda74fSGiovanni Cabiddu  * @slen:	size of the input buffer
2362ebda74fSGiovanni Cabiddu  * @dlen:	size of the output buffer. If dst is NULL, this can be used by
2372ebda74fSGiovanni Cabiddu  *		the user to specify the maximum amount of memory to allocate
2382ebda74fSGiovanni Cabiddu  */
2392ebda74fSGiovanni Cabiddu static inline void acomp_request_set_params(struct acomp_req *req,
2402ebda74fSGiovanni Cabiddu 					    struct scatterlist *src,
2412ebda74fSGiovanni Cabiddu 					    struct scatterlist *dst,
2422ebda74fSGiovanni Cabiddu 					    unsigned int slen,
2432ebda74fSGiovanni Cabiddu 					    unsigned int dlen)
2442ebda74fSGiovanni Cabiddu {
2452ebda74fSGiovanni Cabiddu 	req->src = src;
2462ebda74fSGiovanni Cabiddu 	req->dst = dst;
2472ebda74fSGiovanni Cabiddu 	req->slen = slen;
2482ebda74fSGiovanni Cabiddu 	req->dlen = dlen;
2492ebda74fSGiovanni Cabiddu 
250*4c9edf17SHerbert Xu 	req->flags &= ~CRYPTO_ACOMP_ALLOC_OUTPUT;
2512ebda74fSGiovanni Cabiddu 	if (!req->dst)
2522ebda74fSGiovanni Cabiddu 		req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
2532ebda74fSGiovanni Cabiddu }
2542ebda74fSGiovanni Cabiddu 
2552ebda74fSGiovanni Cabiddu /**
2562ebda74fSGiovanni Cabiddu  * crypto_acomp_compress() -- Invoke asynchronous compress operation
2572ebda74fSGiovanni Cabiddu  *
2582ebda74fSGiovanni Cabiddu  * Function invokes the asynchronous compress operation
2592ebda74fSGiovanni Cabiddu  *
2602ebda74fSGiovanni Cabiddu  * @req:	asynchronous compress request
2612ebda74fSGiovanni Cabiddu  *
2622ebda74fSGiovanni Cabiddu  * Return:	zero on success; error code in case of error
2632ebda74fSGiovanni Cabiddu  */
2642ebda74fSGiovanni Cabiddu static inline int crypto_acomp_compress(struct acomp_req *req)
2652ebda74fSGiovanni Cabiddu {
2662ebda74fSGiovanni Cabiddu 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
267f7d76e05SCorentin Labbe 	struct crypto_alg *alg = tfm->base.__crt_alg;
268f7d76e05SCorentin Labbe 	unsigned int slen = req->slen;
269cac5818cSCorentin Labbe 	int ret;
2702ebda74fSGiovanni Cabiddu 
271f7d76e05SCorentin Labbe 	crypto_stats_get(alg);
272cac5818cSCorentin Labbe 	ret = tfm->compress(req);
273f7d76e05SCorentin Labbe 	crypto_stats_compress(slen, ret, alg);
274cac5818cSCorentin Labbe 	return ret;
2752ebda74fSGiovanni Cabiddu }
2762ebda74fSGiovanni Cabiddu 
2772ebda74fSGiovanni Cabiddu /**
2782ebda74fSGiovanni Cabiddu  * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
2792ebda74fSGiovanni Cabiddu  *
2802ebda74fSGiovanni Cabiddu  * Function invokes the asynchronous decompress operation
2812ebda74fSGiovanni Cabiddu  *
2822ebda74fSGiovanni Cabiddu  * @req:	asynchronous compress request
2832ebda74fSGiovanni Cabiddu  *
2842ebda74fSGiovanni Cabiddu  * Return:	zero on success; error code in case of error
2852ebda74fSGiovanni Cabiddu  */
2862ebda74fSGiovanni Cabiddu static inline int crypto_acomp_decompress(struct acomp_req *req)
2872ebda74fSGiovanni Cabiddu {
2882ebda74fSGiovanni Cabiddu 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
289f7d76e05SCorentin Labbe 	struct crypto_alg *alg = tfm->base.__crt_alg;
290f7d76e05SCorentin Labbe 	unsigned int slen = req->slen;
291cac5818cSCorentin Labbe 	int ret;
2922ebda74fSGiovanni Cabiddu 
293f7d76e05SCorentin Labbe 	crypto_stats_get(alg);
294cac5818cSCorentin Labbe 	ret = tfm->decompress(req);
295f7d76e05SCorentin Labbe 	crypto_stats_decompress(slen, ret, alg);
296cac5818cSCorentin Labbe 	return ret;
2972ebda74fSGiovanni Cabiddu }
2982ebda74fSGiovanni Cabiddu 
2992ebda74fSGiovanni Cabiddu #endif
300