xref: /openbmc/qemu/hw/virtio/vhost-user-blk-pci.c (revision ba632924)
1 /*
2  * Vhost user blk PCI Bindings
3  *
4  * Copyright(C) 2017 Intel Corporation.
5  *
6  * Authors:
7  *  Changpeng Liu <changpeng.liu@intel.com>
8  *
9  * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by:
10  * Felipe Franciosi <felipe@nutanix.com>
11  * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
12  * Nicholas Bellinger <nab@risingtidesystems.com>
13  *
14  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
15  * See the COPYING.LIB file in the top-level directory.
16  *
17  */
18 
19 #include "qemu/osdep.h"
20 
21 #include "standard-headers/linux/virtio_pci.h"
22 #include "hw/virtio/virtio.h"
23 #include "hw/virtio/vhost-user-blk.h"
24 #include "hw/pci/pci.h"
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "virtio-pci.h"
28 
29 typedef struct VHostUserBlkPCI VHostUserBlkPCI;
30 
31 /*
32  * vhost-user-blk-pci: This extends VirtioPCIProxy.
33  */
34 #define TYPE_VHOST_USER_BLK_PCI "vhost-user-blk-pci-base"
35 #define VHOST_USER_BLK_PCI(obj) \
36         OBJECT_CHECK(VHostUserBlkPCI, (obj), TYPE_VHOST_USER_BLK_PCI)
37 
38 struct VHostUserBlkPCI {
39     VirtIOPCIProxy parent_obj;
40     VHostUserBlk vdev;
41 };
42 
43 static Property vhost_user_blk_pci_properties[] = {
44     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
45     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
46                        DEV_NVECTORS_UNSPECIFIED),
47     DEFINE_PROP_END_OF_LIST(),
48 };
49 
50 static void vhost_user_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
51 {
52     VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(vpci_dev);
53     DeviceState *vdev = DEVICE(&dev->vdev);
54 
55     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
56         vpci_dev->nvectors = dev->vdev.num_queues + 1;
57     }
58 
59     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
60     object_property_set_bool(OBJECT(vdev), true, "realized", errp);
61 }
62 
63 static void vhost_user_blk_pci_class_init(ObjectClass *klass, void *data)
64 {
65     DeviceClass *dc = DEVICE_CLASS(klass);
66     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
67     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
68 
69     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
70     dc->props = vhost_user_blk_pci_properties;
71     k->realize = vhost_user_blk_pci_realize;
72     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
73     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
74     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
75     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
76 }
77 
78 static void vhost_user_blk_pci_instance_init(Object *obj)
79 {
80     VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(obj);
81 
82     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
83                                 TYPE_VHOST_USER_BLK);
84     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
85                               "bootindex", &error_abort);
86 }
87 
88 static const VirtioPCIDeviceTypeInfo vhost_user_blk_pci_info = {
89     .base_name               = TYPE_VHOST_USER_BLK_PCI,
90     .generic_name            = "vhost-user-blk-pci",
91     .transitional_name       = "vhost-user-blk-pci-transitional",
92     .non_transitional_name   = "vhost-user-blk-pci-non-transitional",
93     .instance_size  = sizeof(VHostUserBlkPCI),
94     .instance_init  = vhost_user_blk_pci_instance_init,
95     .class_init     = vhost_user_blk_pci_class_init,
96 };
97 
98 static void vhost_user_blk_pci_register(void)
99 {
100     virtio_pci_types_register(&vhost_user_blk_pci_info);
101 }
102 
103 type_init(vhost_user_blk_pci_register)
104