2014-02-22 01:19:43 +04: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 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2018-11-20 21:41:48 +03:00
|
|
|
#include "qemu/units.h"
|
2022-03-30 12:39:05 +03:00
|
|
|
#include "libqtest.h"
|
2018-08-17 14:16:07 +03:00
|
|
|
#include "libqos/qgraph.h"
|
|
|
|
#include "libqos/pci.h"
|
2024-05-07 17:05:48 +03:00
|
|
|
#include "block/nvme.h"
|
2018-11-20 21:41:48 +03:00
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
typedef struct QNvme QNvme;
|
|
|
|
|
|
|
|
struct QNvme {
|
|
|
|
QOSGraphObject obj;
|
|
|
|
QPCIDevice dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *nvme_get_driver(void *obj, const char *interface)
|
2018-11-20 21:41:48 +03:00
|
|
|
{
|
2018-08-17 14:16:07 +03:00
|
|
|
QNvme *nvme = obj;
|
|
|
|
|
|
|
|
if (!g_strcmp0(interface, "pci-device")) {
|
|
|
|
return &nvme->dev;
|
2018-11-20 21:41:48 +03:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
fprintf(stderr, "%s not present in nvme\n", interface);
|
|
|
|
g_assert_not_reached();
|
2018-11-20 21:41:48 +03:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
static void *nvme_create(void *pci_bus, QGuestAllocator *alloc, void *addr)
|
2018-11-20 21:41:48 +03:00
|
|
|
{
|
2018-08-17 14:16:07 +03:00
|
|
|
QNvme *nvme = g_new0(QNvme, 1);
|
|
|
|
QPCIBus *bus = pci_bus;
|
2014-02-22 01:19:43 +04:00
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
qpci_device_init(&nvme->dev, bus, addr);
|
|
|
|
nvme->obj.get_driver = nvme_get_driver;
|
2018-11-20 21:41:48 +03:00
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
return &nvme->obj;
|
2014-02-22 01:19:43 +04:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
/* This used to cause a NULL pointer dereference. */
|
|
|
|
static void nvmetest_oob_cmb_test(void *obj, void *data, QGuestAllocator *alloc)
|
2014-02-22 01:19:43 +04:00
|
|
|
{
|
2018-11-20 21:41:48 +03:00
|
|
|
const int cmb_bar_size = 2 * MiB;
|
2018-08-17 14:16:07 +03:00
|
|
|
QNvme *nvme = obj;
|
|
|
|
QPCIDevice *pdev = &nvme->dev;
|
2018-11-20 21:41:48 +03:00
|
|
|
QPCIBar bar;
|
2014-02-22 01:19:43 +04:00
|
|
|
|
2018-11-20 21:41:48 +03: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-22 01:19:43 +04:00
|
|
|
|
2021-07-13 20:33:14 +03:00
|
|
|
static void nvmetest_reg_read_test(void *obj, void *data, QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
QNvme *nvme = obj;
|
|
|
|
QPCIDevice *pdev = &nvme->dev;
|
|
|
|
QPCIBar bar;
|
|
|
|
uint32_t cap_lo, cap_hi;
|
|
|
|
uint64_t cap;
|
|
|
|
|
|
|
|
qpci_device_enable(pdev);
|
|
|
|
bar = qpci_iomap(pdev, 0, NULL);
|
|
|
|
|
|
|
|
cap_lo = qpci_io_readl(pdev, bar, 0x0);
|
|
|
|
g_assert_cmpint(NVME_CAP_MQES(cap_lo), ==, 0x7ff);
|
|
|
|
|
|
|
|
cap_hi = qpci_io_readl(pdev, bar, 0x4);
|
|
|
|
g_assert_cmpint(NVME_CAP_MPSMAX((uint64_t)cap_hi << 32), ==, 0x4);
|
|
|
|
|
|
|
|
cap = qpci_io_readq(pdev, bar, 0x0);
|
|
|
|
g_assert_cmpint(NVME_CAP_MQES(cap), ==, 0x7ff);
|
|
|
|
g_assert_cmpint(NVME_CAP_MPSMAX(cap), ==, 0x4);
|
|
|
|
|
|
|
|
qpci_iounmap(pdev, bar);
|
|
|
|
}
|
|
|
|
|
2021-06-18 13:34:31 +03:00
|
|
|
static void nvmetest_pmr_reg_test(void *obj, void *data, QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
QNvme *nvme = obj;
|
|
|
|
QPCIDevice *pdev = &nvme->dev;
|
|
|
|
QPCIBar pmr_bar, nvme_bar;
|
|
|
|
uint32_t pmrcap, pmrsts;
|
|
|
|
|
|
|
|
qpci_device_enable(pdev);
|
|
|
|
pmr_bar = qpci_iomap(pdev, 4, NULL);
|
|
|
|
|
|
|
|
/* Without Enabling PMRCTL check bar enablemet */
|
|
|
|
qpci_io_writel(pdev, pmr_bar, 0, 0xccbbaa99);
|
|
|
|
g_assert_cmpint(qpci_io_readb(pdev, pmr_bar, 0), !=, 0x99);
|
|
|
|
g_assert_cmpint(qpci_io_readw(pdev, pmr_bar, 0), !=, 0xaa99);
|
|
|
|
|
|
|
|
/* Map NVMe Bar Register to Enable the Mem Region */
|
|
|
|
nvme_bar = qpci_iomap(pdev, 0, NULL);
|
|
|
|
|
|
|
|
pmrcap = qpci_io_readl(pdev, nvme_bar, 0xe00);
|
|
|
|
g_assert_cmpint(NVME_PMRCAP_RDS(pmrcap), ==, 0x1);
|
|
|
|
g_assert_cmpint(NVME_PMRCAP_WDS(pmrcap), ==, 0x1);
|
|
|
|
g_assert_cmpint(NVME_PMRCAP_BIR(pmrcap), ==, 0x4);
|
|
|
|
g_assert_cmpint(NVME_PMRCAP_PMRWBM(pmrcap), ==, 0x2);
|
|
|
|
g_assert_cmpint(NVME_PMRCAP_CMSS(pmrcap), ==, 0x1);
|
|
|
|
|
|
|
|
/* Enable PMRCTRL */
|
|
|
|
qpci_io_writel(pdev, nvme_bar, 0xe04, 0x1);
|
|
|
|
|
|
|
|
qpci_io_writel(pdev, pmr_bar, 0, 0x44332211);
|
|
|
|
g_assert_cmpint(qpci_io_readb(pdev, pmr_bar, 0), ==, 0x11);
|
|
|
|
g_assert_cmpint(qpci_io_readw(pdev, pmr_bar, 0), ==, 0x2211);
|
|
|
|
g_assert_cmpint(qpci_io_readl(pdev, pmr_bar, 0), ==, 0x44332211);
|
|
|
|
|
|
|
|
pmrsts = qpci_io_readl(pdev, nvme_bar, 0xe08);
|
|
|
|
g_assert_cmpint(NVME_PMRSTS_NRDY(pmrsts), ==, 0x0);
|
|
|
|
|
|
|
|
/* Disable PMRCTRL */
|
|
|
|
qpci_io_writel(pdev, nvme_bar, 0xe04, 0x0);
|
|
|
|
|
|
|
|
qpci_io_writel(pdev, pmr_bar, 0, 0x88776655);
|
|
|
|
g_assert_cmpint(qpci_io_readb(pdev, pmr_bar, 0), !=, 0x55);
|
|
|
|
g_assert_cmpint(qpci_io_readw(pdev, pmr_bar, 0), !=, 0x6655);
|
|
|
|
g_assert_cmpint(qpci_io_readl(pdev, pmr_bar, 0), !=, 0x88776655);
|
|
|
|
|
|
|
|
pmrsts = qpci_io_readl(pdev, nvme_bar, 0xe08);
|
|
|
|
g_assert_cmpint(NVME_PMRSTS_NRDY(pmrsts), ==, 0x1);
|
|
|
|
|
|
|
|
qpci_iounmap(pdev, nvme_bar);
|
|
|
|
qpci_iounmap(pdev, pmr_bar);
|
|
|
|
}
|
|
|
|
|
2018-08-17 14:16:07 +03:00
|
|
|
static void nvme_register_nodes(void)
|
2018-11-20 21:41:48 +03:00
|
|
|
{
|
2018-08-17 14:16:07 +03:00
|
|
|
QOSGraphEdgeOptions opts = {
|
|
|
|
.extra_device_opts = "addr=04.0,drive=drv0,serial=foo",
|
2019-07-29 15:46:00 +03:00
|
|
|
.before_cmd_line = "-drive id=drv0,if=none,file=null-co://,"
|
2021-06-18 13:34:31 +03:00
|
|
|
"file.read-zeroes=on,format=raw "
|
|
|
|
"-object memory-backend-ram,id=pmr0,"
|
|
|
|
"share=on,size=8",
|
2018-08-17 14:16:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
add_qpci_address(&opts, &(QPCIAddress) { .devfn = QPCI_DEVFN(4, 0) });
|
2014-02-22 01:19:43 +04:00
|
|
|
|
2018-08-17 14:16:07 +03: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"
|
|
|
|
});
|
2021-06-18 13:34:31 +03:00
|
|
|
|
|
|
|
qos_add_test("pmr-test-access", "nvme", nvmetest_pmr_reg_test,
|
|
|
|
&(QOSGraphTestOptions) {
|
|
|
|
.edge.extra_device_opts = "pmrdev=pmr0"
|
|
|
|
});
|
2021-07-13 20:33:14 +03:00
|
|
|
|
|
|
|
qos_add_test("reg-read", "nvme", nvmetest_reg_read_test, NULL);
|
2014-02-22 01:19:43 +04:00
|
|
|
}
|
2018-08-17 14:16:07 +03:00
|
|
|
|
|
|
|
libqos_init(nvme_register_nodes);
|