blob: dd04c1699b5186cbf8331b0f34006977b5ccde57 [file] [log] [blame]
Corentin LABBE2589ad82016-08-31 14:02:57 +02001/*
2 * Crypto engine API
3 *
4 * Copyright (c) 2016 Baolin Wang <baolin.wang@linaro.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12#ifndef _CRYPTO_ENGINE_H
13#define _CRYPTO_ENGINE_H
14
15#include <linux/crypto.h>
16#include <linux/list.h>
17#include <linux/kernel.h>
18#include <linux/kthread.h>
19#include <crypto/algapi.h>
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020020#include <crypto/hash.h>
Corentin LABBE2589ad82016-08-31 14:02:57 +020021
22#define ENGINE_NAME_LEN 30
23/*
24 * struct crypto_engine - crypto hardware engine
25 * @name: the engine name
26 * @idling: the engine is entering idle state
27 * @busy: request pump is busy
28 * @running: the engine is on working
29 * @cur_req_prepared: current request is prepared
30 * @list: link with the global crypto engine list
31 * @queue_lock: spinlock to syncronise access to request queue
32 * @queue: the crypto queue of the engine
33 * @rt: whether this queue is set to run as a realtime task
34 * @prepare_crypt_hardware: a request will soon arrive from the queue
35 * so the subsystem requests the driver to prepare the hardware
36 * by issuing this call
37 * @unprepare_crypt_hardware: there are currently no more requests on the
38 * queue so the subsystem notifies the driver that it may relax the
39 * hardware by issuing this call
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020040 * @prepare_cipher_request: do some prepare if need before handle the current request
41 * @unprepare_cipher_request: undo any work done by prepare_cipher_request()
42 * @cipher_one_request: do encryption for current request
43 * @prepare_hash_request: do some prepare if need before handle the current request
44 * @unprepare_hash_request: undo any work done by prepare_hash_request()
45 * @hash_one_request: do hash for current request
Petr Mladekc4ca2b02016-10-19 13:54:30 +020046 * @kworker: kthread worker struct for request pump
Corentin LABBE2589ad82016-08-31 14:02:57 +020047 * @pump_requests: work struct for scheduling work to the request pump
48 * @priv_data: the engine private data
49 * @cur_req: the current request which is on processing
50 */
51struct crypto_engine {
52 char name[ENGINE_NAME_LEN];
53 bool idling;
54 bool busy;
55 bool running;
56 bool cur_req_prepared;
57
58 struct list_head list;
59 spinlock_t queue_lock;
60 struct crypto_queue queue;
Corentin LABBE88d58ef2017-06-06 15:44:16 +020061 struct device *dev;
Corentin LABBE2589ad82016-08-31 14:02:57 +020062
63 bool rt;
64
65 int (*prepare_crypt_hardware)(struct crypto_engine *engine);
66 int (*unprepare_crypt_hardware)(struct crypto_engine *engine);
67
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020068 int (*prepare_cipher_request)(struct crypto_engine *engine,
69 struct ablkcipher_request *req);
70 int (*unprepare_cipher_request)(struct crypto_engine *engine,
71 struct ablkcipher_request *req);
72 int (*prepare_hash_request)(struct crypto_engine *engine,
73 struct ahash_request *req);
74 int (*unprepare_hash_request)(struct crypto_engine *engine,
75 struct ahash_request *req);
76 int (*cipher_one_request)(struct crypto_engine *engine,
77 struct ablkcipher_request *req);
78 int (*hash_one_request)(struct crypto_engine *engine,
79 struct ahash_request *req);
Corentin LABBE2589ad82016-08-31 14:02:57 +020080
Petr Mladekc4ca2b02016-10-19 13:54:30 +020081 struct kthread_worker *kworker;
Corentin LABBE2589ad82016-08-31 14:02:57 +020082 struct kthread_work pump_requests;
83
84 void *priv_data;
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020085 struct crypto_async_request *cur_req;
Corentin LABBE2589ad82016-08-31 14:02:57 +020086};
87
Corentin LABBE4cba7cf2016-08-31 14:02:58 +020088int crypto_transfer_cipher_request(struct crypto_engine *engine,
89 struct ablkcipher_request *req,
90 bool need_pump);
91int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
92 struct ablkcipher_request *req);
93int crypto_transfer_hash_request(struct crypto_engine *engine,
94 struct ahash_request *req, bool need_pump);
95int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
96 struct ahash_request *req);
97void crypto_finalize_cipher_request(struct crypto_engine *engine,
98 struct ablkcipher_request *req, int err);
99void crypto_finalize_hash_request(struct crypto_engine *engine,
100 struct ahash_request *req, int err);
Corentin LABBE2589ad82016-08-31 14:02:57 +0200101int crypto_engine_start(struct crypto_engine *engine);
102int crypto_engine_stop(struct crypto_engine *engine);
103struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt);
104int crypto_engine_exit(struct crypto_engine *engine);
105
106#endif /* _CRYPTO_ENGINE_H */