qemu/tests/libqos/virtio-9p.c
<<
>>
Prefs
   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
  26static QGuestAllocator *alloc;
  27
  28static void virtio_9p_cleanup(QVirtio9P *interface)
  29{
  30    qvirtqueue_cleanup(interface->vdev->bus, interface->vq, alloc);
  31}
  32
  33static void virtio_9p_setup(QVirtio9P *interface)
  34{
  35    interface->vq = qvirtqueue_setup(interface->vdev, alloc, 0);
  36    qvirtio_set_driver_ok(interface->vdev);
  37}
  38
  39/* virtio-9p-device */
  40static void virtio_9p_device_destructor(QOSGraphObject *obj)
  41{
  42    QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
  43    QVirtio9P *v9p = &v_9p->v9p;
  44
  45    virtio_9p_cleanup(v9p);
  46}
  47
  48static void virtio_9p_device_start_hw(QOSGraphObject *obj)
  49{
  50    QVirtio9PDevice *v_9p = (QVirtio9PDevice *) obj;
  51    QVirtio9P *v9p = &v_9p->v9p;
  52
  53    virtio_9p_setup(v9p);
  54}
  55
  56static void *virtio_9p_get_driver(QVirtio9P *v_9p,
  57                                         const char *interface)
  58{
  59    if (!g_strcmp0(interface, "virtio-9p")) {
  60        return v_9p;
  61    }
  62    if (!g_strcmp0(interface, "virtio")) {
  63        return v_9p->vdev;
  64    }
  65
  66    fprintf(stderr, "%s not present in virtio-9p-device\n", interface);
  67    g_assert_not_reached();
  68}
  69
  70static void *virtio_9p_device_get_driver(void *object, const char *interface)
  71{
  72    QVirtio9PDevice *v_9p = object;
  73    return virtio_9p_get_driver(&v_9p->v9p, interface);
  74}
  75
  76static void *virtio_9p_device_create(void *virtio_dev,
  77                                     QGuestAllocator *t_alloc,
  78                                     void *addr)
  79{
  80    QVirtio9PDevice *virtio_device = g_new0(QVirtio9PDevice, 1);
  81    QVirtio9P *interface = &virtio_device->v9p;
  82
  83    interface->vdev = virtio_dev;
  84    alloc = t_alloc;
  85
  86    virtio_device->obj.destructor = virtio_9p_device_destructor;
  87    virtio_device->obj.get_driver = virtio_9p_device_get_driver;
  88    virtio_device->obj.start_hw = virtio_9p_device_start_hw;
  89
  90    return &virtio_device->obj;
  91}
  92
  93/* virtio-9p-pci */
  94static void virtio_9p_pci_destructor(QOSGraphObject *obj)
  95{
  96    QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
  97    QVirtio9P *interface = &v9_pci->v9p;
  98    QOSGraphObject *pci_vobj =  &v9_pci->pci_vdev.obj;
  99
 100    virtio_9p_cleanup(interface);
 101    qvirtio_pci_destructor(pci_vobj);
 102}
 103
 104static void virtio_9p_pci_start_hw(QOSGraphObject *obj)
 105{
 106    QVirtio9PPCI *v9_pci = (QVirtio9PPCI *) obj;
 107    QVirtio9P *interface = &v9_pci->v9p;
 108    QOSGraphObject *pci_vobj =  &v9_pci->pci_vdev.obj;
 109
 110    qvirtio_pci_start_hw(pci_vobj);
 111    virtio_9p_setup(interface);
 112}
 113
 114static void *virtio_9p_pci_get_driver(void *object, const char *interface)
 115{
 116    QVirtio9PPCI *v_9p = object;
 117    if (!g_strcmp0(interface, "pci-device")) {
 118        return v_9p->pci_vdev.pdev;
 119    }
 120    return virtio_9p_get_driver(&v_9p->v9p, interface);
 121}
 122
 123static void *virtio_9p_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
 124                                  void *addr)
 125{
 126    QVirtio9PPCI *v9_pci = g_new0(QVirtio9PPCI, 1);
 127    QVirtio9P *interface = &v9_pci->v9p;
 128    QOSGraphObject *obj = &v9_pci->pci_vdev.obj;
 129
 130    virtio_pci_init(&v9_pci->pci_vdev, pci_bus, addr);
 131    interface->vdev = &v9_pci->pci_vdev.vdev;
 132    alloc = t_alloc;
 133
 134    g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_9P);
 135
 136    obj->destructor = virtio_9p_pci_destructor;
 137    obj->start_hw = virtio_9p_pci_start_hw;
 138    obj->get_driver = virtio_9p_pci_get_driver;
 139
 140    return obj;
 141}
 142
 143static void virtio_9p_register_nodes(void)
 144{
 145    const char *str_simple = "fsdev=fsdev0,mount_tag=" MOUNT_TAG;
 146    const char *str_addr = "fsdev=fsdev0,addr=04.0,mount_tag=" MOUNT_TAG;
 147
 148    QPCIAddress addr = {
 149        .devfn = QPCI_DEVFN(4, 0),
 150    };
 151
 152    QOSGraphEdgeOptions opts = {
 153        .before_cmd_line = "-fsdev synth,id=fsdev0",
 154    };
 155
 156    /* virtio-9p-device */
 157    opts.extra_device_opts = str_simple,
 158    qos_node_create_driver("virtio-9p-device", virtio_9p_device_create);
 159    qos_node_consumes("virtio-9p-device", "virtio-bus", &opts);
 160    qos_node_produces("virtio-9p-device", "virtio");
 161    qos_node_produces("virtio-9p-device", "virtio-9p");
 162
 163    /* virtio-9p-pci */
 164    opts.extra_device_opts = str_addr;
 165    add_qpci_address(&opts, &addr);
 166    qos_node_create_driver("virtio-9p-pci", virtio_9p_pci_create);
 167    qos_node_consumes("virtio-9p-pci", "pci-bus", &opts);
 168    qos_node_produces("virtio-9p-pci", "pci-device");
 169    qos_node_produces("virtio-9p-pci", "virtio");
 170    qos_node_produces("virtio-9p-pci", "virtio-9p");
 171
 172}
 173
 174libqos_init(virtio_9p_register_nodes);
 175