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
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);
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);
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);
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
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.
88 void virtcrypto_devmgr_rm_dev(struct virtio_crypto *vcrypto_dev)
90 mutex_lock(&table_lock);
91 list_del(&vcrypto_dev->list);
93 mutex_unlock(&table_lock);
97 * virtcrypto_devmgr_get_first()
99 * Function returns the first virtio crypto device from the acceleration
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,
115 mutex_unlock(&table_lock);
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))
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.
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
186 * @algo: The algorithm number that needs to be supported by the
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,
199 struct virtio_crypto *vcrypto_dev = NULL, *tmp_dev;
200 unsigned long best = ~0;
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);
212 vcrypto_dev = tmp_dev;
219 pr_info("virtio_crypto: Could not find a device on node %d\n",
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,
227 vcrypto_dev = tmp_dev;
232 mutex_unlock(&table_lock);
236 virtcrypto_dev_get(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");
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.
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
286 * Return true if device supports a service and algo.
289 bool virtcrypto_algo_is_supported(struct virtio_crypto *vcrypto,
293 uint32_t service_mask = 1u << service;
294 uint32_t algo_mask = 0;
302 if (!(vcrypto->crypto_services & service_mask))
306 case VIRTIO_CRYPTO_SERVICE_CIPHER:
308 algo_mask = vcrypto->cipher_algo_l;
310 algo_mask = vcrypto->cipher_algo_h;
313 case VIRTIO_CRYPTO_SERVICE_HASH:
314 algo_mask = vcrypto->hash_algo;
317 case VIRTIO_CRYPTO_SERVICE_MAC:
319 algo_mask = vcrypto->mac_algo_l;
321 algo_mask = vcrypto->mac_algo_h;
324 case VIRTIO_CRYPTO_SERVICE_AEAD:
325 algo_mask = vcrypto->aead_algo;
329 if (!(algo_mask & (1u << algo)))