xref: /openbmc/qemu/hw/virtio/virtio-crypto-pci.c (revision 99d46107)
1 /*
2  * Virtio crypto device
3  *
4  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5  *
6  * Authors:
7  *    Gonglei <arei.gonglei@huawei.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or
10  * (at your option) any later version.  See the COPYING file in the
11  * top-level directory.
12  *
13  */
14 #include "qemu/osdep.h"
15 #include "hw/pci/pci.h"
16 #include "hw/virtio/virtio.h"
17 #include "hw/virtio/virtio-bus.h"
18 #include "hw/virtio/virtio-pci.h"
19 #include "hw/virtio/virtio-crypto.h"
20 #include "qapi/error.h"
21 
22 typedef struct VirtIOCryptoPCI VirtIOCryptoPCI;
23 
24 /*
25  * virtio-crypto-pci: This extends VirtioPCIProxy.
26  */
27 #define TYPE_VIRTIO_CRYPTO_PCI "virtio-crypto-pci"
28 #define VIRTIO_CRYPTO_PCI(obj) \
29         OBJECT_CHECK(VirtIOCryptoPCI, (obj), TYPE_VIRTIO_CRYPTO_PCI)
30 
31 struct VirtIOCryptoPCI {
32     VirtIOPCIProxy parent_obj;
33     VirtIOCrypto vdev;
34 };
35 
36 static Property virtio_crypto_pci_properties[] = {
37     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
38                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
39     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
40     DEFINE_PROP_END_OF_LIST(),
41 };
42 
43 static void virtio_crypto_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
44 {
45     VirtIOCryptoPCI *vcrypto = VIRTIO_CRYPTO_PCI(vpci_dev);
46     DeviceState *vdev = DEVICE(&vcrypto->vdev);
47 
48     if (vcrypto->vdev.conf.cryptodev == NULL) {
49         error_setg(errp, "'cryptodev' parameter expects a valid object");
50         return;
51     }
52 
53     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
54     virtio_pci_force_virtio_1(vpci_dev);
55     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
56     object_property_set_link(OBJECT(vcrypto),
57                  OBJECT(vcrypto->vdev.conf.cryptodev), "cryptodev",
58                  NULL);
59 }
60 
61 static void virtio_crypto_pci_class_init(ObjectClass *klass, void *data)
62 {
63     DeviceClass *dc = DEVICE_CLASS(klass);
64     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
65     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
66 
67     k->realize = virtio_crypto_pci_realize;
68     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
69     dc->props = virtio_crypto_pci_properties;
70     pcidev_k->class_id = PCI_CLASS_OTHERS;
71 }
72 
73 static void virtio_crypto_initfn(Object *obj)
74 {
75     VirtIOCryptoPCI *dev = VIRTIO_CRYPTO_PCI(obj);
76 
77     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
78                                 TYPE_VIRTIO_CRYPTO);
79 }
80 
81 static const VirtioPCIDeviceTypeInfo virtio_crypto_pci_info = {
82     .generic_name  = TYPE_VIRTIO_CRYPTO_PCI,
83     .instance_size = sizeof(VirtIOCryptoPCI),
84     .instance_init = virtio_crypto_initfn,
85     .class_init    = virtio_crypto_pci_class_init,
86 };
87 
88 static void virtio_crypto_pci_register_types(void)
89 {
90     virtio_pci_types_register(&virtio_crypto_pci_info);
91 }
92 type_init(virtio_crypto_pci_register_types)
93