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