1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Driver for Virtio crypto device.
4 * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
8 #include <linux/module.h>
9 #include <linux/virtio_config.h>
10 #include <linux/cpu.h>
12 #include <uapi/linux/virtio_crypto.h>
13 #include "virtio_crypto_common.h"
17 virtcrypto_clear_request(struct virtio_crypto_request
*vc_req
)
20 kzfree(vc_req
->req_data
);
25 static void virtcrypto_dataq_callback(struct virtqueue
*vq
)
27 struct virtio_crypto
*vcrypto
= vq
->vdev
->priv
;
28 struct virtio_crypto_request
*vc_req
;
31 unsigned int qid
= vq
->index
;
33 spin_lock_irqsave(&vcrypto
->data_vq
[qid
].lock
, flags
);
35 virtqueue_disable_cb(vq
);
36 while ((vc_req
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
37 spin_unlock_irqrestore(
38 &vcrypto
->data_vq
[qid
].lock
, flags
);
40 vc_req
->alg_cb(vc_req
, len
);
42 &vcrypto
->data_vq
[qid
].lock
, flags
);
44 } while (!virtqueue_enable_cb(vq
));
45 spin_unlock_irqrestore(&vcrypto
->data_vq
[qid
].lock
, flags
);
48 static int virtcrypto_find_vqs(struct virtio_crypto
*vi
)
50 vq_callback_t
**callbacks
;
51 struct virtqueue
**vqs
;
55 struct device
*dev
= &vi
->vdev
->dev
;
58 * We expect 1 data virtqueue, followed by
59 * possible N-1 data queues used in multiqueue mode,
60 * followed by control vq.
62 total_vqs
= vi
->max_data_queues
+ 1;
64 /* Allocate space for find_vqs parameters */
65 vqs
= kcalloc(total_vqs
, sizeof(*vqs
), GFP_KERNEL
);
68 callbacks
= kcalloc(total_vqs
, sizeof(*callbacks
), GFP_KERNEL
);
71 names
= kcalloc(total_vqs
, sizeof(*names
), GFP_KERNEL
);
75 /* Parameters for control virtqueue */
76 callbacks
[total_vqs
- 1] = NULL
;
77 names
[total_vqs
- 1] = "controlq";
79 /* Allocate/initialize parameters for data virtqueues */
80 for (i
= 0; i
< vi
->max_data_queues
; i
++) {
81 callbacks
[i
] = virtcrypto_dataq_callback
;
82 snprintf(vi
->data_vq
[i
].name
, sizeof(vi
->data_vq
[i
].name
),
84 names
[i
] = vi
->data_vq
[i
].name
;
87 ret
= virtio_find_vqs(vi
->vdev
, total_vqs
, vqs
, callbacks
, names
, NULL
);
91 vi
->ctrl_vq
= vqs
[total_vqs
- 1];
93 for (i
= 0; i
< vi
->max_data_queues
; i
++) {
94 spin_lock_init(&vi
->data_vq
[i
].lock
);
95 vi
->data_vq
[i
].vq
= vqs
[i
];
96 /* Initialize crypto engine */
97 vi
->data_vq
[i
].engine
= crypto_engine_alloc_init(dev
, 1);
98 if (!vi
->data_vq
[i
].engine
) {
121 static int virtcrypto_alloc_queues(struct virtio_crypto
*vi
)
123 vi
->data_vq
= kcalloc(vi
->max_data_queues
, sizeof(*vi
->data_vq
),
131 static void virtcrypto_clean_affinity(struct virtio_crypto
*vi
, long hcpu
)
135 if (vi
->affinity_hint_set
) {
136 for (i
= 0; i
< vi
->max_data_queues
; i
++)
137 virtqueue_set_affinity(vi
->data_vq
[i
].vq
, NULL
);
139 vi
->affinity_hint_set
= false;
143 static void virtcrypto_set_affinity(struct virtio_crypto
*vcrypto
)
149 * In single queue mode, we don't set the cpu affinity.
151 if (vcrypto
->curr_queue
== 1 || vcrypto
->max_data_queues
== 1) {
152 virtcrypto_clean_affinity(vcrypto
, -1);
157 * In multiqueue mode, we let the queue to be private to one cpu
158 * by setting the affinity hint to eliminate the contention.
160 * TODO: adds cpu hotplug support by register cpu notifier.
163 for_each_online_cpu(cpu
) {
164 virtqueue_set_affinity(vcrypto
->data_vq
[i
].vq
, cpumask_of(cpu
));
165 if (++i
>= vcrypto
->max_data_queues
)
169 vcrypto
->affinity_hint_set
= true;
172 static void virtcrypto_free_queues(struct virtio_crypto
*vi
)
177 static int virtcrypto_init_vqs(struct virtio_crypto
*vi
)
181 /* Allocate send & receive queues */
182 ret
= virtcrypto_alloc_queues(vi
);
186 ret
= virtcrypto_find_vqs(vi
);
191 virtcrypto_set_affinity(vi
);
197 virtcrypto_free_queues(vi
);
202 static int virtcrypto_update_status(struct virtio_crypto
*vcrypto
)
207 virtio_cread(vcrypto
->vdev
,
208 struct virtio_crypto_config
, status
, &status
);
211 * Unknown status bits would be a host error and the driver
212 * should consider the device to be broken.
214 if (status
& (~VIRTIO_CRYPTO_S_HW_READY
)) {
215 dev_warn(&vcrypto
->vdev
->dev
,
216 "Unknown status bits: 0x%x\n", status
);
218 virtio_break_device(vcrypto
->vdev
);
222 if (vcrypto
->status
== status
)
225 vcrypto
->status
= status
;
227 if (vcrypto
->status
& VIRTIO_CRYPTO_S_HW_READY
) {
228 err
= virtcrypto_dev_start(vcrypto
);
230 dev_err(&vcrypto
->vdev
->dev
,
231 "Failed to start virtio crypto device.\n");
235 dev_info(&vcrypto
->vdev
->dev
, "Accelerator device is ready\n");
237 virtcrypto_dev_stop(vcrypto
);
238 dev_info(&vcrypto
->vdev
->dev
, "Accelerator is not ready\n");
244 static int virtcrypto_start_crypto_engines(struct virtio_crypto
*vcrypto
)
249 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++) {
250 if (vcrypto
->data_vq
[i
].engine
) {
251 ret
= crypto_engine_start(vcrypto
->data_vq
[i
].engine
);
261 if (vcrypto
->data_vq
[i
].engine
)
262 crypto_engine_exit(vcrypto
->data_vq
[i
].engine
);
267 static void virtcrypto_clear_crypto_engines(struct virtio_crypto
*vcrypto
)
271 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++)
272 if (vcrypto
->data_vq
[i
].engine
)
273 crypto_engine_exit(vcrypto
->data_vq
[i
].engine
);
276 static void virtcrypto_del_vqs(struct virtio_crypto
*vcrypto
)
278 struct virtio_device
*vdev
= vcrypto
->vdev
;
280 virtcrypto_clean_affinity(vcrypto
, -1);
282 vdev
->config
->del_vqs(vdev
);
284 virtcrypto_free_queues(vcrypto
);
287 static int virtcrypto_probe(struct virtio_device
*vdev
)
290 struct virtio_crypto
*vcrypto
;
291 u32 max_data_queues
= 0, max_cipher_key_len
= 0;
292 u32 max_auth_key_len
= 0;
294 u32 cipher_algo_l
= 0;
295 u32 cipher_algo_h
= 0;
300 u32 crypto_services
= 0;
302 if (!virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
))
305 if (!vdev
->config
->get
) {
306 dev_err(&vdev
->dev
, "%s failure: config access disabled\n",
311 if (num_possible_nodes() > 1 && dev_to_node(&vdev
->dev
) < 0) {
313 * If the accelerator is connected to a node with no memory
314 * there is no point in using the accelerator since the remote
315 * memory transaction will be very slow.
317 dev_err(&vdev
->dev
, "Invalid NUMA configuration.\n");
321 vcrypto
= kzalloc_node(sizeof(*vcrypto
), GFP_KERNEL
,
322 dev_to_node(&vdev
->dev
));
326 virtio_cread(vdev
, struct virtio_crypto_config
,
327 max_dataqueues
, &max_data_queues
);
328 if (max_data_queues
< 1)
331 virtio_cread(vdev
, struct virtio_crypto_config
,
332 max_cipher_key_len
, &max_cipher_key_len
);
333 virtio_cread(vdev
, struct virtio_crypto_config
,
334 max_auth_key_len
, &max_auth_key_len
);
335 virtio_cread(vdev
, struct virtio_crypto_config
,
336 max_size
, &max_size
);
337 virtio_cread(vdev
, struct virtio_crypto_config
,
338 crypto_services
, &crypto_services
);
339 virtio_cread(vdev
, struct virtio_crypto_config
,
340 cipher_algo_l
, &cipher_algo_l
);
341 virtio_cread(vdev
, struct virtio_crypto_config
,
342 cipher_algo_h
, &cipher_algo_h
);
343 virtio_cread(vdev
, struct virtio_crypto_config
,
344 hash_algo
, &hash_algo
);
345 virtio_cread(vdev
, struct virtio_crypto_config
,
346 mac_algo_l
, &mac_algo_l
);
347 virtio_cread(vdev
, struct virtio_crypto_config
,
348 mac_algo_h
, &mac_algo_h
);
349 virtio_cread(vdev
, struct virtio_crypto_config
,
350 aead_algo
, &aead_algo
);
352 /* Add virtio crypto device to global table */
353 err
= virtcrypto_devmgr_add_dev(vcrypto
);
355 dev_err(&vdev
->dev
, "Failed to add new virtio crypto device.\n");
358 vcrypto
->owner
= THIS_MODULE
;
359 vcrypto
= vdev
->priv
= vcrypto
;
360 vcrypto
->vdev
= vdev
;
362 spin_lock_init(&vcrypto
->ctrl_lock
);
364 /* Use single data queue as default */
365 vcrypto
->curr_queue
= 1;
366 vcrypto
->max_data_queues
= max_data_queues
;
367 vcrypto
->max_cipher_key_len
= max_cipher_key_len
;
368 vcrypto
->max_auth_key_len
= max_auth_key_len
;
369 vcrypto
->max_size
= max_size
;
370 vcrypto
->crypto_services
= crypto_services
;
371 vcrypto
->cipher_algo_l
= cipher_algo_l
;
372 vcrypto
->cipher_algo_h
= cipher_algo_h
;
373 vcrypto
->mac_algo_l
= mac_algo_l
;
374 vcrypto
->mac_algo_h
= mac_algo_h
;
375 vcrypto
->hash_algo
= hash_algo
;
376 vcrypto
->aead_algo
= aead_algo
;
380 "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
381 vcrypto
->max_data_queues
,
382 vcrypto
->max_cipher_key_len
,
383 vcrypto
->max_auth_key_len
,
386 err
= virtcrypto_init_vqs(vcrypto
);
388 dev_err(&vdev
->dev
, "Failed to initialize vqs.\n");
392 err
= virtcrypto_start_crypto_engines(vcrypto
);
396 virtio_device_ready(vdev
);
398 err
= virtcrypto_update_status(vcrypto
);
405 virtcrypto_clear_crypto_engines(vcrypto
);
407 vcrypto
->vdev
->config
->reset(vdev
);
408 virtcrypto_del_vqs(vcrypto
);
410 virtcrypto_devmgr_rm_dev(vcrypto
);
416 static void virtcrypto_free_unused_reqs(struct virtio_crypto
*vcrypto
)
418 struct virtio_crypto_request
*vc_req
;
420 struct virtqueue
*vq
;
422 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++) {
423 vq
= vcrypto
->data_vq
[i
].vq
;
424 while ((vc_req
= virtqueue_detach_unused_buf(vq
)) != NULL
) {
425 kfree(vc_req
->req_data
);
431 static void virtcrypto_remove(struct virtio_device
*vdev
)
433 struct virtio_crypto
*vcrypto
= vdev
->priv
;
435 dev_info(&vdev
->dev
, "Start virtcrypto_remove.\n");
437 if (virtcrypto_dev_started(vcrypto
))
438 virtcrypto_dev_stop(vcrypto
);
439 vdev
->config
->reset(vdev
);
440 virtcrypto_free_unused_reqs(vcrypto
);
441 virtcrypto_clear_crypto_engines(vcrypto
);
442 virtcrypto_del_vqs(vcrypto
);
443 virtcrypto_devmgr_rm_dev(vcrypto
);
447 static void virtcrypto_config_changed(struct virtio_device
*vdev
)
449 struct virtio_crypto
*vcrypto
= vdev
->priv
;
451 virtcrypto_update_status(vcrypto
);
454 #ifdef CONFIG_PM_SLEEP
455 static int virtcrypto_freeze(struct virtio_device
*vdev
)
457 struct virtio_crypto
*vcrypto
= vdev
->priv
;
459 vdev
->config
->reset(vdev
);
460 virtcrypto_free_unused_reqs(vcrypto
);
461 if (virtcrypto_dev_started(vcrypto
))
462 virtcrypto_dev_stop(vcrypto
);
464 virtcrypto_clear_crypto_engines(vcrypto
);
465 virtcrypto_del_vqs(vcrypto
);
469 static int virtcrypto_restore(struct virtio_device
*vdev
)
471 struct virtio_crypto
*vcrypto
= vdev
->priv
;
474 err
= virtcrypto_init_vqs(vcrypto
);
478 err
= virtcrypto_start_crypto_engines(vcrypto
);
482 virtio_device_ready(vdev
);
484 err
= virtcrypto_dev_start(vcrypto
);
486 dev_err(&vdev
->dev
, "Failed to start virtio crypto device.\n");
493 virtcrypto_clear_crypto_engines(vcrypto
);
495 vcrypto
->vdev
->config
->reset(vdev
);
496 virtcrypto_del_vqs(vcrypto
);
501 static unsigned int features
[] = {
505 static struct virtio_device_id id_table
[] = {
506 { VIRTIO_ID_CRYPTO
, VIRTIO_DEV_ANY_ID
},
510 static struct virtio_driver virtio_crypto_driver
= {
511 .driver
.name
= KBUILD_MODNAME
,
512 .driver
.owner
= THIS_MODULE
,
513 .feature_table
= features
,
514 .feature_table_size
= ARRAY_SIZE(features
),
515 .id_table
= id_table
,
516 .probe
= virtcrypto_probe
,
517 .remove
= virtcrypto_remove
,
518 .config_changed
= virtcrypto_config_changed
,
519 #ifdef CONFIG_PM_SLEEP
520 .freeze
= virtcrypto_freeze
,
521 .restore
= virtcrypto_restore
,
525 module_virtio_driver(virtio_crypto_driver
);
527 MODULE_DEVICE_TABLE(virtio
, id_table
);
528 MODULE_DESCRIPTION("virtio crypto device driver");
529 MODULE_LICENSE("GPL");
530 MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");