sd/milkymist-memcard: Fix format string
[qemu/armbru.git] / tests / qtest / libqos / virtio-9p.c
blobc87b56132a3814a1c0b65a1d6922bf884d99d160
1 /*
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"
20 #include "libqtest.h"
21 #include "qemu/module.h"
22 #include "standard-headers/linux/virtio_ids.h"
23 #include "libqos/virtio-9p.h"
24 #include "libqos/qgraph.h"
26 static QGuestAllocator *alloc;
28 static void virtio_9p_cleanup(QVirtio9P *interface)
30 qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
33 static void virtio_9p_setup(QVirtio9P *interface)
35 uint64_t features;
37 features = qvirtio_get_features(interface->vdev);
38 features &= ~(QVIRTIO_F_BAD_FEATURE | (1ull << VIRTIO_RING_F_EVENT_IDX));
39 qvirtio_set_features(interface->vdev, features);
41 interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
42 qvirtio_set_driver_ok(interface->vdev);
45 /* virtio-9p-device */
46 static void virtio_9p_device_destructor(QOSGraphObject *obj)
48 QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
49 QVirtio9P *v9p = &v_9p->v9p;
51 virtio_9p_cleanup(v9p);
54 static void virtio_9p_device_start_hw(QOSGraphObject *obj)
56 QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
57 QVirtio9P *v9p = &v_9p->v9p;
59 virtio_9p_setup(v9p);
62 static void *virtio_9p_get_driver(QVirtio9P *v_9p,
63 const char *interface)
65 if (!g_strcmp0(interface, "virtio-9p")) {
66 return v_9p;
68 if (!g_strcmp0(interface, "virtio")) {
69 return v_9p->vdev;
72 fprintf(stderr, "%s not present in virtio-9p-device\n", interface);
73 g_assert_not_reached();
76 static void *virtio_9p_device_get_driver(void *object, const char *interface)
78 QVirtio9PDevice *v_9p = object;
79 return virtio_9p_get_driver(&v_9p->v9p, interface);
82 static void *virtio_9p_device_create(void *virtio_dev,
83 QGuestAllocator *t_alloc,
84 void *addr)
86 QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1);
87 QVirtio9P *interface = &virtio_device->v9p;
89 interface->vdev = virtio_dev;
90 alloc = t_alloc;
92 virtio_device->obj.destructor = virtio_9p_device_destructor;
93 virtio_device->obj.get_driver = virtio_9p_device_get_driver;
94 virtio_device->obj.start_hw = virtio_9p_device_start_hw;
96 return &virtio_device->obj;
99 /* virtio-9p-pci */
100 static void virtio_9p_pci_destructor(QOSGraphObject *obj)
102 QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
103 QVirtio9P *interface = &v9_pci->v9p;
104 QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj;
106 virtio_9p_cleanup(interface);
107 qvirtio_pci_destructor(pci_vobj);
110 static void virtio_9p_pci_start_hw(QOSGraphObject *obj)
112 QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
113 QVirtio9P *interface = &v9_pci->v9p;
114 QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj;
116 qvirtio_pci_start_hw(pci_vobj);
117 virtio_9p_setup(interface);
120 static void *virtio_9p_pci_get_driver(void *object, const char *interface)
122 QVirtio9PPCI *v_9p = object;
123 if (!g_strcmp0(interface, "pci-device")) {
124 return v_9p->pci_vdev.pdev;
126 return virtio_9p_get_driver(&v_9p->v9p, interface);
129 static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
130 void *addr)
132 QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1);
133 QVirtio9P *interface = &v9_pci->v9p;
134 QOSGraphObject *obj = &v9_pci->pci_vdev.obj;
136 virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr);
137 interface->vdev = &v9_pci->pci_vdev.vdev;
138 alloc = t_alloc;
140 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P);
142 obj->destructor = virtio_9p_pci_destructor;
143 obj->start_hw = virtio_9p_pci_start_hw;
144 obj->get_driver = virtio_9p_pci_get_driver;
146 return obj;
149 static void virtio_9p_register_nodes(void)
151 const char *str_simple = "fsdev=fsdev0,mount_tag=" MOUNT_TAG;
152 const char *str_addr = "fsdev=fsdev0,addr=04.0,mount_tag=" MOUNT_TAG;
154 QPCIAddress addr = {
155 .devfn = QPCI_DEVFN(4, 0),
158 QOSGraphEdgeOptions opts = {
159 .before_cmd_line = "-fsdev synth,id=fsdev0",
162 /* virtio-9p-device */
163 opts.extra_device_opts = str_simple,
164 qos_node_create_driver("virtio-9p-device", virtio_9p_device_create);
165 qos_node_consumes("virtio-9p-device", "virtio-bus", &opts);
166 qos_node_produces("virtio-9p-device", "virtio");
167 qos_node_produces("virtio-9p-device", "virtio-9p");
169 /* virtio-9p-pci */
170 opts.extra_device_opts = str_addr;
171 add_qpci_address(&opts, &addr);
172 qos_node_create_driver("virtio-9p-pci", virtio_9p_pci_create);
173 qos_node_consumes("virtio-9p-pci", "pci-bus", &opts);
174 qos_node_produces("virtio-9p-pci", "pci-device");
175 qos_node_produces("virtio-9p-pci", "virtio");
176 qos_node_produces("virtio-9p-pci", "virtio-9p");
180 libqos_init(virtio_9p_register_nodes);