1 /*
2  * libqos driver framework
3  *
4  * Based on tests/qtest/libqos/virtio-blk.c
5  *
6  * Copyright (c) 2020 Coiby Xu <coiby.xu@gmail.com>
7  *
8  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License version 2.1 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, see <http://www.gnu.org/licenses/>
21  */
22 
23 #include "qemu/osdep.h"
24 #include "libqtest.h"
25 #include "qemu/module.h"
26 #include "standard-headers/linux/virtio_blk.h"
27 #include "vhost-user-blk.h"
28 
29 #define PCI_SLOT                0x04
30 #define PCI_FN                  0x00
31 
32 /* virtio-blk-device */
33 static void *qvhost_user_blk_get_driver(QVhostUserBlk *v_blk,
34                                     const char *interface)
35 {
36     if (!g_strcmp0(interface, "vhost-user-blk")) {
37         return v_blk;
38     }
39     if (!g_strcmp0(interface, "virtio")) {
40         return v_blk->vdev;
41     }
42 
43     fprintf(stderr, "%s not present in vhost-user-blk-device\n", interface);
44     g_assert_not_reached();
45 }
46 
47 static void *qvhost_user_blk_device_get_driver(void *object,
48                                            const char *interface)
49 {
50     QVhostUserBlkDevice *v_blk = object;
51     return qvhost_user_blk_get_driver(&v_blk->blk, interface);
52 }
53 
54 static void *vhost_user_blk_device_create(void *virtio_dev,
55                                       QGuestAllocator *t_alloc,
56                                       void *addr)
57 {
58     QVhostUserBlkDevice *vhost_user_blk = g_new0(QVhostUserBlkDevice, 1);
59     QVhostUserBlk *interface = &vhost_user_blk->blk;
60 
61     interface->vdev = virtio_dev;
62 
63     vhost_user_blk->obj.get_driver = qvhost_user_blk_device_get_driver;
64 
65     return &vhost_user_blk->obj;
66 }
67 
68 /* virtio-blk-pci */
69 static void *qvhost_user_blk_pci_get_driver(void *object, const char *interface)
70 {
71     QVhostUserBlkPCI *v_blk = object;
72     if (!g_strcmp0(interface, "pci-device")) {
73         return v_blk->pci_vdev.pdev;
74     }
75     return qvhost_user_blk_get_driver(&v_blk->blk, interface);
76 }
77 
78 static void *vhost_user_blk_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
79                                       void *addr)
80 {
81     QVhostUserBlkPCI *vhost_user_blk = g_new0(QVhostUserBlkPCI, 1);
82     QVhostUserBlk *interface = &vhost_user_blk->blk;
83     QOSGraphObject *obj = &vhost_user_blk->pci_vdev.obj;
84 
85     virtio_pci_init(&vhost_user_blk->pci_vdev, pci_bus, addr);
86     interface->vdev = &vhost_user_blk->pci_vdev.vdev;
87 
88     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_BLOCK);
89 
90     obj->get_driver = qvhost_user_blk_pci_get_driver;
91 
92     return obj;
93 }
94 
95 static void vhost_user_blk_register_nodes(void)
96 {
97     /*
98      * FIXME: every test using these two nodes needs to setup a
99      * -drive,id=drive0 otherwise QEMU is not going to start.
100      * Therefore, we do not include "produces" edge for virtio
101      * and pci-device yet.
102      */
103 
104     char *arg = g_strdup_printf("id=drv0,chardev=char1,addr=%x.%x",
105                                 PCI_SLOT, PCI_FN);
106 
107     QPCIAddress addr = {
108         .devfn = QPCI_DEVFN(PCI_SLOT, PCI_FN),
109     };
110 
111     QOSGraphEdgeOptions opts = { };
112 
113     /* virtio-blk-device */
114     /** opts.extra_device_opts = "drive=drive0"; */
115     qos_node_create_driver("vhost-user-blk-device",
116                            vhost_user_blk_device_create);
117     qos_node_consumes("vhost-user-blk-device", "virtio-bus", &opts);
118     qos_node_produces("vhost-user-blk-device", "vhost-user-blk");
119 
120     /* virtio-blk-pci */
121     opts.extra_device_opts = arg;
122     add_qpci_address(&opts, &addr);
123     qos_node_create_driver("vhost-user-blk-pci", vhost_user_blk_pci_create);
124     qos_node_consumes("vhost-user-blk-pci", "pci-bus", &opts);
125     qos_node_produces("vhost-user-blk-pci", "vhost-user-blk");
126 
127     g_free(arg);
128 }
129 
130 libqos_init(vhost_user_blk_register_nodes);
131