Blame view

crypto/crypto_engine.c 12.2 KB
735d37b54   Baolin Wang   crypto: engine - ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  /*
   * Handle async block request by crypto hardware engine.
   *
   * Copyright (C) 2016 Linaro, Inc.
   *
   * Author: Baolin Wang <baolin.wang@linaro.org>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the Free
   * Software Foundation; either version 2 of the License, or (at your option)
   * any later version.
   *
   */
  
  #include <linux/err.h>
  #include <linux/delay.h>
2589ad840   Corentin LABBE   crypto: engine - ...
17
  #include <crypto/engine.h>
4cba7cf02   Corentin LABBE   crypto: engine - ...
18
  #include <crypto/internal/hash.h>
735d37b54   Baolin Wang   crypto: engine - ...
19
20
21
  #include "internal.h"
  
  #define CRYPTO_ENGINE_MAX_QLEN 10
735d37b54   Baolin Wang   crypto: engine - ...
22
23
24
25
26
27
28
29
30
31
32
33
34
  /**
   * crypto_pump_requests - dequeue one request from engine queue to process
   * @engine: the hardware engine
   * @in_kthread: true if we are in the context of the request pump thread
   *
   * This function checks if there is any request in the engine queue that
   * needs processing and if so call out to the driver to initialize hardware
   * and handle each request.
   */
  static void crypto_pump_requests(struct crypto_engine *engine,
  				 bool in_kthread)
  {
  	struct crypto_async_request *async_req, *backlog;
4cba7cf02   Corentin LABBE   crypto: engine - ...
35
36
  	struct ahash_request *hreq;
  	struct ablkcipher_request *breq;
735d37b54   Baolin Wang   crypto: engine - ...
37
38
  	unsigned long flags;
  	bool was_busy = false;
4cba7cf02   Corentin LABBE   crypto: engine - ...
39
  	int ret, rtype;
735d37b54   Baolin Wang   crypto: engine - ...
40
41
42
43
44
45
46
47
48
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
  
  	/* Make sure we are not already running a request */
  	if (engine->cur_req)
  		goto out;
  
  	/* If another context is idling then defer */
  	if (engine->idling) {
3989144f8   Petr Mladek   kthread: kthread ...
49
  		kthread_queue_work(&engine->kworker, &engine->pump_requests);
735d37b54   Baolin Wang   crypto: engine - ...
50
51
52
53
54
55
56
57
58
59
  		goto out;
  	}
  
  	/* Check if the engine queue is idle */
  	if (!crypto_queue_len(&engine->queue) || !engine->running) {
  		if (!engine->busy)
  			goto out;
  
  		/* Only do teardown in the thread */
  		if (!in_kthread) {
3989144f8   Petr Mladek   kthread: kthread ...
60
  			kthread_queue_work(&engine->kworker,
735d37b54   Baolin Wang   crypto: engine - ...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  					   &engine->pump_requests);
  			goto out;
  		}
  
  		engine->busy = false;
  		engine->idling = true;
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  
  		if (engine->unprepare_crypt_hardware &&
  		    engine->unprepare_crypt_hardware(engine))
  			pr_err("failed to unprepare crypt hardware
  ");
  
  		spin_lock_irqsave(&engine->queue_lock, flags);
  		engine->idling = false;
  		goto out;
  	}
  
  	/* Get the fist request from the engine queue to handle */
  	backlog = crypto_get_backlog(&engine->queue);
  	async_req = crypto_dequeue_request(&engine->queue);
  	if (!async_req)
  		goto out;
4cba7cf02   Corentin LABBE   crypto: engine - ...
84
  	engine->cur_req = async_req;
735d37b54   Baolin Wang   crypto: engine - ...
85
86
87
88
89
90
91
92
93
  	if (backlog)
  		backlog->complete(backlog, -EINPROGRESS);
  
  	if (engine->busy)
  		was_busy = true;
  	else
  		engine->busy = true;
  
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
4cba7cf02   Corentin LABBE   crypto: engine - ...
94
  	rtype = crypto_tfm_alg_type(engine->cur_req->tfm);
735d37b54   Baolin Wang   crypto: engine - ...
95
96
97
98
99
100
101
102
103
  	/* Until here we get the request need to be encrypted successfully */
  	if (!was_busy && engine->prepare_crypt_hardware) {
  		ret = engine->prepare_crypt_hardware(engine);
  		if (ret) {
  			pr_err("failed to prepare crypt hardware
  ");
  			goto req_err;
  		}
  	}
4cba7cf02   Corentin LABBE   crypto: engine - ...
104
105
106
107
108
109
110
111
112
113
114
115
116
  	switch (rtype) {
  	case CRYPTO_ALG_TYPE_AHASH:
  		hreq = ahash_request_cast(engine->cur_req);
  		if (engine->prepare_hash_request) {
  			ret = engine->prepare_hash_request(engine, hreq);
  			if (ret) {
  				pr_err("failed to prepare request: %d
  ", ret);
  				goto req_err;
  			}
  			engine->cur_req_prepared = true;
  		}
  		ret = engine->hash_one_request(engine, hreq);
735d37b54   Baolin Wang   crypto: engine - ...
117
  		if (ret) {
4cba7cf02   Corentin LABBE   crypto: engine - ...
118
119
  			pr_err("failed to hash one request from queue
  ");
735d37b54   Baolin Wang   crypto: engine - ...
120
121
  			goto req_err;
  		}
4cba7cf02   Corentin LABBE   crypto: engine - ...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  		return;
  	case CRYPTO_ALG_TYPE_ABLKCIPHER:
  		breq = ablkcipher_request_cast(engine->cur_req);
  		if (engine->prepare_cipher_request) {
  			ret = engine->prepare_cipher_request(engine, breq);
  			if (ret) {
  				pr_err("failed to prepare request: %d
  ", ret);
  				goto req_err;
  			}
  			engine->cur_req_prepared = true;
  		}
  		ret = engine->cipher_one_request(engine, breq);
  		if (ret) {
  			pr_err("failed to cipher one request from queue
  ");
  			goto req_err;
  		}
  		return;
  	default:
  		pr_err("failed to prepare request of unknown type
  ");
  		return;
735d37b54   Baolin Wang   crypto: engine - ...
145
  	}
735d37b54   Baolin Wang   crypto: engine - ...
146
147
  
  req_err:
4cba7cf02   Corentin LABBE   crypto: engine - ...
148
149
150
151
152
153
154
155
156
157
  	switch (rtype) {
  	case CRYPTO_ALG_TYPE_AHASH:
  		hreq = ahash_request_cast(engine->cur_req);
  		crypto_finalize_hash_request(engine, hreq, ret);
  		break;
  	case CRYPTO_ALG_TYPE_ABLKCIPHER:
  		breq = ablkcipher_request_cast(engine->cur_req);
  		crypto_finalize_cipher_request(engine, breq, ret);
  		break;
  	}
735d37b54   Baolin Wang   crypto: engine - ...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  	return;
  
  out:
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
  }
  
  static void crypto_pump_work(struct kthread_work *work)
  {
  	struct crypto_engine *engine =
  		container_of(work, struct crypto_engine, pump_requests);
  
  	crypto_pump_requests(engine, true);
  }
  
  /**
4cba7cf02   Corentin LABBE   crypto: engine - ...
173
174
   * crypto_transfer_cipher_request - transfer the new request into the
   * enginequeue
735d37b54   Baolin Wang   crypto: engine - ...
175
176
177
   * @engine: the hardware engine
   * @req: the request need to be listed into the engine queue
   */
4cba7cf02   Corentin LABBE   crypto: engine - ...
178
179
180
  int crypto_transfer_cipher_request(struct crypto_engine *engine,
  				   struct ablkcipher_request *req,
  				   bool need_pump)
735d37b54   Baolin Wang   crypto: engine - ...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  {
  	unsigned long flags;
  	int ret;
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
  
  	if (!engine->running) {
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  		return -ESHUTDOWN;
  	}
  
  	ret = ablkcipher_enqueue_request(&engine->queue, req);
  
  	if (!engine->busy && need_pump)
3989144f8   Petr Mladek   kthread: kthread ...
195
  		kthread_queue_work(&engine->kworker, &engine->pump_requests);
735d37b54   Baolin Wang   crypto: engine - ...
196
197
198
199
  
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
  	return ret;
  }
4cba7cf02   Corentin LABBE   crypto: engine - ...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request);
  
  /**
   * crypto_transfer_cipher_request_to_engine - transfer one request to list
   * into the engine queue
   * @engine: the hardware engine
   * @req: the request need to be listed into the engine queue
   */
  int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
  					     struct ablkcipher_request *req)
  {
  	return crypto_transfer_cipher_request(engine, req, true);
  }
  EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request_to_engine);
  
  /**
   * crypto_transfer_hash_request - transfer the new request into the
   * enginequeue
   * @engine: the hardware engine
   * @req: the request need to be listed into the engine queue
   */
  int crypto_transfer_hash_request(struct crypto_engine *engine,
  				 struct ahash_request *req, bool need_pump)
  {
  	unsigned long flags;
  	int ret;
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
  
  	if (!engine->running) {
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  		return -ESHUTDOWN;
  	}
  
  	ret = ahash_enqueue_request(&engine->queue, req);
  
  	if (!engine->busy && need_pump)
3989144f8   Petr Mladek   kthread: kthread ...
237
  		kthread_queue_work(&engine->kworker, &engine->pump_requests);
4cba7cf02   Corentin LABBE   crypto: engine - ...
238
239
240
241
242
  
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
  	return ret;
  }
  EXPORT_SYMBOL_GPL(crypto_transfer_hash_request);
735d37b54   Baolin Wang   crypto: engine - ...
243
244
  
  /**
4cba7cf02   Corentin LABBE   crypto: engine - ...
245
246
   * crypto_transfer_hash_request_to_engine - transfer one request to list
   * into the engine queue
735d37b54   Baolin Wang   crypto: engine - ...
247
248
249
   * @engine: the hardware engine
   * @req: the request need to be listed into the engine queue
   */
4cba7cf02   Corentin LABBE   crypto: engine - ...
250
251
  int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
  					   struct ahash_request *req)
735d37b54   Baolin Wang   crypto: engine - ...
252
  {
4cba7cf02   Corentin LABBE   crypto: engine - ...
253
  	return crypto_transfer_hash_request(engine, req, true);
735d37b54   Baolin Wang   crypto: engine - ...
254
  }
4cba7cf02   Corentin LABBE   crypto: engine - ...
255
  EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
735d37b54   Baolin Wang   crypto: engine - ...
256
257
  
  /**
4cba7cf02   Corentin LABBE   crypto: engine - ...
258
   * crypto_finalize_cipher_request - finalize one request if the request is done
735d37b54   Baolin Wang   crypto: engine - ...
259
260
261
262
   * @engine: the hardware engine
   * @req: the request need to be finalized
   * @err: error number
   */
4cba7cf02   Corentin LABBE   crypto: engine - ...
263
264
  void crypto_finalize_cipher_request(struct crypto_engine *engine,
  				    struct ablkcipher_request *req, int err)
735d37b54   Baolin Wang   crypto: engine - ...
265
266
267
268
269
270
  {
  	unsigned long flags;
  	bool finalize_cur_req = false;
  	int ret;
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
4cba7cf02   Corentin LABBE   crypto: engine - ...
271
  	if (engine->cur_req == &req->base)
735d37b54   Baolin Wang   crypto: engine - ...
272
273
274
275
  		finalize_cur_req = true;
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
  
  	if (finalize_cur_req) {
4cba7cf02   Corentin LABBE   crypto: engine - ...
276
277
278
  		if (engine->cur_req_prepared &&
  		    engine->unprepare_cipher_request) {
  			ret = engine->unprepare_cipher_request(engine, req);
735d37b54   Baolin Wang   crypto: engine - ...
279
280
281
282
  			if (ret)
  				pr_err("failed to unprepare request
  ");
  		}
4cba7cf02   Corentin LABBE   crypto: engine - ...
283
284
285
286
287
288
289
  		spin_lock_irqsave(&engine->queue_lock, flags);
  		engine->cur_req = NULL;
  		engine->cur_req_prepared = false;
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  	}
  
  	req->base.complete(&req->base, err);
735d37b54   Baolin Wang   crypto: engine - ...
290

3989144f8   Petr Mladek   kthread: kthread ...
291
  	kthread_queue_work(&engine->kworker, &engine->pump_requests);
4cba7cf02   Corentin LABBE   crypto: engine - ...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  }
  EXPORT_SYMBOL_GPL(crypto_finalize_cipher_request);
  
  /**
   * crypto_finalize_hash_request - finalize one request if the request is done
   * @engine: the hardware engine
   * @req: the request need to be finalized
   * @err: error number
   */
  void crypto_finalize_hash_request(struct crypto_engine *engine,
  				  struct ahash_request *req, int err)
  {
  	unsigned long flags;
  	bool finalize_cur_req = false;
  	int ret;
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
  	if (engine->cur_req == &req->base)
  		finalize_cur_req = true;
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
  
  	if (finalize_cur_req) {
  		if (engine->cur_req_prepared &&
  		    engine->unprepare_hash_request) {
  			ret = engine->unprepare_hash_request(engine, req);
  			if (ret)
  				pr_err("failed to unprepare request
  ");
  		}
735d37b54   Baolin Wang   crypto: engine - ...
321
322
323
324
325
326
327
  		spin_lock_irqsave(&engine->queue_lock, flags);
  		engine->cur_req = NULL;
  		engine->cur_req_prepared = false;
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  	}
  
  	req->base.complete(&req->base, err);
3989144f8   Petr Mladek   kthread: kthread ...
328
  	kthread_queue_work(&engine->kworker, &engine->pump_requests);
735d37b54   Baolin Wang   crypto: engine - ...
329
  }
4cba7cf02   Corentin LABBE   crypto: engine - ...
330
  EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
735d37b54   Baolin Wang   crypto: engine - ...
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  
  /**
   * crypto_engine_start - start the hardware engine
   * @engine: the hardware engine need to be started
   *
   * Return 0 on success, else on fail.
   */
  int crypto_engine_start(struct crypto_engine *engine)
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
  
  	if (engine->running || engine->busy) {
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  		return -EBUSY;
  	}
  
  	engine->running = true;
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
3989144f8   Petr Mladek   kthread: kthread ...
351
  	kthread_queue_work(&engine->kworker, &engine->pump_requests);
735d37b54   Baolin Wang   crypto: engine - ...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(crypto_engine_start);
  
  /**
   * crypto_engine_stop - stop the hardware engine
   * @engine: the hardware engine need to be stopped
   *
   * Return 0 on success, else on fail.
   */
  int crypto_engine_stop(struct crypto_engine *engine)
  {
  	unsigned long flags;
4cba7cf02   Corentin LABBE   crypto: engine - ...
366
  	unsigned int limit = 500;
735d37b54   Baolin Wang   crypto: engine - ...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  	int ret = 0;
  
  	spin_lock_irqsave(&engine->queue_lock, flags);
  
  	/*
  	 * If the engine queue is not empty or the engine is on busy state,
  	 * we need to wait for a while to pump the requests of engine queue.
  	 */
  	while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
  		spin_unlock_irqrestore(&engine->queue_lock, flags);
  		msleep(20);
  		spin_lock_irqsave(&engine->queue_lock, flags);
  	}
  
  	if (crypto_queue_len(&engine->queue) || engine->busy)
  		ret = -EBUSY;
  	else
  		engine->running = false;
  
  	spin_unlock_irqrestore(&engine->queue_lock, flags);
  
  	if (ret)
  		pr_warn("could not stop engine
  ");
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(crypto_engine_stop);
  
  /**
   * crypto_engine_alloc_init - allocate crypto hardware engine structure and
   * initialize it.
   * @dev: the device attached with one hardware engine
   * @rt: whether this queue is set to run as a realtime task
   *
   * This must be called from context that can sleep.
   * Return: the crypto engine structure on success, else NULL.
   */
  struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
  {
  	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  	struct crypto_engine *engine;
  
  	if (!dev)
  		return NULL;
  
  	engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
  	if (!engine)
  		return NULL;
  
  	engine->rt = rt;
  	engine->running = false;
  	engine->busy = false;
  	engine->idling = false;
  	engine->cur_req_prepared = false;
  	engine->priv_data = dev;
  	snprintf(engine->name, sizeof(engine->name),
  		 "%s-engine", dev_name(dev));
  
  	crypto_init_queue(&engine->queue, CRYPTO_ENGINE_MAX_QLEN);
  	spin_lock_init(&engine->queue_lock);
3989144f8   Petr Mladek   kthread: kthread ...
428
  	kthread_init_worker(&engine->kworker);
735d37b54   Baolin Wang   crypto: engine - ...
429
430
431
432
433
434
435
436
  	engine->kworker_task = kthread_run(kthread_worker_fn,
  					   &engine->kworker, "%s",
  					   engine->name);
  	if (IS_ERR(engine->kworker_task)) {
  		dev_err(dev, "failed to create crypto request pump task
  ");
  		return NULL;
  	}
3989144f8   Petr Mladek   kthread: kthread ...
437
  	kthread_init_work(&engine->pump_requests, crypto_pump_work);
735d37b54   Baolin Wang   crypto: engine - ...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
  
  	if (engine->rt) {
  		dev_info(dev, "will run requests pump with realtime priority
  ");
  		sched_setscheduler(engine->kworker_task, SCHED_FIFO, &param);
  	}
  
  	return engine;
  }
  EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
  
  /**
   * crypto_engine_exit - free the resources of hardware engine when exit
   * @engine: the hardware engine need to be freed
   *
   * Return 0 for success.
   */
  int crypto_engine_exit(struct crypto_engine *engine)
  {
  	int ret;
  
  	ret = crypto_engine_stop(engine);
  	if (ret)
  		return ret;
3989144f8   Petr Mladek   kthread: kthread ...
462
  	kthread_flush_worker(&engine->kworker);
735d37b54   Baolin Wang   crypto: engine - ...
463
464
465
466
467
468
469
470
  	kthread_stop(engine->kworker_task);
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(crypto_engine_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Crypto hardware engine framework");