Andreas Färber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 1 | /* |
| 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ärber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 10 | #include "qemu/osdep.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 11 | #include "qemu/module.h" |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 12 | #include "qemu/units.h" |
Paolo Bonzini | a2ce7db | 2020-08-04 20:00:40 +0200 | [diff] [blame] | 13 | #include "libqos/libqtest.h" |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 14 | #include "libqos/qgraph.h" |
| 15 | #include "libqos/pci.h" |
Andreas Färber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 16 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 17 | typedef struct QNvme QNvme; |
| 18 | |
| 19 | struct QNvme { |
| 20 | QOSGraphObject obj; |
| 21 | QPCIDevice dev; |
| 22 | }; |
| 23 | |
| 24 | static void *nvme_get_driver(void *obj, const char *interface) |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 25 | { |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 26 | QNvme *nvme = obj; |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 27 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 28 | if (!g_strcmp0(interface, "pci-device")) { |
| 29 | return &nvme->dev; |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 32 | fprintf(stderr, "%s not present in nvme\n", interface); |
| 33 | g_assert_not_reached(); |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 36 | static void *nvme_create(void *pci_bus, QGuestAllocator *alloc, void *addr) |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 37 | { |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 38 | 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 Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 47 | /* This used to cause a NULL pointer dereference. */ |
| 48 | static void nvmetest_oob_cmb_test(void *obj, void *data, QGuestAllocator *alloc) |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 49 | { |
| 50 | const int cmb_bar_size = 2 * MiB; |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 51 | QNvme *nvme = obj; |
| 52 | QPCIDevice *pdev = &nvme->dev; |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 53 | QPCIBar bar; |
| 54 | |
Paolo Bonzini | 87ad860 | 2018-11-20 19:41:48 +0100 | [diff] [blame] | 55 | 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ärber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 69 | static void nvme_register_nodes(void) |
Andreas Färber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 70 | { |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 71 | QOSGraphEdgeOptions opts = { |
| 72 | .extra_device_opts = "addr=04.0,drive=drv0,serial=foo", |
Andrey Shinkevich | ca1ef1e | 2019-07-29 15:46:00 +0300 | [diff] [blame] | 73 | .before_cmd_line = "-drive id=drv0,if=none,file=null-co://," |
| 74 | "file.read-zeroes=on,format=raw", |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 75 | }; |
Andreas Färber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 76 | |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 77 | 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ärber | fc96779 | 2014-02-21 22:19:43 +0100 | [diff] [blame] | 86 | } |
Emanuele Giuseppe Esposito | 3157ed3 | 2018-08-17 13:16:07 +0200 | [diff] [blame] | 87 | |
| 88 | libqos_init(nvme_register_nodes); |