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 * @__ctx: Start of private context data 28 */ 29 struct acomp_req { 30 struct crypto_async_request base; 31 struct scatterlist *src; 32 struct scatterlist *dst; 33 unsigned int slen; 34 unsigned int dlen; 35 void *__ctx[] CRYPTO_MINALIGN_ATTR; 36 }; 37 38 /** 39 * struct crypto_acomp - user-instantiated objects which encapsulate 40 * algorithms and core processing logic 41 * 42 * @compress: Function performs a compress operation 43 * @decompress: Function performs a de-compress operation 44 * @dst_free: Frees destination buffer if allocated inside the 45 * algorithm 46 * @reqsize: Context size for (de)compression requests 47 * @base: Common crypto API algorithm data structure 48 */ 49 struct crypto_acomp { 50 int (*compress)(struct acomp_req *req); 51 int (*decompress)(struct acomp_req *req); 52 void (*dst_free)(struct scatterlist *dst); 53 unsigned int reqsize; 54 struct crypto_tfm base; 55 }; 56 57 #define COMP_ALG_COMMON { \ 58 struct crypto_alg base; \ 59 } 60 struct comp_alg_common COMP_ALG_COMMON; 61 62 /** 63 * DOC: Asynchronous Compression API 64 * 65 * The Asynchronous Compression API is used with the algorithms of type 66 * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto) 67 */ 68 69 /** 70 * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle 71 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 72 * compression algorithm e.g. "deflate" 73 * @type: specifies the type of the algorithm 74 * @mask: specifies the mask for the algorithm 75 * 76 * Allocate a handle for a compression algorithm. The returned struct 77 * crypto_acomp is the handle that is required for any subsequent 78 * API invocation for the compression operations. 79 * 80 * Return: allocated handle in case of success; IS_ERR() is true in case 81 * of an error, PTR_ERR() returns the error code. 82 */ 83 struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type, 84 u32 mask); 85 /** 86 * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node 87 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 88 * compression algorithm e.g. "deflate" 89 * @type: specifies the type of the algorithm 90 * @mask: specifies the mask for the algorithm 91 * @node: specifies the NUMA node the ZIP hardware belongs to 92 * 93 * Allocate a handle for a compression algorithm. Drivers should try to use 94 * (de)compressors on the specified NUMA node. 95 * The returned struct crypto_acomp is the handle that is required for any 96 * subsequent API invocation for the compression operations. 97 * 98 * Return: allocated handle in case of success; IS_ERR() is true in case 99 * of an error, PTR_ERR() returns the error code. 100 */ 101 struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type, 102 u32 mask, int node); 103 104 static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm) 105 { 106 return &tfm->base; 107 } 108 109 static inline struct comp_alg_common *__crypto_comp_alg_common( 110 struct crypto_alg *alg) 111 { 112 return container_of(alg, struct comp_alg_common, base); 113 } 114 115 static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm) 116 { 117 return container_of(tfm, struct crypto_acomp, base); 118 } 119 120 static inline struct comp_alg_common *crypto_comp_alg_common( 121 struct crypto_acomp *tfm) 122 { 123 return __crypto_comp_alg_common(crypto_acomp_tfm(tfm)->__crt_alg); 124 } 125 126 static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm) 127 { 128 return tfm->reqsize; 129 } 130 131 static inline void acomp_request_set_tfm(struct acomp_req *req, 132 struct crypto_acomp *tfm) 133 { 134 req->base.tfm = crypto_acomp_tfm(tfm); 135 } 136 137 static inline bool acomp_is_async(struct crypto_acomp *tfm) 138 { 139 return crypto_comp_alg_common(tfm)->base.cra_flags & 140 CRYPTO_ALG_ASYNC; 141 } 142 143 static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req) 144 { 145 return __crypto_acomp_tfm(req->base.tfm); 146 } 147 148 /** 149 * crypto_free_acomp() -- free ACOMPRESS tfm handle 150 * 151 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 152 * 153 * If @tfm is a NULL or error pointer, this function does nothing. 154 */ 155 static inline void crypto_free_acomp(struct crypto_acomp *tfm) 156 { 157 crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm)); 158 } 159 160 static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask) 161 { 162 type &= ~CRYPTO_ALG_TYPE_MASK; 163 type |= CRYPTO_ALG_TYPE_ACOMPRESS; 164 mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK; 165 166 return crypto_has_alg(alg_name, type, mask); 167 } 168 169 /** 170 * acomp_request_alloc() -- allocates asynchronous (de)compression request 171 * 172 * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp() 173 * 174 * Return: allocated handle in case of success or NULL in case of an error 175 */ 176 struct acomp_req *acomp_request_alloc(struct crypto_acomp *tfm); 177 178 /** 179 * acomp_request_free() -- zeroize and free asynchronous (de)compression 180 * request as well as the output buffer if allocated 181 * inside the algorithm 182 * 183 * @req: request to free 184 */ 185 void acomp_request_free(struct acomp_req *req); 186 187 /** 188 * acomp_request_set_callback() -- Sets an asynchronous callback 189 * 190 * Callback will be called when an asynchronous operation on a given 191 * request is finished. 192 * 193 * @req: request that the callback will be set for 194 * @flgs: specify for instance if the operation may backlog 195 * @cmlp: callback which will be called 196 * @data: private data used by the caller 197 */ 198 static inline void acomp_request_set_callback(struct acomp_req *req, 199 u32 flgs, 200 crypto_completion_t cmpl, 201 void *data) 202 { 203 req->base.complete = cmpl; 204 req->base.data = data; 205 req->base.flags &= CRYPTO_ACOMP_ALLOC_OUTPUT; 206 req->base.flags |= flgs & ~CRYPTO_ACOMP_ALLOC_OUTPUT; 207 } 208 209 /** 210 * acomp_request_set_params() -- Sets request parameters 211 * 212 * Sets parameters required by an acomp operation 213 * 214 * @req: asynchronous compress request 215 * @src: pointer to input buffer scatterlist 216 * @dst: pointer to output buffer scatterlist. If this is NULL, the 217 * acomp layer will allocate the output memory 218 * @slen: size of the input buffer 219 * @dlen: size of the output buffer. If dst is NULL, this can be used by 220 * the user to specify the maximum amount of memory to allocate 221 */ 222 static inline void acomp_request_set_params(struct acomp_req *req, 223 struct scatterlist *src, 224 struct scatterlist *dst, 225 unsigned int slen, 226 unsigned int dlen) 227 { 228 req->src = src; 229 req->dst = dst; 230 req->slen = slen; 231 req->dlen = dlen; 232 233 req->base.flags &= ~CRYPTO_ACOMP_ALLOC_OUTPUT; 234 if (!req->dst) 235 req->base.flags |= CRYPTO_ACOMP_ALLOC_OUTPUT; 236 } 237 238 /** 239 * crypto_acomp_compress() -- Invoke asynchronous compress operation 240 * 241 * Function invokes the asynchronous compress operation 242 * 243 * @req: asynchronous compress request 244 * 245 * Return: zero on success; error code in case of error 246 */ 247 static inline int crypto_acomp_compress(struct acomp_req *req) 248 { 249 return crypto_acomp_reqtfm(req)->compress(req); 250 } 251 252 /** 253 * crypto_acomp_decompress() -- Invoke asynchronous decompress operation 254 * 255 * Function invokes the asynchronous decompress operation 256 * 257 * @req: asynchronous compress request 258 * 259 * Return: zero on success; error code in case of error 260 */ 261 static inline int crypto_acomp_decompress(struct acomp_req *req) 262 { 263 return crypto_acomp_reqtfm(req)->decompress(req); 264 } 265 266 #endif 267