blob: d32c953a3824e429b7d78f761f30995afad7e06b [file] [log] [blame]
Andreas Färberfc967792014-02-21 22:19:43 +01001/*
2 * QTest testcase for NVMe
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
Andreas Färberfc967792014-02-21 22:19:43 +010010#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020011#include "qemu/module.h"
Paolo Bonzini87ad8602018-11-20 19:41:48 +010012#include "qemu/units.h"
Paolo Bonzinia2ce7db2020-08-04 20:00:40 +020013#include "libqos/libqtest.h"
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020014#include "libqos/qgraph.h"
15#include "libqos/pci.h"
Andreas Färberfc967792014-02-21 22:19:43 +010016
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020017typedef struct QNvme QNvme;
18
19struct QNvme {
20 QOSGraphObject obj;
21 QPCIDevice dev;
22};
23
24static void *nvme_get_driver(void *obj, const char *interface)
Paolo Bonzini87ad8602018-11-20 19:41:48 +010025{
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020026 QNvme *nvme = obj;
Paolo Bonzini87ad8602018-11-20 19:41:48 +010027
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020028 if (!g_strcmp0(interface, "pci-device")) {
29 return &nvme->dev;
Paolo Bonzini87ad8602018-11-20 19:41:48 +010030 }
31
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020032 fprintf(stderr, "%s not present in nvme\n", interface);
33 g_assert_not_reached();
Paolo Bonzini87ad8602018-11-20 19:41:48 +010034}
35
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020036static void *nvme_create(void *pci_bus, QGuestAllocator *alloc, void *addr)
Paolo Bonzini87ad8602018-11-20 19:41:48 +010037{
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020038 QNvme *nvme = g_new0(QNvme, 1);
39 QPCIBus *bus = pci_bus;
40
41 qpci_device_init(&nvme->dev, bus, addr);
42 nvme->obj.get_driver = nvme_get_driver;
43
44 return &nvme->obj;
Paolo Bonzini87ad8602018-11-20 19:41:48 +010045}
46
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020047/* This used to cause a NULL pointer dereference. */
48static void nvmetest_oob_cmb_test(void *obj, void *data, QGuestAllocator *alloc)
Paolo Bonzini87ad8602018-11-20 19:41:48 +010049{
50 const int cmb_bar_size = 2 * MiB;
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020051 QNvme *nvme = obj;
52 QPCIDevice *pdev = &nvme->dev;
Paolo Bonzini87ad8602018-11-20 19:41:48 +010053 QPCIBar bar;
54
Paolo Bonzini87ad8602018-11-20 19:41:48 +010055 qpci_device_enable(pdev);
56 bar = qpci_iomap(pdev, 2, NULL);
57
58 qpci_io_writel(pdev, bar, 0, 0xccbbaa99);
59 g_assert_cmpint(qpci_io_readb(pdev, bar, 0), ==, 0x99);
60 g_assert_cmpint(qpci_io_readw(pdev, bar, 0), ==, 0xaa99);
61
62 /* Test partially out-of-bounds accesses. */
63 qpci_io_writel(pdev, bar, cmb_bar_size - 1, 0x44332211);
64 g_assert_cmpint(qpci_io_readb(pdev, bar, cmb_bar_size - 1), ==, 0x11);
65 g_assert_cmpint(qpci_io_readw(pdev, bar, cmb_bar_size - 1), !=, 0x2211);
66 g_assert_cmpint(qpci_io_readl(pdev, bar, cmb_bar_size - 1), !=, 0x44332211);
Andreas Färberfc967792014-02-21 22:19:43 +010067}
68
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020069static void nvme_register_nodes(void)
Andreas Färberfc967792014-02-21 22:19:43 +010070{
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020071 QOSGraphEdgeOptions opts = {
72 .extra_device_opts = "addr=04.0,drive=drv0,serial=foo",
Andrey Shinkevichca1ef1e2019-07-29 15:46:00 +030073 .before_cmd_line = "-drive id=drv0,if=none,file=null-co://,"
74 "file.read-zeroes=on,format=raw",
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020075 };
Andreas Färberfc967792014-02-21 22:19:43 +010076
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020077 add_qpci_address(&opts, &(QPCIAddress) { .devfn = QPCI_DEVFN(4, 0) });
78
79 qos_node_create_driver("nvme", nvme_create);
80 qos_node_consumes("nvme", "pci-bus", &opts);
81 qos_node_produces("nvme", "pci-device");
82
83 qos_add_test("oob-cmb-access", "nvme", nvmetest_oob_cmb_test, &(QOSGraphTestOptions) {
84 .edge.extra_device_opts = "cmb_size_mb=2"
85 });
Andreas Färberfc967792014-02-21 22:19:43 +010086}
Emanuele Giuseppe Esposito3157ed32018-08-17 13:16:07 +020087
88libqos_init(nvme_register_nodes);