blob: 61e7c4e02fd264b2228da7f25a13ee317767cc87 [file] [log] [blame]
Baolin Wang735d37b2016-01-26 20:25:39 +08001/*
2 * Handle async block request by crypto hardware engine.
3 *
4 * Copyright (C) 2016 Linaro, Inc.
5 *
6 * Author: Baolin Wang <baolin.wang@linaro.org>
7 *
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)
11 * any later version.
12 *
13 */
14
15#include <linux/err.h>
16#include <linux/delay.h>
Corentin LABBE2589ad82016-08-31 14:02:57 +020017#include <crypto/engine.h>
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020018#include <crypto/internal/hash.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010019#include <uapi/linux/sched/types.h>
Baolin Wang735d37b2016-01-26 20:25:39 +080020#include "internal.h"
21
22#define CRYPTO_ENGINE_MAX_QLEN 10
23
Baolin Wang735d37b2016-01-26 20:25:39 +080024/**
25 * crypto_pump_requests - dequeue one request from engine queue to process
26 * @engine: the hardware engine
27 * @in_kthread: true if we are in the context of the request pump thread
28 *
29 * This function checks if there is any request in the engine queue that
30 * needs processing and if so call out to the driver to initialize hardware
31 * and handle each request.
32 */
33static void crypto_pump_requests(struct crypto_engine *engine,
34 bool in_kthread)
35{
36 struct crypto_async_request *async_req, *backlog;
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020037 struct ahash_request *hreq;
38 struct ablkcipher_request *breq;
Baolin Wang735d37b2016-01-26 20:25:39 +080039 unsigned long flags;
40 bool was_busy = false;
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020041 int ret, rtype;
Baolin Wang735d37b2016-01-26 20:25:39 +080042
43 spin_lock_irqsave(&engine->queue_lock, flags);
44
45 /* Make sure we are not already running a request */
46 if (engine->cur_req)
47 goto out;
48
49 /* If another context is idling then defer */
50 if (engine->idling) {
Petr Mladekc4ca2b02016-10-19 13:54:30 +020051 kthread_queue_work(engine->kworker, &engine->pump_requests);
Baolin Wang735d37b2016-01-26 20:25:39 +080052 goto out;
53 }
54
55 /* Check if the engine queue is idle */
56 if (!crypto_queue_len(&engine->queue) || !engine->running) {
57 if (!engine->busy)
58 goto out;
59
60 /* Only do teardown in the thread */
61 if (!in_kthread) {
Petr Mladekc4ca2b02016-10-19 13:54:30 +020062 kthread_queue_work(engine->kworker,
Baolin Wang735d37b2016-01-26 20:25:39 +080063 &engine->pump_requests);
64 goto out;
65 }
66
67 engine->busy = false;
68 engine->idling = true;
69 spin_unlock_irqrestore(&engine->queue_lock, flags);
70
71 if (engine->unprepare_crypt_hardware &&
72 engine->unprepare_crypt_hardware(engine))
Corentin LABBE88d58ef2017-06-06 15:44:16 +020073 dev_err(engine->dev, "failed to unprepare crypt hardware\n");
Baolin Wang735d37b2016-01-26 20:25:39 +080074
75 spin_lock_irqsave(&engine->queue_lock, flags);
76 engine->idling = false;
77 goto out;
78 }
79
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);
83 if (!async_req)
84 goto out;
85
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020086 engine->cur_req = async_req;
Baolin Wang735d37b2016-01-26 20:25:39 +080087 if (backlog)
88 backlog->complete(backlog, -EINPROGRESS);
89
90 if (engine->busy)
91 was_busy = true;
92 else
93 engine->busy = true;
94
95 spin_unlock_irqrestore(&engine->queue_lock, flags);
96
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020097 rtype = crypto_tfm_alg_type(engine->cur_req->tfm);
Baolin Wang735d37b2016-01-26 20:25:39 +080098 /* Until here we get the request need to be encrypted successfully */
99 if (!was_busy && engine->prepare_crypt_hardware) {
100 ret = engine->prepare_crypt_hardware(engine);
101 if (ret) {
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200102 dev_err(engine->dev, "failed to prepare crypt hardware\n");
Baolin Wang735d37b2016-01-26 20:25:39 +0800103 goto req_err;
104 }
105 }
106
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200107 switch (rtype) {
108 case CRYPTO_ALG_TYPE_AHASH:
109 hreq = ahash_request_cast(engine->cur_req);
110 if (engine->prepare_hash_request) {
111 ret = engine->prepare_hash_request(engine, hreq);
112 if (ret) {
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200113 dev_err(engine->dev, "failed to prepare request: %d\n",
114 ret);
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200115 goto req_err;
116 }
117 engine->cur_req_prepared = true;
118 }
119 ret = engine->hash_one_request(engine, hreq);
Baolin Wang735d37b2016-01-26 20:25:39 +0800120 if (ret) {
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200121 dev_err(engine->dev, "failed to hash one request from queue\n");
Baolin Wang735d37b2016-01-26 20:25:39 +0800122 goto req_err;
123 }
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200124 return;
125 case CRYPTO_ALG_TYPE_ABLKCIPHER:
126 breq = ablkcipher_request_cast(engine->cur_req);
127 if (engine->prepare_cipher_request) {
128 ret = engine->prepare_cipher_request(engine, breq);
129 if (ret) {
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200130 dev_err(engine->dev, "failed to prepare request: %d\n",
131 ret);
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200132 goto req_err;
133 }
134 engine->cur_req_prepared = true;
135 }
136 ret = engine->cipher_one_request(engine, breq);
137 if (ret) {
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200138 dev_err(engine->dev, "failed to cipher one request from queue\n");
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200139 goto req_err;
140 }
141 return;
142 default:
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200143 dev_err(engine->dev, "failed to prepare request of unknown type\n");
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200144 return;
Baolin Wang735d37b2016-01-26 20:25:39 +0800145 }
146
Baolin Wang735d37b2016-01-26 20:25:39 +0800147req_err:
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200148 switch (rtype) {
149 case CRYPTO_ALG_TYPE_AHASH:
150 hreq = ahash_request_cast(engine->cur_req);
151 crypto_finalize_hash_request(engine, hreq, ret);
152 break;
153 case CRYPTO_ALG_TYPE_ABLKCIPHER:
154 breq = ablkcipher_request_cast(engine->cur_req);
155 crypto_finalize_cipher_request(engine, breq, ret);
156 break;
157 }
Baolin Wang735d37b2016-01-26 20:25:39 +0800158 return;
159
160out:
161 spin_unlock_irqrestore(&engine->queue_lock, flags);
162}
163
164static void crypto_pump_work(struct kthread_work *work)
165{
166 struct crypto_engine *engine =
167 container_of(work, struct crypto_engine, pump_requests);
168
169 crypto_pump_requests(engine, true);
170}
171
172/**
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200173 * crypto_transfer_cipher_request - transfer the new request into the
174 * enginequeue
Baolin Wang735d37b2016-01-26 20:25:39 +0800175 * @engine: the hardware engine
176 * @req: the request need to be listed into the engine queue
177 */
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200178int crypto_transfer_cipher_request(struct crypto_engine *engine,
179 struct ablkcipher_request *req,
180 bool need_pump)
Baolin Wang735d37b2016-01-26 20:25:39 +0800181{
182 unsigned long flags;
183 int ret;
184
185 spin_lock_irqsave(&engine->queue_lock, flags);
186
187 if (!engine->running) {
188 spin_unlock_irqrestore(&engine->queue_lock, flags);
189 return -ESHUTDOWN;
190 }
191
192 ret = ablkcipher_enqueue_request(&engine->queue, req);
193
194 if (!engine->busy && need_pump)
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200195 kthread_queue_work(engine->kworker, &engine->pump_requests);
Baolin Wang735d37b2016-01-26 20:25:39 +0800196
197 spin_unlock_irqrestore(&engine->queue_lock, flags);
198 return ret;
199}
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200200EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request);
Baolin Wang735d37b2016-01-26 20:25:39 +0800201
202/**
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200203 * crypto_transfer_cipher_request_to_engine - transfer one request to list
204 * into the engine queue
Baolin Wang735d37b2016-01-26 20:25:39 +0800205 * @engine: the hardware engine
206 * @req: the request need to be listed into the engine queue
207 */
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200208int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
209 struct ablkcipher_request *req)
Baolin Wang735d37b2016-01-26 20:25:39 +0800210{
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200211 return crypto_transfer_cipher_request(engine, req, true);
Baolin Wang735d37b2016-01-26 20:25:39 +0800212}
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200213EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request_to_engine);
Baolin Wang735d37b2016-01-26 20:25:39 +0800214
215/**
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200216 * crypto_transfer_hash_request - transfer the new request into the
217 * enginequeue
218 * @engine: the hardware engine
219 * @req: the request need to be listed into the engine queue
220 */
221int crypto_transfer_hash_request(struct crypto_engine *engine,
222 struct ahash_request *req, bool need_pump)
223{
224 unsigned long flags;
225 int ret;
226
227 spin_lock_irqsave(&engine->queue_lock, flags);
228
229 if (!engine->running) {
230 spin_unlock_irqrestore(&engine->queue_lock, flags);
231 return -ESHUTDOWN;
232 }
233
234 ret = ahash_enqueue_request(&engine->queue, req);
235
236 if (!engine->busy && need_pump)
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200237 kthread_queue_work(engine->kworker, &engine->pump_requests);
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200238
239 spin_unlock_irqrestore(&engine->queue_lock, flags);
240 return ret;
241}
242EXPORT_SYMBOL_GPL(crypto_transfer_hash_request);
243
244/**
245 * crypto_transfer_hash_request_to_engine - transfer one request to list
246 * into the engine queue
247 * @engine: the hardware engine
248 * @req: the request need to be listed into the engine queue
249 */
250int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
251 struct ahash_request *req)
252{
253 return crypto_transfer_hash_request(engine, req, true);
254}
255EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
256
257/**
258 * crypto_finalize_cipher_request - finalize one request if the request is done
Baolin Wang735d37b2016-01-26 20:25:39 +0800259 * @engine: the hardware engine
260 * @req: the request need to be finalized
261 * @err: error number
262 */
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200263void crypto_finalize_cipher_request(struct crypto_engine *engine,
264 struct ablkcipher_request *req, int err)
Baolin Wang735d37b2016-01-26 20:25:39 +0800265{
266 unsigned long flags;
267 bool finalize_cur_req = false;
268 int ret;
269
270 spin_lock_irqsave(&engine->queue_lock, flags);
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200271 if (engine->cur_req == &req->base)
Baolin Wang735d37b2016-01-26 20:25:39 +0800272 finalize_cur_req = true;
273 spin_unlock_irqrestore(&engine->queue_lock, flags);
274
275 if (finalize_cur_req) {
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200276 if (engine->cur_req_prepared &&
277 engine->unprepare_cipher_request) {
278 ret = engine->unprepare_cipher_request(engine, req);
Baolin Wang735d37b2016-01-26 20:25:39 +0800279 if (ret)
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200280 dev_err(engine->dev, "failed to unprepare request\n");
Baolin Wang735d37b2016-01-26 20:25:39 +0800281 }
Baolin Wang735d37b2016-01-26 20:25:39 +0800282 spin_lock_irqsave(&engine->queue_lock, flags);
283 engine->cur_req = NULL;
284 engine->cur_req_prepared = false;
285 spin_unlock_irqrestore(&engine->queue_lock, flags);
286 }
287
288 req->base.complete(&req->base, err);
289
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200290 kthread_queue_work(engine->kworker, &engine->pump_requests);
Baolin Wang735d37b2016-01-26 20:25:39 +0800291}
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200292EXPORT_SYMBOL_GPL(crypto_finalize_cipher_request);
293
294/**
295 * crypto_finalize_hash_request - finalize one request if the request is done
296 * @engine: the hardware engine
297 * @req: the request need to be finalized
298 * @err: error number
299 */
300void crypto_finalize_hash_request(struct crypto_engine *engine,
301 struct ahash_request *req, int err)
302{
303 unsigned long flags;
304 bool finalize_cur_req = false;
305 int ret;
306
307 spin_lock_irqsave(&engine->queue_lock, flags);
308 if (engine->cur_req == &req->base)
309 finalize_cur_req = true;
310 spin_unlock_irqrestore(&engine->queue_lock, flags);
311
312 if (finalize_cur_req) {
313 if (engine->cur_req_prepared &&
314 engine->unprepare_hash_request) {
315 ret = engine->unprepare_hash_request(engine, req);
316 if (ret)
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200317 dev_err(engine->dev, "failed to unprepare request\n");
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200318 }
319 spin_lock_irqsave(&engine->queue_lock, flags);
320 engine->cur_req = NULL;
321 engine->cur_req_prepared = false;
322 spin_unlock_irqrestore(&engine->queue_lock, flags);
323 }
324
325 req->base.complete(&req->base, err);
326
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200327 kthread_queue_work(engine->kworker, &engine->pump_requests);
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200328}
329EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
Baolin Wang735d37b2016-01-26 20:25:39 +0800330
331/**
332 * crypto_engine_start - start the hardware engine
333 * @engine: the hardware engine need to be started
334 *
335 * Return 0 on success, else on fail.
336 */
337int crypto_engine_start(struct crypto_engine *engine)
338{
339 unsigned long flags;
340
341 spin_lock_irqsave(&engine->queue_lock, flags);
342
343 if (engine->running || engine->busy) {
344 spin_unlock_irqrestore(&engine->queue_lock, flags);
345 return -EBUSY;
346 }
347
348 engine->running = true;
349 spin_unlock_irqrestore(&engine->queue_lock, flags);
350
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200351 kthread_queue_work(engine->kworker, &engine->pump_requests);
Baolin Wang735d37b2016-01-26 20:25:39 +0800352
353 return 0;
354}
355EXPORT_SYMBOL_GPL(crypto_engine_start);
356
357/**
358 * crypto_engine_stop - stop the hardware engine
359 * @engine: the hardware engine need to be stopped
360 *
361 * Return 0 on success, else on fail.
362 */
363int crypto_engine_stop(struct crypto_engine *engine)
364{
365 unsigned long flags;
Corentin LABBE4cba7cf2016-08-31 14:02:58 +0200366 unsigned int limit = 500;
Baolin Wang735d37b2016-01-26 20:25:39 +0800367 int ret = 0;
368
369 spin_lock_irqsave(&engine->queue_lock, flags);
370
371 /*
372 * If the engine queue is not empty or the engine is on busy state,
373 * we need to wait for a while to pump the requests of engine queue.
374 */
375 while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
376 spin_unlock_irqrestore(&engine->queue_lock, flags);
377 msleep(20);
378 spin_lock_irqsave(&engine->queue_lock, flags);
379 }
380
381 if (crypto_queue_len(&engine->queue) || engine->busy)
382 ret = -EBUSY;
383 else
384 engine->running = false;
385
386 spin_unlock_irqrestore(&engine->queue_lock, flags);
387
388 if (ret)
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200389 dev_warn(engine->dev, "could not stop engine\n");
Baolin Wang735d37b2016-01-26 20:25:39 +0800390
391 return ret;
392}
393EXPORT_SYMBOL_GPL(crypto_engine_stop);
394
395/**
396 * crypto_engine_alloc_init - allocate crypto hardware engine structure and
397 * initialize it.
398 * @dev: the device attached with one hardware engine
399 * @rt: whether this queue is set to run as a realtime task
400 *
401 * This must be called from context that can sleep.
402 * Return: the crypto engine structure on success, else NULL.
403 */
404struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
405{
406 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
407 struct crypto_engine *engine;
408
409 if (!dev)
410 return NULL;
411
412 engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
413 if (!engine)
414 return NULL;
415
Corentin LABBE88d58ef2017-06-06 15:44:16 +0200416 engine->dev = dev;
Baolin Wang735d37b2016-01-26 20:25:39 +0800417 engine->rt = rt;
418 engine->running = false;
419 engine->busy = false;
420 engine->idling = false;
421 engine->cur_req_prepared = false;
422 engine->priv_data = dev;
423 snprintf(engine->name, sizeof(engine->name),
424 "%s-engine", dev_name(dev));
425
426 crypto_init_queue(&engine->queue, CRYPTO_ENGINE_MAX_QLEN);
427 spin_lock_init(&engine->queue_lock);
428
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200429 engine->kworker = kthread_create_worker(0, "%s", engine->name);
430 if (IS_ERR(engine->kworker)) {
Baolin Wang735d37b2016-01-26 20:25:39 +0800431 dev_err(dev, "failed to create crypto request pump task\n");
432 return NULL;
433 }
Petr Mladek39891442016-10-11 13:55:20 -0700434 kthread_init_work(&engine->pump_requests, crypto_pump_work);
Baolin Wang735d37b2016-01-26 20:25:39 +0800435
436 if (engine->rt) {
437 dev_info(dev, "will run requests pump with realtime priority\n");
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200438 sched_setscheduler(engine->kworker->task, SCHED_FIFO, &param);
Baolin Wang735d37b2016-01-26 20:25:39 +0800439 }
440
441 return engine;
442}
443EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
444
445/**
446 * crypto_engine_exit - free the resources of hardware engine when exit
447 * @engine: the hardware engine need to be freed
448 *
449 * Return 0 for success.
450 */
451int crypto_engine_exit(struct crypto_engine *engine)
452{
453 int ret;
454
455 ret = crypto_engine_stop(engine);
456 if (ret)
457 return ret;
458
Petr Mladekc4ca2b02016-10-19 13:54:30 +0200459 kthread_destroy_worker(engine->kworker);
Baolin Wang735d37b2016-01-26 20:25:39 +0800460
461 return 0;
462}
463EXPORT_SYMBOL_GPL(crypto_engine_exit);
464
465MODULE_LICENSE("GPL");
466MODULE_DESCRIPTION("Crypto hardware engine framework");