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 kfree_sensitive(vc_req
->req_data
);
25 static void virtio_crypto_ctrlq_callback(struct virtio_crypto_ctrl_request
*vc_ctrl_req
)
27 complete(&vc_ctrl_req
->compl);
30 static void virtcrypto_ctrlq_callback(struct virtqueue
*vq
)
32 struct virtio_crypto
*vcrypto
= vq
->vdev
->priv
;
33 struct virtio_crypto_ctrl_request
*vc_ctrl_req
;
37 spin_lock_irqsave(&vcrypto
->ctrl_lock
, flags
);
39 virtqueue_disable_cb(vq
);
40 while ((vc_ctrl_req
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
41 spin_unlock_irqrestore(&vcrypto
->ctrl_lock
, flags
);
42 virtio_crypto_ctrlq_callback(vc_ctrl_req
);
43 spin_lock_irqsave(&vcrypto
->ctrl_lock
, flags
);
45 } while (!virtqueue_enable_cb(vq
));
46 spin_unlock_irqrestore(&vcrypto
->ctrl_lock
, flags
);
49 int virtio_crypto_ctrl_vq_request(struct virtio_crypto
*vcrypto
, struct scatterlist
*sgs
[],
50 unsigned int out_sgs
, unsigned int in_sgs
,
51 struct virtio_crypto_ctrl_request
*vc_ctrl_req
)
56 init_completion(&vc_ctrl_req
->compl);
58 spin_lock_irqsave(&vcrypto
->ctrl_lock
, flags
);
59 err
= virtqueue_add_sgs(vcrypto
->ctrl_vq
, sgs
, out_sgs
, in_sgs
, vc_ctrl_req
, GFP_ATOMIC
);
61 spin_unlock_irqrestore(&vcrypto
->ctrl_lock
, flags
);
65 virtqueue_kick(vcrypto
->ctrl_vq
);
66 spin_unlock_irqrestore(&vcrypto
->ctrl_lock
, flags
);
68 wait_for_completion(&vc_ctrl_req
->compl);
73 static void virtcrypto_done_task(unsigned long data
)
75 struct data_queue
*data_vq
= (struct data_queue
*)data
;
76 struct virtqueue
*vq
= data_vq
->vq
;
77 struct virtio_crypto_request
*vc_req
;
81 virtqueue_disable_cb(vq
);
82 while ((vc_req
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
84 vc_req
->alg_cb(vc_req
, len
);
86 } while (!virtqueue_enable_cb(vq
));
89 static void virtcrypto_dataq_callback(struct virtqueue
*vq
)
91 struct virtio_crypto
*vcrypto
= vq
->vdev
->priv
;
92 struct data_queue
*dq
= &vcrypto
->data_vq
[vq
->index
];
94 tasklet_schedule(&dq
->done_task
);
97 static int virtcrypto_find_vqs(struct virtio_crypto
*vi
)
99 struct virtqueue_info
*vqs_info
;
100 struct virtqueue
**vqs
;
103 struct device
*dev
= &vi
->vdev
->dev
;
106 * We expect 1 data virtqueue, followed by
107 * possible N-1 data queues used in multiqueue mode,
108 * followed by control vq.
110 total_vqs
= vi
->max_data_queues
+ 1;
112 /* Allocate space for find_vqs parameters */
113 vqs
= kcalloc(total_vqs
, sizeof(*vqs
), GFP_KERNEL
);
116 vqs_info
= kcalloc(total_vqs
, sizeof(*vqs_info
), GFP_KERNEL
);
120 /* Parameters for control virtqueue */
121 vqs_info
[total_vqs
- 1].callback
= virtcrypto_ctrlq_callback
;
122 vqs_info
[total_vqs
- 1].name
= "controlq";
124 /* Allocate/initialize parameters for data virtqueues */
125 for (i
= 0; i
< vi
->max_data_queues
; i
++) {
126 vqs_info
[i
].callback
= virtcrypto_dataq_callback
;
127 snprintf(vi
->data_vq
[i
].name
, sizeof(vi
->data_vq
[i
].name
),
129 vqs_info
[i
].name
= vi
->data_vq
[i
].name
;
132 ret
= virtio_find_vqs(vi
->vdev
, total_vqs
, vqs
, vqs_info
, NULL
);
136 vi
->ctrl_vq
= vqs
[total_vqs
- 1];
138 for (i
= 0; i
< vi
->max_data_queues
; i
++) {
139 spin_lock_init(&vi
->data_vq
[i
].lock
);
140 vi
->data_vq
[i
].vq
= vqs
[i
];
141 /* Initialize crypto engine */
142 vi
->data_vq
[i
].engine
= crypto_engine_alloc_init_and_set(dev
, true, NULL
, true,
143 virtqueue_get_vring_size(vqs
[i
]));
144 if (!vi
->data_vq
[i
].engine
) {
148 tasklet_init(&vi
->data_vq
[i
].done_task
, virtcrypto_done_task
,
149 (unsigned long)&vi
->data_vq
[i
]);
166 static int virtcrypto_alloc_queues(struct virtio_crypto
*vi
)
168 vi
->data_vq
= kcalloc(vi
->max_data_queues
, sizeof(*vi
->data_vq
),
176 static void virtcrypto_clean_affinity(struct virtio_crypto
*vi
, long hcpu
)
180 if (vi
->affinity_hint_set
) {
181 for (i
= 0; i
< vi
->max_data_queues
; i
++)
182 virtqueue_set_affinity(vi
->data_vq
[i
].vq
, NULL
);
184 vi
->affinity_hint_set
= false;
188 static void virtcrypto_set_affinity(struct virtio_crypto
*vcrypto
)
194 * In single queue mode, we don't set the cpu affinity.
196 if (vcrypto
->curr_queue
== 1 || vcrypto
->max_data_queues
== 1) {
197 virtcrypto_clean_affinity(vcrypto
, -1);
202 * In multiqueue mode, we let the queue to be private to one cpu
203 * by setting the affinity hint to eliminate the contention.
205 * TODO: adds cpu hotplug support by register cpu notifier.
208 for_each_online_cpu(cpu
) {
209 virtqueue_set_affinity(vcrypto
->data_vq
[i
].vq
, cpumask_of(cpu
));
210 if (++i
>= vcrypto
->max_data_queues
)
214 vcrypto
->affinity_hint_set
= true;
217 static void virtcrypto_free_queues(struct virtio_crypto
*vi
)
222 static int virtcrypto_init_vqs(struct virtio_crypto
*vi
)
226 /* Allocate send & receive queues */
227 ret
= virtcrypto_alloc_queues(vi
);
231 ret
= virtcrypto_find_vqs(vi
);
236 virtcrypto_set_affinity(vi
);
242 virtcrypto_free_queues(vi
);
247 static int virtcrypto_update_status(struct virtio_crypto
*vcrypto
)
252 virtio_cread_le(vcrypto
->vdev
,
253 struct virtio_crypto_config
, status
, &status
);
256 * Unknown status bits would be a host error and the driver
257 * should consider the device to be broken.
259 if (status
& (~VIRTIO_CRYPTO_S_HW_READY
)) {
260 dev_warn(&vcrypto
->vdev
->dev
,
261 "Unknown status bits: 0x%x\n", status
);
263 virtio_break_device(vcrypto
->vdev
);
267 if (vcrypto
->status
== status
)
270 vcrypto
->status
= status
;
272 if (vcrypto
->status
& VIRTIO_CRYPTO_S_HW_READY
) {
273 err
= virtcrypto_dev_start(vcrypto
);
275 dev_err(&vcrypto
->vdev
->dev
,
276 "Failed to start virtio crypto device.\n");
280 dev_info(&vcrypto
->vdev
->dev
, "Accelerator device is ready\n");
282 virtcrypto_dev_stop(vcrypto
);
283 dev_info(&vcrypto
->vdev
->dev
, "Accelerator is not ready\n");
289 static int virtcrypto_start_crypto_engines(struct virtio_crypto
*vcrypto
)
294 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++) {
295 if (vcrypto
->data_vq
[i
].engine
) {
296 ret
= crypto_engine_start(vcrypto
->data_vq
[i
].engine
);
306 if (vcrypto
->data_vq
[i
].engine
)
307 crypto_engine_exit(vcrypto
->data_vq
[i
].engine
);
312 static void virtcrypto_clear_crypto_engines(struct virtio_crypto
*vcrypto
)
316 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++)
317 if (vcrypto
->data_vq
[i
].engine
)
318 crypto_engine_exit(vcrypto
->data_vq
[i
].engine
);
321 static void virtcrypto_del_vqs(struct virtio_crypto
*vcrypto
)
323 struct virtio_device
*vdev
= vcrypto
->vdev
;
325 virtcrypto_clean_affinity(vcrypto
, -1);
327 vdev
->config
->del_vqs(vdev
);
329 virtcrypto_free_queues(vcrypto
);
332 static void vcrypto_config_changed_work(struct work_struct
*work
)
334 struct virtio_crypto
*vcrypto
=
335 container_of(work
, struct virtio_crypto
, config_work
);
337 virtcrypto_update_status(vcrypto
);
340 static int virtcrypto_probe(struct virtio_device
*vdev
)
343 struct virtio_crypto
*vcrypto
;
344 u32 max_data_queues
= 0, max_cipher_key_len
= 0;
345 u32 max_auth_key_len
= 0;
347 u32 cipher_algo_l
= 0;
348 u32 cipher_algo_h
= 0;
353 u32 akcipher_algo
= 0;
354 u32 crypto_services
= 0;
356 if (!virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
))
359 if (!vdev
->config
->get
) {
360 dev_err(&vdev
->dev
, "%s failure: config access disabled\n",
365 if (num_possible_nodes() > 1 && dev_to_node(&vdev
->dev
) < 0) {
367 * If the accelerator is connected to a node with no memory
368 * there is no point in using the accelerator since the remote
369 * memory transaction will be very slow.
371 dev_err(&vdev
->dev
, "Invalid NUMA configuration.\n");
375 vcrypto
= kzalloc_node(sizeof(*vcrypto
), GFP_KERNEL
,
376 dev_to_node(&vdev
->dev
));
380 virtio_cread_le(vdev
, struct virtio_crypto_config
,
381 max_dataqueues
, &max_data_queues
);
382 if (max_data_queues
< 1)
385 virtio_cread_le(vdev
, struct virtio_crypto_config
,
386 max_cipher_key_len
, &max_cipher_key_len
);
387 virtio_cread_le(vdev
, struct virtio_crypto_config
,
388 max_auth_key_len
, &max_auth_key_len
);
389 virtio_cread_le(vdev
, struct virtio_crypto_config
,
390 max_size
, &max_size
);
391 virtio_cread_le(vdev
, struct virtio_crypto_config
,
392 crypto_services
, &crypto_services
);
393 virtio_cread_le(vdev
, struct virtio_crypto_config
,
394 cipher_algo_l
, &cipher_algo_l
);
395 virtio_cread_le(vdev
, struct virtio_crypto_config
,
396 cipher_algo_h
, &cipher_algo_h
);
397 virtio_cread_le(vdev
, struct virtio_crypto_config
,
398 hash_algo
, &hash_algo
);
399 virtio_cread_le(vdev
, struct virtio_crypto_config
,
400 mac_algo_l
, &mac_algo_l
);
401 virtio_cread_le(vdev
, struct virtio_crypto_config
,
402 mac_algo_h
, &mac_algo_h
);
403 virtio_cread_le(vdev
, struct virtio_crypto_config
,
404 aead_algo
, &aead_algo
);
405 if (crypto_services
& (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER
))
406 virtio_cread_le(vdev
, struct virtio_crypto_config
,
407 akcipher_algo
, &akcipher_algo
);
409 /* Add virtio crypto device to global table */
410 err
= virtcrypto_devmgr_add_dev(vcrypto
);
412 dev_err(&vdev
->dev
, "Failed to add new virtio crypto device.\n");
415 vcrypto
->owner
= THIS_MODULE
;
416 vcrypto
= vdev
->priv
= vcrypto
;
417 vcrypto
->vdev
= vdev
;
419 spin_lock_init(&vcrypto
->ctrl_lock
);
421 /* Use single data queue as default */
422 vcrypto
->curr_queue
= 1;
423 vcrypto
->max_data_queues
= max_data_queues
;
424 vcrypto
->max_cipher_key_len
= max_cipher_key_len
;
425 vcrypto
->max_auth_key_len
= max_auth_key_len
;
426 vcrypto
->max_size
= max_size
;
427 vcrypto
->crypto_services
= crypto_services
;
428 vcrypto
->cipher_algo_l
= cipher_algo_l
;
429 vcrypto
->cipher_algo_h
= cipher_algo_h
;
430 vcrypto
->mac_algo_l
= mac_algo_l
;
431 vcrypto
->mac_algo_h
= mac_algo_h
;
432 vcrypto
->hash_algo
= hash_algo
;
433 vcrypto
->aead_algo
= aead_algo
;
434 vcrypto
->akcipher_algo
= akcipher_algo
;
437 "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
438 vcrypto
->max_data_queues
,
439 vcrypto
->max_cipher_key_len
,
440 vcrypto
->max_auth_key_len
,
443 err
= virtcrypto_init_vqs(vcrypto
);
445 dev_err(&vdev
->dev
, "Failed to initialize vqs.\n");
449 err
= virtcrypto_start_crypto_engines(vcrypto
);
453 virtio_device_ready(vdev
);
455 err
= virtcrypto_update_status(vcrypto
);
459 INIT_WORK(&vcrypto
->config_work
, vcrypto_config_changed_work
);
464 virtcrypto_clear_crypto_engines(vcrypto
);
466 virtio_reset_device(vdev
);
467 virtcrypto_del_vqs(vcrypto
);
469 virtcrypto_devmgr_rm_dev(vcrypto
);
475 static void virtcrypto_free_unused_reqs(struct virtio_crypto
*vcrypto
)
477 struct virtio_crypto_request
*vc_req
;
479 struct virtqueue
*vq
;
481 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++) {
482 vq
= vcrypto
->data_vq
[i
].vq
;
483 while ((vc_req
= virtqueue_detach_unused_buf(vq
)) != NULL
) {
484 kfree(vc_req
->req_data
);
491 static void virtcrypto_remove(struct virtio_device
*vdev
)
493 struct virtio_crypto
*vcrypto
= vdev
->priv
;
496 dev_info(&vdev
->dev
, "Start virtcrypto_remove.\n");
498 flush_work(&vcrypto
->config_work
);
499 if (virtcrypto_dev_started(vcrypto
))
500 virtcrypto_dev_stop(vcrypto
);
501 for (i
= 0; i
< vcrypto
->max_data_queues
; i
++)
502 tasklet_kill(&vcrypto
->data_vq
[i
].done_task
);
503 virtio_reset_device(vdev
);
504 virtcrypto_free_unused_reqs(vcrypto
);
505 virtcrypto_clear_crypto_engines(vcrypto
);
506 virtcrypto_del_vqs(vcrypto
);
507 virtcrypto_devmgr_rm_dev(vcrypto
);
511 static void virtcrypto_config_changed(struct virtio_device
*vdev
)
513 struct virtio_crypto
*vcrypto
= vdev
->priv
;
515 schedule_work(&vcrypto
->config_work
);
518 #ifdef CONFIG_PM_SLEEP
519 static int virtcrypto_freeze(struct virtio_device
*vdev
)
521 struct virtio_crypto
*vcrypto
= vdev
->priv
;
523 flush_work(&vcrypto
->config_work
);
524 virtio_reset_device(vdev
);
525 virtcrypto_free_unused_reqs(vcrypto
);
526 if (virtcrypto_dev_started(vcrypto
))
527 virtcrypto_dev_stop(vcrypto
);
529 virtcrypto_clear_crypto_engines(vcrypto
);
530 virtcrypto_del_vqs(vcrypto
);
534 static int virtcrypto_restore(struct virtio_device
*vdev
)
536 struct virtio_crypto
*vcrypto
= vdev
->priv
;
539 err
= virtcrypto_init_vqs(vcrypto
);
543 err
= virtcrypto_start_crypto_engines(vcrypto
);
547 virtio_device_ready(vdev
);
549 err
= virtcrypto_dev_start(vcrypto
);
551 dev_err(&vdev
->dev
, "Failed to start virtio crypto device.\n");
558 virtcrypto_clear_crypto_engines(vcrypto
);
560 virtio_reset_device(vdev
);
561 virtcrypto_del_vqs(vcrypto
);
566 static const unsigned int features
[] = {
570 static const struct virtio_device_id id_table
[] = {
571 { VIRTIO_ID_CRYPTO
, VIRTIO_DEV_ANY_ID
},
575 static struct virtio_driver virtio_crypto_driver
= {
576 .driver
.name
= KBUILD_MODNAME
,
577 .feature_table
= features
,
578 .feature_table_size
= ARRAY_SIZE(features
),
579 .id_table
= id_table
,
580 .probe
= virtcrypto_probe
,
581 .remove
= virtcrypto_remove
,
582 .config_changed
= virtcrypto_config_changed
,
583 #ifdef CONFIG_PM_SLEEP
584 .freeze
= virtcrypto_freeze
,
585 .restore
= virtcrypto_restore
,
589 module_virtio_driver(virtio_crypto_driver
);
591 MODULE_DEVICE_TABLE(virtio
, id_table
);
592 MODULE_DESCRIPTION("virtio crypto device driver");
593 MODULE_LICENSE("GPL");
594 MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");