1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Management for virtio crypto devices (refer to adf_dev_mgr.c)
4 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
7 #include <linux/mutex.h>
8 #include <linux/list.h>
9 #include <linux/module.h>
11 #include <uapi/linux/virtio_crypto.h>
12 #include "virtio_crypto_common.h"
14 static LIST_HEAD(virtio_crypto_table);
15 static uint32_t num_devices;
17 /* The table_lock protects the above global list and num_devices */
18 static DEFINE_MUTEX(table_lock);
20 #define VIRTIO_CRYPTO_MAX_DEVICES 32
24 * virtcrypto_devmgr_add_dev() - Add vcrypto_dev to the acceleration
26 * @vcrypto_dev: Pointer to virtio crypto device.
28 * Function adds virtio crypto device to the global list.
29 * To be used by virtio crypto device specific drivers.
31 * Return: 0 on success, error code othewise.
33 int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev)
35 struct list_head *itr;
37 mutex_lock(&table_lock);
38 if (num_devices == VIRTIO_CRYPTO_MAX_DEVICES) {
39 pr_info("virtio_crypto: only support up to %d devices\n",
40 VIRTIO_CRYPTO_MAX_DEVICES);
41 mutex_unlock(&table_lock);
45 list_for_each(itr, &virtio_crypto_table) {
46 struct virtio_crypto *ptr =
47 list_entry(itr, struct virtio_crypto, list);
49 if (ptr == vcrypto_dev) {
50 mutex_unlock(&table_lock);
54 atomic_set(&vcrypto_dev->ref_count, 0);
55 list_add_tail(&vcrypto_dev->list, &virtio_crypto_table);
56 vcrypto_dev->dev_id = num_devices++;
57 mutex_unlock(&table_lock);
61 struct list_head *virtcrypto_devmgr_get_head(void)
63 return &virtio_crypto_table;
67 * virtcrypto_devmgr_rm_dev() - Remove vcrypto_dev from the acceleration
69 * @vcrypto_dev: Pointer to virtio crypto device.
71 * Function removes virtio crypto device from the acceleration framework.
72 * To be used by virtio crypto device specific drivers.
76 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev)
78 mutex_lock(&table_lock);
79 list_del(&vcrypto_dev->list);
81 mutex_unlock(&table_lock);
85 * virtcrypto_devmgr_get_first()
87 * Function returns the first virtio crypto device from the acceleration
90 * To be used by virtio crypto device specific drivers.
92 * Return: pointer to vcrypto_dev or NULL if not found.
94 struct virtio_crypto *virtcrypto_devmgr_get_first(void)
96 struct virtio_crypto *dev = NULL;
98 mutex_lock(&table_lock);
99 if (!list_empty(&virtio_crypto_table))
100 dev = list_first_entry(&virtio_crypto_table,
101 struct virtio_crypto,
103 mutex_unlock(&table_lock);
108 * virtcrypto_dev_in_use() - Check whether vcrypto_dev is currently in use
109 * @vcrypto_dev: Pointer to virtio crypto device.
111 * To be used by virtio crypto device specific drivers.
113 * Return: 1 when device is in use, 0 otherwise.
115 int virtcrypto_dev_in_use(struct virtio_crypto *vcrypto_dev)
117 return atomic_read(&vcrypto_dev->ref_count) != 0;
121 * virtcrypto_dev_get() - Increment vcrypto_dev reference count
122 * @vcrypto_dev: Pointer to virtio crypto device.
124 * Increment the vcrypto_dev refcount and if this is the first time
125 * incrementing it during this period the vcrypto_dev is in use,
126 * increment the module refcount too.
127 * To be used by virtio crypto device specific drivers.
129 * Return: 0 when successful, EFAULT when fail to bump module refcount
131 int virtcrypto_dev_get(struct virtio_crypto *vcrypto_dev)
133 if (atomic_add_return(1, &vcrypto_dev->ref_count) == 1)
134 if (!try_module_get(vcrypto_dev->owner))
140 * virtcrypto_dev_put() - Decrement vcrypto_dev reference count
141 * @vcrypto_dev: Pointer to virtio crypto device.
143 * Decrement the vcrypto_dev refcount and if this is the last time
144 * decrementing it during this period the vcrypto_dev is in use,
145 * decrement the module refcount too.
146 * To be used by virtio crypto device specific drivers.
150 void virtcrypto_dev_put(struct virtio_crypto *vcrypto_dev)
152 if (atomic_sub_return(1, &vcrypto_dev->ref_count) == 0)
153 module_put(vcrypto_dev->owner);
157 * virtcrypto_dev_started() - Check whether device has started
158 * @vcrypto_dev: Pointer to virtio crypto device.
160 * To be used by virtio crypto device specific drivers.
162 * Return: 1 when the device has started, 0 otherwise
164 int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev)
166 return (vcrypto_dev->status & VIRTIO_CRYPTO_S_HW_READY);
170 * virtcrypto_get_dev_node() - Get vcrypto_dev on the node.
171 * @node: Node id the driver works.
172 * @service: Crypto service that needs to be supported by the
174 * @algo: The algorithm number that needs to be supported by the
177 * Function returns the virtio crypto device used fewest on the node,
178 * and supports the given crypto service and algorithm.
180 * To be used by virtio crypto device specific drivers.
182 * Return: pointer to vcrypto_dev or NULL if not found.
184 struct virtio_crypto *virtcrypto_get_dev_node(int node, uint32_t service,
187 struct virtio_crypto *vcrypto_dev = NULL, *tmp_dev;
188 unsigned long best = ~0;
191 mutex_lock(&table_lock);
192 list_for_each_entry(tmp_dev, virtcrypto_devmgr_get_head(), list) {
194 if ((node == dev_to_node(&tmp_dev->vdev->dev) ||
195 dev_to_node(&tmp_dev->vdev->dev) < 0) &&
196 virtcrypto_dev_started(tmp_dev) &&
197 virtcrypto_algo_is_supported(tmp_dev, service, algo)) {
198 ctr = atomic_read(&tmp_dev->ref_count);
200 vcrypto_dev = tmp_dev;
207 pr_info("virtio_crypto: Could not find a device on node %d\n",
209 /* Get any started device */
210 list_for_each_entry(tmp_dev,
211 virtcrypto_devmgr_get_head(), list) {
212 if (virtcrypto_dev_started(tmp_dev) &&
213 virtcrypto_algo_is_supported(tmp_dev,
215 vcrypto_dev = tmp_dev;
220 mutex_unlock(&table_lock);
224 virtcrypto_dev_get(vcrypto_dev);
229 * virtcrypto_dev_start() - Start virtio crypto device
230 * @vcrypto: Pointer to virtio crypto device.
232 * Function notifies all the registered services that the virtio crypto device
233 * is ready to be used.
234 * To be used by virtio crypto device specific drivers.
236 * Return: 0 on success, EFAULT when fail to register algorithms
238 int virtcrypto_dev_start(struct virtio_crypto *vcrypto)
240 if (virtio_crypto_algs_register(vcrypto)) {
241 pr_err("virtio_crypto: Failed to register crypto algs\n");
245 if (virtio_crypto_akcipher_algs_register(vcrypto)) {
246 pr_err("virtio_crypto: Failed to register crypto akcipher algs\n");
247 virtio_crypto_algs_unregister(vcrypto);
255 * virtcrypto_dev_stop() - Stop virtio crypto device
256 * @vcrypto: Pointer to virtio crypto device.
258 * Function notifies all the registered services that the virtio crypto device
259 * is ready to be used.
260 * To be used by virtio crypto device specific drivers.
264 void virtcrypto_dev_stop(struct virtio_crypto *vcrypto)
266 virtio_crypto_algs_unregister(vcrypto);
267 virtio_crypto_akcipher_algs_unregister(vcrypto);
271 * vcrypto_algo_is_supported()
272 * @vcrypto: Pointer to virtio crypto device.
273 * @service: The bit number for service validate.
274 * See VIRTIO_CRYPTO_SERVICE_*
275 * @algo : The bit number for the algorithm to validate.
278 * Validate if the virtio crypto device supports a service and
281 * Return true if device supports a service and algo.
284 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto,
288 uint32_t service_mask = 1u << service;
289 uint32_t algo_mask = 0;
297 if (!(vcrypto->crypto_services & service_mask))
301 case VIRTIO_CRYPTO_SERVICE_CIPHER:
303 algo_mask = vcrypto->cipher_algo_l;
305 algo_mask = vcrypto->cipher_algo_h;
308 case VIRTIO_CRYPTO_SERVICE_HASH:
309 algo_mask = vcrypto->hash_algo;
312 case VIRTIO_CRYPTO_SERVICE_MAC:
314 algo_mask = vcrypto->mac_algo_l;
316 algo_mask = vcrypto->mac_algo_h;
319 case VIRTIO_CRYPTO_SERVICE_AEAD:
320 algo_mask = vcrypto->aead_algo;
323 case VIRTIO_CRYPTO_SERVICE_AKCIPHER:
324 algo_mask = vcrypto->akcipher_algo;
328 if (!(algo_mask & (1u << algo)))