xref: /openbmc/qemu/hw/virtio/virtio-blk-pci.c (revision 354908ce)
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/qdev-properties.h"
21 #include "hw/virtio/virtio-blk.h"
22 #include "virtio-pci.h"
23 #include "qapi/error.h"
24 #include "qemu/module.h"
25 
26 typedef struct VirtIOBlkPCI VirtIOBlkPCI;
27 
28 /*
29  * virtio-blk-pci: This extends VirtioPCIProxy.
30  */
31 #define TYPE_VIRTIO_BLK_PCI "virtio-blk-pci-base"
32 #define VIRTIO_BLK_PCI(obj) \
33         OBJECT_CHECK(VirtIOBlkPCI, (obj), TYPE_VIRTIO_BLK_PCI)
34 
35 struct VirtIOBlkPCI {
36     VirtIOPCIProxy parent_obj;
37     VirtIOBlock vdev;
38 };
39 
40 static Property virtio_blk_pci_properties[] = {
41     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
42     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
43                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
44     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
45                        DEV_NVECTORS_UNSPECIFIED),
46     DEFINE_PROP_END_OF_LIST(),
47 };
48 
49 static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
50 {
51     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
52     DeviceState *vdev = DEVICE(&dev->vdev);
53 
54     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
55         vpci_dev->nvectors = dev->vdev.conf.num_queues + 1;
56     }
57 
58     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
59 }
60 
61 static void virtio_blk_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     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
68     device_class_set_props(dc, virtio_blk_pci_properties);
69     k->realize = virtio_blk_pci_realize;
70     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
71     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
72     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
73     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
74 }
75 
76 static void virtio_blk_pci_instance_init(Object *obj)
77 {
78     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
79 
80     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
81                                 TYPE_VIRTIO_BLK);
82     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
83                               "bootindex");
84 }
85 
86 static const VirtioPCIDeviceTypeInfo virtio_blk_pci_info = {
87     .base_name              = TYPE_VIRTIO_BLK_PCI,
88     .generic_name           = "virtio-blk-pci",
89     .transitional_name      = "virtio-blk-pci-transitional",
90     .non_transitional_name  = "virtio-blk-pci-non-transitional",
91     .instance_size = sizeof(VirtIOBlkPCI),
92     .instance_init = virtio_blk_pci_instance_init,
93     .class_init    = virtio_blk_pci_class_init,
94 };
95 
96 static void virtio_blk_pci_register(void)
97 {
98     virtio_pci_types_register(&virtio_blk_pci_info);
99 }
100 
101 type_init(virtio_blk_pci_register)
102