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
15 #include "qemu/osdep.h"
16 #include "hw/pci/pci.h"
17 #include "hw/qdev-properties.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/virtio/virtio-bus.h"
20 #include "hw/virtio/virtio-pci.h"
21 #include "hw/virtio/virtio-crypto.h"
22 #include "qapi/error.h"
23 #include "qemu/module.h"
24 #include "qom/object.h"
25
26 typedef struct VirtIOCryptoPCI VirtIOCryptoPCI;
27
28 /*
29 * virtio-crypto-pci: This extends VirtioPCIProxy.
30 */
31 #define TYPE_VIRTIO_CRYPTO_PCI "virtio-crypto-pci"
32 DECLARE_INSTANCE_CHECKER(VirtIOCryptoPCI, VIRTIO_CRYPTO_PCI,
33 TYPE_VIRTIO_CRYPTO_PCI)
34
35 struct VirtIOCryptoPCI {
36 VirtIOPCIProxy parent_obj;
37 VirtIOCrypto vdev;
38 };
39
40 static const Property virtio_crypto_pci_properties[] = {
41 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
42 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
43 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
44 };
45
virtio_crypto_pci_realize(VirtIOPCIProxy * vpci_dev,Error ** errp)46 static void virtio_crypto_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
47 {
48 VirtIOCryptoPCI *vcrypto = VIRTIO_CRYPTO_PCI(vpci_dev);
49 DeviceState *vdev = DEVICE(&vcrypto->vdev);
50
51 if (vcrypto->vdev.conf.cryptodev == NULL) {
52 error_setg(errp, "'cryptodev' parameter expects a valid object");
53 return;
54 }
55
56 virtio_pci_force_virtio_1(vpci_dev);
57 if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) {
58 return;
59 }
60 }
61
virtio_crypto_pci_class_init(ObjectClass * klass,const void * data)62 static void virtio_crypto_pci_class_init(ObjectClass *klass, const void *data)
63 {
64 DeviceClass *dc = DEVICE_CLASS(klass);
65 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
66 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
67
68 k->realize = virtio_crypto_pci_realize;
69 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
70 device_class_set_props(dc, virtio_crypto_pci_properties);
71 pcidev_k->class_id = PCI_CLASS_OTHERS;
72 }
73
virtio_crypto_initfn(Object * obj)74 static void virtio_crypto_initfn(Object *obj)
75 {
76 VirtIOCryptoPCI *dev = VIRTIO_CRYPTO_PCI(obj);
77
78 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
79 TYPE_VIRTIO_CRYPTO);
80 }
81
82 static const VirtioPCIDeviceTypeInfo virtio_crypto_pci_info = {
83 .generic_name = TYPE_VIRTIO_CRYPTO_PCI,
84 .instance_size = sizeof(VirtIOCryptoPCI),
85 .instance_init = virtio_crypto_initfn,
86 .class_init = virtio_crypto_pci_class_init,
87 };
88
virtio_crypto_pci_register_types(void)89 static void virtio_crypto_pci_register_types(void)
90 {
91 virtio_pci_types_register(&virtio_crypto_pci_info);
92 }
93 type_init(virtio_crypto_pci_register_types)
94