GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / crypto / virtio / virtio_crypto_common.h
1 /* Common header for Virtio crypto device.
2  *
3  * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef _VIRTIO_CRYPTO_COMMON_H
20 #define _VIRTIO_CRYPTO_COMMON_H
21
22 #include <linux/virtio.h>
23 #include <linux/crypto.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <crypto/aead.h>
27 #include <crypto/aes.h>
28 #include <crypto/engine.h>
29
30
31 /* Internal representation of a data virtqueue */
32 struct data_queue {
33         /* Virtqueue associated with this send _queue */
34         struct virtqueue *vq;
35
36         /* To protect the vq operations for the dataq */
37         spinlock_t lock;
38
39         /* Name of the tx queue: dataq.$index */
40         char name[32];
41
42         struct crypto_engine *engine;
43         struct tasklet_struct done_task;
44 };
45
46 struct virtio_crypto {
47         struct virtio_device *vdev;
48         struct virtqueue *ctrl_vq;
49         struct data_queue *data_vq;
50
51         /* To protect the vq operations for the controlq */
52         spinlock_t ctrl_lock;
53
54         /* Maximum of data queues supported by the device */
55         u32 max_data_queues;
56
57         /* Number of queue currently used by the driver */
58         u32 curr_queue;
59
60         /*
61          * Specifies the services mask which the device support,
62          * see VIRTIO_CRYPTO_SERVICE_*
63          */
64         u32 crypto_services;
65
66         /* Detailed algorithms mask */
67         u32 cipher_algo_l;
68         u32 cipher_algo_h;
69         u32 hash_algo;
70         u32 mac_algo_l;
71         u32 mac_algo_h;
72         u32 aead_algo;
73
74         /* Maximum length of cipher key */
75         u32 max_cipher_key_len;
76         /* Maximum length of authenticated key */
77         u32 max_auth_key_len;
78         /* Maximum size of per request */
79         u64 max_size;
80
81         /* Control VQ buffers: protected by the ctrl_lock */
82         struct virtio_crypto_op_ctrl_req ctrl;
83         struct virtio_crypto_session_input input;
84         struct virtio_crypto_inhdr ctrl_status;
85
86         unsigned long status;
87         atomic_t ref_count;
88         struct list_head list;
89         struct module *owner;
90         uint8_t dev_id;
91
92         /* Does the affinity hint is set for virtqueues? */
93         bool affinity_hint_set;
94 };
95
96 struct virtio_crypto_sym_session_info {
97         /* Backend session id, which come from the host side */
98         __u64 session_id;
99 };
100
101 struct virtio_crypto_request;
102 typedef void (*virtio_crypto_data_callback)
103                 (struct virtio_crypto_request *vc_req, int len);
104
105 struct virtio_crypto_request {
106         uint8_t status;
107         struct virtio_crypto_op_data_req *req_data;
108         struct scatterlist **sgs;
109         struct data_queue *dataq;
110         virtio_crypto_data_callback alg_cb;
111 };
112
113 int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
114 struct list_head *virtcrypto_devmgr_get_head(void);
115 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev);
116 struct virtio_crypto *virtcrypto_devmgr_get_first(void);
117 int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev);
118 int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev);
119 void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev);
120 int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
121 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto_dev,
122                                   uint32_t service,
123                                   uint32_t algo);
124 struct virtio_crypto *virtcrypto_get_dev_node(int node,
125                                               uint32_t service,
126                                               uint32_t algo);
127 int virtcrypto_dev_start(struct virtio_crypto *vcrypto);
128 void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
129 int virtio_crypto_ablkcipher_crypt_req(
130         struct crypto_engine *engine, void *vreq);
131
132 void
133 virtcrypto_clear_request(struct virtio_crypto_request *vc_req);
134
135 static inline int virtio_crypto_get_current_node(void)
136 {
137         int cpu, node;
138
139         cpu = get_cpu();
140         node = topology_physical_package_id(cpu);
141         put_cpu();
142
143         return node;
144 }
145
146 int virtio_crypto_algs_register(struct virtio_crypto *vcrypto);
147 void virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto);
148
149 #endif /* _VIRTIO_CRYPTO_COMMON_H */