xref: /qemu/hw/virtio/virtio-crypto-pci.c (revision 79c3e2bc6e78eb9cb197a9b3a9fc885ec1b7c720)
1b307d308SGonglei /*
2b307d308SGonglei  * Virtio crypto device
3b307d308SGonglei  *
4b307d308SGonglei  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5b307d308SGonglei  *
6b307d308SGonglei  * Authors:
7b307d308SGonglei  *    Gonglei <arei.gonglei@huawei.com>
8b307d308SGonglei  *
9b307d308SGonglei  * This work is licensed under the terms of the GNU GPL, version 2 or
10b307d308SGonglei  * (at your option) any later version.  See the COPYING file in the
11b307d308SGonglei  * top-level directory.
12b307d308SGonglei  *
13b307d308SGonglei  */
140b8fa32fSMarkus Armbruster 
15b307d308SGonglei #include "qemu/osdep.h"
16b307d308SGonglei #include "hw/pci/pci.h"
17a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
18b307d308SGonglei #include "hw/virtio/virtio.h"
19b307d308SGonglei #include "hw/virtio/virtio-bus.h"
20b307d308SGonglei #include "hw/virtio/virtio-pci.h"
21b307d308SGonglei #include "hw/virtio/virtio-crypto.h"
22b307d308SGonglei #include "qapi/error.h"
230b8fa32fSMarkus Armbruster #include "qemu/module.h"
24b307d308SGonglei 
257c8681d0SJuan Quintela typedef struct VirtIOCryptoPCI VirtIOCryptoPCI;
267c8681d0SJuan Quintela 
277c8681d0SJuan Quintela /*
287c8681d0SJuan Quintela  * virtio-crypto-pci: This extends VirtioPCIProxy.
297c8681d0SJuan Quintela  */
307c8681d0SJuan Quintela #define TYPE_VIRTIO_CRYPTO_PCI "virtio-crypto-pci"
317c8681d0SJuan Quintela #define VIRTIO_CRYPTO_PCI(obj) \
327c8681d0SJuan Quintela         OBJECT_CHECK(VirtIOCryptoPCI, (obj), TYPE_VIRTIO_CRYPTO_PCI)
337c8681d0SJuan Quintela 
347c8681d0SJuan Quintela struct VirtIOCryptoPCI {
357c8681d0SJuan Quintela     VirtIOPCIProxy parent_obj;
367c8681d0SJuan Quintela     VirtIOCrypto vdev;
377c8681d0SJuan Quintela };
387c8681d0SJuan Quintela 
39b307d308SGonglei static Property virtio_crypto_pci_properties[] = {
40b307d308SGonglei     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
41b307d308SGonglei                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
42b307d308SGonglei     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
43b307d308SGonglei     DEFINE_PROP_END_OF_LIST(),
44b307d308SGonglei };
45b307d308SGonglei 
46b307d308SGonglei static void virtio_crypto_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
47b307d308SGonglei {
48b307d308SGonglei     VirtIOCryptoPCI *vcrypto = VIRTIO_CRYPTO_PCI(vpci_dev);
49b307d308SGonglei     DeviceState *vdev = DEVICE(&vcrypto->vdev);
50b307d308SGonglei 
51305f5131SGonglei     if (vcrypto->vdev.conf.cryptodev == NULL) {
52305f5131SGonglei         error_setg(errp, "'cryptodev' parameter expects a valid object");
53305f5131SGonglei         return;
54305f5131SGonglei     }
55305f5131SGonglei 
56dd56040dSDr. David Alan Gilbert     virtio_pci_force_virtio_1(vpci_dev);
57*79c3e2bcSMarkus Armbruster     if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) {
58*79c3e2bcSMarkus Armbruster         return;
59*79c3e2bcSMarkus Armbruster     }
60b307d308SGonglei     object_property_set_link(OBJECT(vcrypto),
61b307d308SGonglei                  OBJECT(vcrypto->vdev.conf.cryptodev), "cryptodev",
62b307d308SGonglei                  NULL);
63b307d308SGonglei }
64b307d308SGonglei 
65b307d308SGonglei static void virtio_crypto_pci_class_init(ObjectClass *klass, void *data)
66b307d308SGonglei {
67b307d308SGonglei     DeviceClass *dc = DEVICE_CLASS(klass);
68b307d308SGonglei     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
69b307d308SGonglei     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
70b307d308SGonglei 
71b307d308SGonglei     k->realize = virtio_crypto_pci_realize;
72b307d308SGonglei     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
734f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_crypto_pci_properties);
74b307d308SGonglei     pcidev_k->class_id = PCI_CLASS_OTHERS;
75b307d308SGonglei }
76b307d308SGonglei 
77b307d308SGonglei static void virtio_crypto_initfn(Object *obj)
78b307d308SGonglei {
79b307d308SGonglei     VirtIOCryptoPCI *dev = VIRTIO_CRYPTO_PCI(obj);
80b307d308SGonglei 
81b307d308SGonglei     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
82b307d308SGonglei                                 TYPE_VIRTIO_CRYPTO);
83b307d308SGonglei }
84b307d308SGonglei 
85a4ee4c8bSEduardo Habkost static const VirtioPCIDeviceTypeInfo virtio_crypto_pci_info = {
86a4ee4c8bSEduardo Habkost     .generic_name  = TYPE_VIRTIO_CRYPTO_PCI,
87b307d308SGonglei     .instance_size = sizeof(VirtIOCryptoPCI),
88b307d308SGonglei     .instance_init = virtio_crypto_initfn,
89b307d308SGonglei     .class_init    = virtio_crypto_pci_class_init,
90b307d308SGonglei };
91b307d308SGonglei 
92b307d308SGonglei static void virtio_crypto_pci_register_types(void)
93b307d308SGonglei {
94a4ee4c8bSEduardo Habkost     virtio_pci_types_register(&virtio_crypto_pci_info);
95b307d308SGonglei }
96b307d308SGonglei type_init(virtio_crypto_pci_register_types)
97