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.
185 * Function returns the virtio crypto device used fewest on the node.
187 * To be used by virtio crypto device specific drivers.
189 * Return: pointer to vcrypto_dev or NULL if not found.
191 struct virtio_crypto
*virtcrypto_get_dev_node(int node
)
193 struct virtio_crypto
*vcrypto_dev
= NULL
, *tmp_dev
;
194 unsigned long best
= ~0;
197 mutex_lock(&table_lock
);
198 list_for_each_entry(tmp_dev
, virtcrypto_devmgr_get_head(), list
) {
200 if ((node
== dev_to_node(&tmp_dev
->vdev
->dev
) ||
201 dev_to_node(&tmp_dev
->vdev
->dev
) < 0) &&
202 virtcrypto_dev_started(tmp_dev
)) {
203 ctr
= atomic_read(&tmp_dev
->ref_count
);
205 vcrypto_dev
= tmp_dev
;
212 pr_info("virtio_crypto: Could not find a device on node %d\n",
214 /* Get any started device */
215 list_for_each_entry(tmp_dev
,
216 virtcrypto_devmgr_get_head(), list
) {
217 if (virtcrypto_dev_started(tmp_dev
)) {
218 vcrypto_dev
= tmp_dev
;
223 mutex_unlock(&table_lock
);
227 virtcrypto_dev_get(vcrypto_dev
);
232 * virtcrypto_dev_start() - Start virtio crypto device
233 * @vcrypto: Pointer to virtio crypto device.
235 * Function notifies all the registered services that the virtio crypto device
236 * is ready to be used.
237 * To be used by virtio crypto device specific drivers.
239 * Return: 0 on success, EFAULT when fail to register algorithms
241 int virtcrypto_dev_start(struct virtio_crypto
*vcrypto
)
243 if (virtio_crypto_algs_register()) {
244 pr_err("virtio_crypto: Failed to register crypto algs\n");
252 * virtcrypto_dev_stop() - Stop virtio crypto device
253 * @vcrypto: Pointer to virtio crypto device.
255 * Function notifies all the registered services that the virtio crypto device
256 * is ready to be used.
257 * To be used by virtio crypto device specific drivers.
261 void virtcrypto_dev_stop(struct virtio_crypto
*vcrypto
)
263 virtio_crypto_algs_unregister();