crypto: engine - move crypto engine to its own header
[cascardo/linux.git] / include / crypto / engine.h
1 /*
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>
20
21 #define ENGINE_NAME_LEN 30
22 /*
23  * struct crypto_engine - crypto hardware engine
24  * @name: the engine name
25  * @idling: the engine is entering idle state
26  * @busy: request pump is busy
27  * @running: the engine is on working
28  * @cur_req_prepared: current request is prepared
29  * @list: link with the global crypto engine list
30  * @queue_lock: spinlock to syncronise access to request queue
31  * @queue: the crypto queue of the engine
32  * @rt: whether this queue is set to run as a realtime task
33  * @prepare_crypt_hardware: a request will soon arrive from the queue
34  * so the subsystem requests the driver to prepare the hardware
35  * by issuing this call
36  * @unprepare_crypt_hardware: there are currently no more requests on the
37  * queue so the subsystem notifies the driver that it may relax the
38  * hardware by issuing this call
39  * @prepare_request: do some prepare if need before handle the current request
40  * @unprepare_request: undo any work done by prepare_message()
41  * @crypt_one_request: do encryption for current request
42  * @kworker: thread struct for request pump
43  * @kworker_task: pointer to task for request pump kworker thread
44  * @pump_requests: work struct for scheduling work to the request pump
45  * @priv_data: the engine private data
46  * @cur_req: the current request which is on processing
47  */
48 struct crypto_engine {
49         char                    name[ENGINE_NAME_LEN];
50         bool                    idling;
51         bool                    busy;
52         bool                    running;
53         bool                    cur_req_prepared;
54
55         struct list_head        list;
56         spinlock_t              queue_lock;
57         struct crypto_queue     queue;
58
59         bool                    rt;
60
61         int (*prepare_crypt_hardware)(struct crypto_engine *engine);
62         int (*unprepare_crypt_hardware)(struct crypto_engine *engine);
63
64         int (*prepare_request)(struct crypto_engine *engine,
65                                struct ablkcipher_request *req);
66         int (*unprepare_request)(struct crypto_engine *engine,
67                                  struct ablkcipher_request *req);
68         int (*crypt_one_request)(struct crypto_engine *engine,
69                                  struct ablkcipher_request *req);
70
71         struct kthread_worker           kworker;
72         struct task_struct              *kworker_task;
73         struct kthread_work             pump_requests;
74
75         void                            *priv_data;
76         struct ablkcipher_request       *cur_req;
77 };
78
79 int crypto_transfer_request(struct crypto_engine *engine,
80                             struct ablkcipher_request *req, bool need_pump);
81 int crypto_transfer_request_to_engine(struct crypto_engine *engine,
82                                       struct ablkcipher_request *req);
83 void crypto_finalize_request(struct crypto_engine *engine,
84                              struct ablkcipher_request *req, int err);
85 int crypto_engine_start(struct crypto_engine *engine);
86 int crypto_engine_stop(struct crypto_engine *engine);
87 struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt);
88 int crypto_engine_exit(struct crypto_engine *engine);
89
90 #endif /* _CRYPTO_ENGINE_H */