Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
2589ad84 CL |
2 | /* |
3 | * Crypto engine API | |
4 | * | |
5 | * Copyright (c) 2016 Baolin Wang <baolin.wang@linaro.org> | |
2589ad84 CL |
6 | */ |
7 | #ifndef _CRYPTO_ENGINE_H | |
8 | #define _CRYPTO_ENGINE_H | |
9 | ||
10 | #include <linux/crypto.h> | |
11 | #include <linux/list.h> | |
2589ad84 | 12 | #include <linux/kthread.h> |
244d22ff AS |
13 | #include <linux/spinlock.h> |
14 | #include <linux/types.h> | |
15 | ||
2589ad84 | 16 | #include <crypto/algapi.h> |
218d1cc1 CL |
17 | #include <crypto/aead.h> |
18 | #include <crypto/akcipher.h> | |
4cba7cf0 | 19 | #include <crypto/hash.h> |
218d1cc1 | 20 | #include <crypto/skcipher.h> |
1730c5aa | 21 | #include <crypto/kpp.h> |
2589ad84 | 22 | |
244d22ff AS |
23 | struct device; |
24 | ||
2589ad84 CL |
25 | #define ENGINE_NAME_LEN 30 |
26 | /* | |
27 | * struct crypto_engine - crypto hardware engine | |
28 | * @name: the engine name | |
29 | * @idling: the engine is entering idle state | |
30 | * @busy: request pump is busy | |
31 | * @running: the engine is on working | |
6a89f492 IP |
32 | * @retry_support: indication that the hardware allows re-execution |
33 | * of a failed backlog request | |
34 | * crypto-engine, in head position to keep order | |
2589ad84 | 35 | * @list: link with the global crypto engine list |
155f7d32 | 36 | * @queue_lock: spinlock to synchronise access to request queue |
2589ad84 CL |
37 | * @queue: the crypto queue of the engine |
38 | * @rt: whether this queue is set to run as a realtime task | |
39 | * @prepare_crypt_hardware: a request will soon arrive from the queue | |
40 | * so the subsystem requests the driver to prepare the hardware | |
41 | * by issuing this call | |
42 | * @unprepare_crypt_hardware: there are currently no more requests on the | |
43 | * queue so the subsystem notifies the driver that it may relax the | |
44 | * hardware by issuing this call | |
8d908226 IP |
45 | * @do_batch_requests: execute a batch of requests. Depends on multiple |
46 | * requests support. | |
c4ca2b0b | 47 | * @kworker: kthread worker struct for request pump |
2589ad84 CL |
48 | * @pump_requests: work struct for scheduling work to the request pump |
49 | * @priv_data: the engine private data | |
50 | * @cur_req: the current request which is on processing | |
51 | */ | |
52 | struct crypto_engine { | |
53 | char name[ENGINE_NAME_LEN]; | |
54 | bool idling; | |
55 | bool busy; | |
56 | bool running; | |
6a89f492 IP |
57 | |
58 | bool retry_support; | |
2589ad84 CL |
59 | |
60 | struct list_head list; | |
61 | spinlock_t queue_lock; | |
62 | struct crypto_queue queue; | |
88d58ef8 | 63 | struct device *dev; |
2589ad84 CL |
64 | |
65 | bool rt; | |
66 | ||
67 | int (*prepare_crypt_hardware)(struct crypto_engine *engine); | |
68 | int (*unprepare_crypt_hardware)(struct crypto_engine *engine); | |
8d908226 IP |
69 | int (*do_batch_requests)(struct crypto_engine *engine); |
70 | ||
2589ad84 | 71 | |
c4ca2b0b | 72 | struct kthread_worker *kworker; |
2589ad84 CL |
73 | struct kthread_work pump_requests; |
74 | ||
75 | void *priv_data; | |
4cba7cf0 | 76 | struct crypto_async_request *cur_req; |
2589ad84 CL |
77 | }; |
78 | ||
218d1cc1 CL |
79 | /* |
80 | * struct crypto_engine_op - crypto hardware engine operations | |
81 | * @prepare__request: do some prepare if need before handle the current request | |
82 | * @unprepare_request: undo any work done by prepare_request() | |
83 | * @do_one_request: do encryption for current request | |
84 | */ | |
85 | struct crypto_engine_op { | |
86 | int (*prepare_request)(struct crypto_engine *engine, | |
87 | void *areq); | |
88 | int (*unprepare_request)(struct crypto_engine *engine, | |
89 | void *areq); | |
90 | int (*do_one_request)(struct crypto_engine *engine, | |
91 | void *areq); | |
92 | }; | |
93 | ||
94 | struct crypto_engine_ctx { | |
95 | struct crypto_engine_op op; | |
96 | }; | |
97 | ||
218d1cc1 CL |
98 | int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine, |
99 | struct aead_request *req); | |
100 | int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine, | |
101 | struct akcipher_request *req); | |
4cba7cf0 | 102 | int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine, |
218d1cc1 | 103 | struct ahash_request *req); |
1730c5aa PK |
104 | int crypto_transfer_kpp_request_to_engine(struct crypto_engine *engine, |
105 | struct kpp_request *req); | |
218d1cc1 CL |
106 | int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine, |
107 | struct skcipher_request *req); | |
218d1cc1 CL |
108 | void crypto_finalize_aead_request(struct crypto_engine *engine, |
109 | struct aead_request *req, int err); | |
110 | void crypto_finalize_akcipher_request(struct crypto_engine *engine, | |
111 | struct akcipher_request *req, int err); | |
4cba7cf0 CL |
112 | void crypto_finalize_hash_request(struct crypto_engine *engine, |
113 | struct ahash_request *req, int err); | |
1730c5aa PK |
114 | void crypto_finalize_kpp_request(struct crypto_engine *engine, |
115 | struct kpp_request *req, int err); | |
218d1cc1 CL |
116 | void crypto_finalize_skcipher_request(struct crypto_engine *engine, |
117 | struct skcipher_request *req, int err); | |
2589ad84 CL |
118 | int crypto_engine_start(struct crypto_engine *engine); |
119 | int crypto_engine_stop(struct crypto_engine *engine); | |
120 | struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt); | |
6a89f492 IP |
121 | struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev, |
122 | bool retry_support, | |
8d908226 | 123 | int (*cbk_do_batch)(struct crypto_engine *engine), |
6a89f492 | 124 | bool rt, int qlen); |
2589ad84 CL |
125 | int crypto_engine_exit(struct crypto_engine *engine); |
126 | ||
127 | #endif /* _CRYPTO_ENGINE_H */ |