Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002  /* Driver for Virtio crypto device.
0003   *
0004   * Copyright 2016 HUAWEI TECHNOLOGIES CO., LTD.
0005   */
0006 
0007 #include <linux/err.h>
0008 #include <linux/module.h>
0009 #include <linux/virtio_config.h>
0010 #include <linux/cpu.h>
0011 
0012 #include <uapi/linux/virtio_crypto.h>
0013 #include "virtio_crypto_common.h"
0014 
0015 
0016 void
0017 virtcrypto_clear_request(struct virtio_crypto_request *vc_req)
0018 {
0019     if (vc_req) {
0020         kfree_sensitive(vc_req->req_data);
0021         kfree(vc_req->sgs);
0022     }
0023 }
0024 
0025 static void virtio_crypto_ctrlq_callback(struct virtio_crypto_ctrl_request *vc_ctrl_req)
0026 {
0027     complete(&vc_ctrl_req->compl);
0028 }
0029 
0030 static void virtcrypto_ctrlq_callback(struct virtqueue *vq)
0031 {
0032     struct virtio_crypto *vcrypto = vq->vdev->priv;
0033     struct virtio_crypto_ctrl_request *vc_ctrl_req;
0034     unsigned long flags;
0035     unsigned int len;
0036 
0037     spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
0038     do {
0039         virtqueue_disable_cb(vq);
0040         while ((vc_ctrl_req = virtqueue_get_buf(vq, &len)) != NULL) {
0041             spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
0042             virtio_crypto_ctrlq_callback(vc_ctrl_req);
0043             spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
0044         }
0045         if (unlikely(virtqueue_is_broken(vq)))
0046             break;
0047     } while (!virtqueue_enable_cb(vq));
0048     spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
0049 }
0050 
0051 int virtio_crypto_ctrl_vq_request(struct virtio_crypto *vcrypto, struct scatterlist *sgs[],
0052         unsigned int out_sgs, unsigned int in_sgs,
0053         struct virtio_crypto_ctrl_request *vc_ctrl_req)
0054 {
0055     int err;
0056     unsigned long flags;
0057 
0058     init_completion(&vc_ctrl_req->compl);
0059 
0060     spin_lock_irqsave(&vcrypto->ctrl_lock, flags);
0061     err = virtqueue_add_sgs(vcrypto->ctrl_vq, sgs, out_sgs, in_sgs, vc_ctrl_req, GFP_ATOMIC);
0062     if (err < 0) {
0063         spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
0064         return err;
0065     }
0066 
0067     virtqueue_kick(vcrypto->ctrl_vq);
0068     spin_unlock_irqrestore(&vcrypto->ctrl_lock, flags);
0069 
0070     wait_for_completion(&vc_ctrl_req->compl);
0071 
0072     return 0;
0073 }
0074 
0075 static void virtcrypto_dataq_callback(struct virtqueue *vq)
0076 {
0077     struct virtio_crypto *vcrypto = vq->vdev->priv;
0078     struct virtio_crypto_request *vc_req;
0079     unsigned long flags;
0080     unsigned int len;
0081     unsigned int qid = vq->index;
0082 
0083     spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
0084     do {
0085         virtqueue_disable_cb(vq);
0086         while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
0087             spin_unlock_irqrestore(
0088                 &vcrypto->data_vq[qid].lock, flags);
0089             if (vc_req->alg_cb)
0090                 vc_req->alg_cb(vc_req, len);
0091             spin_lock_irqsave(
0092                 &vcrypto->data_vq[qid].lock, flags);
0093         }
0094     } while (!virtqueue_enable_cb(vq));
0095     spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
0096 }
0097 
0098 static int virtcrypto_find_vqs(struct virtio_crypto *vi)
0099 {
0100     vq_callback_t **callbacks;
0101     struct virtqueue **vqs;
0102     int ret = -ENOMEM;
0103     int i, total_vqs;
0104     const char **names;
0105     struct device *dev = &vi->vdev->dev;
0106 
0107     /*
0108      * We expect 1 data virtqueue, followed by
0109      * possible N-1 data queues used in multiqueue mode,
0110      * followed by control vq.
0111      */
0112     total_vqs = vi->max_data_queues + 1;
0113 
0114     /* Allocate space for find_vqs parameters */
0115     vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
0116     if (!vqs)
0117         goto err_vq;
0118     callbacks = kcalloc(total_vqs, sizeof(*callbacks), GFP_KERNEL);
0119     if (!callbacks)
0120         goto err_callback;
0121     names = kcalloc(total_vqs, sizeof(*names), GFP_KERNEL);
0122     if (!names)
0123         goto err_names;
0124 
0125     /* Parameters for control virtqueue */
0126     callbacks[total_vqs - 1] = virtcrypto_ctrlq_callback;
0127     names[total_vqs - 1] = "controlq";
0128 
0129     /* Allocate/initialize parameters for data virtqueues */
0130     for (i = 0; i < vi->max_data_queues; i++) {
0131         callbacks[i] = virtcrypto_dataq_callback;
0132         snprintf(vi->data_vq[i].name, sizeof(vi->data_vq[i].name),
0133                 "dataq.%d", i);
0134         names[i] = vi->data_vq[i].name;
0135     }
0136 
0137     ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, callbacks, names, NULL);
0138     if (ret)
0139         goto err_find;
0140 
0141     vi->ctrl_vq = vqs[total_vqs - 1];
0142 
0143     for (i = 0; i < vi->max_data_queues; i++) {
0144         spin_lock_init(&vi->data_vq[i].lock);
0145         vi->data_vq[i].vq = vqs[i];
0146         /* Initialize crypto engine */
0147         vi->data_vq[i].engine = crypto_engine_alloc_init_and_set(dev, true, NULL, true,
0148                         virtqueue_get_vring_size(vqs[i]));
0149         if (!vi->data_vq[i].engine) {
0150             ret = -ENOMEM;
0151             goto err_engine;
0152         }
0153     }
0154 
0155     kfree(names);
0156     kfree(callbacks);
0157     kfree(vqs);
0158 
0159     return 0;
0160 
0161 err_engine:
0162 err_find:
0163     kfree(names);
0164 err_names:
0165     kfree(callbacks);
0166 err_callback:
0167     kfree(vqs);
0168 err_vq:
0169     return ret;
0170 }
0171 
0172 static int virtcrypto_alloc_queues(struct virtio_crypto *vi)
0173 {
0174     vi->data_vq = kcalloc(vi->max_data_queues, sizeof(*vi->data_vq),
0175                 GFP_KERNEL);
0176     if (!vi->data_vq)
0177         return -ENOMEM;
0178 
0179     return 0;
0180 }
0181 
0182 static void virtcrypto_clean_affinity(struct virtio_crypto *vi, long hcpu)
0183 {
0184     int i;
0185 
0186     if (vi->affinity_hint_set) {
0187         for (i = 0; i < vi->max_data_queues; i++)
0188             virtqueue_set_affinity(vi->data_vq[i].vq, NULL);
0189 
0190         vi->affinity_hint_set = false;
0191     }
0192 }
0193 
0194 static void virtcrypto_set_affinity(struct virtio_crypto *vcrypto)
0195 {
0196     int i = 0;
0197     int cpu;
0198 
0199     /*
0200      * In single queue mode, we don't set the cpu affinity.
0201      */
0202     if (vcrypto->curr_queue == 1 || vcrypto->max_data_queues == 1) {
0203         virtcrypto_clean_affinity(vcrypto, -1);
0204         return;
0205     }
0206 
0207     /*
0208      * In multiqueue mode, we let the queue to be private to one cpu
0209      * by setting the affinity hint to eliminate the contention.
0210      *
0211      * TODO: adds cpu hotplug support by register cpu notifier.
0212      *
0213      */
0214     for_each_online_cpu(cpu) {
0215         virtqueue_set_affinity(vcrypto->data_vq[i].vq, cpumask_of(cpu));
0216         if (++i >= vcrypto->max_data_queues)
0217             break;
0218     }
0219 
0220     vcrypto->affinity_hint_set = true;
0221 }
0222 
0223 static void virtcrypto_free_queues(struct virtio_crypto *vi)
0224 {
0225     kfree(vi->data_vq);
0226 }
0227 
0228 static int virtcrypto_init_vqs(struct virtio_crypto *vi)
0229 {
0230     int ret;
0231 
0232     /* Allocate send & receive queues */
0233     ret = virtcrypto_alloc_queues(vi);
0234     if (ret)
0235         goto err;
0236 
0237     ret = virtcrypto_find_vqs(vi);
0238     if (ret)
0239         goto err_free;
0240 
0241     cpus_read_lock();
0242     virtcrypto_set_affinity(vi);
0243     cpus_read_unlock();
0244 
0245     return 0;
0246 
0247 err_free:
0248     virtcrypto_free_queues(vi);
0249 err:
0250     return ret;
0251 }
0252 
0253 static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
0254 {
0255     u32 status;
0256     int err;
0257 
0258     virtio_cread_le(vcrypto->vdev,
0259             struct virtio_crypto_config, status, &status);
0260 
0261     /*
0262      * Unknown status bits would be a host error and the driver
0263      * should consider the device to be broken.
0264      */
0265     if (status & (~VIRTIO_CRYPTO_S_HW_READY)) {
0266         dev_warn(&vcrypto->vdev->dev,
0267                 "Unknown status bits: 0x%x\n", status);
0268 
0269         virtio_break_device(vcrypto->vdev);
0270         return -EPERM;
0271     }
0272 
0273     if (vcrypto->status == status)
0274         return 0;
0275 
0276     vcrypto->status = status;
0277 
0278     if (vcrypto->status & VIRTIO_CRYPTO_S_HW_READY) {
0279         err = virtcrypto_dev_start(vcrypto);
0280         if (err) {
0281             dev_err(&vcrypto->vdev->dev,
0282                 "Failed to start virtio crypto device.\n");
0283 
0284             return -EPERM;
0285         }
0286         dev_info(&vcrypto->vdev->dev, "Accelerator device is ready\n");
0287     } else {
0288         virtcrypto_dev_stop(vcrypto);
0289         dev_info(&vcrypto->vdev->dev, "Accelerator is not ready\n");
0290     }
0291 
0292     return 0;
0293 }
0294 
0295 static int virtcrypto_start_crypto_engines(struct virtio_crypto *vcrypto)
0296 {
0297     int32_t i;
0298     int ret;
0299 
0300     for (i = 0; i < vcrypto->max_data_queues; i++) {
0301         if (vcrypto->data_vq[i].engine) {
0302             ret = crypto_engine_start(vcrypto->data_vq[i].engine);
0303             if (ret)
0304                 goto err;
0305         }
0306     }
0307 
0308     return 0;
0309 
0310 err:
0311     while (--i >= 0)
0312         if (vcrypto->data_vq[i].engine)
0313             crypto_engine_exit(vcrypto->data_vq[i].engine);
0314 
0315     return ret;
0316 }
0317 
0318 static void virtcrypto_clear_crypto_engines(struct virtio_crypto *vcrypto)
0319 {
0320     u32 i;
0321 
0322     for (i = 0; i < vcrypto->max_data_queues; i++)
0323         if (vcrypto->data_vq[i].engine)
0324             crypto_engine_exit(vcrypto->data_vq[i].engine);
0325 }
0326 
0327 static void virtcrypto_del_vqs(struct virtio_crypto *vcrypto)
0328 {
0329     struct virtio_device *vdev = vcrypto->vdev;
0330 
0331     virtcrypto_clean_affinity(vcrypto, -1);
0332 
0333     vdev->config->del_vqs(vdev);
0334 
0335     virtcrypto_free_queues(vcrypto);
0336 }
0337 
0338 static int virtcrypto_probe(struct virtio_device *vdev)
0339 {
0340     int err = -EFAULT;
0341     struct virtio_crypto *vcrypto;
0342     u32 max_data_queues = 0, max_cipher_key_len = 0;
0343     u32 max_auth_key_len = 0;
0344     u64 max_size = 0;
0345     u32 cipher_algo_l = 0;
0346     u32 cipher_algo_h = 0;
0347     u32 hash_algo = 0;
0348     u32 mac_algo_l = 0;
0349     u32 mac_algo_h = 0;
0350     u32 aead_algo = 0;
0351     u32 akcipher_algo = 0;
0352     u32 crypto_services = 0;
0353 
0354     if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
0355         return -ENODEV;
0356 
0357     if (!vdev->config->get) {
0358         dev_err(&vdev->dev, "%s failure: config access disabled\n",
0359             __func__);
0360         return -EINVAL;
0361     }
0362 
0363     if (num_possible_nodes() > 1 && dev_to_node(&vdev->dev) < 0) {
0364         /*
0365          * If the accelerator is connected to a node with no memory
0366          * there is no point in using the accelerator since the remote
0367          * memory transaction will be very slow.
0368          */
0369         dev_err(&vdev->dev, "Invalid NUMA configuration.\n");
0370         return -EINVAL;
0371     }
0372 
0373     vcrypto = kzalloc_node(sizeof(*vcrypto), GFP_KERNEL,
0374                     dev_to_node(&vdev->dev));
0375     if (!vcrypto)
0376         return -ENOMEM;
0377 
0378     virtio_cread_le(vdev, struct virtio_crypto_config,
0379             max_dataqueues, &max_data_queues);
0380     if (max_data_queues < 1)
0381         max_data_queues = 1;
0382 
0383     virtio_cread_le(vdev, struct virtio_crypto_config,
0384             max_cipher_key_len, &max_cipher_key_len);
0385     virtio_cread_le(vdev, struct virtio_crypto_config,
0386             max_auth_key_len, &max_auth_key_len);
0387     virtio_cread_le(vdev, struct virtio_crypto_config,
0388             max_size, &max_size);
0389     virtio_cread_le(vdev, struct virtio_crypto_config,
0390             crypto_services, &crypto_services);
0391     virtio_cread_le(vdev, struct virtio_crypto_config,
0392             cipher_algo_l, &cipher_algo_l);
0393     virtio_cread_le(vdev, struct virtio_crypto_config,
0394             cipher_algo_h, &cipher_algo_h);
0395     virtio_cread_le(vdev, struct virtio_crypto_config,
0396             hash_algo, &hash_algo);
0397     virtio_cread_le(vdev, struct virtio_crypto_config,
0398             mac_algo_l, &mac_algo_l);
0399     virtio_cread_le(vdev, struct virtio_crypto_config,
0400             mac_algo_h, &mac_algo_h);
0401     virtio_cread_le(vdev, struct virtio_crypto_config,
0402             aead_algo, &aead_algo);
0403     if (crypto_services & (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER))
0404         virtio_cread_le(vdev, struct virtio_crypto_config,
0405                 akcipher_algo, &akcipher_algo);
0406 
0407     /* Add virtio crypto device to global table */
0408     err = virtcrypto_devmgr_add_dev(vcrypto);
0409     if (err) {
0410         dev_err(&vdev->dev, "Failed to add new virtio crypto device.\n");
0411         goto free;
0412     }
0413     vcrypto->owner = THIS_MODULE;
0414     vcrypto = vdev->priv = vcrypto;
0415     vcrypto->vdev = vdev;
0416 
0417     spin_lock_init(&vcrypto->ctrl_lock);
0418 
0419     /* Use single data queue as default */
0420     vcrypto->curr_queue = 1;
0421     vcrypto->max_data_queues = max_data_queues;
0422     vcrypto->max_cipher_key_len = max_cipher_key_len;
0423     vcrypto->max_auth_key_len = max_auth_key_len;
0424     vcrypto->max_size = max_size;
0425     vcrypto->crypto_services = crypto_services;
0426     vcrypto->cipher_algo_l = cipher_algo_l;
0427     vcrypto->cipher_algo_h = cipher_algo_h;
0428     vcrypto->mac_algo_l = mac_algo_l;
0429     vcrypto->mac_algo_h = mac_algo_h;
0430     vcrypto->hash_algo = hash_algo;
0431     vcrypto->aead_algo = aead_algo;
0432     vcrypto->akcipher_algo = akcipher_algo;
0433 
0434     dev_info(&vdev->dev,
0435         "max_queues: %u, max_cipher_key_len: %u, max_auth_key_len: %u, max_size 0x%llx\n",
0436         vcrypto->max_data_queues,
0437         vcrypto->max_cipher_key_len,
0438         vcrypto->max_auth_key_len,
0439         vcrypto->max_size);
0440 
0441     err = virtcrypto_init_vqs(vcrypto);
0442     if (err) {
0443         dev_err(&vdev->dev, "Failed to initialize vqs.\n");
0444         goto free_dev;
0445     }
0446 
0447     err = virtcrypto_start_crypto_engines(vcrypto);
0448     if (err)
0449         goto free_vqs;
0450 
0451     virtio_device_ready(vdev);
0452 
0453     err = virtcrypto_update_status(vcrypto);
0454     if (err)
0455         goto free_engines;
0456 
0457     return 0;
0458 
0459 free_engines:
0460     virtcrypto_clear_crypto_engines(vcrypto);
0461 free_vqs:
0462     virtio_reset_device(vdev);
0463     virtcrypto_del_vqs(vcrypto);
0464 free_dev:
0465     virtcrypto_devmgr_rm_dev(vcrypto);
0466 free:
0467     kfree(vcrypto);
0468     return err;
0469 }
0470 
0471 static void virtcrypto_free_unused_reqs(struct virtio_crypto *vcrypto)
0472 {
0473     struct virtio_crypto_request *vc_req;
0474     int i;
0475     struct virtqueue *vq;
0476 
0477     for (i = 0; i < vcrypto->max_data_queues; i++) {
0478         vq = vcrypto->data_vq[i].vq;
0479         while ((vc_req = virtqueue_detach_unused_buf(vq)) != NULL) {
0480             kfree(vc_req->req_data);
0481             kfree(vc_req->sgs);
0482         }
0483     }
0484 }
0485 
0486 static void virtcrypto_remove(struct virtio_device *vdev)
0487 {
0488     struct virtio_crypto *vcrypto = vdev->priv;
0489 
0490     dev_info(&vdev->dev, "Start virtcrypto_remove.\n");
0491 
0492     if (virtcrypto_dev_started(vcrypto))
0493         virtcrypto_dev_stop(vcrypto);
0494     virtio_reset_device(vdev);
0495     virtcrypto_free_unused_reqs(vcrypto);
0496     virtcrypto_clear_crypto_engines(vcrypto);
0497     virtcrypto_del_vqs(vcrypto);
0498     virtcrypto_devmgr_rm_dev(vcrypto);
0499     kfree(vcrypto);
0500 }
0501 
0502 static void virtcrypto_config_changed(struct virtio_device *vdev)
0503 {
0504     struct virtio_crypto *vcrypto = vdev->priv;
0505 
0506     virtcrypto_update_status(vcrypto);
0507 }
0508 
0509 #ifdef CONFIG_PM_SLEEP
0510 static int virtcrypto_freeze(struct virtio_device *vdev)
0511 {
0512     struct virtio_crypto *vcrypto = vdev->priv;
0513 
0514     virtio_reset_device(vdev);
0515     virtcrypto_free_unused_reqs(vcrypto);
0516     if (virtcrypto_dev_started(vcrypto))
0517         virtcrypto_dev_stop(vcrypto);
0518 
0519     virtcrypto_clear_crypto_engines(vcrypto);
0520     virtcrypto_del_vqs(vcrypto);
0521     return 0;
0522 }
0523 
0524 static int virtcrypto_restore(struct virtio_device *vdev)
0525 {
0526     struct virtio_crypto *vcrypto = vdev->priv;
0527     int err;
0528 
0529     err = virtcrypto_init_vqs(vcrypto);
0530     if (err)
0531         return err;
0532 
0533     err = virtcrypto_start_crypto_engines(vcrypto);
0534     if (err)
0535         goto free_vqs;
0536 
0537     virtio_device_ready(vdev);
0538 
0539     err = virtcrypto_dev_start(vcrypto);
0540     if (err) {
0541         dev_err(&vdev->dev, "Failed to start virtio crypto device.\n");
0542         goto free_engines;
0543     }
0544 
0545     return 0;
0546 
0547 free_engines:
0548     virtcrypto_clear_crypto_engines(vcrypto);
0549 free_vqs:
0550     virtio_reset_device(vdev);
0551     virtcrypto_del_vqs(vcrypto);
0552     return err;
0553 }
0554 #endif
0555 
0556 static const unsigned int features[] = {
0557     /* none */
0558 };
0559 
0560 static const struct virtio_device_id id_table[] = {
0561     { VIRTIO_ID_CRYPTO, VIRTIO_DEV_ANY_ID },
0562     { 0 },
0563 };
0564 
0565 static struct virtio_driver virtio_crypto_driver = {
0566     .driver.name         = KBUILD_MODNAME,
0567     .driver.owner        = THIS_MODULE,
0568     .feature_table       = features,
0569     .feature_table_size  = ARRAY_SIZE(features),
0570     .id_table            = id_table,
0571     .probe               = virtcrypto_probe,
0572     .remove              = virtcrypto_remove,
0573     .config_changed = virtcrypto_config_changed,
0574 #ifdef CONFIG_PM_SLEEP
0575     .freeze = virtcrypto_freeze,
0576     .restore = virtcrypto_restore,
0577 #endif
0578 };
0579 
0580 module_virtio_driver(virtio_crypto_driver);
0581 
0582 MODULE_DEVICE_TABLE(virtio, id_table);
0583 MODULE_DESCRIPTION("virtio crypto device driver");
0584 MODULE_LICENSE("GPL");
0585 MODULE_AUTHOR("Gonglei <arei.gonglei@huawei.com>");