Lines Matching full:engine

3  * Handle async block request by crypto hardware engine.
12 #include <crypto/engine.h>
20 * @engine: the hardware engine
24 static void crypto_finalize_request(struct crypto_engine *engine, in crypto_finalize_request() argument
32 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_finalize_request()
33 if (engine->cur_req == req) in crypto_finalize_request()
35 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_finalize_request()
39 if (engine->cur_req_prepared && in crypto_finalize_request()
41 ret = enginectx->op.unprepare_request(engine, req); in crypto_finalize_request()
43 dev_err(engine->dev, "failed to unprepare request\n"); in crypto_finalize_request()
45 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_finalize_request()
46 engine->cur_req = NULL; in crypto_finalize_request()
47 engine->cur_req_prepared = false; in crypto_finalize_request()
48 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_finalize_request()
53 kthread_queue_work(engine->kworker, &engine->pump_requests); in crypto_finalize_request()
57 * crypto_pump_requests - dequeue one request from engine queue to process
58 * @engine: the hardware engine
61 * This function checks if there is any request in the engine queue that
65 static void crypto_pump_requests(struct crypto_engine *engine, in crypto_pump_requests() argument
74 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_pump_requests()
77 if (engine->cur_req) in crypto_pump_requests()
81 if (engine->idling) { in crypto_pump_requests()
82 kthread_queue_work(engine->kworker, &engine->pump_requests); in crypto_pump_requests()
86 /* Check if the engine queue is idle */ in crypto_pump_requests()
87 if (!crypto_queue_len(&engine->queue) || !engine->running) { in crypto_pump_requests()
88 if (!engine->busy) in crypto_pump_requests()
93 kthread_queue_work(engine->kworker, in crypto_pump_requests()
94 &engine->pump_requests); in crypto_pump_requests()
98 engine->busy = false; in crypto_pump_requests()
99 engine->idling = true; in crypto_pump_requests()
100 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_pump_requests()
102 if (engine->unprepare_crypt_hardware && in crypto_pump_requests()
103 engine->unprepare_crypt_hardware(engine)) in crypto_pump_requests()
104 dev_err(engine->dev, "failed to unprepare crypt hardware\n"); in crypto_pump_requests()
106 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_pump_requests()
107 engine->idling = false; in crypto_pump_requests()
111 /* Get the fist request from the engine queue to handle */ in crypto_pump_requests()
112 backlog = crypto_get_backlog(&engine->queue); in crypto_pump_requests()
113 async_req = crypto_dequeue_request(&engine->queue); in crypto_pump_requests()
117 engine->cur_req = async_req; in crypto_pump_requests()
121 if (engine->busy) in crypto_pump_requests()
124 engine->busy = true; in crypto_pump_requests()
126 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_pump_requests()
129 if (!was_busy && engine->prepare_crypt_hardware) { in crypto_pump_requests()
130 ret = engine->prepare_crypt_hardware(engine); in crypto_pump_requests()
132 dev_err(engine->dev, "failed to prepare crypt hardware\n"); in crypto_pump_requests()
140 ret = enginectx->op.prepare_request(engine, async_req); in crypto_pump_requests()
142 dev_err(engine->dev, "failed to prepare request: %d\n", in crypto_pump_requests()
146 engine->cur_req_prepared = true; in crypto_pump_requests()
149 dev_err(engine->dev, "failed to do request\n"); in crypto_pump_requests()
153 ret = enginectx->op.do_one_request(engine, async_req); in crypto_pump_requests()
155 dev_err(engine->dev, "Failed to do one request from queue: %d\n", ret); in crypto_pump_requests()
161 crypto_finalize_request(engine, async_req, ret); in crypto_pump_requests()
165 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_pump_requests()
170 struct crypto_engine *engine = in crypto_pump_work() local
173 crypto_pump_requests(engine, true); in crypto_pump_work()
177 * crypto_transfer_request - transfer the new request into the engine queue
178 * @engine: the hardware engine
179 * @req: the request need to be listed into the engine queue
181 static int crypto_transfer_request(struct crypto_engine *engine, in crypto_transfer_request() argument
188 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_transfer_request()
190 if (!engine->running) { in crypto_transfer_request()
191 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_transfer_request()
195 ret = crypto_enqueue_request(&engine->queue, req); in crypto_transfer_request()
197 if (!engine->busy && need_pump) in crypto_transfer_request()
198 kthread_queue_work(engine->kworker, &engine->pump_requests); in crypto_transfer_request()
200 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_transfer_request()
206 * into the engine queue
207 * @engine: the hardware engine
208 * @req: the request need to be listed into the engine queue
210 static int crypto_transfer_request_to_engine(struct crypto_engine *engine, in crypto_transfer_request_to_engine() argument
213 return crypto_transfer_request(engine, req, true); in crypto_transfer_request_to_engine()
218 * to list into the engine queue
219 * @engine: the hardware engine
220 * @req: the request need to be listed into the engine queue
223 int crypto_transfer_ablkcipher_request_to_engine(struct crypto_engine *engine, in crypto_transfer_ablkcipher_request_to_engine() argument
226 return crypto_transfer_request_to_engine(engine, &req->base); in crypto_transfer_ablkcipher_request_to_engine()
232 * to list into the engine queue
233 * @engine: the hardware engine
234 * @req: the request need to be listed into the engine queue
236 int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine, in crypto_transfer_aead_request_to_engine() argument
239 return crypto_transfer_request_to_engine(engine, &req->base); in crypto_transfer_aead_request_to_engine()
245 * to list into the engine queue
246 * @engine: the hardware engine
247 * @req: the request need to be listed into the engine queue
249 int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine, in crypto_transfer_akcipher_request_to_engine() argument
252 return crypto_transfer_request_to_engine(engine, &req->base); in crypto_transfer_akcipher_request_to_engine()
258 * to list into the engine queue
259 * @engine: the hardware engine
260 * @req: the request need to be listed into the engine queue
262 int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine, in crypto_transfer_hash_request_to_engine() argument
265 return crypto_transfer_request_to_engine(engine, &req->base); in crypto_transfer_hash_request_to_engine()
271 * to list into the engine queue
272 * @engine: the hardware engine
273 * @req: the request need to be listed into the engine queue
275 int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine, in crypto_transfer_skcipher_request_to_engine() argument
278 return crypto_transfer_request_to_engine(engine, &req->base); in crypto_transfer_skcipher_request_to_engine()
285 * @engine: the hardware engine
290 void crypto_finalize_ablkcipher_request(struct crypto_engine *engine, in crypto_finalize_ablkcipher_request() argument
293 return crypto_finalize_request(engine, &req->base, err); in crypto_finalize_ablkcipher_request()
300 * @engine: the hardware engine
304 void crypto_finalize_aead_request(struct crypto_engine *engine, in crypto_finalize_aead_request() argument
307 return crypto_finalize_request(engine, &req->base, err); in crypto_finalize_aead_request()
314 * @engine: the hardware engine
318 void crypto_finalize_akcipher_request(struct crypto_engine *engine, in crypto_finalize_akcipher_request() argument
321 return crypto_finalize_request(engine, &req->base, err); in crypto_finalize_akcipher_request()
328 * @engine: the hardware engine
332 void crypto_finalize_hash_request(struct crypto_engine *engine, in crypto_finalize_hash_request() argument
335 return crypto_finalize_request(engine, &req->base, err); in crypto_finalize_hash_request()
342 * @engine: the hardware engine
346 void crypto_finalize_skcipher_request(struct crypto_engine *engine, in crypto_finalize_skcipher_request() argument
349 return crypto_finalize_request(engine, &req->base, err); in crypto_finalize_skcipher_request()
354 * crypto_engine_start - start the hardware engine
355 * @engine: the hardware engine need to be started
359 int crypto_engine_start(struct crypto_engine *engine) in crypto_engine_start() argument
363 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_engine_start()
365 if (engine->running || engine->busy) { in crypto_engine_start()
366 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_engine_start()
370 engine->running = true; in crypto_engine_start()
371 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_engine_start()
373 kthread_queue_work(engine->kworker, &engine->pump_requests); in crypto_engine_start()
380 * crypto_engine_stop - stop the hardware engine
381 * @engine: the hardware engine need to be stopped
385 int crypto_engine_stop(struct crypto_engine *engine) in crypto_engine_stop() argument
391 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_engine_stop()
394 * If the engine queue is not empty or the engine is on busy state, in crypto_engine_stop()
395 * we need to wait for a while to pump the requests of engine queue. in crypto_engine_stop()
397 while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) { in crypto_engine_stop()
398 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_engine_stop()
400 spin_lock_irqsave(&engine->queue_lock, flags); in crypto_engine_stop()
403 if (crypto_queue_len(&engine->queue) || engine->busy) in crypto_engine_stop()
406 engine->running = false; in crypto_engine_stop()
408 spin_unlock_irqrestore(&engine->queue_lock, flags); in crypto_engine_stop()
411 dev_warn(engine->dev, "could not stop engine\n"); in crypto_engine_stop()
418 * crypto_engine_alloc_init - allocate crypto hardware engine structure and
420 * @dev: the device attached with one hardware engine
424 * Return: the crypto engine structure on success, else NULL.
429 struct crypto_engine *engine; in crypto_engine_alloc_init() local
434 engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL); in crypto_engine_alloc_init()
435 if (!engine) in crypto_engine_alloc_init()
438 engine->dev = dev; in crypto_engine_alloc_init()
439 engine->rt = rt; in crypto_engine_alloc_init()
440 engine->running = false; in crypto_engine_alloc_init()
441 engine->busy = false; in crypto_engine_alloc_init()
442 engine->idling = false; in crypto_engine_alloc_init()
443 engine->cur_req_prepared = false; in crypto_engine_alloc_init()
444 engine->priv_data = dev; in crypto_engine_alloc_init()
445 snprintf(engine->name, sizeof(engine->name), in crypto_engine_alloc_init()
446 "%s-engine", dev_name(dev)); in crypto_engine_alloc_init()
448 crypto_init_queue(&engine->queue, CRYPTO_ENGINE_MAX_QLEN); in crypto_engine_alloc_init()
449 spin_lock_init(&engine->queue_lock); in crypto_engine_alloc_init()
451 engine->kworker = kthread_create_worker(0, "%s", engine->name); in crypto_engine_alloc_init()
452 if (IS_ERR(engine->kworker)) { in crypto_engine_alloc_init()
456 kthread_init_work(&engine->pump_requests, crypto_pump_work); in crypto_engine_alloc_init()
458 if (engine->rt) { in crypto_engine_alloc_init()
460 sched_setscheduler(engine->kworker->task, SCHED_FIFO, &param); in crypto_engine_alloc_init()
463 return engine; in crypto_engine_alloc_init()
468 * crypto_engine_exit - free the resources of hardware engine when exit
469 * @engine: the hardware engine need to be freed
473 int crypto_engine_exit(struct crypto_engine *engine) in crypto_engine_exit() argument
477 ret = crypto_engine_stop(engine); in crypto_engine_exit()
481 kthread_destroy_worker(engine->kworker); in crypto_engine_exit()
488 MODULE_DESCRIPTION("Crypto hardware engine framework");