MAINTAINERS: list smbios maintainers
[qemu.git] / tests / virtio-scsi-test.c
blob11ccdd632eb9d238fff8dd5994668f8779e1be88
1 /*
2 * QTest testcase for VirtIO SCSI
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 * Copyright (c) 2015 Red Hat Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include <glib.h>
12 #include <string.h>
13 #include "libqtest.h"
14 #include "qemu/osdep.h"
15 #include <stdio.h>
16 #include "libqos/virtio.h"
17 #include "libqos/virtio-pci.h"
18 #include "libqos/pci-pc.h"
19 #include "libqos/malloc.h"
20 #include "libqos/malloc-pc.h"
21 #include "libqos/malloc-generic.h"
23 #define PCI_SLOT 0x02
24 #define PCI_FN 0x00
25 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
26 #define CDB_SIZE 32
28 #define MAX_NUM_QUEUES 64
30 typedef struct {
31 QVirtioDevice *dev;
32 QGuestAllocator *alloc;
33 QPCIBus *bus;
34 int num_queues;
35 QVirtQueue *vq[MAX_NUM_QUEUES + 2];
36 } QVirtIOSCSI;
38 typedef struct {
39 uint8_t lun[8];
40 int64_t tag;
41 uint8_t task_attr;
42 uint8_t prio;
43 uint8_t crn;
44 uint8_t cdb[CDB_SIZE];
45 } QEMU_PACKED QVirtIOSCSICmdReq;
47 typedef struct {
48 uint32_t sense_len;
49 uint32_t resid;
50 uint16_t status_qualifier;
51 uint8_t status;
52 uint8_t response;
53 uint8_t sense[96];
54 } QEMU_PACKED QVirtIOSCSICmdResp;
56 static void qvirtio_scsi_start(const char *extra_opts)
58 char *cmdline;
60 cmdline = g_strdup_printf(
61 "-drive id=drv0,if=none,file=/dev/null,format=raw "
62 "-device virtio-scsi-pci,id=vs0 "
63 "-device scsi-hd,bus=vs0.0,drive=drv0 %s",
64 extra_opts ? : "");
65 qtest_start(cmdline);
66 g_free(cmdline);
69 static void qvirtio_scsi_stop(void)
71 qtest_end();
74 static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
76 QVirtIOSCSI *vs;
77 QVirtioPCIDevice *dev;
78 void *addr;
79 int i;
81 vs = g_new0(QVirtIOSCSI, 1);
82 vs->alloc = pc_alloc_init();
83 vs->bus = qpci_init_pc();
85 dev = qvirtio_pci_device_find(vs->bus, QVIRTIO_SCSI_DEVICE_ID);
86 vs->dev = (QVirtioDevice *)dev;
87 g_assert(dev != NULL);
88 g_assert_cmphex(vs->dev->device_type, ==, QVIRTIO_SCSI_DEVICE_ID);
90 qvirtio_pci_device_enable(dev);
91 qvirtio_reset(&qvirtio_pci, vs->dev);
92 qvirtio_set_acknowledge(&qvirtio_pci, vs->dev);
93 qvirtio_set_driver(&qvirtio_pci, vs->dev);
95 addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
96 vs->num_queues = qvirtio_config_readl(&qvirtio_pci, vs->dev,
97 (uint64_t)(uintptr_t)addr);
99 g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
101 for (i = 0; i < vs->num_queues + 2; i++) {
102 vs->vq[i] = qvirtqueue_setup(&qvirtio_pci, vs->dev, vs->alloc, i);
105 return vs;
108 static void qvirtio_scsi_pci_free(QVirtIOSCSI *vs)
110 int i;
112 for (i = 0; i < vs->num_queues + 2; i++) {
113 guest_free(vs->alloc, vs->vq[i]->desc);
115 pc_alloc_uninit(vs->alloc);
116 qvirtio_pci_device_disable(container_of(vs->dev, QVirtioPCIDevice, vdev));
117 g_free(vs->dev);
118 qpci_free_pc(vs->bus);
121 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,
122 const void *data)
124 uint64_t addr;
126 addr = guest_alloc(vs->alloc, alloc_size);
127 if (data) {
128 memwrite(addr, data, alloc_size);
131 return addr;
134 static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
135 const uint8_t *data_in,
136 size_t data_in_len,
137 uint8_t *data_out, size_t data_out_len)
139 QVirtQueue *vq;
140 QVirtIOSCSICmdReq req = { { 0 } };
141 QVirtIOSCSICmdResp resp = { .response = 0xff, .status = 0xff };
142 uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
143 uint8_t response;
144 uint32_t free_head;
146 vq = vs->vq[2];
148 req.lun[0] = 1; /* Select LUN */
149 req.lun[1] = 1; /* Select target 1 */
150 memcpy(req.cdb, cdb, CDB_SIZE);
152 /* XXX: Fix endian if any multi-byte field in req/resp is used */
154 /* Add request header */
155 req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
156 free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
158 if (data_out_len) {
159 data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
160 qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
163 /* Add response header */
164 resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
165 qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
167 if (data_in_len) {
168 data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
169 qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
172 qvirtqueue_kick(&qvirtio_pci, vs->dev, vq, free_head);
173 qvirtio_wait_queue_isr(&qvirtio_pci, vs->dev, vq, QVIRTIO_SCSI_TIMEOUT_US);
175 response = readb(resp_addr + offsetof(QVirtIOSCSICmdResp, response));
177 guest_free(vs->alloc, req_addr);
178 guest_free(vs->alloc, resp_addr);
179 guest_free(vs->alloc, data_in_addr);
180 guest_free(vs->alloc, data_out_addr);
181 return response;
184 /* Tests only initialization so far. TODO: Replace with functional tests */
185 static void pci_nop(void)
187 qvirtio_scsi_start(NULL);
188 qvirtio_scsi_stop();
191 static void hotplug(void)
193 QDict *response;
195 qvirtio_scsi_start("-drive id=drv1,if=none,file=/dev/null,format=raw");
196 response = qmp("{\"execute\": \"device_add\","
197 " \"arguments\": {"
198 " \"driver\": \"scsi-hd\","
199 " \"id\": \"scsi-hd\","
200 " \"drive\": \"drv1\""
201 "}}");
203 g_assert(response);
204 g_assert(!qdict_haskey(response, "error"));
205 QDECREF(response);
207 response = qmp("{\"execute\": \"device_del\","
208 " \"arguments\": {"
209 " \"id\": \"scsi-hd\""
210 "}}");
212 g_assert(response);
213 g_assert(!qdict_haskey(response, "error"));
214 g_assert(qdict_haskey(response, "event"));
215 g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
216 QDECREF(response);
217 qvirtio_scsi_stop();
220 /* Test WRITE SAME with the lba not aligned */
221 static void test_unaligned_write_same(void)
223 QVirtIOSCSI *vs;
224 uint8_t buf[512] = { 0 };
225 const uint8_t write_same_cdb[CDB_SIZE] = { 0x41, 0x00, 0x00, 0x00, 0x00,
226 0x01, 0x00, 0x00, 0x02, 0x00 };
228 qvirtio_scsi_start("-drive file=blkdebug::null-co://,if=none,id=dr1"
229 ",format=raw,file.align=4k "
230 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
231 vs = qvirtio_scsi_pci_init(PCI_SLOT);
233 g_assert_cmphex(0, ==,
234 virtio_scsi_do_command(vs, write_same_cdb, NULL, 0, buf, 512));
236 qvirtio_scsi_pci_free(vs);
237 qvirtio_scsi_stop();
240 int main(int argc, char **argv)
242 int ret;
244 g_test_init(&argc, &argv, NULL);
245 qtest_add_func("/virtio/scsi/pci/nop", pci_nop);
246 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug);
247 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
248 test_unaligned_write_same);
250 ret = g_test_run();
252 return ret;