269 lines · c
1/* SPDX-License-Identifier: GPL-2.0-or-later */2/*3 * Asynchronous Compression operations4 *5 * Copyright (c) 2016, Intel Corporation6 * Authors: Weigang Li <weigang.li@intel.com>7 * Giovanni Cabiddu <giovanni.cabiddu@intel.com>8 */9#ifndef _CRYPTO_ACOMP_H10#define _CRYPTO_ACOMP_H11 12#include <linux/atomic.h>13#include <linux/container_of.h>14#include <linux/crypto.h>15 16#define CRYPTO_ACOMP_ALLOC_OUTPUT 0x0000000117#define CRYPTO_ACOMP_DST_MAX 13107218 19/**20 * struct acomp_req - asynchronous (de)compression request21 *22 * @base: Common attributes for asynchronous crypto requests23 * @src: Source Data24 * @dst: Destination data25 * @slen: Size of the input buffer26 * @dlen: Size of the output buffer and number of bytes produced27 * @flags: Internal flags28 * @__ctx: Start of private context data29 */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 encapsulate42 * algorithms and core processing logic43 *44 * @compress: Function performs a compress operation45 * @decompress: Function performs a de-compress operation46 * @dst_free: Frees destination buffer if allocated inside the47 * algorithm48 * @reqsize: Context size for (de)compression requests49 * @base: Common crypto API algorithm data structure50 */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#define COMP_ALG_COMMON { \60 struct crypto_alg base; \61}62struct comp_alg_common COMP_ALG_COMMON;63 64/**65 * DOC: Asynchronous Compression API66 *67 * The Asynchronous Compression API is used with the algorithms of type68 * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)69 */70 71/**72 * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle73 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the74 * compression algorithm e.g. "deflate"75 * @type: specifies the type of the algorithm76 * @mask: specifies the mask for the algorithm77 *78 * Allocate a handle for a compression algorithm. The returned struct79 * crypto_acomp is the handle that is required for any subsequent80 * API invocation for the compression operations.81 *82 * Return: allocated handle in case of success; IS_ERR() is true in case83 * of an error, PTR_ERR() returns the error code.84 */85struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,86 u32 mask);87/**88 * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node89 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the90 * compression algorithm e.g. "deflate"91 * @type: specifies the type of the algorithm92 * @mask: specifies the mask for the algorithm93 * @node: specifies the NUMA node the ZIP hardware belongs to94 *95 * Allocate a handle for a compression algorithm. Drivers should try to use96 * (de)compressors on the specified NUMA node.97 * The returned struct crypto_acomp is the handle that is required for any98 * subsequent API invocation for the compression operations.99 *100 * Return: allocated handle in case of success; IS_ERR() is true in case101 * of an error, PTR_ERR() returns the error code.102 */103struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,104 u32 mask, int node);105 106static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)107{108 return &tfm->base;109}110 111static inline struct comp_alg_common *__crypto_comp_alg_common(112 struct crypto_alg *alg)113{114 return container_of(alg, struct comp_alg_common, base);115}116 117static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)118{119 return container_of(tfm, struct crypto_acomp, base);120}121 122static inline struct comp_alg_common *crypto_comp_alg_common(123 struct crypto_acomp *tfm)124{125 return __crypto_comp_alg_common(crypto_acomp_tfm(tfm)->__crt_alg);126}127 128static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)129{130 return tfm->reqsize;131}132 133static inline void acomp_request_set_tfm(struct acomp_req *req,134 struct crypto_acomp *tfm)135{136 req->base.tfm = crypto_acomp_tfm(tfm);137}138 139static inline bool acomp_is_async(struct crypto_acomp *tfm)140{141 return crypto_comp_alg_common(tfm)->base.cra_flags &142 CRYPTO_ALG_ASYNC;143}144 145static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)146{147 return __crypto_acomp_tfm(req->base.tfm);148}149 150/**151 * crypto_free_acomp() -- free ACOMPRESS tfm handle152 *153 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()154 *155 * If @tfm is a NULL or error pointer, this function does nothing.156 */157static inline void crypto_free_acomp(struct crypto_acomp *tfm)158{159 crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));160}161 162static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)163{164 type &= ~CRYPTO_ALG_TYPE_MASK;165 type |= CRYPTO_ALG_TYPE_ACOMPRESS;166 mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;167 168 return crypto_has_alg(alg_name, type, mask);169}170 171/**172 * acomp_request_alloc() -- allocates asynchronous (de)compression request173 *174 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()175 *176 * Return: allocated handle in case of success or NULL in case of an error177 */178struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);179 180/**181 * acomp_request_free() -- zeroize and free asynchronous (de)compression182 * request as well as the output buffer if allocated183 * inside the algorithm184 *185 * @req: request to free186 */187void acomp_request_free(struct acomp_req *req);188 189/**190 * acomp_request_set_callback() -- Sets an asynchronous callback191 *192 * Callback will be called when an asynchronous operation on a given193 * request is finished.194 *195 * @req: request that the callback will be set for196 * @flgs: specify for instance if the operation may backlog197 * @cmlp: callback which will be called198 * @data: private data used by the caller199 */200static inline void acomp_request_set_callback(struct acomp_req *req,201 u32 flgs,202 crypto_completion_t cmpl,203 void *data)204{205 req->base.complete = cmpl;206 req->base.data = data;207 req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT;208 req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT;209}210 211/**212 * acomp_request_set_params() -- Sets request parameters213 *214 * Sets parameters required by an acomp operation215 *216 * @req: asynchronous compress request217 * @src: pointer to input buffer scatterlist218 * @dst: pointer to output buffer scatterlist. If this is NULL, the219 * acomp layer will allocate the output memory220 * @slen: size of the input buffer221 * @dlen: size of the output buffer. If dst is NULL, this can be used by222 * the user to specify the maximum amount of memory to allocate223 */224static inline void acomp_request_set_params(struct acomp_req *req,225 struct scatterlist *src,226 struct scatterlist *dst,227 unsigned int slen,228 unsigned int dlen)229{230 req->src = src;231 req->dst = dst;232 req->slen = slen;233 req->dlen = dlen;234 235 req->flags &= ~CRYPTO_ACOMP_ALLOC_OUTPUT;236 if (!req->dst)237 req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;238}239 240/**241 * crypto_acomp_compress() -- Invoke asynchronous compress operation242 *243 * Function invokes the asynchronous compress operation244 *245 * @req: asynchronous compress request246 *247 * Return: zero on success; error code in case of error248 */249static inline int crypto_acomp_compress(struct acomp_req *req)250{251 return crypto_acomp_reqtfm(req)->compress(req);252}253 254/**255 * crypto_acomp_decompress() -- Invoke asynchronous decompress operation256 *257 * Function invokes the asynchronous decompress operation258 *259 * @req: asynchronous compress request260 *261 * Return: zero on success; error code in case of error262 */263static inline int crypto_acomp_decompress(struct acomp_req *req)264{265 return crypto_acomp_reqtfm(req)->decompress(req);266}267 268#endif269