Linux 4.19.133
[linux/fpc-iii.git] / drivers / crypto / virtio / virtio_crypto_mgr.c
blobd70de3a4f7d7937f0e500def156492f339992556
1 /* Management for virtio crypto devices (refer to adf_dev_mgr.c)
3 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
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.
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.
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/>.
19 #include <linux/mutex.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
23 #include <uapi/linux/virtio_crypto.h>
24 #include "virtio_crypto_common.h"
26 static LIST_HEAD(virtio_crypto_table);
27 static uint32_t num_devices;
29 /* The table_lock protects the above global list and num_devices */
30 static DEFINE_MUTEX(table_lock);
32 #define VIRTIO_CRYPTO_MAX_DEVICES 32
36 * virtcrypto_devmgr_add_dev() - Add vcrypto_dev to the acceleration
37 * framework.
38 * @vcrypto_dev: Pointer to virtio crypto device.
40 * Function adds virtio crypto device to the global list.
41 * To be used by virtio crypto device specific drivers.
43 * Return: 0 on success, error code othewise.
45 int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev)
47 struct list_head *itr;
49 mutex_lock(&table_lock);
50 if (num_devices == VIRTIO_CRYPTO_MAX_DEVICES) {
51 pr_info("virtio_crypto: only support up to %d devices\n",
52 VIRTIO_CRYPTO_MAX_DEVICES);
53 mutex_unlock(&table_lock);
54 return -EFAULT;
57 list_for_each(itr, &virtio_crypto_table) {
58 struct virtio_crypto *ptr =
59 list_entry(itr, struct virtio_crypto, list);
61 if (ptr == vcrypto_dev) {
62 mutex_unlock(&table_lock);
63 return -EEXIST;
66 atomic_set(&vcrypto_dev->ref_count, 0);
67 list_add_tail(&vcrypto_dev->list, &virtio_crypto_table);
68 vcrypto_dev->dev_id = num_devices++;
69 mutex_unlock(&table_lock);
70 return 0;
73 struct list_head *virtcrypto_devmgr_get_head(void)
75 return &virtio_crypto_table;
79 * virtcrypto_devmgr_rm_dev() - Remove vcrypto_dev from the acceleration
80 * framework.
81 * @vcrypto_dev: Pointer to virtio crypto device.
83 * Function removes virtio crypto device from the acceleration framework.
84 * To be used by virtio crypto device specific drivers.
86 * Return: void
88 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev)
90 mutex_lock(&table_lock);
91 list_del(&vcrypto_dev->list);
92 num_devices--;
93 mutex_unlock(&table_lock);
97 * virtcrypto_devmgr_get_first()
99 * Function returns the first virtio crypto device from the acceleration
100 * framework.
102 * To be used by virtio crypto device specific drivers.
104 * Return: pointer to vcrypto_dev or NULL if not found.
106 struct virtio_crypto *virtcrypto_devmgr_get_first(void)
108 struct virtio_crypto *dev = NULL;
110 mutex_lock(&table_lock);
111 if (!list_empty(&virtio_crypto_table))
112 dev = list_first_entry(&virtio_crypto_table,
113 struct virtio_crypto,
114 list);
115 mutex_unlock(&table_lock);
116 return dev;
120 * virtcrypto_dev_in_use() - Check whether vcrypto_dev is currently in use
121 * @vcrypto_dev: Pointer to virtio crypto device.
123 * To be used by virtio crypto device specific drivers.
125 * Return: 1 when device is in use, 0 otherwise.
127 int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev)
129 return atomic_read(&vcrypto_dev->ref_count) != 0;
133 * virtcrypto_dev_get() - Increment vcrypto_dev reference count
134 * @vcrypto_dev: Pointer to virtio crypto device.
136 * Increment the vcrypto_dev refcount and if this is the first time
137 * incrementing it during this period the vcrypto_dev is in use,
138 * increment the module refcount too.
139 * To be used by virtio crypto device specific drivers.
141 * Return: 0 when successful, EFAULT when fail to bump module refcount
143 int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev)
145 if (atomic_add_return(1, &vcrypto_dev->ref_count) == 1)
146 if (!try_module_get(vcrypto_dev->owner))
147 return -EFAULT;
148 return 0;
152 * virtcrypto_dev_put() - Decrement vcrypto_dev reference count
153 * @vcrypto_dev: Pointer to virtio crypto device.
155 * Decrement the vcrypto_dev refcount and if this is the last time
156 * decrementing it during this period the vcrypto_dev is in use,
157 * decrement the module refcount too.
158 * To be used by virtio crypto device specific drivers.
160 * Return: void
162 void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev)
164 if (atomic_sub_return(1, &vcrypto_dev->ref_count) == 0)
165 module_put(vcrypto_dev->owner);
169 * virtcrypto_dev_started() - Check whether device has started
170 * @vcrypto_dev: Pointer to virtio crypto device.
172 * To be used by virtio crypto device specific drivers.
174 * Return: 1 when the device has started, 0 otherwise
176 int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev)
178 return (vcrypto_dev->status & VIRTIO_CRYPTO_S_HW_READY);
182 * virtcrypto_get_dev_node() - Get vcrypto_dev on the node.
183 * @node: Node id the driver works.
184 * @service: Crypto service that needs to be supported by the
185 * dev
186 * @algo: The algorithm number that needs to be supported by the
187 * dev
189 * Function returns the virtio crypto device used fewest on the node,
190 * and supports the given crypto service and algorithm.
192 * To be used by virtio crypto device specific drivers.
194 * Return: pointer to vcrypto_dev or NULL if not found.
196 struct virtio_crypto *virtcrypto_get_dev_node(int node, uint32_t service,
197 uint32_t algo)
199 struct virtio_crypto *vcrypto_dev = NULL, *tmp_dev;
200 unsigned long best = ~0;
201 unsigned long ctr;
203 mutex_lock(&table_lock);
204 list_for_each_entry(tmp_dev, virtcrypto_devmgr_get_head(), list) {
206 if ((node == dev_to_node(&tmp_dev->vdev->dev) ||
207 dev_to_node(&tmp_dev->vdev->dev) < 0) &&
208 virtcrypto_dev_started(tmp_dev) &&
209 virtcrypto_algo_is_supported(tmp_dev, service, algo)) {
210 ctr = atomic_read(&tmp_dev->ref_count);
211 if (best > ctr) {
212 vcrypto_dev = tmp_dev;
213 best = ctr;
218 if (!vcrypto_dev) {
219 pr_info("virtio_crypto: Could not find a device on node %d\n",
220 node);
221 /* Get any started device */
222 list_for_each_entry(tmp_dev,
223 virtcrypto_devmgr_get_head(), list) {
224 if (virtcrypto_dev_started(tmp_dev) &&
225 virtcrypto_algo_is_supported(tmp_dev,
226 service, algo)) {
227 vcrypto_dev = tmp_dev;
228 break;
232 mutex_unlock(&table_lock);
233 if (!vcrypto_dev)
234 return NULL;
236 virtcrypto_dev_get(vcrypto_dev);
237 return vcrypto_dev;
241 * virtcrypto_dev_start() - Start virtio crypto device
242 * @vcrypto: Pointer to virtio crypto device.
244 * Function notifies all the registered services that the virtio crypto device
245 * is ready to be used.
246 * To be used by virtio crypto device specific drivers.
248 * Return: 0 on success, EFAULT when fail to register algorithms
250 int virtcrypto_dev_start(struct virtio_crypto *vcrypto)
252 if (virtio_crypto_algs_register(vcrypto)) {
253 pr_err("virtio_crypto: Failed to register crypto algs\n");
254 return -EFAULT;
257 return 0;
261 * virtcrypto_dev_stop() - Stop virtio crypto device
262 * @vcrypto: Pointer to virtio crypto device.
264 * Function notifies all the registered services that the virtio crypto device
265 * is ready to be used.
266 * To be used by virtio crypto device specific drivers.
268 * Return: void
270 void virtcrypto_dev_stop(struct virtio_crypto *vcrypto)
272 virtio_crypto_algs_unregister(vcrypto);
276 * vcrypto_algo_is_supported()
277 * @vcrypto: Pointer to virtio crypto device.
278 * @service: The bit number for service validate.
279 * See VIRTIO_CRYPTO_SERVICE_*
280 * @algo : The bit number for the algorithm to validate.
283 * Validate if the virtio crypto device supports a service and
284 * algo.
286 * Return true if device supports a service and algo.
289 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto,
290 uint32_t service,
291 uint32_t algo)
293 uint32_t service_mask = 1u << service;
294 uint32_t algo_mask = 0;
295 bool low = true;
297 if (algo > 31) {
298 algo -= 32;
299 low = false;
302 if (!(vcrypto->crypto_services & service_mask))
303 return false;
305 switch (service) {
306 case VIRTIO_CRYPTO_SERVICE_CIPHER:
307 if (low)
308 algo_mask = vcrypto->cipher_algo_l;
309 else
310 algo_mask = vcrypto->cipher_algo_h;
311 break;
313 case VIRTIO_CRYPTO_SERVICE_HASH:
314 algo_mask = vcrypto->hash_algo;
315 break;
317 case VIRTIO_CRYPTO_SERVICE_MAC:
318 if (low)
319 algo_mask = vcrypto->mac_algo_l;
320 else
321 algo_mask = vcrypto->mac_algo_h;
322 break;
324 case VIRTIO_CRYPTO_SERVICE_AEAD:
325 algo_mask = vcrypto->aead_algo;
326 break;
329 if (!(algo_mask & (1u << algo)))
330 return false;
332 return true;