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 142ebda74fSGiovanni Cabiddu 152ebda74fSGiovanni Cabiddu /** 162ebda74fSGiovanni Cabiddu * struct acomp_req - asynchronous (de)compression request 172ebda74fSGiovanni Cabiddu * 182ebda74fSGiovanni Cabiddu * @base: Common attributes for asynchronous crypto requests 192ebda74fSGiovanni Cabiddu * @src: Source Data 202ebda74fSGiovanni Cabiddu * @dst: Destination data 212ebda74fSGiovanni Cabiddu * @slen: Size of the input buffer 222ebda74fSGiovanni Cabiddu * @dlen: Size of the output buffer and number of bytes produced 232ebda74fSGiovanni Cabiddu * @flags: Internal flags 242ebda74fSGiovanni Cabiddu * @__ctx: Start of private context data 252ebda74fSGiovanni Cabiddu */ 262ebda74fSGiovanni Cabiddu struct acomp_req { 272ebda74fSGiovanni Cabiddu struct crypto_async_request base; 282ebda74fSGiovanni Cabiddu struct scatterlist *src; 292ebda74fSGiovanni Cabiddu struct scatterlist *dst; 302ebda74fSGiovanni Cabiddu unsigned int slen; 312ebda74fSGiovanni Cabiddu unsigned int dlen; 322ebda74fSGiovanni Cabiddu u32 flags; 332ebda74fSGiovanni Cabiddu void *__ctx[] CRYPTO_MINALIGN_ATTR; 342ebda74fSGiovanni Cabiddu }; 352ebda74fSGiovanni Cabiddu 362ebda74fSGiovanni Cabiddu /** 372ebda74fSGiovanni Cabiddu * struct crypto_acomp - user-instantiated objects which encapsulate 382ebda74fSGiovanni Cabiddu * algorithms and core processing logic 392ebda74fSGiovanni Cabiddu * 401ab53a77SGiovanni Cabiddu * @compress: Function performs a compress operation 411ab53a77SGiovanni Cabiddu * @decompress: Function performs a de-compress operation 421ab53a77SGiovanni Cabiddu * @dst_free: Frees destination buffer if allocated inside the 431ab53a77SGiovanni Cabiddu * algorithm 441ab53a77SGiovanni Cabiddu * @reqsize: Context size for (de)compression requests 452ebda74fSGiovanni Cabiddu * @base: Common crypto API algorithm data structure 462ebda74fSGiovanni Cabiddu */ 472ebda74fSGiovanni Cabiddu struct crypto_acomp { 481ab53a77SGiovanni Cabiddu int (*compress)(struct acomp_req *req); 491ab53a77SGiovanni Cabiddu int (*decompress)(struct acomp_req *req); 501ab53a77SGiovanni Cabiddu void (*dst_free)(struct scatterlist *dst); 511ab53a77SGiovanni Cabiddu unsigned int reqsize; 522ebda74fSGiovanni Cabiddu struct crypto_tfm base; 532ebda74fSGiovanni Cabiddu }; 542ebda74fSGiovanni Cabiddu 552ebda74fSGiovanni Cabiddu /** 562ebda74fSGiovanni Cabiddu * struct acomp_alg - asynchronous compression algorithm 572ebda74fSGiovanni Cabiddu * 582ebda74fSGiovanni Cabiddu * @compress: Function performs a compress operation 592ebda74fSGiovanni Cabiddu * @decompress: Function performs a de-compress operation 602ebda74fSGiovanni Cabiddu * @dst_free: Frees destination buffer if allocated inside the algorithm 612ebda74fSGiovanni Cabiddu * @init: Initialize the cryptographic transformation object. 622ebda74fSGiovanni Cabiddu * This function is used to initialize the cryptographic 632ebda74fSGiovanni Cabiddu * transformation object. This function is called only once at 642ebda74fSGiovanni Cabiddu * the instantiation time, right after the transformation context 652ebda74fSGiovanni Cabiddu * was allocated. In case the cryptographic hardware has some 662ebda74fSGiovanni Cabiddu * special requirements which need to be handled by software, this 672ebda74fSGiovanni Cabiddu * function shall check for the precise requirement of the 682ebda74fSGiovanni Cabiddu * transformation and put any software fallbacks in place. 692ebda74fSGiovanni Cabiddu * @exit: Deinitialize the cryptographic transformation object. This is a 702ebda74fSGiovanni Cabiddu * counterpart to @init, used to remove various changes set in 712ebda74fSGiovanni Cabiddu * @init. 722ebda74fSGiovanni Cabiddu * 732ebda74fSGiovanni Cabiddu * @reqsize: Context size for (de)compression requests 742ebda74fSGiovanni Cabiddu * @base: Common crypto API algorithm data structure 752ebda74fSGiovanni Cabiddu */ 762ebda74fSGiovanni Cabiddu struct acomp_alg { 772ebda74fSGiovanni Cabiddu int (*compress)(struct acomp_req *req); 782ebda74fSGiovanni Cabiddu int (*decompress)(struct acomp_req *req); 792ebda74fSGiovanni Cabiddu void (*dst_free)(struct scatterlist *dst); 802ebda74fSGiovanni Cabiddu int (*init)(struct crypto_acomp *tfm); 812ebda74fSGiovanni Cabiddu void (*exit)(struct crypto_acomp *tfm); 822ebda74fSGiovanni Cabiddu unsigned int reqsize; 832ebda74fSGiovanni Cabiddu struct crypto_alg base; 842ebda74fSGiovanni Cabiddu }; 852ebda74fSGiovanni Cabiddu 862ebda74fSGiovanni Cabiddu /** 872ebda74fSGiovanni Cabiddu * DOC: Asynchronous Compression API 882ebda74fSGiovanni Cabiddu * 892ebda74fSGiovanni Cabiddu * The Asynchronous Compression API is used with the algorithms of type 902ebda74fSGiovanni Cabiddu * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto) 912ebda74fSGiovanni Cabiddu */ 922ebda74fSGiovanni Cabiddu 932ebda74fSGiovanni Cabiddu /** 942ebda74fSGiovanni Cabiddu * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle 952ebda74fSGiovanni Cabiddu * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 962ebda74fSGiovanni Cabiddu * compression algorithm e.g. "deflate" 972ebda74fSGiovanni Cabiddu * @type: specifies the type of the algorithm 982ebda74fSGiovanni Cabiddu * @mask: specifies the mask for the algorithm 992ebda74fSGiovanni Cabiddu * 1002ebda74fSGiovanni Cabiddu * Allocate a handle for a compression algorithm. The returned struct 1012ebda74fSGiovanni Cabiddu * crypto_acomp is the handle that is required for any subsequent 1022ebda74fSGiovanni Cabiddu * API invocation for the compression operations. 1032ebda74fSGiovanni Cabiddu * 1042ebda74fSGiovanni Cabiddu * Return: allocated handle in case of success; IS_ERR() is true in case 1052ebda74fSGiovanni Cabiddu * of an error, PTR_ERR() returns the error code. 1062ebda74fSGiovanni Cabiddu */ 1072ebda74fSGiovanni Cabiddu struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type, 1082ebda74fSGiovanni Cabiddu u32 mask); 1097bc13b5bSBarry Song /** 1107bc13b5bSBarry Song * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node 1117bc13b5bSBarry Song * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1127bc13b5bSBarry Song * compression algorithm e.g. "deflate" 1137bc13b5bSBarry Song * @type: specifies the type of the algorithm 1147bc13b5bSBarry Song * @mask: specifies the mask for the algorithm 1157bc13b5bSBarry Song * @node: specifies the NUMA node the ZIP hardware belongs to 1167bc13b5bSBarry Song * 1177bc13b5bSBarry Song * Allocate a handle for a compression algorithm. Drivers should try to use 1187bc13b5bSBarry Song * (de)compressors on the specified NUMA node. 1197bc13b5bSBarry Song * The returned struct crypto_acomp is the handle that is required for any 1207bc13b5bSBarry Song * subsequent API invocation for the compression operations. 1217bc13b5bSBarry Song * 1227bc13b5bSBarry Song * Return: allocated handle in case of success; IS_ERR() is true in case 1237bc13b5bSBarry Song * of an error, PTR_ERR() returns the error code. 1247bc13b5bSBarry Song */ 1257bc13b5bSBarry Song struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type, 1267bc13b5bSBarry Song u32 mask, int node); 1272ebda74fSGiovanni Cabiddu 1282ebda74fSGiovanni Cabiddu static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm) 1292ebda74fSGiovanni Cabiddu { 1302ebda74fSGiovanni Cabiddu return &tfm->base; 1312ebda74fSGiovanni Cabiddu } 1322ebda74fSGiovanni Cabiddu 1332ebda74fSGiovanni Cabiddu static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg) 1342ebda74fSGiovanni Cabiddu { 1352ebda74fSGiovanni Cabiddu return container_of(alg, struct acomp_alg, base); 1362ebda74fSGiovanni Cabiddu } 1372ebda74fSGiovanni Cabiddu 1382ebda74fSGiovanni Cabiddu static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm) 1392ebda74fSGiovanni Cabiddu { 1402ebda74fSGiovanni Cabiddu return container_of(tfm, struct crypto_acomp, base); 1412ebda74fSGiovanni Cabiddu } 1422ebda74fSGiovanni Cabiddu 1432ebda74fSGiovanni Cabiddu static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm) 1442ebda74fSGiovanni Cabiddu { 1452ebda74fSGiovanni Cabiddu return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg); 1462ebda74fSGiovanni Cabiddu } 1472ebda74fSGiovanni Cabiddu 1482ebda74fSGiovanni Cabiddu static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm) 1492ebda74fSGiovanni Cabiddu { 1501ab53a77SGiovanni Cabiddu return tfm->reqsize; 1512ebda74fSGiovanni Cabiddu } 1522ebda74fSGiovanni Cabiddu 1532ebda74fSGiovanni Cabiddu static inline void acomp_request_set_tfm(struct acomp_req *req, 1542ebda74fSGiovanni Cabiddu struct crypto_acomp *tfm) 1552ebda74fSGiovanni Cabiddu { 1562ebda74fSGiovanni Cabiddu req->base.tfm = crypto_acomp_tfm(tfm); 1572ebda74fSGiovanni Cabiddu } 1582ebda74fSGiovanni Cabiddu 1592ebda74fSGiovanni Cabiddu static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req) 1602ebda74fSGiovanni Cabiddu { 1612ebda74fSGiovanni Cabiddu return __crypto_acomp_tfm(req->base.tfm); 1622ebda74fSGiovanni Cabiddu } 1632ebda74fSGiovanni Cabiddu 1642ebda74fSGiovanni Cabiddu /** 1652ebda74fSGiovanni Cabiddu * crypto_free_acomp() -- free ACOMPRESS tfm handle 1662ebda74fSGiovanni Cabiddu * 1672ebda74fSGiovanni Cabiddu * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 168*83681f2bSArd Biesheuvel * 169*83681f2bSArd Biesheuvel * If @tfm is a NULL or error pointer, this function does nothing. 1702ebda74fSGiovanni Cabiddu */ 1712ebda74fSGiovanni Cabiddu static inline void crypto_free_acomp(struct crypto_acomp *tfm) 1722ebda74fSGiovanni Cabiddu { 1732ebda74fSGiovanni Cabiddu crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm)); 1742ebda74fSGiovanni Cabiddu } 1752ebda74fSGiovanni Cabiddu 1762ebda74fSGiovanni Cabiddu static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask) 1772ebda74fSGiovanni Cabiddu { 1782ebda74fSGiovanni Cabiddu type &= ~CRYPTO_ALG_TYPE_MASK; 1792ebda74fSGiovanni Cabiddu type |= CRYPTO_ALG_TYPE_ACOMPRESS; 180c5492269SBarry Song mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK; 1812ebda74fSGiovanni Cabiddu 1822ebda74fSGiovanni Cabiddu return crypto_has_alg(alg_name, type, mask); 1832ebda74fSGiovanni Cabiddu } 1842ebda74fSGiovanni Cabiddu 1852ebda74fSGiovanni Cabiddu /** 1862ebda74fSGiovanni Cabiddu * acomp_request_alloc() -- allocates asynchronous (de)compression request 1872ebda74fSGiovanni Cabiddu * 1882ebda74fSGiovanni Cabiddu * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 1892ebda74fSGiovanni Cabiddu * 1902ebda74fSGiovanni Cabiddu * Return: allocated handle in case of success or NULL in case of an error 1912ebda74fSGiovanni Cabiddu */ 1921ab53a77SGiovanni Cabiddu struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm); 1932ebda74fSGiovanni Cabiddu 1942ebda74fSGiovanni Cabiddu /** 1952ebda74fSGiovanni Cabiddu * acomp_request_free() -- zeroize and free asynchronous (de)compression 1962ebda74fSGiovanni Cabiddu * request as well as the output buffer if allocated 1972ebda74fSGiovanni Cabiddu * inside the algorithm 1982ebda74fSGiovanni Cabiddu * 1992ebda74fSGiovanni Cabiddu * @req: request to free 2002ebda74fSGiovanni Cabiddu */ 2011ab53a77SGiovanni Cabiddu void acomp_request_free(struct acomp_req *req); 2022ebda74fSGiovanni Cabiddu 2032ebda74fSGiovanni Cabiddu /** 2042ebda74fSGiovanni Cabiddu * acomp_request_set_callback() -- Sets an asynchronous callback 2052ebda74fSGiovanni Cabiddu * 2062ebda74fSGiovanni Cabiddu * Callback will be called when an asynchronous operation on a given 2072ebda74fSGiovanni Cabiddu * request is finished. 2082ebda74fSGiovanni Cabiddu * 2092ebda74fSGiovanni Cabiddu * @req: request that the callback will be set for 2102ebda74fSGiovanni Cabiddu * @flgs: specify for instance if the operation may backlog 2112ebda74fSGiovanni Cabiddu * @cmlp: callback which will be called 2122ebda74fSGiovanni Cabiddu * @data: private data used by the caller 2132ebda74fSGiovanni Cabiddu */ 2142ebda74fSGiovanni Cabiddu static inline void acomp_request_set_callback(struct acomp_req *req, 2152ebda74fSGiovanni Cabiddu u32 flgs, 2162ebda74fSGiovanni Cabiddu crypto_completion_t cmpl, 2172ebda74fSGiovanni Cabiddu void *data) 2182ebda74fSGiovanni Cabiddu { 2192ebda74fSGiovanni Cabiddu req->base.complete = cmpl; 2202ebda74fSGiovanni Cabiddu req->base.data = data; 2212ebda74fSGiovanni Cabiddu req->base.flags = flgs; 2222ebda74fSGiovanni Cabiddu } 2232ebda74fSGiovanni Cabiddu 2242ebda74fSGiovanni Cabiddu /** 2252ebda74fSGiovanni Cabiddu * acomp_request_set_params() -- Sets request parameters 2262ebda74fSGiovanni Cabiddu * 2272ebda74fSGiovanni Cabiddu * Sets parameters required by an acomp operation 2282ebda74fSGiovanni Cabiddu * 2292ebda74fSGiovanni Cabiddu * @req: asynchronous compress request 2302ebda74fSGiovanni Cabiddu * @src: pointer to input buffer scatterlist 2312ebda74fSGiovanni Cabiddu * @dst: pointer to output buffer scatterlist. If this is NULL, the 2322ebda74fSGiovanni Cabiddu * acomp layer will allocate the output memory 2332ebda74fSGiovanni Cabiddu * @slen: size of the input buffer 2342ebda74fSGiovanni Cabiddu * @dlen: size of the output buffer. If dst is NULL, this can be used by 2352ebda74fSGiovanni Cabiddu * the user to specify the maximum amount of memory to allocate 2362ebda74fSGiovanni Cabiddu */ 2372ebda74fSGiovanni Cabiddu static inline void acomp_request_set_params(struct acomp_req *req, 2382ebda74fSGiovanni Cabiddu struct scatterlist *src, 2392ebda74fSGiovanni Cabiddu struct scatterlist *dst, 2402ebda74fSGiovanni Cabiddu unsigned int slen, 2412ebda74fSGiovanni Cabiddu unsigned int dlen) 2422ebda74fSGiovanni Cabiddu { 2432ebda74fSGiovanni Cabiddu req->src = src; 2442ebda74fSGiovanni Cabiddu req->dst = dst; 2452ebda74fSGiovanni Cabiddu req->slen = slen; 2462ebda74fSGiovanni Cabiddu req->dlen = dlen; 2472ebda74fSGiovanni Cabiddu 2482ebda74fSGiovanni Cabiddu if (!req->dst) 2492ebda74fSGiovanni Cabiddu req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT; 2502ebda74fSGiovanni Cabiddu } 2512ebda74fSGiovanni Cabiddu 2522ebda74fSGiovanni Cabiddu /** 2532ebda74fSGiovanni Cabiddu * crypto_acomp_compress() -- Invoke asynchronous compress operation 2542ebda74fSGiovanni Cabiddu * 2552ebda74fSGiovanni Cabiddu * Function invokes the asynchronous compress operation 2562ebda74fSGiovanni Cabiddu * 2572ebda74fSGiovanni Cabiddu * @req: asynchronous compress request 2582ebda74fSGiovanni Cabiddu * 2592ebda74fSGiovanni Cabiddu * Return: zero on success; error code in case of error 2602ebda74fSGiovanni Cabiddu */ 2612ebda74fSGiovanni Cabiddu static inline int crypto_acomp_compress(struct acomp_req *req) 2622ebda74fSGiovanni Cabiddu { 2632ebda74fSGiovanni Cabiddu struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); 264f7d76e05SCorentin Labbe struct crypto_alg *alg = tfm->base.__crt_alg; 265f7d76e05SCorentin Labbe unsigned int slen = req->slen; 266cac5818cSCorentin Labbe int ret; 2672ebda74fSGiovanni Cabiddu 268f7d76e05SCorentin Labbe crypto_stats_get(alg); 269cac5818cSCorentin Labbe ret = tfm->compress(req); 270f7d76e05SCorentin Labbe crypto_stats_compress(slen, ret, alg); 271cac5818cSCorentin Labbe return ret; 2722ebda74fSGiovanni Cabiddu } 2732ebda74fSGiovanni Cabiddu 2742ebda74fSGiovanni Cabiddu /** 2752ebda74fSGiovanni Cabiddu * crypto_acomp_decompress() -- Invoke asynchronous decompress operation 2762ebda74fSGiovanni Cabiddu * 2772ebda74fSGiovanni Cabiddu * Function invokes the asynchronous decompress operation 2782ebda74fSGiovanni Cabiddu * 2792ebda74fSGiovanni Cabiddu * @req: asynchronous compress request 2802ebda74fSGiovanni Cabiddu * 2812ebda74fSGiovanni Cabiddu * Return: zero on success; error code in case of error 2822ebda74fSGiovanni Cabiddu */ 2832ebda74fSGiovanni Cabiddu static inline int crypto_acomp_decompress(struct acomp_req *req) 2842ebda74fSGiovanni Cabiddu { 2852ebda74fSGiovanni Cabiddu struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); 286f7d76e05SCorentin Labbe struct crypto_alg *alg = tfm->base.__crt_alg; 287f7d76e05SCorentin Labbe unsigned int slen = req->slen; 288cac5818cSCorentin Labbe int ret; 2892ebda74fSGiovanni Cabiddu 290f7d76e05SCorentin Labbe crypto_stats_get(alg); 291cac5818cSCorentin Labbe ret = tfm->decompress(req); 292f7d76e05SCorentin Labbe crypto_stats_decompress(slen, ret, alg); 293cac5818cSCorentin Labbe return ret; 2942ebda74fSGiovanni Cabiddu } 2952ebda74fSGiovanni Cabiddu 2962ebda74fSGiovanni Cabiddu #endif 297