2 * libqos driver framework
4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>
19 #include "qemu/osdep.h"
21 #include "standard-headers/linux/virtio_blk.h"
22 #include "libqos/qgraph.h"
23 #include "libqos/virtio-blk.h"
28 /* virtio-blk-device */
29 static void *qvirtio_blk_get_driver(QVirtioBlk
*v_blk
,
30 const char *interface
)
32 if (!g_strcmp0(interface
, "virtio-blk")) {
35 if (!g_strcmp0(interface
, "virtio")) {
39 fprintf(stderr
, "%s not present in virtio-blk-device\n", interface
);
40 g_assert_not_reached();
43 static void *qvirtio_blk_device_get_driver(void *object
,
44 const char *interface
)
46 QVirtioBlkDevice
*v_blk
= object
;
47 return qvirtio_blk_get_driver(&v_blk
->blk
, interface
);
50 static void *virtio_blk_device_create(void *virtio_dev
,
51 QGuestAllocator
*t_alloc
,
54 QVirtioBlkDevice
*virtio_blk
= g_new0(QVirtioBlkDevice
, 1);
55 QVirtioBlk
*interface
= &virtio_blk
->blk
;
57 interface
->vdev
= virtio_dev
;
59 virtio_blk
->obj
.get_driver
= qvirtio_blk_device_get_driver
;
61 return &virtio_blk
->obj
;
65 static void *qvirtio_blk_pci_get_driver(void *object
, const char *interface
)
67 QVirtioBlkPCI
*v_blk
= object
;
68 if (!g_strcmp0(interface
, "pci-device")) {
69 return v_blk
->pci_vdev
.pdev
;
71 return qvirtio_blk_get_driver(&v_blk
->blk
, interface
);
74 static void *virtio_blk_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
77 QVirtioBlkPCI
*virtio_blk
= g_new0(QVirtioBlkPCI
, 1);
78 QVirtioBlk
*interface
= &virtio_blk
->blk
;
79 QOSGraphObject
*obj
= &virtio_blk
->pci_vdev
.obj
;
81 virtio_pci_init(&virtio_blk
->pci_vdev
, pci_bus
, addr
);
82 interface
->vdev
= &virtio_blk
->pci_vdev
.vdev
;
84 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_BLOCK
);
86 obj
->get_driver
= qvirtio_blk_pci_get_driver
;
91 static void virtio_blk_register_nodes(void)
93 /* FIXME: every test using these two nodes needs to setup a
94 * -drive,id=drive0 otherwise QEMU is not going to start.
95 * Therefore, we do not include "produces" edge for virtio
99 char *arg
= g_strdup_printf("id=drv0,drive=drive0,addr=%x.%x",
103 .devfn
= QPCI_DEVFN(PCI_SLOT
, PCI_FN
),
106 QOSGraphEdgeOptions opts
= { };
108 /* virtio-blk-device */
109 opts
.extra_device_opts
= "drive=drive0";
110 qos_node_create_driver("virtio-blk-device", virtio_blk_device_create
);
111 qos_node_consumes("virtio-blk-device", "virtio-bus", &opts
);
112 qos_node_produces("virtio-blk-device", "virtio-blk");
115 opts
.extra_device_opts
= arg
;
116 add_qpci_address(&opts
, &addr
);
117 qos_node_create_driver("virtio-blk-pci", virtio_blk_pci_create
);
118 qos_node_consumes("virtio-blk-pci", "pci-bus", &opts
);
119 qos_node_produces("virtio-blk-pci", "virtio-blk");
124 libqos_init(virtio_blk_register_nodes
);