xref: /openbmc/qemu/tests/qtest/libqos/virtio-blk.c (revision 2e3408b3cc7de4e87a9adafc8c19bfce3abec947)
11cf4323eSThomas Huth /*
21cf4323eSThomas Huth  * libqos driver framework
31cf4323eSThomas Huth  *
41cf4323eSThomas Huth  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
51cf4323eSThomas Huth  *
61cf4323eSThomas Huth  * This library is free software; you can redistribute it and/or
71cf4323eSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8dc0ad02dSThomas Huth  * License version 2.1 as published by the Free Software Foundation.
91cf4323eSThomas Huth  *
101cf4323eSThomas Huth  * This library is distributed in the hope that it will be useful,
111cf4323eSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
121cf4323eSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131cf4323eSThomas Huth  * Lesser General Public License for more details.
141cf4323eSThomas Huth  *
151cf4323eSThomas Huth  * You should have received a copy of the GNU Lesser General Public
161cf4323eSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>
171cf4323eSThomas Huth  */
181cf4323eSThomas Huth 
191cf4323eSThomas Huth #include "qemu/osdep.h"
20*907b5105SMarc-André Lureau #include "../libqtest.h"
211cf4323eSThomas Huth #include "qemu/module.h"
221cf4323eSThomas Huth #include "standard-headers/linux/virtio_blk.h"
23a2ce7dbdSPaolo Bonzini #include "qgraph.h"
24a2ce7dbdSPaolo Bonzini #include "virtio-blk.h"
251cf4323eSThomas Huth 
261cf4323eSThomas Huth #define PCI_SLOT                0x04
271cf4323eSThomas Huth #define PCI_FN                  0x00
281cf4323eSThomas Huth 
291cf4323eSThomas Huth /* virtio-blk-device */
qvirtio_blk_get_driver(QVirtioBlk * v_blk,const char * interface)301cf4323eSThomas Huth static void *qvirtio_blk_get_driver(QVirtioBlk *v_blk,
311cf4323eSThomas Huth                                     const char *interface)
321cf4323eSThomas Huth {
331cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio-blk")) {
341cf4323eSThomas Huth         return v_blk;
351cf4323eSThomas Huth     }
361cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio")) {
371cf4323eSThomas Huth         return v_blk->vdev;
381cf4323eSThomas Huth     }
391cf4323eSThomas Huth 
401cf4323eSThomas Huth     fprintf(stderr, "%s not present in virtio-blk-device\n", interface);
411cf4323eSThomas Huth     g_assert_not_reached();
421cf4323eSThomas Huth }
431cf4323eSThomas Huth 
qvirtio_blk_device_get_driver(void * object,const char * interface)441cf4323eSThomas Huth static void *qvirtio_blk_device_get_driver(void *object,
451cf4323eSThomas Huth                                            const char *interface)
461cf4323eSThomas Huth {
471cf4323eSThomas Huth     QVirtioBlkDevice *v_blk = object;
481cf4323eSThomas Huth     return qvirtio_blk_get_driver(&v_blk->blk, interface);
491cf4323eSThomas Huth }
501cf4323eSThomas Huth 
virtio_blk_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)511cf4323eSThomas Huth static void *virtio_blk_device_create(void *virtio_dev,
521cf4323eSThomas Huth                                       QGuestAllocator *t_alloc,
531cf4323eSThomas Huth                                       void *addr)
541cf4323eSThomas Huth {
551cf4323eSThomas Huth     QVirtioBlkDevice *virtio_blk = g_new0(QVirtioBlkDevice, 1);
561cf4323eSThomas Huth     QVirtioBlk *interface = &virtio_blk->blk;
571cf4323eSThomas Huth 
581cf4323eSThomas Huth     interface->vdev = virtio_dev;
591cf4323eSThomas Huth 
601cf4323eSThomas Huth     virtio_blk->obj.get_driver = qvirtio_blk_device_get_driver;
611cf4323eSThomas Huth 
621cf4323eSThomas Huth     return &virtio_blk->obj;
631cf4323eSThomas Huth }
641cf4323eSThomas Huth 
651cf4323eSThomas Huth /* virtio-blk-pci */
qvirtio_blk_pci_get_driver(void * object,const char * interface)661cf4323eSThomas Huth static void *qvirtio_blk_pci_get_driver(void *object, const char *interface)
671cf4323eSThomas Huth {
681cf4323eSThomas Huth     QVirtioBlkPCI *v_blk = object;
691cf4323eSThomas Huth     if (!g_strcmp0(interface, "pci-device")) {
701cf4323eSThomas Huth         return v_blk->pci_vdev.pdev;
711cf4323eSThomas Huth     }
721cf4323eSThomas Huth     return qvirtio_blk_get_driver(&v_blk->blk, interface);
731cf4323eSThomas Huth }
741cf4323eSThomas Huth 
virtio_blk_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)751cf4323eSThomas Huth static void *virtio_blk_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
761cf4323eSThomas Huth                                       void *addr)
771cf4323eSThomas Huth {
781cf4323eSThomas Huth     QVirtioBlkPCI *virtio_blk = g_new0(QVirtioBlkPCI, 1);
791cf4323eSThomas Huth     QVirtioBlk *interface = &virtio_blk->blk;
801cf4323eSThomas Huth     QOSGraphObject *obj = &virtio_blk->pci_vdev.obj;
811cf4323eSThomas Huth 
821cf4323eSThomas Huth     virtio_pci_init(&virtio_blk->pci_vdev, pci_bus, addr);
831cf4323eSThomas Huth     interface->vdev = &virtio_blk->pci_vdev.vdev;
841cf4323eSThomas Huth 
851cf4323eSThomas Huth     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_BLOCK);
861cf4323eSThomas Huth 
871cf4323eSThomas Huth     obj->get_driver = qvirtio_blk_pci_get_driver;
881cf4323eSThomas Huth 
891cf4323eSThomas Huth     return obj;
901cf4323eSThomas Huth }
911cf4323eSThomas Huth 
virtio_blk_register_nodes(void)921cf4323eSThomas Huth static void virtio_blk_register_nodes(void)
931cf4323eSThomas Huth {
941cf4323eSThomas Huth     /* FIXME: every test using these two nodes needs to setup a
951cf4323eSThomas Huth      * -drive,id=drive0 otherwise QEMU is not going to start.
961cf4323eSThomas Huth      * Therefore, we do not include "produces" edge for virtio
971cf4323eSThomas Huth      * and pci-device yet.
981cf4323eSThomas Huth     */
991cf4323eSThomas Huth 
1001cf4323eSThomas Huth     char *arg = g_strdup_printf("id=drv0,drive=drive0,addr=%x.%x",
1011cf4323eSThomas Huth                                 PCI_SLOT, PCI_FN);
1021cf4323eSThomas Huth 
1031cf4323eSThomas Huth     QPCIAddress addr = {
1041cf4323eSThomas Huth         .devfn = QPCI_DEVFN(PCI_SLOT, PCI_FN),
1051cf4323eSThomas Huth     };
1061cf4323eSThomas Huth 
1071cf4323eSThomas Huth     QOSGraphEdgeOptions opts = { };
1081cf4323eSThomas Huth 
1091cf4323eSThomas Huth     /* virtio-blk-device */
1101cf4323eSThomas Huth     opts.extra_device_opts = "drive=drive0";
1111cf4323eSThomas Huth     qos_node_create_driver("virtio-blk-device", virtio_blk_device_create);
1121cf4323eSThomas Huth     qos_node_consumes("virtio-blk-device", "virtio-bus", &opts);
1131cf4323eSThomas Huth     qos_node_produces("virtio-blk-device", "virtio-blk");
1141cf4323eSThomas Huth 
1151cf4323eSThomas Huth     /* virtio-blk-pci */
1161cf4323eSThomas Huth     opts.extra_device_opts = arg;
1171cf4323eSThomas Huth     add_qpci_address(&opts, &addr);
1181cf4323eSThomas Huth     qos_node_create_driver("virtio-blk-pci", virtio_blk_pci_create);
1191cf4323eSThomas Huth     qos_node_consumes("virtio-blk-pci", "pci-bus", &opts);
1201cf4323eSThomas Huth     qos_node_produces("virtio-blk-pci", "virtio-blk");
1211cf4323eSThomas Huth 
1221cf4323eSThomas Huth     g_free(arg);
1231cf4323eSThomas Huth }
1241cf4323eSThomas Huth 
1251cf4323eSThomas Huth libqos_init(virtio_blk_register_nodes);
126