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 "standard-headers/linux/virtio_blk.h" 22 #include "libqos/qgraph.h" 23 #include "libqos/virtio-blk.h" 24 25 #define PCI_SLOT 0x04 26 #define PCI_FN 0x00 27 28 /* virtio-blk-device */ 29 static void *qvirtio_blk_get_driver(QVirtioBlk *v_blk, 30 const char *interface) 31 { 32 if (!g_strcmp0(interface, "virtio-blk")) { 33 return v_blk; 34 } 35 if (!g_strcmp0(interface, "virtio")) { 36 return v_blk->vdev; 37 } 38 39 fprintf(stderr, "%s not present in virtio-blk-device\n", interface); 40 g_assert_not_reached(); 41 } 42 43 static void *qvirtio_blk_device_get_driver(void *object, 44 const char *interface) 45 { 46 QVirtioBlkDevice *v_blk = object; 47 return qvirtio_blk_get_driver(&v_blk->blk, interface); 48 } 49 50 static void *virtio_blk_device_create(void *virtio_dev, 51 QGuestAllocator *t_alloc, 52 void *addr) 53 { 54 QVirtioBlkDevice *virtio_blk = g_new0(QVirtioBlkDevice, 1); 55 QVirtioBlk *interface = &virtio_blk->blk; 56 57 interface->vdev = virtio_dev; 58 59 virtio_blk->obj.get_driver = qvirtio_blk_device_get_driver; 60 61 return &virtio_blk->obj; 62 } 63 64 /* virtio-blk-pci */ 65 static void *qvirtio_blk_pci_get_driver(void *object, const char *interface) 66 { 67 QVirtioBlkPCI *v_blk = object; 68 if (!g_strcmp0(interface, "pci-device")) { 69 return v_blk->pci_vdev.pdev; 70 } 71 return qvirtio_blk_get_driver(&v_blk->blk, interface); 72 } 73 74 static void *virtio_blk_pci_create(void *pci_bus, QGuestAllocator *t_alloc, 75 void *addr) 76 { 77 QVirtioBlkPCI *virtio_blk = g_new0(QVirtioBlkPCI, 1); 78 QVirtioBlk *interface = &virtio_blk->blk; 79 QOSGraphObject *obj = &virtio_blk->pci_vdev.obj; 80 81 virtio_pci_init(&virtio_blk->pci_vdev, pci_bus, addr); 82 interface->vdev = &virtio_blk->pci_vdev.vdev; 83 84 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_BLOCK); 85 86 obj->get_driver = qvirtio_blk_pci_get_driver; 87 88 return obj; 89 } 90 91 static void virtio_blk_register_nodes(void) 92 { 93 /* FIXME: every test using these two nodes needs to setup a 94 * -drive,id=drive0 otherwise QEMU is not going to start. 95 * Therefore, we do not include "produces" edge for virtio 96 * and pci-device yet. 97 */ 98 99 char *arg = g_strdup_printf("id=drv0,drive=drive0,addr=%x.%x", 100 PCI_SLOT, PCI_FN); 101 102 QPCIAddress addr = { 103 .devfn = QPCI_DEVFN(PCI_SLOT, PCI_FN), 104 }; 105 106 QOSGraphEdgeOptions opts = { }; 107 108 /* virtio-blk-device */ 109 opts.extra_device_opts = "drive=drive0"; 110 qos_node_create_driver("virtio-blk-device", virtio_blk_device_create); 111 qos_node_consumes("virtio-blk-device", "virtio-bus", &opts); 112 qos_node_produces("virtio-blk-device", "virtio-blk"); 113 114 /* virtio-blk-pci */ 115 opts.extra_device_opts = arg; 116 add_qpci_address(&opts, &addr); 117 qos_node_create_driver("virtio-blk-pci", virtio_blk_pci_create); 118 qos_node_consumes("virtio-blk-pci", "pci-bus", &opts); 119 qos_node_produces("virtio-blk-pci", "virtio-blk"); 120 121 g_free(arg); 122 } 123 124 libqos_init(virtio_blk_register_nodes); 125