1 // SPDX-License-Identifier: GPL-2.0-or-later 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 2 /* 3 * Handle async block request by crypto hardwa 3 * Handle async block request by crypto hardware engine. 4 * 4 * 5 * Copyright (C) 2016 Linaro, Inc. 5 * Copyright (C) 2016 Linaro, Inc. 6 * 6 * 7 * Author: Baolin Wang <baolin.wang@linaro.org 7 * Author: Baolin Wang <baolin.wang@linaro.org> 8 */ 8 */ 9 9 10 #include <crypto/internal/aead.h> << 11 #include <crypto/internal/akcipher.h> << 12 #include <crypto/internal/engine.h> << 13 #include <crypto/internal/hash.h> << 14 #include <crypto/internal/kpp.h> << 15 #include <crypto/internal/skcipher.h> << 16 #include <linux/err.h> 10 #include <linux/err.h> 17 #include <linux/delay.h> 11 #include <linux/delay.h> 18 #include <linux/device.h> !! 12 #include <crypto/engine.h> 19 #include <linux/kernel.h> << 20 #include <linux/module.h> << 21 #include <uapi/linux/sched/types.h> 13 #include <uapi/linux/sched/types.h> 22 #include "internal.h" 14 #include "internal.h" 23 15 24 #define CRYPTO_ENGINE_MAX_QLEN 10 16 #define CRYPTO_ENGINE_MAX_QLEN 10 25 17 26 /* Temporary algorithm flag used to indicate a << 27 #define CRYPTO_ALG_ENGINE 0x200 << 28 << 29 struct crypto_engine_alg { << 30 struct crypto_alg base; << 31 struct crypto_engine_op op; << 32 }; << 33 << 34 /** 18 /** 35 * crypto_finalize_request - finalize one requ 19 * crypto_finalize_request - finalize one request if the request is done 36 * @engine: the hardware engine 20 * @engine: the hardware engine 37 * @req: the request need to be finalized 21 * @req: the request need to be finalized 38 * @err: error number 22 * @err: error number 39 */ 23 */ 40 static void crypto_finalize_request(struct cry 24 static void crypto_finalize_request(struct crypto_engine *engine, 41 struct cry 25 struct crypto_async_request *req, int err) 42 { 26 { 43 unsigned long flags; 27 unsigned long flags; >> 28 bool finalize_req = false; >> 29 int ret; >> 30 struct crypto_engine_ctx *enginectx; 44 31 45 /* 32 /* 46 * If hardware cannot enqueue more req 33 * If hardware cannot enqueue more requests 47 * and retry mechanism is not supporte 34 * and retry mechanism is not supported 48 * make sure we are completing the cur 35 * make sure we are completing the current request 49 */ 36 */ 50 if (!engine->retry_support) { 37 if (!engine->retry_support) { 51 spin_lock_irqsave(&engine->que 38 spin_lock_irqsave(&engine->queue_lock, flags); 52 if (engine->cur_req == req) { 39 if (engine->cur_req == req) { >> 40 finalize_req = true; 53 engine->cur_req = NULL 41 engine->cur_req = NULL; 54 } 42 } 55 spin_unlock_irqrestore(&engine 43 spin_unlock_irqrestore(&engine->queue_lock, flags); 56 } 44 } 57 45 58 lockdep_assert_in_softirq(); !! 46 if (finalize_req || engine->retry_support) { 59 crypto_request_complete(req, err); !! 47 enginectx = crypto_tfm_ctx(req->tfm); >> 48 if (enginectx->op.prepare_request && >> 49 enginectx->op.unprepare_request) { >> 50 ret = enginectx->op.unprepare_request(engine, req); >> 51 if (ret) >> 52 dev_err(engine->dev, "failed to unprepare request\n"); >> 53 } >> 54 } >> 55 req->complete(req, err); 60 56 61 kthread_queue_work(engine->kworker, &e 57 kthread_queue_work(engine->kworker, &engine->pump_requests); 62 } 58 } 63 59 64 /** 60 /** 65 * crypto_pump_requests - dequeue one request 61 * crypto_pump_requests - dequeue one request from engine queue to process 66 * @engine: the hardware engine 62 * @engine: the hardware engine 67 * @in_kthread: true if we are in the context 63 * @in_kthread: true if we are in the context of the request pump thread 68 * 64 * 69 * This function checks if there is any reques 65 * This function checks if there is any request in the engine queue that 70 * needs processing and if so call out to the 66 * needs processing and if so call out to the driver to initialize hardware 71 * and handle each request. 67 * and handle each request. 72 */ 68 */ 73 static void crypto_pump_requests(struct crypto 69 static void crypto_pump_requests(struct crypto_engine *engine, 74 bool in_kthre 70 bool in_kthread) 75 { 71 { 76 struct crypto_async_request *async_req 72 struct crypto_async_request *async_req, *backlog; 77 struct crypto_engine_alg *alg; << 78 struct crypto_engine_op *op; << 79 unsigned long flags; 73 unsigned long flags; 80 bool was_busy = false; 74 bool was_busy = false; 81 int ret; 75 int ret; >> 76 struct crypto_engine_ctx *enginectx; 82 77 83 spin_lock_irqsave(&engine->queue_lock, 78 spin_lock_irqsave(&engine->queue_lock, flags); 84 79 85 /* Make sure we are not already runnin 80 /* Make sure we are not already running a request */ 86 if (!engine->retry_support && engine-> 81 if (!engine->retry_support && engine->cur_req) 87 goto out; 82 goto out; 88 83 89 /* If another context is idling then d 84 /* If another context is idling then defer */ 90 if (engine->idling) { 85 if (engine->idling) { 91 kthread_queue_work(engine->kwo 86 kthread_queue_work(engine->kworker, &engine->pump_requests); 92 goto out; 87 goto out; 93 } 88 } 94 89 95 /* Check if the engine queue is idle * 90 /* Check if the engine queue is idle */ 96 if (!crypto_queue_len(&engine->queue) 91 if (!crypto_queue_len(&engine->queue) || !engine->running) { 97 if (!engine->busy) 92 if (!engine->busy) 98 goto out; 93 goto out; 99 94 100 /* Only do teardown in the thr 95 /* Only do teardown in the thread */ 101 if (!in_kthread) { 96 if (!in_kthread) { 102 kthread_queue_work(eng 97 kthread_queue_work(engine->kworker, 103 &en 98 &engine->pump_requests); 104 goto out; 99 goto out; 105 } 100 } 106 101 107 engine->busy = false; 102 engine->busy = false; 108 engine->idling = true; 103 engine->idling = true; 109 spin_unlock_irqrestore(&engine 104 spin_unlock_irqrestore(&engine->queue_lock, flags); 110 105 111 if (engine->unprepare_crypt_ha 106 if (engine->unprepare_crypt_hardware && 112 engine->unprepare_crypt_ha 107 engine->unprepare_crypt_hardware(engine)) 113 dev_err(engine->dev, " 108 dev_err(engine->dev, "failed to unprepare crypt hardware\n"); 114 109 115 spin_lock_irqsave(&engine->que 110 spin_lock_irqsave(&engine->queue_lock, flags); 116 engine->idling = false; 111 engine->idling = false; 117 goto out; 112 goto out; 118 } 113 } 119 114 120 start_request: 115 start_request: 121 /* Get the fist request from the engin 116 /* Get the fist request from the engine queue to handle */ 122 backlog = crypto_get_backlog(&engine-> 117 backlog = crypto_get_backlog(&engine->queue); 123 async_req = crypto_dequeue_request(&en 118 async_req = crypto_dequeue_request(&engine->queue); 124 if (!async_req) 119 if (!async_req) 125 goto out; 120 goto out; 126 121 127 /* 122 /* 128 * If hardware doesn't support the ret 123 * If hardware doesn't support the retry mechanism, 129 * keep track of the request we are pr 124 * keep track of the request we are processing now. 130 * We'll need it on completion (crypto 125 * We'll need it on completion (crypto_finalize_request). 131 */ 126 */ 132 if (!engine->retry_support) 127 if (!engine->retry_support) 133 engine->cur_req = async_req; 128 engine->cur_req = async_req; 134 129 >> 130 if (backlog) >> 131 backlog->complete(backlog, -EINPROGRESS); >> 132 135 if (engine->busy) 133 if (engine->busy) 136 was_busy = true; 134 was_busy = true; 137 else 135 else 138 engine->busy = true; 136 engine->busy = true; 139 137 140 spin_unlock_irqrestore(&engine->queue_ 138 spin_unlock_irqrestore(&engine->queue_lock, flags); 141 139 142 /* Until here we get the request need 140 /* Until here we get the request need to be encrypted successfully */ 143 if (!was_busy && engine->prepare_crypt 141 if (!was_busy && engine->prepare_crypt_hardware) { 144 ret = engine->prepare_crypt_ha 142 ret = engine->prepare_crypt_hardware(engine); 145 if (ret) { 143 if (ret) { 146 dev_err(engine->dev, " 144 dev_err(engine->dev, "failed to prepare crypt hardware\n"); 147 goto req_err_1; !! 145 goto req_err_2; 148 } 146 } 149 } 147 } 150 148 151 if (async_req->tfm->__crt_alg->cra_fla !! 149 enginectx = crypto_tfm_ctx(async_req->tfm); 152 alg = container_of(async_req-> !! 150 153 struct cryp !! 151 if (enginectx->op.prepare_request) { 154 op = &alg->op; !! 152 ret = enginectx->op.prepare_request(engine, async_req); 155 } else { !! 153 if (ret) { >> 154 dev_err(engine->dev, "failed to prepare request: %d\n", >> 155 ret); >> 156 goto req_err_2; >> 157 } >> 158 } >> 159 if (!enginectx->op.do_one_request) { 156 dev_err(engine->dev, "failed t 160 dev_err(engine->dev, "failed to do request\n"); 157 ret = -EINVAL; 161 ret = -EINVAL; 158 goto req_err_1; 162 goto req_err_1; 159 } 163 } 160 164 161 ret = op->do_one_request(engine, async !! 165 ret = enginectx->op.do_one_request(engine, async_req); 162 166 163 /* Request unsuccessfully executed by 167 /* Request unsuccessfully executed by hardware */ 164 if (ret < 0) { 168 if (ret < 0) { 165 /* 169 /* 166 * If hardware queue is full ( 170 * If hardware queue is full (-ENOSPC), requeue request 167 * regardless of backlog flag. 171 * regardless of backlog flag. 168 * Otherwise, unprepare and co 172 * Otherwise, unprepare and complete the request. 169 */ 173 */ 170 if (!engine->retry_support || 174 if (!engine->retry_support || 171 (ret != -ENOSPC)) { 175 (ret != -ENOSPC)) { 172 dev_err(engine->dev, 176 dev_err(engine->dev, 173 "Failed to do 177 "Failed to do one request from queue: %d\n", 174 ret); 178 ret); 175 goto req_err_1; 179 goto req_err_1; 176 } 180 } >> 181 /* >> 182 * If retry mechanism is supported, >> 183 * unprepare current request and >> 184 * enqueue it back into crypto-engine queue. >> 185 */ >> 186 if (enginectx->op.unprepare_request) { >> 187 ret = enginectx->op.unprepare_request(engine, >> 188 async_req); >> 189 if (ret) >> 190 dev_err(engine->dev, >> 191 "failed to unprepare request\n"); >> 192 } 177 spin_lock_irqsave(&engine->que 193 spin_lock_irqsave(&engine->queue_lock, flags); 178 /* 194 /* 179 * If hardware was unable to e 195 * If hardware was unable to execute request, enqueue it 180 * back in front of crypto-eng 196 * back in front of crypto-engine queue, to keep the order 181 * of requests. 197 * of requests. 182 */ 198 */ 183 crypto_enqueue_request_head(&e 199 crypto_enqueue_request_head(&engine->queue, async_req); 184 200 185 kthread_queue_work(engine->kwo 201 kthread_queue_work(engine->kworker, &engine->pump_requests); 186 goto out; 202 goto out; 187 } 203 } 188 204 189 goto retry; 205 goto retry; 190 206 191 req_err_1: 207 req_err_1: 192 crypto_request_complete(async_req, ret !! 208 if (enginectx->op.unprepare_request) { >> 209 ret = enginectx->op.unprepare_request(engine, async_req); >> 210 if (ret) >> 211 dev_err(engine->dev, "failed to unprepare request\n"); >> 212 } 193 213 194 retry: !! 214 req_err_2: 195 if (backlog) !! 215 async_req->complete(async_req, ret); 196 crypto_request_complete(backlo << 197 216 >> 217 retry: 198 /* If retry mechanism is supported, se 218 /* If retry mechanism is supported, send new requests to engine */ 199 if (engine->retry_support) { 219 if (engine->retry_support) { 200 spin_lock_irqsave(&engine->que 220 spin_lock_irqsave(&engine->queue_lock, flags); 201 goto start_request; 221 goto start_request; 202 } 222 } 203 return; 223 return; 204 224 205 out: 225 out: 206 spin_unlock_irqrestore(&engine->queue_ 226 spin_unlock_irqrestore(&engine->queue_lock, flags); 207 227 208 /* 228 /* 209 * Batch requests is possible only if 229 * Batch requests is possible only if 210 * hardware can enqueue multiple reque 230 * hardware can enqueue multiple requests 211 */ 231 */ 212 if (engine->do_batch_requests) { 232 if (engine->do_batch_requests) { 213 ret = engine->do_batch_request 233 ret = engine->do_batch_requests(engine); 214 if (ret) 234 if (ret) 215 dev_err(engine->dev, " 235 dev_err(engine->dev, "failed to do batch requests: %d\n", 216 ret); 236 ret); 217 } 237 } 218 238 219 return; 239 return; 220 } 240 } 221 241 222 static void crypto_pump_work(struct kthread_wo 242 static void crypto_pump_work(struct kthread_work *work) 223 { 243 { 224 struct crypto_engine *engine = 244 struct crypto_engine *engine = 225 container_of(work, struct cryp 245 container_of(work, struct crypto_engine, pump_requests); 226 246 227 crypto_pump_requests(engine, true); 247 crypto_pump_requests(engine, true); 228 } 248 } 229 249 230 /** 250 /** 231 * crypto_transfer_request - transfer the new 251 * crypto_transfer_request - transfer the new request into the engine queue 232 * @engine: the hardware engine 252 * @engine: the hardware engine 233 * @req: the request need to be listed into th 253 * @req: the request need to be listed into the engine queue 234 * @need_pump: indicates whether queue the pum << 235 */ 254 */ 236 static int crypto_transfer_request(struct cryp 255 static int crypto_transfer_request(struct crypto_engine *engine, 237 struct cryp 256 struct crypto_async_request *req, 238 bool need_p 257 bool need_pump) 239 { 258 { 240 unsigned long flags; 259 unsigned long flags; 241 int ret; 260 int ret; 242 261 243 spin_lock_irqsave(&engine->queue_lock, 262 spin_lock_irqsave(&engine->queue_lock, flags); 244 263 245 if (!engine->running) { 264 if (!engine->running) { 246 spin_unlock_irqrestore(&engine 265 spin_unlock_irqrestore(&engine->queue_lock, flags); 247 return -ESHUTDOWN; 266 return -ESHUTDOWN; 248 } 267 } 249 268 250 ret = crypto_enqueue_request(&engine-> 269 ret = crypto_enqueue_request(&engine->queue, req); 251 270 252 if (!engine->busy && need_pump) 271 if (!engine->busy && need_pump) 253 kthread_queue_work(engine->kwo 272 kthread_queue_work(engine->kworker, &engine->pump_requests); 254 273 255 spin_unlock_irqrestore(&engine->queue_ 274 spin_unlock_irqrestore(&engine->queue_lock, flags); 256 return ret; 275 return ret; 257 } 276 } 258 277 259 /** 278 /** 260 * crypto_transfer_request_to_engine - transfe 279 * crypto_transfer_request_to_engine - transfer one request to list 261 * into the engine queue 280 * into the engine queue 262 * @engine: the hardware engine 281 * @engine: the hardware engine 263 * @req: the request need to be listed into th 282 * @req: the request need to be listed into the engine queue 264 */ 283 */ 265 static int crypto_transfer_request_to_engine(s 284 static int crypto_transfer_request_to_engine(struct crypto_engine *engine, 266 s 285 struct crypto_async_request *req) 267 { 286 { 268 return crypto_transfer_request(engine, 287 return crypto_transfer_request(engine, req, true); 269 } 288 } 270 289 271 /** 290 /** 272 * crypto_transfer_aead_request_to_engine - tr 291 * crypto_transfer_aead_request_to_engine - transfer one aead_request 273 * to list into the engine queue 292 * to list into the engine queue 274 * @engine: the hardware engine 293 * @engine: the hardware engine 275 * @req: the request need to be listed into th 294 * @req: the request need to be listed into the engine queue 276 */ 295 */ 277 int crypto_transfer_aead_request_to_engine(str 296 int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine, 278 str 297 struct aead_request *req) 279 { 298 { 280 return crypto_transfer_request_to_engi 299 return crypto_transfer_request_to_engine(engine, &req->base); 281 } 300 } 282 EXPORT_SYMBOL_GPL(crypto_transfer_aead_request 301 EXPORT_SYMBOL_GPL(crypto_transfer_aead_request_to_engine); 283 302 284 /** 303 /** 285 * crypto_transfer_akcipher_request_to_engine 304 * crypto_transfer_akcipher_request_to_engine - transfer one akcipher_request 286 * to list into the engine queue 305 * to list into the engine queue 287 * @engine: the hardware engine 306 * @engine: the hardware engine 288 * @req: the request need to be listed into th 307 * @req: the request need to be listed into the engine queue 289 */ 308 */ 290 int crypto_transfer_akcipher_request_to_engine 309 int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine, 291 310 struct akcipher_request *req) 292 { 311 { 293 return crypto_transfer_request_to_engi 312 return crypto_transfer_request_to_engine(engine, &req->base); 294 } 313 } 295 EXPORT_SYMBOL_GPL(crypto_transfer_akcipher_req 314 EXPORT_SYMBOL_GPL(crypto_transfer_akcipher_request_to_engine); 296 315 297 /** 316 /** 298 * crypto_transfer_hash_request_to_engine - tr 317 * crypto_transfer_hash_request_to_engine - transfer one ahash_request 299 * to list into the engine queue 318 * to list into the engine queue 300 * @engine: the hardware engine 319 * @engine: the hardware engine 301 * @req: the request need to be listed into th 320 * @req: the request need to be listed into the engine queue 302 */ 321 */ 303 int crypto_transfer_hash_request_to_engine(str 322 int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine, 304 str 323 struct ahash_request *req) 305 { 324 { 306 return crypto_transfer_request_to_engi 325 return crypto_transfer_request_to_engine(engine, &req->base); 307 } 326 } 308 EXPORT_SYMBOL_GPL(crypto_transfer_hash_request 327 EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine); 309 328 310 /** 329 /** 311 * crypto_transfer_kpp_request_to_engine - tra << 312 * into the engine queue << 313 * @engine: the hardware engine << 314 * @req: the request need to be listed into th << 315 */ << 316 int crypto_transfer_kpp_request_to_engine(stru << 317 stru << 318 { << 319 return crypto_transfer_request_to_engi << 320 } << 321 EXPORT_SYMBOL_GPL(crypto_transfer_kpp_request_ << 322 << 323 /** << 324 * crypto_transfer_skcipher_request_to_engine 330 * crypto_transfer_skcipher_request_to_engine - transfer one skcipher_request 325 * to list into the engine queue 331 * to list into the engine queue 326 * @engine: the hardware engine 332 * @engine: the hardware engine 327 * @req: the request need to be listed into th 333 * @req: the request need to be listed into the engine queue 328 */ 334 */ 329 int crypto_transfer_skcipher_request_to_engine 335 int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine, 330 336 struct skcipher_request *req) 331 { 337 { 332 return crypto_transfer_request_to_engi 338 return crypto_transfer_request_to_engine(engine, &req->base); 333 } 339 } 334 EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_req 340 EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request_to_engine); 335 341 336 /** 342 /** 337 * crypto_finalize_aead_request - finalize one 343 * crypto_finalize_aead_request - finalize one aead_request if 338 * the request is done 344 * the request is done 339 * @engine: the hardware engine 345 * @engine: the hardware engine 340 * @req: the request need to be finalized 346 * @req: the request need to be finalized 341 * @err: error number 347 * @err: error number 342 */ 348 */ 343 void crypto_finalize_aead_request(struct crypt 349 void crypto_finalize_aead_request(struct crypto_engine *engine, 344 struct aead_ 350 struct aead_request *req, int err) 345 { 351 { 346 return crypto_finalize_request(engine, 352 return crypto_finalize_request(engine, &req->base, err); 347 } 353 } 348 EXPORT_SYMBOL_GPL(crypto_finalize_aead_request 354 EXPORT_SYMBOL_GPL(crypto_finalize_aead_request); 349 355 350 /** 356 /** 351 * crypto_finalize_akcipher_request - finalize 357 * crypto_finalize_akcipher_request - finalize one akcipher_request if 352 * the request is done 358 * the request is done 353 * @engine: the hardware engine 359 * @engine: the hardware engine 354 * @req: the request need to be finalized 360 * @req: the request need to be finalized 355 * @err: error number 361 * @err: error number 356 */ 362 */ 357 void crypto_finalize_akcipher_request(struct c 363 void crypto_finalize_akcipher_request(struct crypto_engine *engine, 358 struct a 364 struct akcipher_request *req, int err) 359 { 365 { 360 return crypto_finalize_request(engine, 366 return crypto_finalize_request(engine, &req->base, err); 361 } 367 } 362 EXPORT_SYMBOL_GPL(crypto_finalize_akcipher_req 368 EXPORT_SYMBOL_GPL(crypto_finalize_akcipher_request); 363 369 364 /** 370 /** 365 * crypto_finalize_hash_request - finalize one 371 * crypto_finalize_hash_request - finalize one ahash_request if 366 * the request is done 372 * the request is done 367 * @engine: the hardware engine 373 * @engine: the hardware engine 368 * @req: the request need to be finalized 374 * @req: the request need to be finalized 369 * @err: error number 375 * @err: error number 370 */ 376 */ 371 void crypto_finalize_hash_request(struct crypt 377 void crypto_finalize_hash_request(struct crypto_engine *engine, 372 struct ahash 378 struct ahash_request *req, int err) 373 { 379 { 374 return crypto_finalize_request(engine, 380 return crypto_finalize_request(engine, &req->base, err); 375 } 381 } 376 EXPORT_SYMBOL_GPL(crypto_finalize_hash_request 382 EXPORT_SYMBOL_GPL(crypto_finalize_hash_request); 377 383 378 /** 384 /** 379 * crypto_finalize_kpp_request - finalize one << 380 * @engine: the hardware engine << 381 * @req: the request need to be finalized << 382 * @err: error number << 383 */ << 384 void crypto_finalize_kpp_request(struct crypto << 385 struct kpp_re << 386 { << 387 return crypto_finalize_request(engine, << 388 } << 389 EXPORT_SYMBOL_GPL(crypto_finalize_kpp_request) << 390 << 391 /** << 392 * crypto_finalize_skcipher_request - finalize 385 * crypto_finalize_skcipher_request - finalize one skcipher_request if 393 * the request is done 386 * the request is done 394 * @engine: the hardware engine 387 * @engine: the hardware engine 395 * @req: the request need to be finalized 388 * @req: the request need to be finalized 396 * @err: error number 389 * @err: error number 397 */ 390 */ 398 void crypto_finalize_skcipher_request(struct c 391 void crypto_finalize_skcipher_request(struct crypto_engine *engine, 399 struct s 392 struct skcipher_request *req, int err) 400 { 393 { 401 return crypto_finalize_request(engine, 394 return crypto_finalize_request(engine, &req->base, err); 402 } 395 } 403 EXPORT_SYMBOL_GPL(crypto_finalize_skcipher_req 396 EXPORT_SYMBOL_GPL(crypto_finalize_skcipher_request); 404 397 405 /** 398 /** 406 * crypto_engine_start - start the hardware en 399 * crypto_engine_start - start the hardware engine 407 * @engine: the hardware engine need to be sta 400 * @engine: the hardware engine need to be started 408 * 401 * 409 * Return 0 on success, else on fail. 402 * Return 0 on success, else on fail. 410 */ 403 */ 411 int crypto_engine_start(struct crypto_engine * 404 int crypto_engine_start(struct crypto_engine *engine) 412 { 405 { 413 unsigned long flags; 406 unsigned long flags; 414 407 415 spin_lock_irqsave(&engine->queue_lock, 408 spin_lock_irqsave(&engine->queue_lock, flags); 416 409 417 if (engine->running || engine->busy) { 410 if (engine->running || engine->busy) { 418 spin_unlock_irqrestore(&engine 411 spin_unlock_irqrestore(&engine->queue_lock, flags); 419 return -EBUSY; 412 return -EBUSY; 420 } 413 } 421 414 422 engine->running = true; 415 engine->running = true; 423 spin_unlock_irqrestore(&engine->queue_ 416 spin_unlock_irqrestore(&engine->queue_lock, flags); 424 417 425 kthread_queue_work(engine->kworker, &e 418 kthread_queue_work(engine->kworker, &engine->pump_requests); 426 419 427 return 0; 420 return 0; 428 } 421 } 429 EXPORT_SYMBOL_GPL(crypto_engine_start); 422 EXPORT_SYMBOL_GPL(crypto_engine_start); 430 423 431 /** 424 /** 432 * crypto_engine_stop - stop the hardware engi 425 * crypto_engine_stop - stop the hardware engine 433 * @engine: the hardware engine need to be sto 426 * @engine: the hardware engine need to be stopped 434 * 427 * 435 * Return 0 on success, else on fail. 428 * Return 0 on success, else on fail. 436 */ 429 */ 437 int crypto_engine_stop(struct crypto_engine *e 430 int crypto_engine_stop(struct crypto_engine *engine) 438 { 431 { 439 unsigned long flags; 432 unsigned long flags; 440 unsigned int limit = 500; 433 unsigned int limit = 500; 441 int ret = 0; 434 int ret = 0; 442 435 443 spin_lock_irqsave(&engine->queue_lock, 436 spin_lock_irqsave(&engine->queue_lock, flags); 444 437 445 /* 438 /* 446 * If the engine queue is not empty or 439 * If the engine queue is not empty or the engine is on busy state, 447 * we need to wait for a while to pump 440 * we need to wait for a while to pump the requests of engine queue. 448 */ 441 */ 449 while ((crypto_queue_len(&engine->queu 442 while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) { 450 spin_unlock_irqrestore(&engine 443 spin_unlock_irqrestore(&engine->queue_lock, flags); 451 msleep(20); 444 msleep(20); 452 spin_lock_irqsave(&engine->que 445 spin_lock_irqsave(&engine->queue_lock, flags); 453 } 446 } 454 447 455 if (crypto_queue_len(&engine->queue) | 448 if (crypto_queue_len(&engine->queue) || engine->busy) 456 ret = -EBUSY; 449 ret = -EBUSY; 457 else 450 else 458 engine->running = false; 451 engine->running = false; 459 452 460 spin_unlock_irqrestore(&engine->queue_ 453 spin_unlock_irqrestore(&engine->queue_lock, flags); 461 454 462 if (ret) 455 if (ret) 463 dev_warn(engine->dev, "could n 456 dev_warn(engine->dev, "could not stop engine\n"); 464 457 465 return ret; 458 return ret; 466 } 459 } 467 EXPORT_SYMBOL_GPL(crypto_engine_stop); 460 EXPORT_SYMBOL_GPL(crypto_engine_stop); 468 461 469 /** 462 /** 470 * crypto_engine_alloc_init_and_set - allocate 463 * crypto_engine_alloc_init_and_set - allocate crypto hardware engine structure 471 * and initialize it by setting the maximum nu 464 * and initialize it by setting the maximum number of entries in the software 472 * crypto-engine queue. 465 * crypto-engine queue. 473 * @dev: the device attached with one hardware 466 * @dev: the device attached with one hardware engine 474 * @retry_support: whether hardware has suppor 467 * @retry_support: whether hardware has support for retry mechanism 475 * @cbk_do_batch: pointer to a callback functi !! 468 * @cbk_do_batch: pointer to a callback function to be invoked when executing a 476 * a batch of requests. 469 * a batch of requests. 477 * This has the form: 470 * This has the form: 478 * callback(struct crypto_engin 471 * callback(struct crypto_engine *engine) 479 * where: 472 * where: 480 * engine: the crypto engine st !! 473 * @engine: the crypto engine structure. 481 * @rt: whether this queue is set to run as a 474 * @rt: whether this queue is set to run as a realtime task 482 * @qlen: maximum size of the crypto-engine qu 475 * @qlen: maximum size of the crypto-engine queue 483 * 476 * 484 * This must be called from context that can s 477 * This must be called from context that can sleep. 485 * Return: the crypto engine structure on succ 478 * Return: the crypto engine structure on success, else NULL. 486 */ 479 */ 487 struct crypto_engine *crypto_engine_alloc_init 480 struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev, 488 481 bool retry_support, 489 482 int (*cbk_do_batch)(struct crypto_engine *engine), 490 483 bool rt, int qlen) 491 { 484 { 492 struct crypto_engine *engine; 485 struct crypto_engine *engine; 493 486 494 if (!dev) 487 if (!dev) 495 return NULL; 488 return NULL; 496 489 497 engine = devm_kzalloc(dev, sizeof(*eng 490 engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL); 498 if (!engine) 491 if (!engine) 499 return NULL; 492 return NULL; 500 493 501 engine->dev = dev; 494 engine->dev = dev; 502 engine->rt = rt; 495 engine->rt = rt; 503 engine->running = false; 496 engine->running = false; 504 engine->busy = false; 497 engine->busy = false; 505 engine->idling = false; 498 engine->idling = false; 506 engine->retry_support = retry_support; 499 engine->retry_support = retry_support; 507 engine->priv_data = dev; 500 engine->priv_data = dev; 508 /* 501 /* 509 * Batch requests is possible only if 502 * Batch requests is possible only if 510 * hardware has support for retry mech 503 * hardware has support for retry mechanism. 511 */ 504 */ 512 engine->do_batch_requests = retry_supp 505 engine->do_batch_requests = retry_support ? cbk_do_batch : NULL; 513 506 514 snprintf(engine->name, sizeof(engine-> 507 snprintf(engine->name, sizeof(engine->name), 515 "%s-engine", dev_name(dev)); 508 "%s-engine", dev_name(dev)); 516 509 517 crypto_init_queue(&engine->queue, qlen 510 crypto_init_queue(&engine->queue, qlen); 518 spin_lock_init(&engine->queue_lock); 511 spin_lock_init(&engine->queue_lock); 519 512 520 engine->kworker = kthread_create_worke 513 engine->kworker = kthread_create_worker(0, "%s", engine->name); 521 if (IS_ERR(engine->kworker)) { 514 if (IS_ERR(engine->kworker)) { 522 dev_err(dev, "failed to create 515 dev_err(dev, "failed to create crypto request pump task\n"); 523 return NULL; 516 return NULL; 524 } 517 } 525 kthread_init_work(&engine->pump_reques 518 kthread_init_work(&engine->pump_requests, crypto_pump_work); 526 519 527 if (engine->rt) { 520 if (engine->rt) { 528 dev_info(dev, "will run reques 521 dev_info(dev, "will run requests pump with realtime priority\n"); 529 sched_set_fifo(engine->kworker 522 sched_set_fifo(engine->kworker->task); 530 } 523 } 531 524 532 return engine; 525 return engine; 533 } 526 } 534 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init_and 527 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init_and_set); 535 528 536 /** 529 /** 537 * crypto_engine_alloc_init - allocate crypto 530 * crypto_engine_alloc_init - allocate crypto hardware engine structure and 538 * initialize it. 531 * initialize it. 539 * @dev: the device attached with one hardware 532 * @dev: the device attached with one hardware engine 540 * @rt: whether this queue is set to run as a 533 * @rt: whether this queue is set to run as a realtime task 541 * 534 * 542 * This must be called from context that can s 535 * This must be called from context that can sleep. 543 * Return: the crypto engine structure on succ 536 * Return: the crypto engine structure on success, else NULL. 544 */ 537 */ 545 struct crypto_engine *crypto_engine_alloc_init 538 struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt) 546 { 539 { 547 return crypto_engine_alloc_init_and_se 540 return crypto_engine_alloc_init_and_set(dev, false, NULL, rt, 548 541 CRYPTO_ENGINE_MAX_QLEN); 549 } 542 } 550 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init); 543 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init); 551 544 552 /** 545 /** 553 * crypto_engine_exit - free the resources of 546 * crypto_engine_exit - free the resources of hardware engine when exit 554 * @engine: the hardware engine need to be fre 547 * @engine: the hardware engine need to be freed >> 548 * >> 549 * Return 0 for success. 555 */ 550 */ 556 void crypto_engine_exit(struct crypto_engine * !! 551 int crypto_engine_exit(struct crypto_engine *engine) 557 { 552 { 558 int ret; 553 int ret; 559 554 560 ret = crypto_engine_stop(engine); 555 ret = crypto_engine_stop(engine); 561 if (ret) 556 if (ret) 562 return; !! 557 return ret; 563 558 564 kthread_destroy_worker(engine->kworker 559 kthread_destroy_worker(engine->kworker); 565 } << 566 EXPORT_SYMBOL_GPL(crypto_engine_exit); << 567 << 568 int crypto_engine_register_aead(struct aead_en << 569 { << 570 if (!alg->op.do_one_request) << 571 return -EINVAL; << 572 << 573 alg->base.base.cra_flags |= CRYPTO_ALG << 574 << 575 return crypto_register_aead(&alg->base << 576 } << 577 EXPORT_SYMBOL_GPL(crypto_engine_register_aead) << 578 << 579 void crypto_engine_unregister_aead(struct aead << 580 { << 581 crypto_unregister_aead(&alg->base); << 582 } << 583 EXPORT_SYMBOL_GPL(crypto_engine_unregister_aea << 584 << 585 int crypto_engine_register_aeads(struct aead_e << 586 { << 587 int i, ret; << 588 << 589 for (i = 0; i < count; i++) { << 590 ret = crypto_engine_register_a << 591 if (ret) << 592 goto err; << 593 } << 594 560 595 return 0; 561 return 0; 596 << 597 err: << 598 crypto_engine_unregister_aeads(algs, i << 599 << 600 return ret; << 601 } 562 } 602 EXPORT_SYMBOL_GPL(crypto_engine_register_aeads !! 563 EXPORT_SYMBOL_GPL(crypto_engine_exit); 603 << 604 void crypto_engine_unregister_aeads(struct aea << 605 { << 606 int i; << 607 << 608 for (i = count - 1; i >= 0; --i) << 609 crypto_engine_unregister_aead( << 610 } << 611 EXPORT_SYMBOL_GPL(crypto_engine_unregister_aea << 612 << 613 int crypto_engine_register_ahash(struct ahash_ << 614 { << 615 if (!alg->op.do_one_request) << 616 return -EINVAL; << 617 << 618 alg->base.halg.base.cra_flags |= CRYPT << 619 << 620 return crypto_register_ahash(&alg->bas << 621 } << 622 EXPORT_SYMBOL_GPL(crypto_engine_register_ahash << 623 << 624 void crypto_engine_unregister_ahash(struct aha << 625 { << 626 crypto_unregister_ahash(&alg->base); << 627 } << 628 EXPORT_SYMBOL_GPL(crypto_engine_unregister_aha << 629 << 630 int crypto_engine_register_ahashes(struct ahas << 631 { << 632 int i, ret; << 633 << 634 for (i = 0; i < count; i++) { << 635 ret = crypto_engine_register_a << 636 if (ret) << 637 goto err; << 638 } << 639 << 640 return 0; << 641 << 642 err: << 643 crypto_engine_unregister_ahashes(algs, << 644 << 645 return ret; << 646 } << 647 EXPORT_SYMBOL_GPL(crypto_engine_register_ahash << 648 << 649 void crypto_engine_unregister_ahashes(struct a << 650 int coun << 651 { << 652 int i; << 653 << 654 for (i = count - 1; i >= 0; --i) << 655 crypto_engine_unregister_ahash << 656 } << 657 EXPORT_SYMBOL_GPL(crypto_engine_unregister_aha << 658 << 659 int crypto_engine_register_akcipher(struct akc << 660 { << 661 if (!alg->op.do_one_request) << 662 return -EINVAL; << 663 << 664 alg->base.base.cra_flags |= CRYPTO_ALG << 665 << 666 return crypto_register_akcipher(&alg-> << 667 } << 668 EXPORT_SYMBOL_GPL(crypto_engine_register_akcip << 669 << 670 void crypto_engine_unregister_akcipher(struct << 671 { << 672 crypto_unregister_akcipher(&alg->base) << 673 } << 674 EXPORT_SYMBOL_GPL(crypto_engine_unregister_akc << 675 << 676 int crypto_engine_register_kpp(struct kpp_engi << 677 { << 678 if (!alg->op.do_one_request) << 679 return -EINVAL; << 680 << 681 alg->base.base.cra_flags |= CRYPTO_ALG << 682 << 683 return crypto_register_kpp(&alg->base) << 684 } << 685 EXPORT_SYMBOL_GPL(crypto_engine_register_kpp); << 686 << 687 void crypto_engine_unregister_kpp(struct kpp_e << 688 { << 689 crypto_unregister_kpp(&alg->base); << 690 } << 691 EXPORT_SYMBOL_GPL(crypto_engine_unregister_kpp << 692 << 693 int crypto_engine_register_skcipher(struct skc << 694 { << 695 if (!alg->op.do_one_request) << 696 return -EINVAL; << 697 << 698 alg->base.base.cra_flags |= CRYPTO_ALG << 699 << 700 return crypto_register_skcipher(&alg-> << 701 } << 702 EXPORT_SYMBOL_GPL(crypto_engine_register_skcip << 703 << 704 void crypto_engine_unregister_skcipher(struct << 705 { << 706 return crypto_unregister_skcipher(&alg << 707 } << 708 EXPORT_SYMBOL_GPL(crypto_engine_unregister_skc << 709 << 710 int crypto_engine_register_skciphers(struct sk << 711 int count << 712 { << 713 int i, ret; << 714 << 715 for (i = 0; i < count; i++) { << 716 ret = crypto_engine_register_s << 717 if (ret) << 718 goto err; << 719 } << 720 << 721 return 0; << 722 << 723 err: << 724 crypto_engine_unregister_skciphers(alg << 725 << 726 return ret; << 727 } << 728 EXPORT_SYMBOL_GPL(crypto_engine_register_skcip << 729 << 730 void crypto_engine_unregister_skciphers(struct << 731 int co << 732 { << 733 int i; << 734 << 735 for (i = count - 1; i >= 0; --i) << 736 crypto_engine_unregister_skcip << 737 } << 738 EXPORT_SYMBOL_GPL(crypto_engine_unregister_skc << 739 564 740 MODULE_LICENSE("GPL"); 565 MODULE_LICENSE("GPL"); 741 MODULE_DESCRIPTION("Crypto hardware engine fra 566 MODULE_DESCRIPTION("Crypto hardware engine framework"); 742 567
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.