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.1 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 "qemu/module.h"
22 #include "standard-headers/linux/virtio_blk.h"
24 #include "virtio-blk.h"
29 /* virtio-blk-device */
30 static void *qvirtio_blk_get_driver(QVirtioBlk
*v_blk
,
31 const char *interface
)
33 if (!g_strcmp0(interface
, "virtio-blk")) {
36 if (!g_strcmp0(interface
, "virtio")) {
40 fprintf(stderr
, "%s not present in virtio-blk-device\n", interface
);
41 g_assert_not_reached();
44 static void *qvirtio_blk_device_get_driver(void *object
,
45 const char *interface
)
47 QVirtioBlkDevice
*v_blk
= object
;
48 return qvirtio_blk_get_driver(&v_blk
->blk
, interface
);
51 static void *virtio_blk_device_create(void *virtio_dev
,
52 QGuestAllocator
*t_alloc
,
55 QVirtioBlkDevice
*virtio_blk
= g_new0(QVirtioBlkDevice
, 1);
56 QVirtioBlk
*interface
= &virtio_blk
->blk
;
58 interface
->vdev
= virtio_dev
;
60 virtio_blk
->obj
.get_driver
= qvirtio_blk_device_get_driver
;
62 return &virtio_blk
->obj
;
66 static void *qvirtio_blk_pci_get_driver(void *object
, const char *interface
)
68 QVirtioBlkPCI
*v_blk
= object
;
69 if (!g_strcmp0(interface
, "pci-device")) {
70 return v_blk
->pci_vdev
.pdev
;
72 return qvirtio_blk_get_driver(&v_blk
->blk
, interface
);
75 static void *virtio_blk_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
78 QVirtioBlkPCI
*virtio_blk
= g_new0(QVirtioBlkPCI
, 1);
79 QVirtioBlk
*interface
= &virtio_blk
->blk
;
80 QOSGraphObject
*obj
= &virtio_blk
->pci_vdev
.obj
;
82 virtio_pci_init(&virtio_blk
->pci_vdev
, pci_bus
, addr
);
83 interface
->vdev
= &virtio_blk
->pci_vdev
.vdev
;
85 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_BLOCK
);
87 obj
->get_driver
= qvirtio_blk_pci_get_driver
;
92 static void virtio_blk_register_nodes(void)
94 /* FIXME: every test using these two nodes needs to setup a
95 * -drive,id=drive0 otherwise QEMU is not going to start.
96 * Therefore, we do not include "produces" edge for virtio
100 char *arg
= g_strdup_printf("id=drv0,drive=drive0,addr=%x.%x",
104 .devfn
= QPCI_DEVFN(PCI_SLOT
, PCI_FN
),
107 QOSGraphEdgeOptions opts
= { };
109 /* virtio-blk-device */
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");
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");
125 libqos_init(virtio_blk_register_nodes
);