xref: /openbmc/qemu/tests/qtest/libqos/virtio-scsi.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_ids.h"
23a2ce7dbdSPaolo Bonzini #include "qgraph.h"
24a2ce7dbdSPaolo Bonzini #include "virtio-scsi.h"
251cf4323eSThomas Huth 
261cf4323eSThomas Huth /* virtio-scsi-device */
qvirtio_scsi_get_driver(QVirtioSCSI * v_scsi,const char * interface)271cf4323eSThomas Huth static void *qvirtio_scsi_get_driver(QVirtioSCSI *v_scsi,
281cf4323eSThomas Huth                                      const char *interface)
291cf4323eSThomas Huth {
301cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio-scsi")) {
311cf4323eSThomas Huth         return v_scsi;
321cf4323eSThomas Huth     }
331cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio")) {
341cf4323eSThomas Huth         return v_scsi->vdev;
351cf4323eSThomas Huth     }
361cf4323eSThomas Huth 
371cf4323eSThomas Huth     fprintf(stderr, "%s not present in virtio-scsi-device\n", interface);
381cf4323eSThomas Huth     g_assert_not_reached();
391cf4323eSThomas Huth }
401cf4323eSThomas Huth 
qvirtio_scsi_device_get_driver(void * object,const char * interface)411cf4323eSThomas Huth static void *qvirtio_scsi_device_get_driver(void *object,
421cf4323eSThomas Huth                                             const char *interface)
431cf4323eSThomas Huth {
441cf4323eSThomas Huth     QVirtioSCSIDevice *v_scsi = object;
451cf4323eSThomas Huth     return qvirtio_scsi_get_driver(&v_scsi->scsi, interface);
461cf4323eSThomas Huth }
471cf4323eSThomas Huth 
virtio_scsi_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)481cf4323eSThomas Huth static void *virtio_scsi_device_create(void *virtio_dev,
491cf4323eSThomas Huth                                           QGuestAllocator *t_alloc,
501cf4323eSThomas Huth                                           void *addr)
511cf4323eSThomas Huth {
521cf4323eSThomas Huth     QVirtioSCSIDevice *virtio_bdevice = g_new0(QVirtioSCSIDevice, 1);
531cf4323eSThomas Huth     QVirtioSCSI *interface = &virtio_bdevice->scsi;
541cf4323eSThomas Huth 
551cf4323eSThomas Huth     interface->vdev = virtio_dev;
561cf4323eSThomas Huth 
571cf4323eSThomas Huth     virtio_bdevice->obj.get_driver = qvirtio_scsi_device_get_driver;
581cf4323eSThomas Huth 
591cf4323eSThomas Huth     return &virtio_bdevice->obj;
601cf4323eSThomas Huth }
611cf4323eSThomas Huth 
621cf4323eSThomas Huth /* virtio-scsi-pci */
qvirtio_scsi_pci_get_driver(void * object,const char * interface)631cf4323eSThomas Huth static void *qvirtio_scsi_pci_get_driver(void *object,
641cf4323eSThomas Huth                                          const char *interface)
651cf4323eSThomas Huth {
661cf4323eSThomas Huth     QVirtioSCSIPCI *v_scsi = object;
671cf4323eSThomas Huth     if (!g_strcmp0(interface, "pci-device")) {
681cf4323eSThomas Huth         return v_scsi->pci_vdev.pdev;
691cf4323eSThomas Huth     }
701cf4323eSThomas Huth     return qvirtio_scsi_get_driver(&v_scsi->scsi, interface);
711cf4323eSThomas Huth }
721cf4323eSThomas Huth 
virtio_scsi_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)731cf4323eSThomas Huth static void *virtio_scsi_pci_create(void *pci_bus,
741cf4323eSThomas Huth                                     QGuestAllocator *t_alloc,
751cf4323eSThomas Huth                                     void *addr)
761cf4323eSThomas Huth {
771cf4323eSThomas Huth     QVirtioSCSIPCI *virtio_spci = g_new0(QVirtioSCSIPCI, 1);
781cf4323eSThomas Huth     QVirtioSCSI *interface = &virtio_spci->scsi;
791cf4323eSThomas Huth     QOSGraphObject *obj = &virtio_spci->pci_vdev.obj;
801cf4323eSThomas Huth 
811cf4323eSThomas Huth     virtio_pci_init(&virtio_spci->pci_vdev, pci_bus, addr);
821cf4323eSThomas Huth     interface->vdev = &virtio_spci->pci_vdev.vdev;
831cf4323eSThomas Huth 
841cf4323eSThomas Huth     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_SCSI);
851cf4323eSThomas Huth 
861cf4323eSThomas Huth     obj->get_driver = qvirtio_scsi_pci_get_driver;
871cf4323eSThomas Huth 
881cf4323eSThomas Huth     return obj;
891cf4323eSThomas Huth }
901cf4323eSThomas Huth 
virtio_scsi_register_nodes(void)911cf4323eSThomas Huth static void virtio_scsi_register_nodes(void)
921cf4323eSThomas Huth {
931cf4323eSThomas Huth     QPCIAddress addr = {
941cf4323eSThomas Huth         .devfn = QPCI_DEVFN(4, 0),
951cf4323eSThomas Huth     };
961cf4323eSThomas Huth 
971cf4323eSThomas Huth     QOSGraphEdgeOptions opts = {
981cf4323eSThomas Huth         .before_cmd_line = "-drive id=drv0,if=none,file=null-co://,"
991cf4323eSThomas Huth                            "file.read-zeroes=on,format=raw",
1001cf4323eSThomas Huth         .after_cmd_line = "-device scsi-hd,bus=vs0.0,drive=drv0",
1011cf4323eSThomas Huth     };
1021cf4323eSThomas Huth 
1031cf4323eSThomas Huth     /* virtio-scsi-device */
1041cf4323eSThomas Huth     opts.extra_device_opts = "id=vs0";
1051cf4323eSThomas Huth     qos_node_create_driver("virtio-scsi-device",
1061cf4323eSThomas Huth                             virtio_scsi_device_create);
1071cf4323eSThomas Huth     qos_node_consumes("virtio-scsi-device", "virtio-bus", &opts);
1081cf4323eSThomas Huth     qos_node_produces("virtio-scsi-device", "virtio-scsi");
1091cf4323eSThomas Huth 
1101cf4323eSThomas Huth     /* virtio-scsi-pci */
1111cf4323eSThomas Huth     opts.extra_device_opts = "id=vs0,addr=04.0";
1121cf4323eSThomas Huth     add_qpci_address(&opts, &addr);
1131cf4323eSThomas Huth     qos_node_create_driver("virtio-scsi-pci", virtio_scsi_pci_create);
1141cf4323eSThomas Huth     qos_node_consumes("virtio-scsi-pci", "pci-bus", &opts);
1151cf4323eSThomas Huth     qos_node_produces("virtio-scsi-pci", "pci-device");
1161cf4323eSThomas Huth     qos_node_produces("virtio-scsi-pci", "virtio-scsi");
1171cf4323eSThomas Huth }
1181cf4323eSThomas Huth 
1191cf4323eSThomas Huth libqos_init(virtio_scsi_register_nodes);
120