2014-02-21 22:19:43 +01:00
|
|
|
/*
|
|
|
|
* QTest testcase for NVMe
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 SUSE LINUX Products GmbH
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-11-20 19:41:48 +01:00
|
|
|
#include "qemu/units.h"
|
2014-02-21 22:19:43 +01:00
|
|
|
#include "libqtest.h"
|
2018-08-17 13:16:07 +02:00
|
|
|
#include "libqos/qgraph.h"
|
|
|
|
#include "libqos/pci.h"
|
2018-11-20 19:41:48 +01:00
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
typedef struct QNvme QNvme;
|
|
|
|
|
|
|
|
struct QNvme {
|
|
|
|
QOSGraphObject obj;
|
|
|
|
QPCIDevice dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *nvme_get_driver(void *obj, const char *interface)
|
2018-11-20 19:41:48 +01:00
|
|
|
{
|
2018-08-17 13:16:07 +02:00
|
|
|
QNvme *nvme = obj;
|
|
|
|
|
|
|
|
if (!g_strcmp0(interface, "pci-device")) {
|
|
|
|
return &nvme->dev;
|
2018-11-20 19:41:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
fprintf(stderr, "%s not present in nvme\n", interface);
|
|
|
|
g_assert_not_reached();
|
2018-11-20 19:41:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
static void *nvme_create(void *pci_bus, QGuestAllocator *alloc, void *addr)
|
2018-11-20 19:41:48 +01:00
|
|
|
{
|
2018-08-17 13:16:07 +02:00
|
|
|
QNvme *nvme = g_new0(QNvme, 1);
|
|
|
|
QPCIBus *bus = pci_bus;
|
2014-02-21 22:19:43 +01:00
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
qpci_device_init(&nvme->dev, bus, addr);
|
|
|
|
nvme->obj.get_driver = nvme_get_driver;
|
2018-11-20 19:41:48 +01:00
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
return &nvme->obj;
|
2014-02-21 22:19:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
/* This used to cause a NULL pointer dereference. */
|
|
|
|
static void nvmetest_oob_cmb_test(void *obj, void *data, QGuestAllocator *alloc)
|
2014-02-21 22:19:43 +01:00
|
|
|
{
|
2018-11-20 19:41:48 +01:00
|
|
|
const int cmb_bar_size = 2 * MiB;
|
2018-08-17 13:16:07 +02:00
|
|
|
QNvme *nvme = obj;
|
|
|
|
QPCIDevice *pdev = &nvme->dev;
|
2018-11-20 19:41:48 +01:00
|
|
|
QPCIBar bar;
|
2014-02-21 22:19:43 +01:00
|
|
|
|
2018-11-20 19:41:48 +01:00
|
|
|
qpci_device_enable(pdev);
|
|
|
|
bar = qpci_iomap(pdev, 2, NULL);
|
|
|
|
|
|
|
|
qpci_io_writel(pdev, bar, 0, 0xccbbaa99);
|
|
|
|
g_assert_cmpint(qpci_io_readb(pdev, bar, 0), ==, 0x99);
|
|
|
|
g_assert_cmpint(qpci_io_readw(pdev, bar, 0), ==, 0xaa99);
|
|
|
|
|
|
|
|
/* Test partially out-of-bounds accesses. */
|
|
|
|
qpci_io_writel(pdev, bar, cmb_bar_size - 1, 0x44332211);
|
|
|
|
g_assert_cmpint(qpci_io_readb(pdev, bar, cmb_bar_size - 1), ==, 0x11);
|
|
|
|
g_assert_cmpint(qpci_io_readw(pdev, bar, cmb_bar_size - 1), !=, 0x2211);
|
|
|
|
g_assert_cmpint(qpci_io_readl(pdev, bar, cmb_bar_size - 1), !=, 0x44332211);
|
|
|
|
}
|
2014-02-21 22:19:43 +01:00
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
static void nvme_register_nodes(void)
|
2018-11-20 19:41:48 +01:00
|
|
|
{
|
2018-08-17 13:16:07 +02:00
|
|
|
QOSGraphEdgeOptions opts = {
|
|
|
|
.extra_device_opts = "addr=04.0,drive=drv0,serial=foo",
|
|
|
|
.before_cmd_line = "-drive id=drv0,if=none,file=null-co://,format=raw",
|
|
|
|
};
|
|
|
|
|
|
|
|
add_qpci_address(&opts, &(QPCIAddress) { .devfn = QPCI_DEVFN(4, 0) });
|
2014-02-21 22:19:43 +01:00
|
|
|
|
2018-08-17 13:16:07 +02:00
|
|
|
qos_node_create_driver("nvme", nvme_create);
|
|
|
|
qos_node_consumes("nvme", "pci-bus", &opts);
|
|
|
|
qos_node_produces("nvme", "pci-device");
|
|
|
|
|
|
|
|
qos_add_test("oob-cmb-access", "nvme", nvmetest_oob_cmb_test, &(QOSGraphTestOptions) {
|
|
|
|
.edge.extra_device_opts = "cmb_size_mb=2"
|
|
|
|
});
|
2014-02-21 22:19:43 +01:00
|
|
|
}
|
2018-08-17 13:16:07 +02:00
|
|
|
|
|
|
|
libqos_init(nvme_register_nodes);
|