xref: /openbmc/qemu/hw/virtio/virtio-blk-pci.c (revision 520e210c)
1 /*
2  * Virtio blk PCI Bindings
3  *
4  * Copyright IBM, Corp. 2007
5  * Copyright (c) 2009 CodeSourcery
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *  Paul Brook        <paul@codesourcery.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  *
14  * Contributions after 2012-01-13 are licensed under the terms of the
15  * GNU GPL, version 2 or (at your option) any later version.
16  */
17 
18 #include "qemu/osdep.h"
19 
20 #include "hw/virtio/virtio-blk.h"
21 #include "virtio-pci.h"
22 #include "qapi/error.h"
23 
24 typedef struct VirtIOBlkPCI VirtIOBlkPCI;
25 
26 /*
27  * virtio-blk-pci: This extends VirtioPCIProxy.
28  */
29 #define TYPE_VIRTIO_BLK_PCI "virtio-blk-pci-base"
30 #define VIRTIO_BLK_PCI(obj) \
31         OBJECT_CHECK(VirtIOBlkPCI, (obj), TYPE_VIRTIO_BLK_PCI)
32 
33 struct VirtIOBlkPCI {
34     VirtIOPCIProxy parent_obj;
35     VirtIOBlock vdev;
36 };
37 
38 static Property virtio_blk_pci_properties[] = {
39     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
40     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
41                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
42     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
43                        DEV_NVECTORS_UNSPECIFIED),
44     DEFINE_PROP_END_OF_LIST(),
45 };
46 
47 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
48 {
49     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
50     DeviceState *vdev = DEVICE(&dev->vdev);
51 
52     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
53         vpci_dev->nvectors = dev->vdev.conf.num_queues + 1;
54     }
55 
56     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
57     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
58 }
59 
60 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
61 {
62     DeviceClass *dc = DEVICE_CLASS(klass);
63     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
64     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
65 
66     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
67     dc->props = virtio_blk_pci_properties;
68     k->realize = virtio_blk_pci_realize;
69     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
70     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
71     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
72     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
73 }
74 
75 static void virtio_blk_pci_instance_init(Object *obj)
76 {
77     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
78 
79     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
80                                 TYPE_VIRTIO_BLK);
81     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
82                               "bootindex", &error_abort);
83 }
84 
85 static const VirtioPCIDeviceTypeInfo virtio_blk_pci_info = {
86     .base_name              = TYPE_VIRTIO_BLK_PCI,
87     .generic_name           = "virtio-blk-pci",
88     .transitional_name      = "virtio-blk-pci-transitional",
89     .non_transitional_name  = "virtio-blk-pci-non-transitional",
90     .instance_size = sizeof(VirtIOBlkPCI),
91     .instance_init = virtio_blk_pci_instance_init,
92     .class_init    = virtio_blk_pci_class_init,
93 };
94 
95 static void virtio_blk_pci_register(void)
96 {
97     virtio_pci_types_register(&virtio_blk_pci_info);
98 }
99 
100 type_init(virtio_blk_pci_register)
101