xref: /openbmc/qemu/tests/qtest/libqos/virtio-scsi.c (revision de15df5e)
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_ids.h"
23 #include "libqos/qgraph.h"
24 #include "libqos/virtio-scsi.h"
25 
26 /* virtio-scsi-device */
27 static void *qvirtio_scsi_get_driver(QVirtioSCSI *v_scsi,
28                                      const char *interface)
29 {
30     if (!g_strcmp0(interface, "virtio-scsi")) {
31         return v_scsi;
32     }
33     if (!g_strcmp0(interface, "virtio")) {
34         return v_scsi->vdev;
35     }
36 
37     fprintf(stderr, "%s not present in virtio-scsi-device\n", interface);
38     g_assert_not_reached();
39 }
40 
41 static void *qvirtio_scsi_device_get_driver(void *object,
42                                             const char *interface)
43 {
44     QVirtioSCSIDevice *v_scsi = object;
45     return qvirtio_scsi_get_driver(&v_scsi->scsi, interface);
46 }
47 
48 static void *virtio_scsi_device_create(void *virtio_dev,
49                                           QGuestAllocator *t_alloc,
50                                           void *addr)
51 {
52     QVirtioSCSIDevice *virtio_bdevice = g_new0(QVirtioSCSIDevice, 1);
53     QVirtioSCSI *interface = &virtio_bdevice->scsi;
54 
55     interface->vdev = virtio_dev;
56 
57     virtio_bdevice->obj.get_driver = qvirtio_scsi_device_get_driver;
58 
59     return &virtio_bdevice->obj;
60 }
61 
62 /* virtio-scsi-pci */
63 static void *qvirtio_scsi_pci_get_driver(void *object,
64                                          const char *interface)
65 {
66     QVirtioSCSIPCI *v_scsi = object;
67     if (!g_strcmp0(interface, "pci-device")) {
68         return v_scsi->pci_vdev.pdev;
69     }
70     return qvirtio_scsi_get_driver(&v_scsi->scsi, interface);
71 }
72 
73 static void *virtio_scsi_pci_create(void *pci_bus,
74                                     QGuestAllocator *t_alloc,
75                                     void *addr)
76 {
77     QVirtioSCSIPCI *virtio_spci = g_new0(QVirtioSCSIPCI, 1);
78     QVirtioSCSI *interface = &virtio_spci->scsi;
79     QOSGraphObject *obj = &virtio_spci->pci_vdev.obj;
80 
81     virtio_pci_init(&virtio_spci->pci_vdev, pci_bus, addr);
82     interface->vdev = &virtio_spci->pci_vdev.vdev;
83 
84     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_SCSI);
85 
86     obj->get_driver = qvirtio_scsi_pci_get_driver;
87 
88     return obj;
89 }
90 
91 static void virtio_scsi_register_nodes(void)
92 {
93     QPCIAddress addr = {
94         .devfn = QPCI_DEVFN(4, 0),
95     };
96 
97     QOSGraphEdgeOptions opts = {
98         .before_cmd_line = "-drive id=drv0,if=none,file=null-co://,"
99                            "file.read-zeroes=on,format=raw",
100         .after_cmd_line = "-device scsi-hd,bus=vs0.0,drive=drv0",
101     };
102 
103     /* virtio-scsi-device */
104     opts.extra_device_opts = "id=vs0";
105     qos_node_create_driver("virtio-scsi-device",
106                             virtio_scsi_device_create);
107     qos_node_consumes("virtio-scsi-device", "virtio-bus", &opts);
108     qos_node_produces("virtio-scsi-device", "virtio-scsi");
109 
110     /* virtio-scsi-pci */
111     opts.extra_device_opts = "id=vs0,addr=04.0";
112     add_qpci_address(&opts, &addr);
113     qos_node_create_driver("virtio-scsi-pci", virtio_scsi_pci_create);
114     qos_node_consumes("virtio-scsi-pci", "pci-bus", &opts);
115     qos_node_produces("virtio-scsi-pci", "pci-device");
116     qos_node_produces("virtio-scsi-pci", "virtio-scsi");
117 }
118 
119 libqos_init(virtio_scsi_register_nodes);
120