1 /*
2 * libqos driver framework
3 *
4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5 *
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.
9 *
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.
14 *
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/>
17 */
18
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"
25
26 static QGuestAllocator *alloc;
27
virtio_9p_cleanup(QVirtio9P * interface)28 static void virtio_9p_cleanup(QVirtio9P *interface)
29 {
30 qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
31 }
32
virtio_9p_setup(QVirtio9P * interface)33 static void virtio_9p_setup(QVirtio9P *interface)
34 {
35 uint64_t features;
36
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);
40
41 interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
42 qvirtio_set_driver_ok(interface->vdev);
43 }
44
45 /* virtio-9p-device */
virtio_9p_device_destructor(QOSGraphObject * obj)46 static void virtio_9p_device_destructor(QOSGraphObject *obj)
47 {
48 QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
49 QVirtio9P *v9p = &v_9p->v9p;
50
51 virtio_9p_cleanup(v9p);
52 }
53
virtio_9p_device_start_hw(QOSGraphObject * obj)54 static void virtio_9p_device_start_hw(QOSGraphObject *obj)
55 {
56 QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
57 QVirtio9P *v9p = &v_9p->v9p;
58
59 virtio_9p_setup(v9p);
60 }
61
virtio_9p_get_driver(QVirtio9P * v_9p,const char * interface)62 static void *virtio_9p_get_driver(QVirtio9P *v_9p,
63 const char *interface)
64 {
65 if (!g_strcmp0(interface, "virtio-9p")) {
66 return v_9p;
67 }
68 if (!g_strcmp0(interface, "virtio")) {
69 return v_9p->vdev;
70 }
71
72 fprintf(stderr, "%s not present in virtio-9p-device\n", interface);
73 g_assert_not_reached();
74 }
75
virtio_9p_device_get_driver(void * object,const char * interface)76 static void *virtio_9p_device_get_driver(void *object, const char *interface)
77 {
78 QVirtio9PDevice *v_9p = object;
79 return virtio_9p_get_driver(&v_9p->v9p, interface);
80 }
81
virtio_9p_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)82 static void *virtio_9p_device_create(void *virtio_dev,
83 QGuestAllocator *t_alloc,
84 void *addr)
85 {
86 QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1);
87 QVirtio9P *interface = &virtio_device->v9p;
88
89 interface->vdev = virtio_dev;
90 alloc = t_alloc;
91
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;
95
96 return &virtio_device->obj;
97 }
98
99 /* virtio-9p-pci */
virtio_9p_pci_destructor(QOSGraphObject * obj)100 static void virtio_9p_pci_destructor(QOSGraphObject *obj)
101 {
102 QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
103 QVirtio9P *interface = &v9_pci->v9p;
104 QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj;
105
106 virtio_9p_cleanup(interface);
107 qvirtio_pci_destructor(pci_vobj);
108 }
109
virtio_9p_pci_start_hw(QOSGraphObject * obj)110 static void virtio_9p_pci_start_hw(QOSGraphObject *obj)
111 {
112 QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
113 QVirtio9P *interface = &v9_pci->v9p;
114 QOSGraphObject *pci_vobj = &v9_pci->pci_vdev.obj;
115
116 qvirtio_pci_start_hw(pci_vobj);
117 virtio_9p_setup(interface);
118 }
119
virtio_9p_pci_get_driver(void * object,const char * interface)120 static void *virtio_9p_pci_get_driver(void *object, const char *interface)
121 {
122 QVirtio9PPCI *v_9p = object;
123 if (!g_strcmp0(interface, "pci-device")) {
124 return v_9p->pci_vdev.pdev;
125 }
126 return virtio_9p_get_driver(&v_9p->v9p, interface);
127 }
128
virtio_9p_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)129 static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
130 void *addr)
131 {
132 QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1);
133 QVirtio9P *interface = &v9_pci->v9p;
134 QOSGraphObject *obj = &v9_pci->pci_vdev.obj;
135
136 virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr);
137 interface->vdev = &v9_pci->pci_vdev.vdev;
138 alloc = t_alloc;
139
140 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P);
141
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;
145
146 return obj;
147 }
148
virtio_9p_register_nodes(void)149 static void virtio_9p_register_nodes(void)
150 {
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;
153
154 QPCIAddress addr = {
155 .devfn = QPCI_DEVFN(4, 0),
156 };
157
158 QOSGraphEdgeOptions opts = {
159 .before_cmd_line = "-fsdev synth,id=fsdev0",
160 };
161
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");
168
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");
177
178 }
179
180 libqos_init(virtio_9p_register_nodes);
181