xref: /linux-6.15/crypto/crypto_engine.c (revision bcd6e41d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Handle async block request by crypto hardware engine.
4  *
5  * Copyright (C) 2016 Linaro, Inc.
6  *
7  * Author: Baolin Wang <[email protected]>
8  */
9 
10 #include <linux/err.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <crypto/engine.h>
14 #include <uapi/linux/sched/types.h>
15 #include "internal.h"
16 
17 #define CRYPTO_ENGINE_MAX_QLEN 10
18 
19 /**
20  * crypto_finalize_request - finalize one request if the request is done
21  * @engine: the hardware engine
22  * @req: the request need to be finalized
23  * @err: error number
24  */
25 static void crypto_finalize_request(struct crypto_engine *engine,
26 				    struct crypto_async_request *req, int err)
27 {
28 	unsigned long flags;
29 
30 	/*
31 	 * If hardware cannot enqueue more requests
32 	 * and retry mechanism is not supported
33 	 * make sure we are completing the current request
34 	 */
35 	if (!engine->retry_support) {
36 		spin_lock_irqsave(&engine->queue_lock, flags);
37 		if (engine->cur_req == req) {
38 			engine->cur_req = NULL;
39 		}
40 		spin_unlock_irqrestore(&engine->queue_lock, flags);
41 	}
42 
43 	lockdep_assert_in_softirq();
44 	crypto_request_complete(req, err);
45 
46 	kthread_queue_work(engine->kworker, &engine->pump_requests);
47 }
48 
49 /**
50  * crypto_pump_requests - dequeue one request from engine queue to process
51  * @engine: the hardware engine
52  * @in_kthread: true if we are in the context of the request pump thread
53  *
54  * This function checks if there is any request in the engine queue that
55  * needs processing and if so call out to the driver to initialize hardware
56  * and handle each request.
57  */
58 static void crypto_pump_requests(struct crypto_engine *engine,
59 				 bool in_kthread)
60 {
61 	struct crypto_async_request *async_req, *backlog;
62 	unsigned long flags;
63 	bool was_busy = false;
64 	int ret;
65 	struct crypto_engine_ctx *enginectx;
66 
67 	spin_lock_irqsave(&engine->queue_lock, flags);
68 
69 	/* Make sure we are not already running a request */
70 	if (!engine->retry_support && engine->cur_req)
71 		goto out;
72 
73 	/* If another context is idling then defer */
74 	if (engine->idling) {
75 		kthread_queue_work(engine->kworker, &engine->pump_requests);
76 		goto out;
77 	}
78 
79 	/* Check if the engine queue is idle */
80 	if (!crypto_queue_len(&engine->queue) || !engine->running) {
81 		if (!engine->busy)
82 			goto out;
83 
84 		/* Only do teardown in the thread */
85 		if (!in_kthread) {
86 			kthread_queue_work(engine->kworker,
87 					   &engine->pump_requests);
88 			goto out;
89 		}
90 
91 		engine->busy = false;
92 		engine->idling = true;
93 		spin_unlock_irqrestore(&engine->queue_lock, flags);
94 
95 		if (engine->unprepare_crypt_hardware &&
96 		    engine->unprepare_crypt_hardware(engine))
97 			dev_err(engine->dev, "failed to unprepare crypt hardware\n");
98 
99 		spin_lock_irqsave(&engine->queue_lock, flags);
100 		engine->idling = false;
101 		goto out;
102 	}
103 
104 start_request:
105 	/* Get the fist request from the engine queue to handle */
106 	backlog = crypto_get_backlog(&engine->queue);
107 	async_req = crypto_dequeue_request(&engine->queue);
108 	if (!async_req)
109 		goto out;
110 
111 	/*
112 	 * If hardware doesn't support the retry mechanism,
113 	 * keep track of the request we are processing now.
114 	 * We'll need it on completion (crypto_finalize_request).
115 	 */
116 	if (!engine->retry_support)
117 		engine->cur_req = async_req;
118 
119 	if (engine->busy)
120 		was_busy = true;
121 	else
122 		engine->busy = true;
123 
124 	spin_unlock_irqrestore(&engine->queue_lock, flags);
125 
126 	/* Until here we get the request need to be encrypted successfully */
127 	if (!was_busy && engine->prepare_crypt_hardware) {
128 		ret = engine->prepare_crypt_hardware(engine);
129 		if (ret) {
130 			dev_err(engine->dev, "failed to prepare crypt hardware\n");
131 			goto req_err_1;
132 		}
133 	}
134 
135 	enginectx = crypto_tfm_ctx(async_req->tfm);
136 
137 	if (!enginectx->op.do_one_request) {
138 		dev_err(engine->dev, "failed to do request\n");
139 		ret = -EINVAL;
140 		goto req_err_1;
141 	}
142 
143 	ret = enginectx->op.do_one_request(engine, async_req);
144 
145 	/* Request unsuccessfully executed by hardware */
146 	if (ret < 0) {
147 		/*
148 		 * If hardware queue is full (-ENOSPC), requeue request
149 		 * regardless of backlog flag.
150 		 * Otherwise, unprepare and complete the request.
151 		 */
152 		if (!engine->retry_support ||
153 		    (ret != -ENOSPC)) {
154 			dev_err(engine->dev,
155 				"Failed to do one request from queue: %d\n",
156 				ret);
157 			goto req_err_1;
158 		}
159 		spin_lock_irqsave(&engine->queue_lock, flags);
160 		/*
161 		 * If hardware was unable to execute request, enqueue it
162 		 * back in front of crypto-engine queue, to keep the order
163 		 * of requests.
164 		 */
165 		crypto_enqueue_request_head(&engine->queue, async_req);
166 
167 		kthread_queue_work(engine->kworker, &engine->pump_requests);
168 		goto out;
169 	}
170 
171 	goto retry;
172 
173 req_err_1:
174 	crypto_request_complete(async_req, ret);
175 
176 retry:
177 	if (backlog)
178 		crypto_request_complete(backlog, -EINPROGRESS);
179 
180 	/* If retry mechanism is supported, send new requests to engine */
181 	if (engine->retry_support) {
182 		spin_lock_irqsave(&engine->queue_lock, flags);
183 		goto start_request;
184 	}
185 	return;
186 
187 out:
188 	spin_unlock_irqrestore(&engine->queue_lock, flags);
189 
190 	/*
191 	 * Batch requests is possible only if
192 	 * hardware can enqueue multiple requests
193 	 */
194 	if (engine->do_batch_requests) {
195 		ret = engine->do_batch_requests(engine);
196 		if (ret)
197 			dev_err(engine->dev, "failed to do batch requests: %d\n",
198 				ret);
199 	}
200 
201 	return;
202 }
203 
204 static void crypto_pump_work(struct kthread_work *work)
205 {
206 	struct crypto_engine *engine =
207 		container_of(work, struct crypto_engine, pump_requests);
208 
209 	crypto_pump_requests(engine, true);
210 }
211 
212 /**
213  * crypto_transfer_request - transfer the new request into the engine queue
214  * @engine: the hardware engine
215  * @req: the request need to be listed into the engine queue
216  * @need_pump: indicates whether queue the pump of request to kthread_work
217  */
218 static int crypto_transfer_request(struct crypto_engine *engine,
219 				   struct crypto_async_request *req,
220 				   bool need_pump)
221 {
222 	unsigned long flags;
223 	int ret;
224 
225 	spin_lock_irqsave(&engine->queue_lock, flags);
226 
227 	if (!engine->running) {
228 		spin_unlock_irqrestore(&engine->queue_lock, flags);
229 		return -ESHUTDOWN;
230 	}
231 
232 	ret = crypto_enqueue_request(&engine->queue, req);
233 
234 	if (!engine->busy && need_pump)
235 		kthread_queue_work(engine->kworker, &engine->pump_requests);
236 
237 	spin_unlock_irqrestore(&engine->queue_lock, flags);
238 	return ret;
239 }
240 
241 /**
242  * crypto_transfer_request_to_engine - transfer one request to list
243  * into the engine queue
244  * @engine: the hardware engine
245  * @req: the request need to be listed into the engine queue
246  */
247 static int crypto_transfer_request_to_engine(struct crypto_engine *engine,
248 					     struct crypto_async_request *req)
249 {
250 	return crypto_transfer_request(engine, req, true);
251 }
252 
253 /**
254  * crypto_transfer_aead_request_to_engine - transfer one aead_request
255  * to list into the engine queue
256  * @engine: the hardware engine
257  * @req: the request need to be listed into the engine queue
258  */
259 int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine,
260 					   struct aead_request *req)
261 {
262 	return crypto_transfer_request_to_engine(engine, &req->base);
263 }
264 EXPORT_SYMBOL_GPL(crypto_transfer_aead_request_to_engine);
265 
266 /**
267  * crypto_transfer_akcipher_request_to_engine - transfer one akcipher_request
268  * to list into the engine queue
269  * @engine: the hardware engine
270  * @req: the request need to be listed into the engine queue
271  */
272 int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine,
273 					       struct akcipher_request *req)
274 {
275 	return crypto_transfer_request_to_engine(engine, &req->base);
276 }
277 EXPORT_SYMBOL_GPL(crypto_transfer_akcipher_request_to_engine);
278 
279 /**
280  * crypto_transfer_hash_request_to_engine - transfer one ahash_request
281  * to list into the engine queue
282  * @engine: the hardware engine
283  * @req: the request need to be listed into the engine queue
284  */
285 int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
286 					   struct ahash_request *req)
287 {
288 	return crypto_transfer_request_to_engine(engine, &req->base);
289 }
290 EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
291 
292 /**
293  * crypto_transfer_kpp_request_to_engine - transfer one kpp_request to list
294  * into the engine queue
295  * @engine: the hardware engine
296  * @req: the request need to be listed into the engine queue
297  */
298 int crypto_transfer_kpp_request_to_engine(struct crypto_engine *engine,
299 					  struct kpp_request *req)
300 {
301 	return crypto_transfer_request_to_engine(engine, &req->base);
302 }
303 EXPORT_SYMBOL_GPL(crypto_transfer_kpp_request_to_engine);
304 
305 /**
306  * crypto_transfer_skcipher_request_to_engine - transfer one skcipher_request
307  * to list into the engine queue
308  * @engine: the hardware engine
309  * @req: the request need to be listed into the engine queue
310  */
311 int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine,
312 					       struct skcipher_request *req)
313 {
314 	return crypto_transfer_request_to_engine(engine, &req->base);
315 }
316 EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request_to_engine);
317 
318 /**
319  * crypto_finalize_aead_request - finalize one aead_request if
320  * the request is done
321  * @engine: the hardware engine
322  * @req: the request need to be finalized
323  * @err: error number
324  */
325 void crypto_finalize_aead_request(struct crypto_engine *engine,
326 				  struct aead_request *req, int err)
327 {
328 	return crypto_finalize_request(engine, &req->base, err);
329 }
330 EXPORT_SYMBOL_GPL(crypto_finalize_aead_request);
331 
332 /**
333  * crypto_finalize_akcipher_request - finalize one akcipher_request if
334  * the request is done
335  * @engine: the hardware engine
336  * @req: the request need to be finalized
337  * @err: error number
338  */
339 void crypto_finalize_akcipher_request(struct crypto_engine *engine,
340 				      struct akcipher_request *req, int err)
341 {
342 	return crypto_finalize_request(engine, &req->base, err);
343 }
344 EXPORT_SYMBOL_GPL(crypto_finalize_akcipher_request);
345 
346 /**
347  * crypto_finalize_hash_request - finalize one ahash_request if
348  * the request is done
349  * @engine: the hardware engine
350  * @req: the request need to be finalized
351  * @err: error number
352  */
353 void crypto_finalize_hash_request(struct crypto_engine *engine,
354 				  struct ahash_request *req, int err)
355 {
356 	return crypto_finalize_request(engine, &req->base, err);
357 }
358 EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
359 
360 /**
361  * crypto_finalize_kpp_request - finalize one kpp_request if the request is done
362  * @engine: the hardware engine
363  * @req: the request need to be finalized
364  * @err: error number
365  */
366 void crypto_finalize_kpp_request(struct crypto_engine *engine,
367 				 struct kpp_request *req, int err)
368 {
369 	return crypto_finalize_request(engine, &req->base, err);
370 }
371 EXPORT_SYMBOL_GPL(crypto_finalize_kpp_request);
372 
373 /**
374  * crypto_finalize_skcipher_request - finalize one skcipher_request if
375  * the request is done
376  * @engine: the hardware engine
377  * @req: the request need to be finalized
378  * @err: error number
379  */
380 void crypto_finalize_skcipher_request(struct crypto_engine *engine,
381 				      struct skcipher_request *req, int err)
382 {
383 	return crypto_finalize_request(engine, &req->base, err);
384 }
385 EXPORT_SYMBOL_GPL(crypto_finalize_skcipher_request);
386 
387 /**
388  * crypto_engine_start - start the hardware engine
389  * @engine: the hardware engine need to be started
390  *
391  * Return 0 on success, else on fail.
392  */
393 int crypto_engine_start(struct crypto_engine *engine)
394 {
395 	unsigned long flags;
396 
397 	spin_lock_irqsave(&engine->queue_lock, flags);
398 
399 	if (engine->running || engine->busy) {
400 		spin_unlock_irqrestore(&engine->queue_lock, flags);
401 		return -EBUSY;
402 	}
403 
404 	engine->running = true;
405 	spin_unlock_irqrestore(&engine->queue_lock, flags);
406 
407 	kthread_queue_work(engine->kworker, &engine->pump_requests);
408 
409 	return 0;
410 }
411 EXPORT_SYMBOL_GPL(crypto_engine_start);
412 
413 /**
414  * crypto_engine_stop - stop the hardware engine
415  * @engine: the hardware engine need to be stopped
416  *
417  * Return 0 on success, else on fail.
418  */
419 int crypto_engine_stop(struct crypto_engine *engine)
420 {
421 	unsigned long flags;
422 	unsigned int limit = 500;
423 	int ret = 0;
424 
425 	spin_lock_irqsave(&engine->queue_lock, flags);
426 
427 	/*
428 	 * If the engine queue is not empty or the engine is on busy state,
429 	 * we need to wait for a while to pump the requests of engine queue.
430 	 */
431 	while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
432 		spin_unlock_irqrestore(&engine->queue_lock, flags);
433 		msleep(20);
434 		spin_lock_irqsave(&engine->queue_lock, flags);
435 	}
436 
437 	if (crypto_queue_len(&engine->queue) || engine->busy)
438 		ret = -EBUSY;
439 	else
440 		engine->running = false;
441 
442 	spin_unlock_irqrestore(&engine->queue_lock, flags);
443 
444 	if (ret)
445 		dev_warn(engine->dev, "could not stop engine\n");
446 
447 	return ret;
448 }
449 EXPORT_SYMBOL_GPL(crypto_engine_stop);
450 
451 /**
452  * crypto_engine_alloc_init_and_set - allocate crypto hardware engine structure
453  * and initialize it by setting the maximum number of entries in the software
454  * crypto-engine queue.
455  * @dev: the device attached with one hardware engine
456  * @retry_support: whether hardware has support for retry mechanism
457  * @cbk_do_batch: pointer to a callback function to be invoked when executing
458  *                a batch of requests.
459  *                This has the form:
460  *                callback(struct crypto_engine *engine)
461  *                where:
462  *                engine: the crypto engine structure.
463  * @rt: whether this queue is set to run as a realtime task
464  * @qlen: maximum size of the crypto-engine queue
465  *
466  * This must be called from context that can sleep.
467  * Return: the crypto engine structure on success, else NULL.
468  */
469 struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev,
470 						       bool retry_support,
471 						       int (*cbk_do_batch)(struct crypto_engine *engine),
472 						       bool rt, int qlen)
473 {
474 	struct crypto_engine *engine;
475 
476 	if (!dev)
477 		return NULL;
478 
479 	engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
480 	if (!engine)
481 		return NULL;
482 
483 	engine->dev = dev;
484 	engine->rt = rt;
485 	engine->running = false;
486 	engine->busy = false;
487 	engine->idling = false;
488 	engine->retry_support = retry_support;
489 	engine->priv_data = dev;
490 	/*
491 	 * Batch requests is possible only if
492 	 * hardware has support for retry mechanism.
493 	 */
494 	engine->do_batch_requests = retry_support ? cbk_do_batch : NULL;
495 
496 	snprintf(engine->name, sizeof(engine->name),
497 		 "%s-engine", dev_name(dev));
498 
499 	crypto_init_queue(&engine->queue, qlen);
500 	spin_lock_init(&engine->queue_lock);
501 
502 	engine->kworker = kthread_create_worker(0, "%s", engine->name);
503 	if (IS_ERR(engine->kworker)) {
504 		dev_err(dev, "failed to create crypto request pump task\n");
505 		return NULL;
506 	}
507 	kthread_init_work(&engine->pump_requests, crypto_pump_work);
508 
509 	if (engine->rt) {
510 		dev_info(dev, "will run requests pump with realtime priority\n");
511 		sched_set_fifo(engine->kworker->task);
512 	}
513 
514 	return engine;
515 }
516 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init_and_set);
517 
518 /**
519  * crypto_engine_alloc_init - allocate crypto hardware engine structure and
520  * initialize it.
521  * @dev: the device attached with one hardware engine
522  * @rt: whether this queue is set to run as a realtime task
523  *
524  * This must be called from context that can sleep.
525  * Return: the crypto engine structure on success, else NULL.
526  */
527 struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
528 {
529 	return crypto_engine_alloc_init_and_set(dev, false, NULL, rt,
530 						CRYPTO_ENGINE_MAX_QLEN);
531 }
532 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
533 
534 /**
535  * crypto_engine_exit - free the resources of hardware engine when exit
536  * @engine: the hardware engine need to be freed
537  *
538  * Return 0 for success.
539  */
540 int crypto_engine_exit(struct crypto_engine *engine)
541 {
542 	int ret;
543 
544 	ret = crypto_engine_stop(engine);
545 	if (ret)
546 		return ret;
547 
548 	kthread_destroy_worker(engine->kworker);
549 
550 	return 0;
551 }
552 EXPORT_SYMBOL_GPL(crypto_engine_exit);
553 
554 MODULE_LICENSE("GPL");
555 MODULE_DESCRIPTION("Crypto hardware engine framework");
556