1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Asynchronous Compression operations 4 * 5 * Copyright (c) 2016, Intel Corporation 6 * Authors: Weigang Li <weigang.li@intel.com> 7 * Giovanni Cabiddu <giovanni.cabiddu@intel.com> 8 */ 9 #ifndef _CRYPTO_ACOMP_H 10 #define _CRYPTO_ACOMP_H 11 12 #include <linux/atomic.h> 13 #include <linux/container_of.h> 14 #include <linux/crypto.h> 15 16 #define CRYPTO_ACOMP_ALLOC_OUTPUT 0x00000001 17 #define CRYPTO_ACOMP_DST_MAX 131072 18 19 /** 20 * struct acomp_req - asynchronous (de)compression request 21 * 22 * @base: Common attributes for asynchronous crypto requests 23 * @src: Source Data 24 * @dst: Destination data 25 * @slen: Size of the input buffer 26 * @dlen: Size of the output buffer and number of bytes produced 27 * @flags: Internal flags 28 * @__ctx: Start of private context data 29 */ 30 struct 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 encapsulate 42 * algorithms and core processing logic 43 * 44 * @compress: Function performs a compress operation 45 * @decompress: Function performs a de-compress operation 46 * @dst_free: Frees destination buffer if allocated inside the 47 * algorithm 48 * @reqsize: Context size for (de)compression requests 49 * @base: Common crypto API algorithm data structure 50 */ 51 struct 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 /** 60 * DOC: Asynchronous Compression API 61 * 62 * The Asynchronous Compression API is used with the algorithms of type 63 * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto) 64 */ 65 66 /** 67 * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle 68 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 69 * compression algorithm e.g. "deflate" 70 * @type: specifies the type of the algorithm 71 * @mask: specifies the mask for the algorithm 72 * 73 * Allocate a handle for a compression algorithm. The returned struct 74 * crypto_acomp is the handle that is required for any subsequent 75 * API invocation for the compression operations. 76 * 77 * Return: allocated handle in case of success; IS_ERR() is true in case 78 * of an error, PTR_ERR() returns the error code. 79 */ 80 struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type, 81 u32 mask); 82 /** 83 * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node 84 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 85 * compression algorithm e.g. "deflate" 86 * @type: specifies the type of the algorithm 87 * @mask: specifies the mask for the algorithm 88 * @node: specifies the NUMA node the ZIP hardware belongs to 89 * 90 * Allocate a handle for a compression algorithm. Drivers should try to use 91 * (de)compressors on the specified NUMA node. 92 * The returned struct crypto_acomp is the handle that is required for any 93 * subsequent API invocation for the compression operations. 94 * 95 * Return: allocated handle in case of success; IS_ERR() is true in case 96 * of an error, PTR_ERR() returns the error code. 97 */ 98 struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type, 99 u32 mask, int node); 100 101 static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm) 102 { 103 return &tfm->base; 104 } 105 106 static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm) 107 { 108 return container_of(tfm, struct crypto_acomp, base); 109 } 110 111 static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm) 112 { 113 return tfm->reqsize; 114 } 115 116 static inline void acomp_request_set_tfm(struct acomp_req *req, 117 struct crypto_acomp *tfm) 118 { 119 req->base.tfm = crypto_acomp_tfm(tfm); 120 } 121 122 static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req) 123 { 124 return __crypto_acomp_tfm(req->base.tfm); 125 } 126 127 /** 128 * crypto_free_acomp() -- free ACOMPRESS tfm handle 129 * 130 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 131 * 132 * If @tfm is a NULL or error pointer, this function does nothing. 133 */ 134 static inline void crypto_free_acomp(struct crypto_acomp *tfm) 135 { 136 crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm)); 137 } 138 139 static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask) 140 { 141 type &= ~CRYPTO_ALG_TYPE_MASK; 142 type |= CRYPTO_ALG_TYPE_ACOMPRESS; 143 mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK; 144 145 return crypto_has_alg(alg_name, type, mask); 146 } 147 148 /** 149 * acomp_request_alloc() -- allocates asynchronous (de)compression request 150 * 151 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 152 * 153 * Return: allocated handle in case of success or NULL in case of an error 154 */ 155 struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm); 156 157 /** 158 * acomp_request_free() -- zeroize and free asynchronous (de)compression 159 * request as well as the output buffer if allocated 160 * inside the algorithm 161 * 162 * @req: request to free 163 */ 164 void acomp_request_free(struct acomp_req *req); 165 166 /** 167 * acomp_request_set_callback() -- Sets an asynchronous callback 168 * 169 * Callback will be called when an asynchronous operation on a given 170 * request is finished. 171 * 172 * @req: request that the callback will be set for 173 * @flgs: specify for instance if the operation may backlog 174 * @cmlp: callback which will be called 175 * @data: private data used by the caller 176 */ 177 static inline void acomp_request_set_callback(struct acomp_req *req, 178 u32 flgs, 179 crypto_completion_t cmpl, 180 void *data) 181 { 182 req->base.complete = cmpl; 183 req->base.data = data; 184 req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT; 185 req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT; 186 } 187 188 /** 189 * acomp_request_set_params() -- Sets request parameters 190 * 191 * Sets parameters required by an acomp operation 192 * 193 * @req: asynchronous compress request 194 * @src: pointer to input buffer scatterlist 195 * @dst: pointer to output buffer scatterlist. If this is NULL, the 196 * acomp layer will allocate the output memory 197 * @slen: size of the input buffer 198 * @dlen: size of the output buffer. If dst is NULL, this can be used by 199 * the user to specify the maximum amount of memory to allocate 200 */ 201 static inline void acomp_request_set_params(struct acomp_req *req, 202 struct scatterlist *src, 203 struct scatterlist *dst, 204 unsigned int slen, 205 unsigned int dlen) 206 { 207 req->src = src; 208 req->dst = dst; 209 req->slen = slen; 210 req->dlen = dlen; 211 212 req->flags &= ~CRYPTO_ACOMP_ALLOC_OUTPUT; 213 if (!req->dst) 214 req->flags |= CRYPTO_ACOMP_ALLOC_OUTPUT; 215 } 216 217 /** 218 * crypto_acomp_compress() -- Invoke asynchronous compress operation 219 * 220 * Function invokes the asynchronous compress operation 221 * 222 * @req: asynchronous compress request 223 * 224 * Return: zero on success; error code in case of error 225 */ 226 static inline int crypto_acomp_compress(struct acomp_req *req) 227 { 228 return crypto_acomp_reqtfm(req)->compress(req); 229 } 230 231 /** 232 * crypto_acomp_decompress() -- Invoke asynchronous decompress operation 233 * 234 * Function invokes the asynchronous decompress operation 235 * 236 * @req: asynchronous compress request 237 * 238 * Return: zero on success; error code in case of error 239 */ 240 static inline int crypto_acomp_decompress(struct acomp_req *req) 241 { 242 return crypto_acomp_reqtfm(req)->decompress(req); 243 } 244 245 #endif 246