Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / tests / virtio-scsi-test.c
blobf1489e68a0c4eeaddfc623d101156ae0ddefb739
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 "qemu/osdep.h"
12 #include "libqtest.h"
13 #include "block/scsi.h"
14 #include "libqos/virtio.h"
15 #include "libqos/virtio-pci.h"
16 #include "libqos/pci-pc.h"
17 #include "libqos/malloc.h"
18 #include "libqos/malloc-pc.h"
19 #include "libqos/malloc-generic.h"
20 #include "standard-headers/linux/virtio_ids.h"
21 #include "standard-headers/linux/virtio_pci.h"
22 #include "standard-headers/linux/virtio_scsi.h"
24 #define PCI_SLOT 0x02
25 #define PCI_FN 0x00
26 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
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 static void qvirtio_scsi_start(const char *extra_opts)
40 char *cmdline;
42 cmdline = g_strdup_printf(
43 "-drive id=drv0,if=none,file=/dev/null,format=raw "
44 "-device virtio-scsi-pci,id=vs0 "
45 "-device scsi-hd,bus=vs0.0,drive=drv0 %s",
46 extra_opts ? : "");
47 qtest_start(cmdline);
48 g_free(cmdline);
51 static void qvirtio_scsi_stop(void)
53 qtest_end();
56 static void qvirtio_scsi_pci_free(QVirtIOSCSI *vs)
58 int i;
60 for (i = 0; i < vs->num_queues + 2; i++) {
61 qvirtqueue_cleanup(&qvirtio_pci, vs->vq[i], vs->alloc);
63 pc_alloc_uninit(vs->alloc);
64 qvirtio_pci_device_disable(container_of(vs->dev, QVirtioPCIDevice, vdev));
65 g_free(vs->dev);
66 qpci_free_pc(vs->bus);
69 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,
70 const void *data)
72 uint64_t addr;
74 addr = guest_alloc(vs->alloc, alloc_size);
75 if (data) {
76 memwrite(addr, data, alloc_size);
79 return addr;
82 static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
83 const uint8_t *data_in,
84 size_t data_in_len,
85 uint8_t *data_out, size_t data_out_len,
86 struct virtio_scsi_cmd_resp *resp_out)
88 QVirtQueue *vq;
89 struct virtio_scsi_cmd_req req = { { 0 } };
90 struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
91 uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
92 uint8_t response;
93 uint32_t free_head;
95 vq = vs->vq[2];
97 req.lun[0] = 1; /* Select LUN */
98 req.lun[1] = 1; /* Select target 1 */
99 memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
101 /* XXX: Fix endian if any multi-byte field in req/resp is used */
103 /* Add request header */
104 req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
105 free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
107 if (data_out_len) {
108 data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
109 qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
112 /* Add response header */
113 resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
114 qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
116 if (data_in_len) {
117 data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
118 qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
121 qvirtqueue_kick(&qvirtio_pci, vs->dev, vq, free_head);
122 qvirtio_wait_queue_isr(&qvirtio_pci, vs->dev, vq, QVIRTIO_SCSI_TIMEOUT_US);
124 response = readb(resp_addr +
125 offsetof(struct virtio_scsi_cmd_resp, response));
127 if (resp_out) {
128 memread(resp_addr, resp_out, sizeof(*resp_out));
131 guest_free(vs->alloc, req_addr);
132 guest_free(vs->alloc, resp_addr);
133 guest_free(vs->alloc, data_in_addr);
134 guest_free(vs->alloc, data_out_addr);
135 return response;
138 static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
140 const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
141 QVirtIOSCSI *vs;
142 QVirtioPCIDevice *dev;
143 struct virtio_scsi_cmd_resp resp;
144 void *addr;
145 int i;
147 vs = g_new0(QVirtIOSCSI, 1);
148 vs->alloc = pc_alloc_init();
149 vs->bus = qpci_init_pc();
151 dev = qvirtio_pci_device_find(vs->bus, VIRTIO_ID_SCSI);
152 vs->dev = (QVirtioDevice *)dev;
153 g_assert(dev != NULL);
154 g_assert_cmphex(vs->dev->device_type, ==, VIRTIO_ID_SCSI);
156 qvirtio_pci_device_enable(dev);
157 qvirtio_reset(&qvirtio_pci, vs->dev);
158 qvirtio_set_acknowledge(&qvirtio_pci, vs->dev);
159 qvirtio_set_driver(&qvirtio_pci, vs->dev);
161 addr = dev->addr + VIRTIO_PCI_CONFIG_OFF(false);
162 vs->num_queues = qvirtio_config_readl(&qvirtio_pci, vs->dev,
163 (uint64_t)(uintptr_t)addr);
165 g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
167 for (i = 0; i < vs->num_queues + 2; i++) {
168 vs->vq[i] = qvirtqueue_setup(&qvirtio_pci, vs->dev, vs->alloc, i);
171 /* Clear the POWER ON OCCURRED unit attention */
172 g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
173 NULL, 0, NULL, 0, &resp),
174 ==, 0);
175 g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
176 g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
177 g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
178 g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
179 g_assert_cmpint(resp.sense[13], ==, 0x00);
181 return vs;
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 buf1[512] = { 0 };
225 uint8_t buf2[512] = { 1 };
226 const uint8_t write_same_cdb_1[VIRTIO_SCSI_CDB_SIZE] = {
227 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
229 const uint8_t write_same_cdb_2[VIRTIO_SCSI_CDB_SIZE] = {
230 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
233 qvirtio_scsi_start("-drive file=blkdebug::null-co://,if=none,id=dr1"
234 ",format=raw,file.align=4k "
235 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
236 vs = qvirtio_scsi_pci_init(PCI_SLOT);
238 g_assert_cmphex(0, ==,
239 virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512, NULL));
241 g_assert_cmphex(0, ==,
242 virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512, NULL));
244 qvirtio_scsi_pci_free(vs);
245 qvirtio_scsi_stop();
248 int main(int argc, char **argv)
250 g_test_init(&argc, &argv, NULL);
251 qtest_add_func("/virtio/scsi/pci/nop", pci_nop);
252 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug);
253 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
254 test_unaligned_write_same);
256 return g_test_run();