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 if (unlikely(virtqueue_is_broken(vq)))
47 } while (!virtqueue_enable_cb(vq));
48 spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
51 int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[],
52 unsigned int out_sgs, unsigned int in_sgs,
53 struct virtio_crypto_ctrl_request *vc_ctrl_req)
58 init_completion(&vc_ctrl_req->compl);
60 spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
61 err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, out_sgs, in_sgs, vc_ctrl_req, GFP_ATOMIC);
63 spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
67 virtqueue_kick(vcrypto->ctrl_vq);
68 spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
70 wait_for_completion(&vc_ctrl_req->compl);
75 static void virtcrypto_done_task(unsigned long data)
77 struct data_queue *data_vq = (struct data_queue *)data;
78 struct virtqueue *vq = data_vq->vq;
79 struct virtio_crypto_request *vc_req;
83 virtqueue_disable_cb(vq);
84 while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
86 vc_req->alg_cb(vc_req, len);
88 } while (!virtqueue_enable_cb(vq));
91 static void virtcrypto_dataq_callback(struct virtqueue *vq)
93 struct virtio_crypto *vcrypto = vq->vdev->priv;
94 struct data_queue *dq = &vcrypto->data_vq[vq->index];
96 tasklet_schedule(&dq->done_task);
99 static int virtcrypto_find_vqs(struct virtio_crypto *vi)
101 vq_callback_t **callbacks;
102 struct virtqueue **vqs;
106 struct device *dev = &vi->vdev->dev;
109 * We expect 1 data virtqueue, followed by
110 * possible N-1 data queues used in multiqueue mode,
111 * followed by control vq.
113 total_vqs = vi->max_data_queues + 1;
115 /* Allocate space for find_vqs parameters */
116 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
119 callbacks = kcalloc(total_vqs, sizeof(*callbacks), GFP_KERNEL);
122 names = kcalloc(total_vqs, sizeof(*names), GFP_KERNEL);
126 /* Parameters for control virtqueue */
127 callbacks[total_vqs - 1] = virtcrypto_ctrlq_callback;
128 names[total_vqs - 1] = "controlq";
130 /* Allocate/initialize parameters for data virtqueues */
131 for (i = 0; i < vi->max_data_queues; i++) {
132 callbacks[i] = virtcrypto_dataq_callback;
133 snprintf(vi->data_vq[i].name, sizeof(vi->data_vq[i].name),
135 names[i] = vi->data_vq[i].name;
138 ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
142 vi->ctrl_vq = vqs[total_vqs - 1];
144 for (i = 0; i < vi->max_data_queues; i++) {
145 spin_lock_init(&vi->data_vq[i].lock);
146 vi->data_vq[i].vq = vqs[i];
147 /* Initialize crypto engine */
148 vi->data_vq[i].engine = crypto_engine_alloc_init_and_set(dev, true, NULL, true,
149 virtqueue_get_vring_size(vqs[i]));
150 if (!vi->data_vq[i].engine) {
154 tasklet_init(&vi->data_vq[i].done_task, virtcrypto_done_task,
155 (unsigned long)&vi->data_vq[i]);
175 static int virtcrypto_alloc_queues(struct virtio_crypto *vi)
177 vi->data_vq = kcalloc(vi->max_data_queues, sizeof(*vi->data_vq),
185 static void virtcrypto_clean_affinity(struct virtio_crypto *vi, long hcpu)
189 if (vi->affinity_hint_set) {
190 for (i = 0; i < vi->max_data_queues; i++)
191 virtqueue_set_affinity(vi->data_vq[i].vq, NULL);
193 vi->affinity_hint_set = false;
197 static void virtcrypto_set_affinity(struct virtio_crypto *vcrypto)
203 * In single queue mode, we don't set the cpu affinity.
205 if (vcrypto->curr_queue == 1 || vcrypto->max_data_queues == 1) {
206 virtcrypto_clean_affinity(vcrypto, -1);
211 * In multiqueue mode, we let the queue to be private to one cpu
212 * by setting the affinity hint to eliminate the contention.
214 * TODO: adds cpu hotplug support by register cpu notifier.
217 for_each_online_cpu(cpu) {
218 virtqueue_set_affinity(vcrypto->data_vq[i].vq, cpumask_of(cpu));
219 if (++i >= vcrypto->max_data_queues)
223 vcrypto->affinity_hint_set = true;
226 static void virtcrypto_free_queues(struct virtio_crypto *vi)
231 static int virtcrypto_init_vqs(struct virtio_crypto *vi)
235 /* Allocate send & receive queues */
236 ret = virtcrypto_alloc_queues(vi);
240 ret = virtcrypto_find_vqs(vi);
245 virtcrypto_set_affinity(vi);
251 virtcrypto_free_queues(vi);
256 static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
261 virtio_cread_le(vcrypto->vdev,
262 struct virtio_crypto_config, status, &status);
265 * Unknown status bits would be a host error and the driver
266 * should consider the device to be broken.
268 if (status & (~VIRTIO_CRYPTO_S_HW_READY)) {
269 dev_warn(&vcrypto->vdev->dev,
270 "Unknown status bits: 0x%x\n", status);
272 virtio_break_device(vcrypto->vdev);
276 if (vcrypto->status == status)
279 vcrypto->status = status;
281 if (vcrypto->status & VIRTIO_CRYPTO_S_HW_READY) {
282 err = virtcrypto_dev_start(vcrypto);
284 dev_err(&vcrypto->vdev->dev,
285 "Failed to start virtio crypto device.\n");
289 dev_info(&vcrypto->vdev->dev, "Accelerator device is ready\n");
291 virtcrypto_dev_stop(vcrypto);
292 dev_info(&vcrypto->vdev->dev, "Accelerator is not ready\n");
298 static int virtcrypto_start_crypto_engines(struct virtio_crypto *vcrypto)
303 for (i = 0; i < vcrypto->max_data_queues; i++) {
304 if (vcrypto->data_vq[i].engine) {
305 ret = crypto_engine_start(vcrypto->data_vq[i].engine);
315 if (vcrypto->data_vq[i].engine)
316 crypto_engine_exit(vcrypto->data_vq[i].engine);
321 static void virtcrypto_clear_crypto_engines(struct virtio_crypto *vcrypto)
325 for (i = 0; i < vcrypto->max_data_queues; i++)
326 if (vcrypto->data_vq[i].engine)
327 crypto_engine_exit(vcrypto->data_vq[i].engine);
330 static void virtcrypto_del_vqs(struct virtio_crypto *vcrypto)
332 struct virtio_device *vdev = vcrypto->vdev;
334 virtcrypto_clean_affinity(vcrypto, -1);
336 vdev->config->del_vqs(vdev);
338 virtcrypto_free_queues(vcrypto);
341 static void vcrypto_config_changed_work(struct work_struct *work)
343 struct virtio_crypto *vcrypto =
344 container_of(work, struct virtio_crypto, config_work);
346 virtcrypto_update_status(vcrypto);
349 static int virtcrypto_probe(struct virtio_device *vdev)
352 struct virtio_crypto *vcrypto;
353 u32 max_data_queues = 0, max_cipher_key_len = 0;
354 u32 max_auth_key_len = 0;
356 u32 cipher_algo_l = 0;
357 u32 cipher_algo_h = 0;
362 u32 akcipher_algo = 0;
363 u32 crypto_services = 0;
365 if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
368 if (!vdev->config->get) {
369 dev_err(&vdev->dev, "%s failure: config access disabled\n",
374 if (num_possible_nodes() > 1 && dev_to_node(&vdev->dev) < 0) {
376 * If the accelerator is connected to a node with no memory
377 * there is no point in using the accelerator since the remote
378 * memory transaction will be very slow.
380 dev_err(&vdev->dev, "Invalid NUMA configuration.\n");
384 vcrypto = kzalloc_node(sizeof(*vcrypto), GFP_KERNEL,
385 dev_to_node(&vdev->dev));
389 virtio_cread_le(vdev, struct virtio_crypto_config,
390 max_dataqueues, &max_data_queues);
391 if (max_data_queues < 1)
394 virtio_cread_le(vdev, struct virtio_crypto_config,
395 max_cipher_key_len, &max_cipher_key_len);
396 virtio_cread_le(vdev, struct virtio_crypto_config,
397 max_auth_key_len, &max_auth_key_len);
398 virtio_cread_le(vdev, struct virtio_crypto_config,
399 max_size, &max_size);
400 virtio_cread_le(vdev, struct virtio_crypto_config,
401 crypto_services, &crypto_services);
402 virtio_cread_le(vdev, struct virtio_crypto_config,
403 cipher_algo_l, &cipher_algo_l);
404 virtio_cread_le(vdev, struct virtio_crypto_config,
405 cipher_algo_h, &cipher_algo_h);
406 virtio_cread_le(vdev, struct virtio_crypto_config,
407 hash_algo, &hash_algo);
408 virtio_cread_le(vdev, struct virtio_crypto_config,
409 mac_algo_l, &mac_algo_l);
410 virtio_cread_le(vdev, struct virtio_crypto_config,
411 mac_algo_h, &mac_algo_h);
412 virtio_cread_le(vdev, struct virtio_crypto_config,
413 aead_algo, &aead_algo);
414 if (crypto_services & (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER))
415 virtio_cread_le(vdev, struct virtio_crypto_config,
416 akcipher_algo, &akcipher_algo);
418 /* Add virtio crypto device to global table */
419 err = virtcrypto_devmgr_add_dev(vcrypto);
421 dev_err(&vdev->dev, "Failed to add new virtio crypto device.\n");
424 vcrypto->owner = THIS_MODULE;
425 vcrypto = vdev->priv = vcrypto;
426 vcrypto->vdev = vdev;
428 spin_lock_init(&vcrypto->ctrl_lock);
430 /* Use single data queue as default */
431 vcrypto->curr_queue = 1;
432 vcrypto->max_data_queues = max_data_queues;
433 vcrypto->max_cipher_key_len = max_cipher_key_len;
434 vcrypto->max_auth_key_len = max_auth_key_len;
435 vcrypto->max_size = max_size;
436 vcrypto->crypto_services = crypto_services;
437 vcrypto->cipher_algo_l = cipher_algo_l;
438 vcrypto->cipher_algo_h = cipher_algo_h;
439 vcrypto->mac_algo_l = mac_algo_l;
440 vcrypto->mac_algo_h = mac_algo_h;
441 vcrypto->hash_algo = hash_algo;
442 vcrypto->aead_algo = aead_algo;
443 vcrypto->akcipher_algo = akcipher_algo;
446 "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
447 vcrypto->max_data_queues,
448 vcrypto->max_cipher_key_len,
449 vcrypto->max_auth_key_len,
452 err = virtcrypto_init_vqs(vcrypto);
454 dev_err(&vdev->dev, "Failed to initialize vqs.\n");
458 err = virtcrypto_start_crypto_engines(vcrypto);
462 virtio_device_ready(vdev);
464 err = virtcrypto_update_status(vcrypto);
468 INIT_WORK(&vcrypto->config_work, vcrypto_config_changed_work);
473 virtcrypto_clear_crypto_engines(vcrypto);
475 virtio_reset_device(vdev);
476 virtcrypto_del_vqs(vcrypto);
478 virtcrypto_devmgr_rm_dev(vcrypto);
484 static void virtcrypto_free_unused_reqs(struct virtio_crypto *vcrypto)
486 struct virtio_crypto_request *vc_req;
488 struct virtqueue *vq;
490 for (i = 0; i < vcrypto->max_data_queues; i++) {
491 vq = vcrypto->data_vq[i].vq;
492 while ((vc_req = virtqueue_detach_unused_buf(vq)) != NULL) {
493 kfree(vc_req->req_data);
500 static void virtcrypto_remove(struct virtio_device *vdev)
502 struct virtio_crypto *vcrypto = vdev->priv;
505 dev_info(&vdev->dev, "Start virtcrypto_remove.\n");
507 flush_work(&vcrypto->config_work);
508 if (virtcrypto_dev_started(vcrypto))
509 virtcrypto_dev_stop(vcrypto);
510 for (i = 0; i < vcrypto->max_data_queues; i++)
511 tasklet_kill(&vcrypto->data_vq[i].done_task);
512 virtio_reset_device(vdev);
513 virtcrypto_free_unused_reqs(vcrypto);
514 virtcrypto_clear_crypto_engines(vcrypto);
515 virtcrypto_del_vqs(vcrypto);
516 virtcrypto_devmgr_rm_dev(vcrypto);
520 static void virtcrypto_config_changed(struct virtio_device *vdev)
522 struct virtio_crypto *vcrypto = vdev->priv;
524 schedule_work(&vcrypto->config_work);
527 #ifdef CONFIG_PM_SLEEP
528 static int virtcrypto_freeze(struct virtio_device *vdev)
530 struct virtio_crypto *vcrypto = vdev->priv;
532 flush_work(&vcrypto->config_work);
533 virtio_reset_device(vdev);
534 virtcrypto_free_unused_reqs(vcrypto);
535 if (virtcrypto_dev_started(vcrypto))
536 virtcrypto_dev_stop(vcrypto);
538 virtcrypto_clear_crypto_engines(vcrypto);
539 virtcrypto_del_vqs(vcrypto);
543 static int virtcrypto_restore(struct virtio_device *vdev)
545 struct virtio_crypto *vcrypto = vdev->priv;
548 err = virtcrypto_init_vqs(vcrypto);
552 err = virtcrypto_start_crypto_engines(vcrypto);
556 virtio_device_ready(vdev);
558 err = virtcrypto_dev_start(vcrypto);
560 dev_err(&vdev->dev, "Failed to start virtio crypto device.\n");
567 virtcrypto_clear_crypto_engines(vcrypto);
569 virtio_reset_device(vdev);
570 virtcrypto_del_vqs(vcrypto);
575 static const unsigned int features[] = {
579 static const struct virtio_device_id id_table[] = {
580 { VIRTIO_ID_CRYPTO, VIRTIO_DEV_ANY_ID },
584 static struct virtio_driver virtio_crypto_driver = {
585 .driver.name = KBUILD_MODNAME,
586 .driver.owner = THIS_MODULE,
587 .feature_table = features,
588 .feature_table_size = ARRAY_SIZE(features),
589 .id_table = id_table,
590 .probe = virtcrypto_probe,
591 .remove = virtcrypto_remove,
592 .config_changed = virtcrypto_config_changed,
593 #ifdef CONFIG_PM_SLEEP
594 .freeze = virtcrypto_freeze,
595 .restore = virtcrypto_restore,
599 module_virtio_driver(virtio_crypto_driver);
601 MODULE_DEVICE_TABLE(virtio, id_table);
602 MODULE_DESCRIPTION("virtio crypto device driver");
603 MODULE_LICENSE("GPL");
604 MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");