aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorCorentin LABBE <clabbe.montjoie@gmail.com>2016-08-31 08:02:58 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-09-07 09:08:27 -0400
commit4cba7cf025f35599f8de3282c8a7278ecc43eea4 (patch)
tree01467e06408825abc6bc9b370b575d7b21774a5d /crypto
parent2589ad84047f1dbed741b48785680b152db2e5db (diff)
crypto: engine - permit to enqueue ashash_request
The current crypto engine allow only ablkcipher_request to be enqueued. Thus denying any use of it for hardware that also handle hash algo. This patch modify the API for allowing to enqueue ciphers and hash. Since omap-aes/omap-des are the only users, this patch also convert them to the new cryptoengine API. Signed-off-by: Corentin Labbe <clabbe.montjoie@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto_engine.c186
1 files changed, 148 insertions, 38 deletions
diff --git a/crypto/crypto_engine.c b/crypto/crypto_engine.c
index 795b6f9412ba..bfb92ace2c91 100644
--- a/crypto/crypto_engine.c
+++ b/crypto/crypto_engine.c
@@ -15,13 +15,11 @@
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/delay.h> 16#include <linux/delay.h>
17#include <crypto/engine.h> 17#include <crypto/engine.h>
18#include <crypto/internal/hash.h>
18#include "internal.h" 19#include "internal.h"
19 20
20#define CRYPTO_ENGINE_MAX_QLEN 10 21#define CRYPTO_ENGINE_MAX_QLEN 10
21 22
22void crypto_finalize_request(struct crypto_engine *engine,
23 struct ablkcipher_request *req, int err);
24
25/** 23/**
26 * crypto_pump_requests - dequeue one request from engine queue to process 24 * crypto_pump_requests - dequeue one request from engine queue to process
27 * @engine: the hardware engine 25 * @engine: the hardware engine
@@ -35,10 +33,11 @@ static void crypto_pump_requests(struct crypto_engine *engine,
35 bool in_kthread) 33 bool in_kthread)
36{ 34{
37 struct crypto_async_request *async_req, *backlog; 35 struct crypto_async_request *async_req, *backlog;
38 struct ablkcipher_request *req; 36 struct ahash_request *hreq;
37 struct ablkcipher_request *breq;
39 unsigned long flags; 38 unsigned long flags;
40 bool was_busy = false; 39 bool was_busy = false;
41 int ret; 40 int ret, rtype;
42 41
43 spin_lock_irqsave(&engine->queue_lock, flags); 42 spin_lock_irqsave(&engine->queue_lock, flags);
44 43
@@ -83,9 +82,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
83 if (!async_req) 82 if (!async_req)
84 goto out; 83 goto out;
85 84
86 req = ablkcipher_request_cast(async_req); 85 engine->cur_req = async_req;
87
88 engine->cur_req = req;
89 if (backlog) 86 if (backlog)
90 backlog->complete(backlog, -EINPROGRESS); 87 backlog->complete(backlog, -EINPROGRESS);
91 88
@@ -96,6 +93,7 @@ static void crypto_pump_requests(struct crypto_engine *engine,
96 93
97 spin_unlock_irqrestore(&engine->queue_lock, flags); 94 spin_unlock_irqrestore(&engine->queue_lock, flags);
98 95
96 rtype = crypto_tfm_alg_type(engine->cur_req->tfm);
99 /* Until here we get the request need to be encrypted successfully */ 97 /* Until here we get the request need to be encrypted successfully */
100 if (!was_busy && engine->prepare_crypt_hardware) { 98 if (!was_busy && engine->prepare_crypt_hardware) {
101 ret = engine->prepare_crypt_hardware(engine); 99 ret = engine->prepare_crypt_hardware(engine);
@@ -105,24 +103,55 @@ static void crypto_pump_requests(struct crypto_engine *engine,
105 } 103 }
106 } 104 }
107 105
108 if (engine->prepare_request) { 106 switch (rtype) {
109 ret = engine->prepare_request(engine, engine->cur_req); 107 case CRYPTO_ALG_TYPE_AHASH:
108 hreq = ahash_request_cast(engine->cur_req);
109 if (engine->prepare_hash_request) {
110 ret = engine->prepare_hash_request(engine, hreq);
111 if (ret) {
112 pr_err("failed to prepare request: %d\n", ret);
113 goto req_err;
114 }
115 engine->cur_req_prepared = true;
116 }
117 ret = engine->hash_one_request(engine, hreq);
110 if (ret) { 118 if (ret) {
111 pr_err("failed to prepare request: %d\n", ret); 119 pr_err("failed to hash one request from queue\n");
112 goto req_err; 120 goto req_err;
113 } 121 }
114 engine->cur_req_prepared = true; 122 return;
115 } 123 case CRYPTO_ALG_TYPE_ABLKCIPHER:
116 124 breq = ablkcipher_request_cast(engine->cur_req);
117 ret = engine->crypt_one_request(engine, engine->cur_req); 125 if (engine->prepare_cipher_request) {
118 if (ret) { 126 ret = engine->prepare_cipher_request(engine, breq);
119 pr_err("failed to crypt one request from queue\n"); 127 if (ret) {
120 goto req_err; 128 pr_err("failed to prepare request: %d\n", ret);
129 goto req_err;
130 }
131 engine->cur_req_prepared = true;
132 }
133 ret = engine->cipher_one_request(engine, breq);
134 if (ret) {
135 pr_err("failed to cipher one request from queue\n");
136 goto req_err;
137 }
138 return;
139 default:
140 pr_err("failed to prepare request of unknown type\n");
141 return;
121 } 142 }
122 return;
123 143
124req_err: 144req_err:
125 crypto_finalize_request(engine, engine->cur_req, ret); 145 switch (rtype) {
146 case CRYPTO_ALG_TYPE_AHASH:
147 hreq = ahash_request_cast(engine->cur_req);
148 crypto_finalize_hash_request(engine, hreq, ret);
149 break;
150 case CRYPTO_ALG_TYPE_ABLKCIPHER:
151 breq = ablkcipher_request_cast(engine->cur_req);
152 crypto_finalize_cipher_request(engine, breq, ret);
153 break;
154 }
126 return; 155 return;
127 156
128out: 157out:
@@ -138,12 +167,14 @@ static void crypto_pump_work(struct kthread_work *work)
138} 167}
139 168
140/** 169/**
141 * crypto_transfer_request - transfer the new request into the engine queue 170 * crypto_transfer_cipher_request - transfer the new request into the
171 * enginequeue
142 * @engine: the hardware engine 172 * @engine: the hardware engine
143 * @req: the request need to be listed into the engine queue 173 * @req: the request need to be listed into the engine queue
144 */ 174 */
145int crypto_transfer_request(struct crypto_engine *engine, 175int crypto_transfer_cipher_request(struct crypto_engine *engine,
146 struct ablkcipher_request *req, bool need_pump) 176 struct ablkcipher_request *req,
177 bool need_pump)
147{ 178{
148 unsigned long flags; 179 unsigned long flags;
149 int ret; 180 int ret;
@@ -163,46 +194,125 @@ int crypto_transfer_request(struct crypto_engine *engine,
163 spin_unlock_irqrestore(&engine->queue_lock, flags); 194 spin_unlock_irqrestore(&engine->queue_lock, flags);
164 return ret; 195 return ret;
165} 196}
166EXPORT_SYMBOL_GPL(crypto_transfer_request); 197EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request);
198
199/**
200 * crypto_transfer_cipher_request_to_engine - transfer one request to list
201 * into the engine queue
202 * @engine: the hardware engine
203 * @req: the request need to be listed into the engine queue
204 */
205int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
206 struct ablkcipher_request *req)
207{
208 return crypto_transfer_cipher_request(engine, req, true);
209}
210EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request_to_engine);
211
212/**
213 * crypto_transfer_hash_request - transfer the new request into the
214 * enginequeue
215 * @engine: the hardware engine
216 * @req: the request need to be listed into the engine queue
217 */
218int crypto_transfer_hash_request(struct crypto_engine *engine,
219 struct ahash_request *req, bool need_pump)
220{
221 unsigned long flags;
222 int ret;
223
224 spin_lock_irqsave(&engine->queue_lock, flags);
225
226 if (!engine->running) {
227 spin_unlock_irqrestore(&engine->queue_lock, flags);
228 return -ESHUTDOWN;
229 }
230
231 ret = ahash_enqueue_request(&engine->queue, req);
232
233 if (!engine->busy && need_pump)
234 queue_kthread_work(&engine->kworker, &engine->pump_requests);
235
236 spin_unlock_irqrestore(&engine->queue_lock, flags);
237 return ret;
238}
239EXPORT_SYMBOL_GPL(crypto_transfer_hash_request);
167 240
168/** 241/**
169 * crypto_transfer_request_to_engine - transfer one request to list into the 242 * crypto_transfer_hash_request_to_engine - transfer one request to list
170 * engine queue 243 * into the engine queue
171 * @engine: the hardware engine 244 * @engine: the hardware engine
172 * @req: the request need to be listed into the engine queue 245 * @req: the request need to be listed into the engine queue
173 */ 246 */
174int crypto_transfer_request_to_engine(struct crypto_engine *engine, 247int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
175 struct ablkcipher_request *req) 248 struct ahash_request *req)
176{ 249{
177 return crypto_transfer_request(engine, req, true); 250 return crypto_transfer_hash_request(engine, req, true);
178} 251}
179EXPORT_SYMBOL_GPL(crypto_transfer_request_to_engine); 252EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
180 253
181/** 254/**
182 * crypto_finalize_request - finalize one request if the request is done 255 * crypto_finalize_cipher_request - finalize one request if the request is done
183 * @engine: the hardware engine 256 * @engine: the hardware engine
184 * @req: the request need to be finalized 257 * @req: the request need to be finalized
185 * @err: error number 258 * @err: error number
186 */ 259 */
187void crypto_finalize_request(struct crypto_engine *engine, 260void crypto_finalize_cipher_request(struct crypto_engine *engine,
188 struct ablkcipher_request *req, int err) 261 struct ablkcipher_request *req, int err)
189{ 262{
190 unsigned long flags; 263 unsigned long flags;
191 bool finalize_cur_req = false; 264 bool finalize_cur_req = false;
192 int ret; 265 int ret;
193 266
194 spin_lock_irqsave(&engine->queue_lock, flags); 267 spin_lock_irqsave(&engine->queue_lock, flags);
195 if (engine->cur_req == req) 268 if (engine->cur_req == &req->base)
196 finalize_cur_req = true; 269 finalize_cur_req = true;
197 spin_unlock_irqrestore(&engine->queue_lock, flags); 270 spin_unlock_irqrestore(&engine->queue_lock, flags);
198 271
199 if (finalize_cur_req) { 272 if (finalize_cur_req) {
200 if (engine->cur_req_prepared && engine->unprepare_request) { 273 if (engine->cur_req_prepared &&
201 ret = engine->unprepare_request(engine, req); 274 engine->unprepare_cipher_request) {
275 ret = engine->unprepare_cipher_request(engine, req);
202 if (ret) 276 if (ret)
203 pr_err("failed to unprepare request\n"); 277 pr_err("failed to unprepare request\n");
204 } 278 }
279 spin_lock_irqsave(&engine->queue_lock, flags);
280 engine->cur_req = NULL;
281 engine->cur_req_prepared = false;
282 spin_unlock_irqrestore(&engine->queue_lock, flags);
283 }
284
285 req->base.complete(&req->base, err);
205 286
287 queue_kthread_work(&engine->kworker, &engine->pump_requests);
288}
289EXPORT_SYMBOL_GPL(crypto_finalize_cipher_request);
290
291/**
292 * crypto_finalize_hash_request - finalize one request if the request is done
293 * @engine: the hardware engine
294 * @req: the request need to be finalized
295 * @err: error number
296 */
297void crypto_finalize_hash_request(struct crypto_engine *engine,
298 struct ahash_request *req, int err)
299{
300 unsigned long flags;
301 bool finalize_cur_req = false;
302 int ret;
303
304 spin_lock_irqsave(&engine->queue_lock, flags);
305 if (engine->cur_req == &req->base)
306 finalize_cur_req = true;
307 spin_unlock_irqrestore(&engine->queue_lock, flags);
308
309 if (finalize_cur_req) {
310 if (engine->cur_req_prepared &&
311 engine->unprepare_hash_request) {
312 ret = engine->unprepare_hash_request(engine, req);
313 if (ret)
314 pr_err("failed to unprepare request\n");
315 }
206 spin_lock_irqsave(&engine->queue_lock, flags); 316 spin_lock_irqsave(&engine->queue_lock, flags);
207 engine->cur_req = NULL; 317 engine->cur_req = NULL;
208 engine->cur_req_prepared = false; 318 engine->cur_req_prepared = false;
@@ -213,7 +323,7 @@ void crypto_finalize_request(struct crypto_engine *engine,
213 323
214 queue_kthread_work(&engine->kworker, &engine->pump_requests); 324 queue_kthread_work(&engine->kworker, &engine->pump_requests);
215} 325}
216EXPORT_SYMBOL_GPL(crypto_finalize_request); 326EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
217 327
218/** 328/**
219 * crypto_engine_start - start the hardware engine 329 * crypto_engine_start - start the hardware engine
@@ -250,7 +360,7 @@ EXPORT_SYMBOL_GPL(crypto_engine_start);
250int crypto_engine_stop(struct crypto_engine *engine) 360int crypto_engine_stop(struct crypto_engine *engine)
251{ 361{
252 unsigned long flags; 362 unsigned long flags;
253 unsigned limit = 500; 363 unsigned int limit = 500;
254 int ret = 0; 364 int ret = 0;
255 365
256 spin_lock_irqsave(&engine->queue_lock, flags); 366 spin_lock_irqsave(&engine->queue_lock, flags);