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