2 * Handle async block request by crypto hardware engine.
4 * Copyright (C) 2016 Linaro, Inc.
6 * Author: Baolin Wang <baolin.wang@linaro.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <crypto/engine.h>
20 #define CRYPTO_ENGINE_MAX_QLEN 10
22 void crypto_finalize_request(struct crypto_engine *engine,
23 struct ablkcipher_request *req, int err);
26 * crypto_pump_requests - dequeue one request from engine queue to process
27 * @engine: the hardware engine
28 * @in_kthread: true if we are in the context of the request pump thread
30 * This function checks if there is any request in the engine queue that
31 * needs processing and if so call out to the driver to initialize hardware
32 * and handle each request.
34 static void crypto_pump_requests(struct crypto_engine *engine,
37 struct crypto_async_request *async_req, *backlog;
38 struct ablkcipher_request *req;
40 bool was_busy = false;
43 spin_lock_irqsave(&engine->queue_lock, flags);
45 /* Make sure we are not already running a request */
49 /* If another context is idling then defer */
51 queue_kthread_work(&engine->kworker, &engine->pump_requests);
55 /* Check if the engine queue is idle */
56 if (!crypto_queue_len(&engine->queue) || !engine->running) {
60 /* Only do teardown in the thread */
62 queue_kthread_work(&engine->kworker,
63 &engine->pump_requests);
68 engine->idling = true;
69 spin_unlock_irqrestore(&engine->queue_lock, flags);
71 if (engine->unprepare_crypt_hardware &&
72 engine->unprepare_crypt_hardware(engine))
73 pr_err("failed to unprepare crypt hardware\n");
75 spin_lock_irqsave(&engine->queue_lock, flags);
76 engine->idling = false;
80 /* Get the fist request from the engine queue to handle */
81 backlog = crypto_get_backlog(&engine->queue);
82 async_req = crypto_dequeue_request(&engine->queue);
86 req = ablkcipher_request_cast(async_req);
88 engine->cur_req = req;
90 backlog->complete(backlog, -EINPROGRESS);
97 spin_unlock_irqrestore(&engine->queue_lock, flags);
99 /* Until here we get the request need to be encrypted successfully */
100 if (!was_busy && engine->prepare_crypt_hardware) {
101 ret = engine->prepare_crypt_hardware(engine);
103 pr_err("failed to prepare crypt hardware\n");
108 if (engine->prepare_request) {
109 ret = engine->prepare_request(engine, engine->cur_req);
111 pr_err("failed to prepare request: %d\n", ret);
114 engine->cur_req_prepared = true;
117 ret = engine->crypt_one_request(engine, engine->cur_req);
119 pr_err("failed to crypt one request from queue\n");
125 crypto_finalize_request(engine, engine->cur_req, ret);
129 spin_unlock_irqrestore(&engine->queue_lock, flags);
132 static void crypto_pump_work(struct kthread_work *work)
134 struct crypto_engine *engine =
135 container_of(work, struct crypto_engine, pump_requests);
137 crypto_pump_requests(engine, true);
141 * crypto_transfer_request - transfer the new request into the engine queue
142 * @engine: the hardware engine
143 * @req: the request need to be listed into the engine queue
145 int crypto_transfer_request(struct crypto_engine *engine,
146 struct ablkcipher_request *req, bool need_pump)
151 spin_lock_irqsave(&engine->queue_lock, flags);
153 if (!engine->running) {
154 spin_unlock_irqrestore(&engine->queue_lock, flags);
158 ret = ablkcipher_enqueue_request(&engine->queue, req);
160 if (!engine->busy && need_pump)
161 queue_kthread_work(&engine->kworker, &engine->pump_requests);
163 spin_unlock_irqrestore(&engine->queue_lock, flags);
166 EXPORT_SYMBOL_GPL(crypto_transfer_request);
169 * crypto_transfer_request_to_engine - transfer one request to list into the
171 * @engine: the hardware engine
172 * @req: the request need to be listed into the engine queue
174 int crypto_transfer_request_to_engine(struct crypto_engine *engine,
175 struct ablkcipher_request *req)
177 return crypto_transfer_request(engine, req, true);
179 EXPORT_SYMBOL_GPL(crypto_transfer_request_to_engine);
182 * crypto_finalize_request - finalize one request if the request is done
183 * @engine: the hardware engine
184 * @req: the request need to be finalized
187 void crypto_finalize_request(struct crypto_engine *engine,
188 struct ablkcipher_request *req, int err)
191 bool finalize_cur_req = false;
194 spin_lock_irqsave(&engine->queue_lock, flags);
195 if (engine->cur_req == req)
196 finalize_cur_req = true;
197 spin_unlock_irqrestore(&engine->queue_lock, flags);
199 if (finalize_cur_req) {
200 if (engine->cur_req_prepared && engine->unprepare_request) {
201 ret = engine->unprepare_request(engine, req);
203 pr_err("failed to unprepare request\n");
206 spin_lock_irqsave(&engine->queue_lock, flags);
207 engine->cur_req = NULL;
208 engine->cur_req_prepared = false;
209 spin_unlock_irqrestore(&engine->queue_lock, flags);
212 req->base.complete(&req->base, err);
214 queue_kthread_work(&engine->kworker, &engine->pump_requests);
216 EXPORT_SYMBOL_GPL(crypto_finalize_request);
219 * crypto_engine_start - start the hardware engine
220 * @engine: the hardware engine need to be started
222 * Return 0 on success, else on fail.
224 int crypto_engine_start(struct crypto_engine *engine)
228 spin_lock_irqsave(&engine->queue_lock, flags);
230 if (engine->running || engine->busy) {
231 spin_unlock_irqrestore(&engine->queue_lock, flags);
235 engine->running = true;
236 spin_unlock_irqrestore(&engine->queue_lock, flags);
238 queue_kthread_work(&engine->kworker, &engine->pump_requests);
242 EXPORT_SYMBOL_GPL(crypto_engine_start);
245 * crypto_engine_stop - stop the hardware engine
246 * @engine: the hardware engine need to be stopped
248 * Return 0 on success, else on fail.
250 int crypto_engine_stop(struct crypto_engine *engine)
253 unsigned limit = 500;
256 spin_lock_irqsave(&engine->queue_lock, flags);
259 * If the engine queue is not empty or the engine is on busy state,
260 * we need to wait for a while to pump the requests of engine queue.
262 while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
263 spin_unlock_irqrestore(&engine->queue_lock, flags);
265 spin_lock_irqsave(&engine->queue_lock, flags);
268 if (crypto_queue_len(&engine->queue) || engine->busy)
271 engine->running = false;
273 spin_unlock_irqrestore(&engine->queue_lock, flags);
276 pr_warn("could not stop engine\n");
280 EXPORT_SYMBOL_GPL(crypto_engine_stop);
283 * crypto_engine_alloc_init - allocate crypto hardware engine structure and
285 * @dev: the device attached with one hardware engine
286 * @rt: whether this queue is set to run as a realtime task
288 * This must be called from context that can sleep.
289 * Return: the crypto engine structure on success, else NULL.
291 struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
293 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
294 struct crypto_engine *engine;
299 engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
304 engine->running = false;
305 engine->busy = false;
306 engine->idling = false;
307 engine->cur_req_prepared = false;
308 engine->priv_data = dev;
309 snprintf(engine->name, sizeof(engine->name),
310 "%s-engine", dev_name(dev));
312 crypto_init_queue(&engine->queue, CRYPTO_ENGINE_MAX_QLEN);
313 spin_lock_init(&engine->queue_lock);
315 init_kthread_worker(&engine->kworker);
316 engine->kworker_task = kthread_run(kthread_worker_fn,
317 &engine->kworker, "%s",
319 if (IS_ERR(engine->kworker_task)) {
320 dev_err(dev, "failed to create crypto request pump task\n");
323 init_kthread_work(&engine->pump_requests, crypto_pump_work);
326 dev_info(dev, "will run requests pump with realtime priority\n");
327 sched_setscheduler(engine->kworker_task, SCHED_FIFO, ¶m);
332 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
335 * crypto_engine_exit - free the resources of hardware engine when exit
336 * @engine: the hardware engine need to be freed
338 * Return 0 for success.
340 int crypto_engine_exit(struct crypto_engine *engine)
344 ret = crypto_engine_stop(engine);
348 flush_kthread_worker(&engine->kworker);
349 kthread_stop(engine->kworker_task);
353 EXPORT_SYMBOL_GPL(crypto_engine_exit);
355 MODULE_LICENSE("GPL");
356 MODULE_DESCRIPTION("Crypto hardware engine framework");