1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include "../libqtest.h"
21#include "qemu/module.h"
22#include "standard-headers/linux/virtio_blk.h"
23#include "qgraph.h"
24#include "virtio-blk.h"
25
26#define PCI_SLOT 0x04
27#define PCI_FN 0x00
28
29
30static void *qvirtio_blk_get_driver(QVirtioBlk *v_blk,
31 const char *interface)
32{
33 if (!g_strcmp0(interface, "virtio-blk")) {
34 return v_blk;
35 }
36 if (!g_strcmp0(interface, "virtio")) {
37 return v_blk->vdev;
38 }
39
40 fprintf(stderr, "%s not present in virtio-blk-device\n", interface);
41 g_assert_not_reached();
42}
43
44static void *qvirtio_blk_device_get_driver(void *object,
45 const char *interface)
46{
47 QVirtioBlkDevice *v_blk = object;
48 return qvirtio_blk_get_driver(&v_blk->blk, interface);
49}
50
51static void *virtio_blk_device_create(void *virtio_dev,
52 QGuestAllocator *t_alloc,
53 void *addr)
54{
55 QVirtioBlkDevice *virtio_blk = g_new0(QVirtioBlkDevice, 1);
56 QVirtioBlk *interface = &virtio_blk->blk;
57
58 interface->vdev = virtio_dev;
59
60 virtio_blk->obj.get_driver = qvirtio_blk_device_get_driver;
61
62 return &virtio_blk->obj;
63}
64
65
66static void *qvirtio_blk_pci_get_driver(void *object, const char *interface)
67{
68 QVirtioBlkPCI *v_blk = object;
69 if (!g_strcmp0(interface, "pci-device")) {
70 return v_blk->pci_vdev.pdev;
71 }
72 return qvirtio_blk_get_driver(&v_blk->blk, interface);
73}
74
75static void *virtio_blk_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
76 void *addr)
77{
78 QVirtioBlkPCI *virtio_blk = g_new0(QVirtioBlkPCI, 1);
79 QVirtioBlk *interface = &virtio_blk->blk;
80 QOSGraphObject *obj = &virtio_blk->pci_vdev.obj;
81
82 virtio_pci_init(&virtio_blk->pci_vdev, pci_bus, addr);
83 interface->vdev = &virtio_blk->pci_vdev.vdev;
84
85 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_BLOCK);
86
87 obj->get_driver = qvirtio_blk_pci_get_driver;
88
89 return obj;
90}
91
92static void virtio_blk_register_nodes(void)
93{
94
95
96
97
98
99
100 char *arg = g_strdup_printf("id=drv0,drive=drive0,addr=%x.%x",
101 PCI_SLOT, PCI_FN);
102
103 QPCIAddress addr = {
104 .devfn = QPCI_DEVFN(PCI_SLOT, PCI_FN),
105 };
106
107 QOSGraphEdgeOptions opts = { };
108
109
110 opts.extra_device_opts = "drive=drive0";
111 qos_node_create_driver("virtio-blk-device", virtio_blk_device_create);
112 qos_node_consumes("virtio-blk-device", "virtio-bus", &opts);
113 qos_node_produces("virtio-blk-device", "virtio-blk");
114
115
116 opts.extra_device_opts = arg;
117 add_qpci_address(&opts, &addr);
118 qos_node_create_driver("virtio-blk-pci", virtio_blk_pci_create);
119 qos_node_consumes("virtio-blk-pci", "pci-bus", &opts);
120 qos_node_produces("virtio-blk-pci", "virtio-blk");
121
122 g_free(arg);
123}
124
125libqos_init(virtio_blk_register_nodes);
126