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_INT_H 10 #define _CRYPTO_ACOMP_INT_H 11 12 #include <crypto/acompress.h> 13 #include <crypto/algapi.h> 14 #include <crypto/scatterwalk.h> 15 #include <linux/compiler_types.h> 16 #include <linux/cpumask_types.h> 17 #include <linux/spinlock.h> 18 #include <linux/workqueue_types.h> 19 20 #define ACOMP_FBREQ_ON_STACK(name, req) \ 21 char __##name##_req[sizeof(struct acomp_req) + \ 22 MAX_SYNC_COMP_REQSIZE] CRYPTO_MINALIGN_ATTR; \ 23 struct acomp_req *name = acomp_fbreq_on_stack_init( \ 24 __##name##_req, (req)) 25 26 /** 27 * struct acomp_alg - asynchronous compression algorithm 28 * 29 * @compress: Function performs a compress operation 30 * @decompress: Function performs a de-compress operation 31 * @init: Initialize the cryptographic transformation object. 32 * This function is used to initialize the cryptographic 33 * transformation object. This function is called only once at 34 * the instantiation time, right after the transformation context 35 * was allocated. In case the cryptographic hardware has some 36 * special requirements which need to be handled by software, this 37 * function shall check for the precise requirement of the 38 * transformation and put any software fallbacks in place. 39 * @exit: Deinitialize the cryptographic transformation object. This is a 40 * counterpart to @init, used to remove various changes set in 41 * @init. 42 * 43 * @base: Common crypto API algorithm data structure 44 * @calg: Cmonn algorithm data structure shared with scomp 45 * @COMP_ALG_COMMON: see struct comp_alg_common 46 */ 47 struct acomp_alg { 48 int (*compress)(struct acomp_req *req); 49 int (*decompress)(struct acomp_req *req); 50 int (*init)(struct crypto_acomp *tfm); 51 void (*exit)(struct crypto_acomp *tfm); 52 53 union { 54 struct COMP_ALG_COMMON; 55 struct comp_alg_common calg; 56 }; 57 }; 58 59 struct crypto_acomp_stream { 60 spinlock_t lock; 61 void *ctx; 62 }; 63 64 struct crypto_acomp_streams { 65 /* These must come first because of struct scomp_alg. */ 66 void *(*alloc_ctx)(void); 67 void (*free_ctx)(void *); 68 69 struct crypto_acomp_stream __percpu *streams; 70 struct work_struct stream_work; 71 cpumask_t stream_want; 72 }; 73 74 struct acomp_walk { 75 union { 76 /* Virtual address of the source. */ 77 struct { 78 struct { 79 const void *const addr; 80 } virt; 81 } src; 82 83 /* Private field for the API, do not use. */ 84 struct scatter_walk in; 85 }; 86 87 union { 88 /* Virtual address of the destination. */ 89 struct { 90 struct { 91 void *const addr; 92 } virt; 93 } dst; 94 95 /* Private field for the API, do not use. */ 96 struct scatter_walk out; 97 }; 98 99 unsigned int slen; 100 unsigned int dlen; 101 102 int flags; 103 }; 104 105 /* 106 * Transform internal helpers. 107 */ 108 static inline void *acomp_request_ctx(struct acomp_req *req) 109 { 110 return req->__ctx; 111 } 112 113 static inline void *acomp_tfm_ctx(struct crypto_acomp *tfm) 114 { 115 return tfm->base.__crt_ctx; 116 } 117 118 static inline void acomp_request_complete(struct acomp_req *req, 119 int err) 120 { 121 crypto_request_complete(&req->base, err); 122 } 123 124 /** 125 * crypto_register_acomp() -- Register asynchronous compression algorithm 126 * 127 * Function registers an implementation of an asynchronous 128 * compression algorithm 129 * 130 * @alg: algorithm definition 131 * 132 * Return: zero on success; error code in case of error 133 */ 134 int crypto_register_acomp(struct acomp_alg *alg); 135 136 /** 137 * crypto_unregister_acomp() -- Unregister asynchronous compression algorithm 138 * 139 * Function unregisters an implementation of an asynchronous 140 * compression algorithm 141 * 142 * @alg: algorithm definition 143 */ 144 void crypto_unregister_acomp(struct acomp_alg *alg); 145 146 int crypto_register_acomps(struct acomp_alg *algs, int count); 147 void crypto_unregister_acomps(struct acomp_alg *algs, int count); 148 149 static inline bool acomp_request_issg(struct acomp_req *req) 150 { 151 return !(req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT | 152 CRYPTO_ACOMP_REQ_DST_VIRT)); 153 } 154 155 static inline bool acomp_request_src_isvirt(struct acomp_req *req) 156 { 157 return req->base.flags & CRYPTO_ACOMP_REQ_SRC_VIRT; 158 } 159 160 static inline bool acomp_request_dst_isvirt(struct acomp_req *req) 161 { 162 return req->base.flags & CRYPTO_ACOMP_REQ_DST_VIRT; 163 } 164 165 static inline bool acomp_request_isvirt(struct acomp_req *req) 166 { 167 return req->base.flags & (CRYPTO_ACOMP_REQ_SRC_VIRT | 168 CRYPTO_ACOMP_REQ_DST_VIRT); 169 } 170 171 static inline bool acomp_request_src_isnondma(struct acomp_req *req) 172 { 173 return req->base.flags & CRYPTO_ACOMP_REQ_SRC_NONDMA; 174 } 175 176 static inline bool acomp_request_dst_isnondma(struct acomp_req *req) 177 { 178 return req->base.flags & CRYPTO_ACOMP_REQ_DST_NONDMA; 179 } 180 181 static inline bool acomp_request_isnondma(struct acomp_req *req) 182 { 183 return req->base.flags & (CRYPTO_ACOMP_REQ_SRC_NONDMA | 184 CRYPTO_ACOMP_REQ_DST_NONDMA); 185 } 186 187 static inline bool crypto_acomp_req_virt(struct crypto_acomp *tfm) 188 { 189 return crypto_tfm_req_virt(&tfm->base); 190 } 191 192 void crypto_acomp_free_streams(struct crypto_acomp_streams *s); 193 int crypto_acomp_alloc_streams(struct crypto_acomp_streams *s); 194 195 #define crypto_acomp_lock_stream_bh(...) __acquire_ret(_crypto_acomp_lock_stream_bh(__VA_ARGS__), &__ret->lock); 196 struct crypto_acomp_stream *_crypto_acomp_lock_stream_bh( 197 struct crypto_acomp_streams *s) __acquires_ret; 198 199 static inline void crypto_acomp_unlock_stream_bh( 200 struct crypto_acomp_stream *stream) __releases(&stream->lock) 201 { 202 spin_unlock_bh(&stream->lock); 203 } 204 205 void acomp_walk_done_src(struct acomp_walk *walk, int used); 206 void acomp_walk_done_dst(struct acomp_walk *walk, int used); 207 int acomp_walk_next_src(struct acomp_walk *walk); 208 int acomp_walk_next_dst(struct acomp_walk *walk); 209 int acomp_walk_virt(struct acomp_walk *__restrict walk, 210 struct acomp_req *__restrict req, bool atomic); 211 212 static inline bool acomp_walk_more_src(const struct acomp_walk *walk, int cur) 213 { 214 return walk->slen != cur; 215 } 216 217 static inline u32 acomp_request_flags(struct acomp_req *req) 218 { 219 return crypto_request_flags(&req->base) & ~CRYPTO_ACOMP_REQ_PRIVATE; 220 } 221 222 static inline struct crypto_acomp *crypto_acomp_fb(struct crypto_acomp *tfm) 223 { 224 return __crypto_acomp_tfm(crypto_acomp_tfm(tfm)->fb); 225 } 226 227 static inline struct acomp_req *acomp_fbreq_on_stack_init( 228 char *buf, struct acomp_req *old) 229 { 230 struct crypto_acomp *tfm = crypto_acomp_reqtfm(old); 231 struct acomp_req *req = (void *)buf; 232 233 crypto_stack_request_init(&req->base, 234 crypto_acomp_tfm(crypto_acomp_fb(tfm))); 235 acomp_request_set_callback(req, acomp_request_flags(old), NULL, NULL); 236 req->base.flags &= ~CRYPTO_ACOMP_REQ_PRIVATE; 237 req->base.flags |= old->base.flags & CRYPTO_ACOMP_REQ_PRIVATE; 238 req->src = old->src; 239 req->dst = old->dst; 240 req->slen = old->slen; 241 req->dlen = old->dlen; 242 243 return req; 244 } 245 246 #endif 247