GNU Linux-libre 5.10.217-gnu1
[releases.git] / drivers / crypto / virtio / virtio_crypto_common.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Common header for Virtio crypto device.
3  *
4  * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
5  */
6
7 #ifndef _VIRTIO_CRYPTO_COMMON_H
8 #define _VIRTIO_CRYPTO_COMMON_H
9
10 #include <linux/virtio.h>
11 #include <linux/crypto.h>
12 #include <linux/spinlock.h>
13 #include <linux/interrupt.h>
14 #include <crypto/aead.h>
15 #include <crypto/aes.h>
16 #include <crypto/engine.h>
17 #include <uapi/linux/virtio_crypto.h>
18
19
20 /* Internal representation of a data virtqueue */
21 struct data_queue {
22         /* Virtqueue associated with this send _queue */
23         struct virtqueue *vq;
24
25         /* To protect the vq operations for the dataq */
26         spinlock_t lock;
27
28         /* Name of the tx queue: dataq.$index */
29         char name[32];
30
31         struct crypto_engine *engine;
32         struct tasklet_struct done_task;
33 };
34
35 struct virtio_crypto {
36         struct virtio_device *vdev;
37         struct virtqueue *ctrl_vq;
38         struct data_queue *data_vq;
39
40         /* To protect the vq operations for the controlq */
41         spinlock_t ctrl_lock;
42
43         /* Maximum of data queues supported by the device */
44         u32 max_data_queues;
45
46         /* Number of queue currently used by the driver */
47         u32 curr_queue;
48
49         /*
50          * Specifies the services mask which the device support,
51          * see VIRTIO_CRYPTO_SERVICE_*
52          */
53         u32 crypto_services;
54
55         /* Detailed algorithms mask */
56         u32 cipher_algo_l;
57         u32 cipher_algo_h;
58         u32 hash_algo;
59         u32 mac_algo_l;
60         u32 mac_algo_h;
61         u32 aead_algo;
62         u32 akcipher_algo;
63
64         /* Maximum length of cipher key */
65         u32 max_cipher_key_len;
66         /* Maximum length of authenticated key */
67         u32 max_auth_key_len;
68         /* Maximum size of per request */
69         u64 max_size;
70
71         unsigned long status;
72         atomic_t ref_count;
73         struct list_head list;
74         struct module *owner;
75         uint8_t dev_id;
76
77         /* Does the affinity hint is set for virtqueues? */
78         bool affinity_hint_set;
79 };
80
81 struct virtio_crypto_sym_session_info {
82         /* Backend session id, which come from the host side */
83         __u64 session_id;
84 };
85
86 /*
87  * Note: there are padding fields in request, clear them to zero before
88  *       sending to host to avoid to divulge any information.
89  * Ex, virtio_crypto_ctrl_request::ctrl::u::destroy_session::padding[48]
90  */
91 struct virtio_crypto_ctrl_request {
92         struct virtio_crypto_op_ctrl_req ctrl;
93         struct virtio_crypto_session_input input;
94         struct virtio_crypto_inhdr ctrl_status;
95         struct completion compl;
96 };
97
98 struct virtio_crypto_request;
99 typedef void (*virtio_crypto_data_callback)
100                 (struct virtio_crypto_request *vc_req, int len);
101
102 struct virtio_crypto_request {
103         uint8_t status;
104         struct virtio_crypto_op_data_req *req_data;
105         struct scatterlist **sgs;
106         struct data_queue *dataq;
107         virtio_crypto_data_callback alg_cb;
108 };
109
110 int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
111 struct list_head *virtcrypto_devmgr_get_head(void);
112 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev);
113 struct virtio_crypto *virtcrypto_devmgr_get_first(void);
114 int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev);
115 int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev);
116 void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev);
117 int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
118 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto_dev,
119                                   uint32_t service,
120                                   uint32_t algo);
121 struct virtio_crypto *virtcrypto_get_dev_node(int node,
122                                               uint32_t service,
123                                               uint32_t algo);
124 int virtcrypto_dev_start(struct virtio_crypto *vcrypto);
125 void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
126 int virtio_crypto_skcipher_crypt_req(
127         struct crypto_engine *engine, void *vreq);
128
129 void
130 virtcrypto_clear_request(struct virtio_crypto_request *vc_req);
131
132 static inline int virtio_crypto_get_current_node(void)
133 {
134         int cpu, node;
135
136         cpu = get_cpu();
137         node = topology_physical_package_id(cpu);
138         put_cpu();
139
140         return node;
141 }
142
143 int virtio_crypto_algs_register(struct virtio_crypto *vcrypto);
144 void virtio_crypto_algs_unregister(struct virtio_crypto *vcrypto);
145 int virtio_crypto_akcipher_algs_register(struct virtio_crypto *vcrypto);
146 void virtio_crypto_akcipher_algs_unregister(struct virtio_crypto *vcrypto);
147 int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[],
148                                   unsigned int out_sgs, unsigned int in_sgs,
149                                   struct virtio_crypto_ctrl_request *vc_ctrl_req);
150
151 #endif /* _VIRTIO_CRYPTO_COMMON_H */