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 11*0a742389SHerbert Xu 12*0a742389SHerbert Xu #include <linux/atomic.h> 13*0a742389SHerbert Xu #include <linux/container_of.h> 142ebda74fSGiovanni Cabiddu #include <linux/crypto.h> 152ebda74fSGiovanni Cabiddu 162ebda74fSGiovanni Cabiddu #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001 175fc8041eSGiovanni Cabiddu #define CRYPTO_ACOMP_DST_MAX 131072 182ebda74fSGiovanni Cabiddu 192ebda74fSGiovanni Cabiddu /** 202ebda74fSGiovanni Cabiddu * struct acomp_req - asynchronous (de)compression request 212ebda74fSGiovanni Cabiddu * 222ebda74fSGiovanni Cabiddu * @base: Common attributes for asynchronous crypto requests 232ebda74fSGiovanni Cabiddu * @src: Source Data 242ebda74fSGiovanni Cabiddu * @dst: Destination data 252ebda74fSGiovanni Cabiddu * @slen: Size of the input buffer 262ebda74fSGiovanni Cabiddu * @dlen: Size of the output buffer and number of bytes produced 272ebda74fSGiovanni Cabiddu * @flags: Internal flags 282ebda74fSGiovanni Cabiddu * @__ctx: Start of private context data 292ebda74fSGiovanni Cabiddu */ 302ebda74fSGiovanni Cabiddu struct acomp_req { 312ebda74fSGiovanni Cabiddu struct crypto_async_request base; 322ebda74fSGiovanni Cabiddu struct scatterlist *src; 332ebda74fSGiovanni Cabiddu struct scatterlist *dst; 342ebda74fSGiovanni Cabiddu unsigned int slen; 352ebda74fSGiovanni Cabiddu unsigned int dlen; 362ebda74fSGiovanni Cabiddu u32 flags; 372ebda74fSGiovanni Cabiddu void *__ctx[] CRYPTO_MINALIGN_ATTR; 382ebda74fSGiovanni Cabiddu }; 392ebda74fSGiovanni Cabiddu 402ebda74fSGiovanni Cabiddu /** 412ebda74fSGiovanni Cabiddu * struct crypto_acomp - user-instantiated objects which encapsulate 422ebda74fSGiovanni Cabiddu * algorithms and core processing logic 432ebda74fSGiovanni Cabiddu * 441ab53a77SGiovanni Cabiddu * @compress: Function performs a compress operation 451ab53a77SGiovanni Cabiddu * @decompress: Function performs a de-compress operation 461ab53a77SGiovanni Cabiddu * @dst_free: Frees destination buffer if allocated inside the 471ab53a77SGiovanni Cabiddu * algorithm 481ab53a77SGiovanni Cabiddu * @reqsize: Context size for (de)compression requests 492ebda74fSGiovanni Cabiddu * @base: Common crypto API algorithm data structure 502ebda74fSGiovanni Cabiddu */ 512ebda74fSGiovanni Cabiddu struct crypto_acomp { 521ab53a77SGiovanni Cabiddu int (*compress)(struct acomp_req *req); 531ab53a77SGiovanni Cabiddu int (*decompress)(struct acomp_req *req); 541ab53a77SGiovanni Cabiddu void (*dst_free)(struct scatterlist *dst); 551ab53a77SGiovanni Cabiddu unsigned int reqsize; 562ebda74fSGiovanni Cabiddu struct crypto_tfm base; 572ebda74fSGiovanni Cabiddu }; 582ebda74fSGiovanni Cabiddu 59*0a742389SHerbert Xu /* 60*0a742389SHerbert Xu * struct crypto_istat_compress - statistics for compress algorithm 61*0a742389SHerbert Xu * @compress_cnt: number of compress requests 62*0a742389SHerbert Xu * @compress_tlen: total data size handled by compress requests 63*0a742389SHerbert Xu * @decompress_cnt: number of decompress requests 64*0a742389SHerbert Xu * @decompress_tlen: total data size handled by decompress requests 65*0a742389SHerbert Xu * @err_cnt: number of error for compress requests 662ebda74fSGiovanni Cabiddu */ 67*0a742389SHerbert Xu struct crypto_istat_compress { 68*0a742389SHerbert Xu atomic64_t compress_cnt; 69*0a742389SHerbert Xu atomic64_t compress_tlen; 70*0a742389SHerbert Xu atomic64_t decompress_cnt; 71*0a742389SHerbert Xu atomic64_t decompress_tlen; 72*0a742389SHerbert Xu atomic64_t err_cnt; 732ebda74fSGiovanni Cabiddu }; 742ebda74fSGiovanni Cabiddu 75*0a742389SHerbert Xu #ifdef CONFIG_CRYPTO_STATS 76*0a742389SHerbert Xu #define COMP_ALG_COMMON_STATS struct crypto_istat_compress stat; 77*0a742389SHerbert Xu #else 78*0a742389SHerbert Xu #define COMP_ALG_COMMON_STATS 79*0a742389SHerbert Xu #endif 80*0a742389SHerbert Xu 81*0a742389SHerbert Xu #define COMP_ALG_COMMON { \ 82*0a742389SHerbert Xu COMP_ALG_COMMON_STATS \ 83*0a742389SHerbert Xu \ 84*0a742389SHerbert Xu struct crypto_alg base; \ 85*0a742389SHerbert Xu } 86*0a742389SHerbert Xu struct comp_alg_common COMP_ALG_COMMON; 87*0a742389SHerbert Xu 882ebda74fSGiovanni Cabiddu /** 892ebda74fSGiovanni Cabiddu * DOC: Asynchronous Compression API 902ebda74fSGiovanni Cabiddu * 912ebda74fSGiovanni Cabiddu * The Asynchronous Compression API is used with the algorithms of type 922ebda74fSGiovanni Cabiddu * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto) 932ebda74fSGiovanni Cabiddu */ 942ebda74fSGiovanni Cabiddu 952ebda74fSGiovanni Cabiddu /** 962ebda74fSGiovanni Cabiddu * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle 972ebda74fSGiovanni Cabiddu * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 982ebda74fSGiovanni Cabiddu * compression algorithm e.g. "deflate" 992ebda74fSGiovanni Cabiddu * @type: specifies the type of the algorithm 1002ebda74fSGiovanni Cabiddu * @mask: specifies the mask for the algorithm 1012ebda74fSGiovanni Cabiddu * 1022ebda74fSGiovanni Cabiddu * Allocate a handle for a compression algorithm. The returned struct 1032ebda74fSGiovanni Cabiddu * crypto_acomp is the handle that is required for any subsequent 1042ebda74fSGiovanni Cabiddu * API invocation for the compression operations. 1052ebda74fSGiovanni Cabiddu * 1062ebda74fSGiovanni Cabiddu * Return: allocated handle in case of success; IS_ERR() is true in case 1072ebda74fSGiovanni Cabiddu * of an error, PTR_ERR() returns the error code. 1082ebda74fSGiovanni Cabiddu */ 1092ebda74fSGiovanni Cabiddu struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type, 1102ebda74fSGiovanni Cabiddu u32 mask); 1117bc13b5bSBarry Song /** 1127bc13b5bSBarry Song * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node 1137bc13b5bSBarry Song * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1147bc13b5bSBarry Song * compression algorithm e.g. "deflate" 1157bc13b5bSBarry Song * @type: specifies the type of the algorithm 1167bc13b5bSBarry Song * @mask: specifies the mask for the algorithm 1177bc13b5bSBarry Song * @node: specifies the NUMA node the ZIP hardware belongs to 1187bc13b5bSBarry Song * 1197bc13b5bSBarry Song * Allocate a handle for a compression algorithm. Drivers should try to use 1207bc13b5bSBarry Song * (de)compressors on the specified NUMA node. 1217bc13b5bSBarry Song * The returned struct crypto_acomp is the handle that is required for any 1227bc13b5bSBarry Song * subsequent API invocation for the compression operations. 1237bc13b5bSBarry Song * 1247bc13b5bSBarry Song * Return: allocated handle in case of success; IS_ERR() is true in case 1257bc13b5bSBarry Song * of an error, PTR_ERR() returns the error code. 1267bc13b5bSBarry Song */ 1277bc13b5bSBarry Song struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type, 1287bc13b5bSBarry Song u32 mask, int node); 1292ebda74fSGiovanni Cabiddu 1302ebda74fSGiovanni Cabiddu static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm) 1312ebda74fSGiovanni Cabiddu { 1322ebda74fSGiovanni Cabiddu return &tfm->base; 1332ebda74fSGiovanni Cabiddu } 1342ebda74fSGiovanni Cabiddu 135*0a742389SHerbert Xu static inline struct comp_alg_common *__crypto_comp_alg_common( 136*0a742389SHerbert Xu struct crypto_alg *alg) 1372ebda74fSGiovanni Cabiddu { 138*0a742389SHerbert Xu return container_of(alg, struct comp_alg_common, base); 1392ebda74fSGiovanni Cabiddu } 1402ebda74fSGiovanni Cabiddu 1412ebda74fSGiovanni Cabiddu static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm) 1422ebda74fSGiovanni Cabiddu { 1432ebda74fSGiovanni Cabiddu return container_of(tfm, struct crypto_acomp, base); 1442ebda74fSGiovanni Cabiddu } 1452ebda74fSGiovanni Cabiddu 146*0a742389SHerbert Xu static inline struct comp_alg_common *crypto_comp_alg_common( 147*0a742389SHerbert Xu struct crypto_acomp *tfm) 1482ebda74fSGiovanni Cabiddu { 149*0a742389SHerbert Xu return __crypto_comp_alg_common(crypto_acomp_tfm(tfm)->__crt_alg); 1502ebda74fSGiovanni Cabiddu } 1512ebda74fSGiovanni Cabiddu 1522ebda74fSGiovanni Cabiddu static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm) 1532ebda74fSGiovanni Cabiddu { 1541ab53a77SGiovanni Cabiddu return tfm->reqsize; 1552ebda74fSGiovanni Cabiddu } 1562ebda74fSGiovanni Cabiddu 1572ebda74fSGiovanni Cabiddu static inline void acomp_request_set_tfm(struct acomp_req *req, 1582ebda74fSGiovanni Cabiddu struct crypto_acomp *tfm) 1592ebda74fSGiovanni Cabiddu { 1602ebda74fSGiovanni Cabiddu req->base.tfm = crypto_acomp_tfm(tfm); 1612ebda74fSGiovanni Cabiddu } 1622ebda74fSGiovanni Cabiddu 1632ebda74fSGiovanni Cabiddu static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req) 1642ebda74fSGiovanni Cabiddu { 1652ebda74fSGiovanni Cabiddu return __crypto_acomp_tfm(req->base.tfm); 1662ebda74fSGiovanni Cabiddu } 1672ebda74fSGiovanni Cabiddu 1682ebda74fSGiovanni Cabiddu /** 1692ebda74fSGiovanni Cabiddu * crypto_free_acomp() -- free ACOMPRESS tfm handle 1702ebda74fSGiovanni Cabiddu * 1712ebda74fSGiovanni Cabiddu * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 17283681f2bSArd Biesheuvel * 17383681f2bSArd Biesheuvel * If @tfm is a NULL or error pointer, this function does nothing. 1742ebda74fSGiovanni Cabiddu */ 1752ebda74fSGiovanni Cabiddu static inline void crypto_free_acomp(struct crypto_acomp *tfm) 1762ebda74fSGiovanni Cabiddu { 1772ebda74fSGiovanni Cabiddu crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm)); 1782ebda74fSGiovanni Cabiddu } 1792ebda74fSGiovanni Cabiddu 1802ebda74fSGiovanni Cabiddu static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask) 1812ebda74fSGiovanni Cabiddu { 1822ebda74fSGiovanni Cabiddu type &= ~CRYPTO_ALG_TYPE_MASK; 1832ebda74fSGiovanni Cabiddu type |= CRYPTO_ALG_TYPE_ACOMPRESS; 184c5492269SBarry Song mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK; 1852ebda74fSGiovanni Cabiddu 1862ebda74fSGiovanni Cabiddu return crypto_has_alg(alg_name, type, mask); 1872ebda74fSGiovanni Cabiddu } 1882ebda74fSGiovanni Cabiddu 1892ebda74fSGiovanni Cabiddu /** 1902ebda74fSGiovanni Cabiddu * acomp_request_alloc() -- allocates asynchronous (de)compression request 1912ebda74fSGiovanni Cabiddu * 1922ebda74fSGiovanni Cabiddu * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 1932ebda74fSGiovanni Cabiddu * 1942ebda74fSGiovanni Cabiddu * Return: allocated handle in case of success or NULL in case of an error 1952ebda74fSGiovanni Cabiddu */ 1961ab53a77SGiovanni Cabiddu struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm); 1972ebda74fSGiovanni Cabiddu 1982ebda74fSGiovanni Cabiddu /** 1992ebda74fSGiovanni Cabiddu * acomp_request_free() -- zeroize and free asynchronous (de)compression 2002ebda74fSGiovanni Cabiddu * request as well as the output buffer if allocated 2012ebda74fSGiovanni Cabiddu * inside the algorithm 2022ebda74fSGiovanni Cabiddu * 2032ebda74fSGiovanni Cabiddu * @req: request to free 2042ebda74fSGiovanni Cabiddu */ 2051ab53a77SGiovanni Cabiddu void acomp_request_free(struct acomp_req *req); 2062ebda74fSGiovanni Cabiddu 2072ebda74fSGiovanni Cabiddu /** 2082ebda74fSGiovanni Cabiddu * acomp_request_set_callback() -- Sets an asynchronous callback 2092ebda74fSGiovanni Cabiddu * 2102ebda74fSGiovanni Cabiddu * Callback will be called when an asynchronous operation on a given 2112ebda74fSGiovanni Cabiddu * request is finished. 2122ebda74fSGiovanni Cabiddu * 2132ebda74fSGiovanni Cabiddu * @req: request that the callback will be set for 2142ebda74fSGiovanni Cabiddu * @flgs: specify for instance if the operation may backlog 2152ebda74fSGiovanni Cabiddu * @cmlp: callback which will be called 2162ebda74fSGiovanni Cabiddu * @data: private data used by the caller 2172ebda74fSGiovanni Cabiddu */ 2182ebda74fSGiovanni Cabiddu static inline void acomp_request_set_callback(struct acomp_req *req, 2192ebda74fSGiovanni Cabiddu u32 flgs, 2202ebda74fSGiovanni Cabiddu crypto_completion_t cmpl, 2212ebda74fSGiovanni Cabiddu void *data) 2222ebda74fSGiovanni Cabiddu { 2232ebda74fSGiovanni Cabiddu req->base.complete = cmpl; 2242ebda74fSGiovanni Cabiddu req->base.data = data; 2254c9edf17SHerbert Xu req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT; 2264c9edf17SHerbert Xu req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT; 2272ebda74fSGiovanni Cabiddu } 2282ebda74fSGiovanni Cabiddu 2292ebda74fSGiovanni Cabiddu /** 2302ebda74fSGiovanni Cabiddu * acomp_request_set_params() -- Sets request parameters 2312ebda74fSGiovanni Cabiddu * 2322ebda74fSGiovanni Cabiddu * Sets parameters required by an acomp operation 2332ebda74fSGiovanni Cabiddu * 2342ebda74fSGiovanni Cabiddu * @req: asynchronous compress request 2352ebda74fSGiovanni Cabiddu * @src: pointer to input buffer scatterlist 2362ebda74fSGiovanni Cabiddu * @dst: pointer to output buffer scatterlist. If this is NULL, the 2372ebda74fSGiovanni Cabiddu * acomp layer will allocate the output memory 2382ebda74fSGiovanni Cabiddu * @slen: size of the input buffer 2392ebda74fSGiovanni Cabiddu * @dlen: size of the output buffer. If dst is NULL, this can be used by 2402ebda74fSGiovanni Cabiddu * the user to specify the maximum amount of memory to allocate 2412ebda74fSGiovanni Cabiddu */ 2422ebda74fSGiovanni Cabiddu static inline void acomp_request_set_params(struct acomp_req *req, 2432ebda74fSGiovanni Cabiddu struct scatterlist *src, 2442ebda74fSGiovanni Cabiddu struct scatterlist *dst, 2452ebda74fSGiovanni Cabiddu unsigned int slen, 2462ebda74fSGiovanni Cabiddu unsigned int dlen) 2472ebda74fSGiovanni Cabiddu { 2482ebda74fSGiovanni Cabiddu req->src = src; 2492ebda74fSGiovanni Cabiddu req->dst = dst; 2502ebda74fSGiovanni Cabiddu req->slen = slen; 2512ebda74fSGiovanni Cabiddu req->dlen = dlen; 2522ebda74fSGiovanni Cabiddu 2534c9edf17SHerbert Xu req->flags &= ~CRYPTO_ACOMP_ALLOC_OUTPUT; 2542ebda74fSGiovanni Cabiddu if (!req->dst) 2552ebda74fSGiovanni Cabiddu req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT; 2562ebda74fSGiovanni Cabiddu } 2572ebda74fSGiovanni Cabiddu 258*0a742389SHerbert Xu static inline struct crypto_istat_compress *comp_get_stat( 259*0a742389SHerbert Xu struct comp_alg_common *alg) 260*0a742389SHerbert Xu { 261*0a742389SHerbert Xu #ifdef CONFIG_CRYPTO_STATS 262*0a742389SHerbert Xu return &alg->stat; 263*0a742389SHerbert Xu #else 264*0a742389SHerbert Xu return NULL; 265*0a742389SHerbert Xu #endif 266*0a742389SHerbert Xu } 267*0a742389SHerbert Xu 268*0a742389SHerbert Xu static inline int crypto_comp_errstat(struct comp_alg_common *alg, int err) 269*0a742389SHerbert Xu { 270*0a742389SHerbert Xu if (!IS_ENABLED(CONFIG_CRYPTO_STATS)) 271*0a742389SHerbert Xu return err; 272*0a742389SHerbert Xu 273*0a742389SHerbert Xu if (err && err != -EINPROGRESS && err != -EBUSY) 274*0a742389SHerbert Xu atomic64_inc(&comp_get_stat(alg)->err_cnt); 275*0a742389SHerbert Xu 276*0a742389SHerbert Xu return err; 277*0a742389SHerbert Xu } 278*0a742389SHerbert Xu 2792ebda74fSGiovanni Cabiddu /** 2802ebda74fSGiovanni Cabiddu * crypto_acomp_compress() -- Invoke asynchronous compress operation 2812ebda74fSGiovanni Cabiddu * 2822ebda74fSGiovanni Cabiddu * Function invokes the asynchronous compress operation 2832ebda74fSGiovanni Cabiddu * 2842ebda74fSGiovanni Cabiddu * @req: asynchronous compress request 2852ebda74fSGiovanni Cabiddu * 2862ebda74fSGiovanni Cabiddu * Return: zero on success; error code in case of error 2872ebda74fSGiovanni Cabiddu */ 2882ebda74fSGiovanni Cabiddu static inline int crypto_acomp_compress(struct acomp_req *req) 2892ebda74fSGiovanni Cabiddu { 2902ebda74fSGiovanni Cabiddu struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); 291*0a742389SHerbert Xu struct comp_alg_common *alg; 2922ebda74fSGiovanni Cabiddu 293*0a742389SHerbert Xu alg = crypto_comp_alg_common(tfm); 294*0a742389SHerbert Xu 295*0a742389SHerbert Xu if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { 296*0a742389SHerbert Xu struct crypto_istat_compress *istat = comp_get_stat(alg); 297*0a742389SHerbert Xu 298*0a742389SHerbert Xu atomic64_inc(&istat->compress_cnt); 299*0a742389SHerbert Xu atomic64_add(req->slen, &istat->compress_tlen); 300*0a742389SHerbert Xu } 301*0a742389SHerbert Xu 302*0a742389SHerbert Xu return crypto_comp_errstat(alg, tfm->compress(req)); 3032ebda74fSGiovanni Cabiddu } 3042ebda74fSGiovanni Cabiddu 3052ebda74fSGiovanni Cabiddu /** 3062ebda74fSGiovanni Cabiddu * crypto_acomp_decompress() -- Invoke asynchronous decompress operation 3072ebda74fSGiovanni Cabiddu * 3082ebda74fSGiovanni Cabiddu * Function invokes the asynchronous decompress operation 3092ebda74fSGiovanni Cabiddu * 3102ebda74fSGiovanni Cabiddu * @req: asynchronous compress request 3112ebda74fSGiovanni Cabiddu * 3122ebda74fSGiovanni Cabiddu * Return: zero on success; error code in case of error 3132ebda74fSGiovanni Cabiddu */ 3142ebda74fSGiovanni Cabiddu static inline int crypto_acomp_decompress(struct acomp_req *req) 3152ebda74fSGiovanni Cabiddu { 3162ebda74fSGiovanni Cabiddu struct crypto_acomp *tfm = crypto_acomp_reqtfm(req); 317*0a742389SHerbert Xu struct comp_alg_common *alg; 3182ebda74fSGiovanni Cabiddu 319*0a742389SHerbert Xu alg = crypto_comp_alg_common(tfm); 320*0a742389SHerbert Xu 321*0a742389SHerbert Xu if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { 322*0a742389SHerbert Xu struct crypto_istat_compress *istat = comp_get_stat(alg); 323*0a742389SHerbert Xu 324*0a742389SHerbert Xu atomic64_inc(&istat->decompress_cnt); 325*0a742389SHerbert Xu atomic64_add(req->slen, &istat->decompress_tlen); 326*0a742389SHerbert Xu } 327*0a742389SHerbert Xu 328*0a742389SHerbert Xu return crypto_comp_errstat(alg, tfm->decompress(req)); 3292ebda74fSGiovanni Cabiddu } 3302ebda74fSGiovanni Cabiddu 3312ebda74fSGiovanni Cabiddu #endif 332