Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Asynchronous Compression operations
0004  *
0005  * Copyright (c) 2016, Intel Corporation
0006  * Authors: Weigang Li <weigang.li@intel.com>
0007  *          Giovanni Cabiddu <giovanni.cabiddu@intel.com>
0008  */
0009 #ifndef _CRYPTO_ACOMP_H
0010 #define _CRYPTO_ACOMP_H
0011 #include <linux/crypto.h>
0012 
0013 #define CRYPTO_ACOMP_ALLOC_OUTPUT   0x00000001
0014 
0015 /**
0016  * struct acomp_req - asynchronous (de)compression request
0017  *
0018  * @base:   Common attributes for asynchronous crypto requests
0019  * @src:    Source Data
0020  * @dst:    Destination data
0021  * @slen:   Size of the input buffer
0022  * @dlen:   Size of the output buffer and number of bytes produced
0023  * @flags:  Internal flags
0024  * @__ctx:  Start of private context data
0025  */
0026 struct acomp_req {
0027     struct crypto_async_request base;
0028     struct scatterlist *src;
0029     struct scatterlist *dst;
0030     unsigned int slen;
0031     unsigned int dlen;
0032     u32 flags;
0033     void *__ctx[] CRYPTO_MINALIGN_ATTR;
0034 };
0035 
0036 /**
0037  * struct crypto_acomp - user-instantiated objects which encapsulate
0038  * algorithms and core processing logic
0039  *
0040  * @compress:       Function performs a compress operation
0041  * @decompress:     Function performs a de-compress operation
0042  * @dst_free:       Frees destination buffer if allocated inside the
0043  *          algorithm
0044  * @reqsize:        Context size for (de)compression requests
0045  * @base:       Common crypto API algorithm data structure
0046  */
0047 struct crypto_acomp {
0048     int (*compress)(struct acomp_req *req);
0049     int (*decompress)(struct acomp_req *req);
0050     void (*dst_free)(struct scatterlist *dst);
0051     unsigned int reqsize;
0052     struct crypto_tfm base;
0053 };
0054 
0055 /**
0056  * struct acomp_alg - asynchronous compression algorithm
0057  *
0058  * @compress:   Function performs a compress operation
0059  * @decompress: Function performs a de-compress operation
0060  * @dst_free:   Frees destination buffer if allocated inside the algorithm
0061  * @init:   Initialize the cryptographic transformation object.
0062  *      This function is used to initialize the cryptographic
0063  *      transformation object. This function is called only once at
0064  *      the instantiation time, right after the transformation context
0065  *      was allocated. In case the cryptographic hardware has some
0066  *      special requirements which need to be handled by software, this
0067  *      function shall check for the precise requirement of the
0068  *      transformation and put any software fallbacks in place.
0069  * @exit:   Deinitialize the cryptographic transformation object. This is a
0070  *      counterpart to @init, used to remove various changes set in
0071  *      @init.
0072  *
0073  * @reqsize:    Context size for (de)compression requests
0074  * @base:   Common crypto API algorithm data structure
0075  */
0076 struct acomp_alg {
0077     int (*compress)(struct acomp_req *req);
0078     int (*decompress)(struct acomp_req *req);
0079     void (*dst_free)(struct scatterlist *dst);
0080     int (*init)(struct crypto_acomp *tfm);
0081     void (*exit)(struct crypto_acomp *tfm);
0082     unsigned int reqsize;
0083     struct crypto_alg base;
0084 };
0085 
0086 /**
0087  * DOC: Asynchronous Compression API
0088  *
0089  * The Asynchronous Compression API is used with the algorithms of type
0090  * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
0091  */
0092 
0093 /**
0094  * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
0095  * @alg_name:   is the cra_name / name or cra_driver_name / driver name of the
0096  *      compression algorithm e.g. "deflate"
0097  * @type:   specifies the type of the algorithm
0098  * @mask:   specifies the mask for the algorithm
0099  *
0100  * Allocate a handle for a compression algorithm. The returned struct
0101  * crypto_acomp is the handle that is required for any subsequent
0102  * API invocation for the compression operations.
0103  *
0104  * Return:  allocated handle in case of success; IS_ERR() is true in case
0105  *      of an error, PTR_ERR() returns the error code.
0106  */
0107 struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
0108                     u32 mask);
0109 /**
0110  * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
0111  * @alg_name:   is the cra_name / name or cra_driver_name / driver name of the
0112  *      compression algorithm e.g. "deflate"
0113  * @type:   specifies the type of the algorithm
0114  * @mask:   specifies the mask for the algorithm
0115  * @node:   specifies the NUMA node the ZIP hardware belongs to
0116  *
0117  * Allocate a handle for a compression algorithm. Drivers should try to use
0118  * (de)compressors on the specified NUMA node.
0119  * The returned struct crypto_acomp is the handle that is required for any
0120  * subsequent API invocation for the compression operations.
0121  *
0122  * Return:  allocated handle in case of success; IS_ERR() is true in case
0123  *      of an error, PTR_ERR() returns the error code.
0124  */
0125 struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
0126                     u32 mask, int node);
0127 
0128 static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
0129 {
0130     return &tfm->base;
0131 }
0132 
0133 static inline struct acomp_alg *__crypto_acomp_alg(struct crypto_alg *alg)
0134 {
0135     return container_of(alg, struct acomp_alg, base);
0136 }
0137 
0138 static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
0139 {
0140     return container_of(tfm, struct crypto_acomp, base);
0141 }
0142 
0143 static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
0144 {
0145     return __crypto_acomp_alg(crypto_acomp_tfm(tfm)->__crt_alg);
0146 }
0147 
0148 static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
0149 {
0150     return tfm->reqsize;
0151 }
0152 
0153 static inline void acomp_request_set_tfm(struct acomp_req *req,
0154                      struct crypto_acomp *tfm)
0155 {
0156     req->base.tfm = crypto_acomp_tfm(tfm);
0157 }
0158 
0159 static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
0160 {
0161     return __crypto_acomp_tfm(req->base.tfm);
0162 }
0163 
0164 /**
0165  * crypto_free_acomp() -- free ACOMPRESS tfm handle
0166  *
0167  * @tfm:    ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
0168  *
0169  * If @tfm is a NULL or error pointer, this function does nothing.
0170  */
0171 static inline void crypto_free_acomp(struct crypto_acomp *tfm)
0172 {
0173     crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
0174 }
0175 
0176 static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
0177 {
0178     type &= ~CRYPTO_ALG_TYPE_MASK;
0179     type |= CRYPTO_ALG_TYPE_ACOMPRESS;
0180     mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
0181 
0182     return crypto_has_alg(alg_name, type, mask);
0183 }
0184 
0185 /**
0186  * acomp_request_alloc() -- allocates asynchronous (de)compression request
0187  *
0188  * @tfm:    ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
0189  *
0190  * Return:  allocated handle in case of success or NULL in case of an error
0191  */
0192 struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm);
0193 
0194 /**
0195  * acomp_request_free() -- zeroize and free asynchronous (de)compression
0196  *             request as well as the output buffer if allocated
0197  *             inside the algorithm
0198  *
0199  * @req:    request to free
0200  */
0201 void acomp_request_free(struct acomp_req *req);
0202 
0203 /**
0204  * acomp_request_set_callback() -- Sets an asynchronous callback
0205  *
0206  * Callback will be called when an asynchronous operation on a given
0207  * request is finished.
0208  *
0209  * @req:    request that the callback will be set for
0210  * @flgs:   specify for instance if the operation may backlog
0211  * @cmlp:   callback which will be called
0212  * @data:   private data used by the caller
0213  */
0214 static inline void acomp_request_set_callback(struct acomp_req *req,
0215                           u32 flgs,
0216                           crypto_completion_t cmpl,
0217                           void *data)
0218 {
0219     req->base.complete = cmpl;
0220     req->base.data = data;
0221     req->base.flags = flgs;
0222 }
0223 
0224 /**
0225  * acomp_request_set_params() -- Sets request parameters
0226  *
0227  * Sets parameters required by an acomp operation
0228  *
0229  * @req:    asynchronous compress request
0230  * @src:    pointer to input buffer scatterlist
0231  * @dst:    pointer to output buffer scatterlist. If this is NULL, the
0232  *      acomp layer will allocate the output memory
0233  * @slen:   size of the input buffer
0234  * @dlen:   size of the output buffer. If dst is NULL, this can be used by
0235  *      the user to specify the maximum amount of memory to allocate
0236  */
0237 static inline void acomp_request_set_params(struct acomp_req *req,
0238                         struct scatterlist *src,
0239                         struct scatterlist *dst,
0240                         unsigned int slen,
0241                         unsigned int dlen)
0242 {
0243     req->src = src;
0244     req->dst = dst;
0245     req->slen = slen;
0246     req->dlen = dlen;
0247 
0248     if (!req->dst)
0249         req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT;
0250 }
0251 
0252 /**
0253  * crypto_acomp_compress() -- Invoke asynchronous compress operation
0254  *
0255  * Function invokes the asynchronous compress operation
0256  *
0257  * @req:    asynchronous compress request
0258  *
0259  * Return:  zero on success; error code in case of error
0260  */
0261 static inline int crypto_acomp_compress(struct acomp_req *req)
0262 {
0263     struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
0264     struct crypto_alg *alg = tfm->base.__crt_alg;
0265     unsigned int slen = req->slen;
0266     int ret;
0267 
0268     crypto_stats_get(alg);
0269     ret = tfm->compress(req);
0270     crypto_stats_compress(slen, ret, alg);
0271     return ret;
0272 }
0273 
0274 /**
0275  * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
0276  *
0277  * Function invokes the asynchronous decompress operation
0278  *
0279  * @req:    asynchronous compress request
0280  *
0281  * Return:  zero on success; error code in case of error
0282  */
0283 static inline int crypto_acomp_decompress(struct acomp_req *req)
0284 {
0285     struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
0286     struct crypto_alg *alg = tfm->base.__crt_alg;
0287     unsigned int slen = req->slen;
0288     int ret;
0289 
0290     crypto_stats_get(alg);
0291     ret = tfm->decompress(req);
0292     crypto_stats_decompress(slen, ret, alg);
0293     return ret;
0294 }
0295 
0296 #endif