Merge remote-tracking branch 'remotes/amit-migration/tags/migration-for-2.7-5' into...
[qemu/kevin.git] / tests / virtio-scsi-test.c
blob5f1a8aefeb75cfcb9a4c8b86d96f4bcab295f7f7
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"
21 #define PCI_SLOT 0x02
22 #define PCI_FN 0x00
23 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
24 #define CDB_SIZE 32
26 #define MAX_NUM_QUEUES 64
28 typedef struct {
29 QVirtioDevice *dev;
30 QGuestAllocator *alloc;
31 QPCIBus *bus;
32 int num_queues;
33 QVirtQueue *vq[MAX_NUM_QUEUES + 2];
34 } QVirtIOSCSI;
36 typedef struct {
37 uint8_t lun[8];
38 int64_t tag;
39 uint8_t task_attr;
40 uint8_t prio;
41 uint8_t crn;
42 uint8_t cdb[CDB_SIZE];
43 } QEMU_PACKED QVirtIOSCSICmdReq;
45 typedef struct {
46 uint32_t sense_len;
47 uint32_t resid;
48 uint16_t status_qualifier;
49 uint8_t status;
50 uint8_t response;
51 uint8_t sense[96];
52 } QEMU_PACKED QVirtIOSCSICmdResp;
54 static void qvirtio_scsi_start(const char *extra_opts)
56 char *cmdline;
58 cmdline = g_strdup_printf(
59 "-drive id=drv0,if=none,file=/dev/null,format=raw "
60 "-device virtio-scsi-pci,id=vs0 "
61 "-device scsi-hd,bus=vs0.0,drive=drv0 %s",
62 extra_opts ? : "");
63 qtest_start(cmdline);
64 g_free(cmdline);
67 static void qvirtio_scsi_stop(void)
69 qtest_end();
72 static void qvirtio_scsi_pci_free(QVirtIOSCSI *vs)
74 int i;
76 for (i = 0; i < vs->num_queues + 2; i++) {
77 guest_free(vs->alloc, vs->vq[i]->desc);
79 pc_alloc_uninit(vs->alloc);
80 qvirtio_pci_device_disable(container_of(vs->dev, QVirtioPCIDevice, vdev));
81 g_free(vs->dev);
82 qpci_free_pc(vs->bus);
85 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,
86 const void *data)
88 uint64_t addr;
90 addr = guest_alloc(vs->alloc, alloc_size);
91 if (data) {
92 memwrite(addr, data, alloc_size);
95 return addr;
98 static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
99 const uint8_t *data_in,
100 size_t data_in_len,
101 uint8_t *data_out, size_t data_out_len,
102 QVirtIOSCSICmdResp *resp_out)
104 QVirtQueue *vq;
105 QVirtIOSCSICmdReq req = { { 0 } };
106 QVirtIOSCSICmdResp resp = { .response = 0xff, .status = 0xff };
107 uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
108 uint8_t response;
109 uint32_t free_head;
111 vq = vs->vq[2];
113 req.lun[0] = 1; /* Select LUN */
114 req.lun[1] = 1; /* Select target 1 */
115 memcpy(req.cdb, cdb, CDB_SIZE);
117 /* XXX: Fix endian if any multi-byte field in req/resp is used */
119 /* Add request header */
120 req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
121 free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
123 if (data_out_len) {
124 data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
125 qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
128 /* Add response header */
129 resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
130 qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
132 if (data_in_len) {
133 data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
134 qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
137 qvirtqueue_kick(&qvirtio_pci, vs->dev, vq, free_head);
138 qvirtio_wait_queue_isr(&qvirtio_pci, vs->dev, vq, QVIRTIO_SCSI_TIMEOUT_US);
140 response = readb(resp_addr + offsetof(QVirtIOSCSICmdResp, response));
142 if (resp_out) {
143 memread(resp_addr, resp_out, sizeof(*resp_out));
146 guest_free(vs->alloc, req_addr);
147 guest_free(vs->alloc, resp_addr);
148 guest_free(vs->alloc, data_in_addr);
149 guest_free(vs->alloc, data_out_addr);
150 return response;
153 static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
155 const uint8_t test_unit_ready_cdb[CDB_SIZE] = {};
156 QVirtIOSCSI *vs;
157 QVirtioPCIDevice *dev;
158 QVirtIOSCSICmdResp resp;
159 void *addr;
160 int i;
162 vs = g_new0(QVirtIOSCSI, 1);
163 vs->alloc = pc_alloc_init();
164 vs->bus = qpci_init_pc();
166 dev = qvirtio_pci_device_find(vs->bus, QVIRTIO_SCSI_DEVICE_ID);
167 vs->dev = (QVirtioDevice *)dev;
168 g_assert(dev != NULL);
169 g_assert_cmphex(vs->dev->device_type, ==, QVIRTIO_SCSI_DEVICE_ID);
171 qvirtio_pci_device_enable(dev);
172 qvirtio_reset(&qvirtio_pci, vs->dev);
173 qvirtio_set_acknowledge(&qvirtio_pci, vs->dev);
174 qvirtio_set_driver(&qvirtio_pci, vs->dev);
176 addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
177 vs->num_queues = qvirtio_config_readl(&qvirtio_pci, vs->dev,
178 (uint64_t)(uintptr_t)addr);
180 g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
182 for (i = 0; i < vs->num_queues + 2; i++) {
183 vs->vq[i] = qvirtqueue_setup(&qvirtio_pci, vs->dev, vs->alloc, i);
186 /* Clear the POWER ON OCCURRED unit attention */
187 g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
188 NULL, 0, NULL, 0, &resp),
189 ==, 0);
190 g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
191 g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
192 g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
193 g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
194 g_assert_cmpint(resp.sense[13], ==, 0x00);
196 return vs;
199 /* Tests only initialization so far. TODO: Replace with functional tests */
200 static void pci_nop(void)
202 qvirtio_scsi_start(NULL);
203 qvirtio_scsi_stop();
206 static void hotplug(void)
208 QDict *response;
210 qvirtio_scsi_start("-drive id=drv1,if=none,file=/dev/null,format=raw");
211 response = qmp("{\"execute\": \"device_add\","
212 " \"arguments\": {"
213 " \"driver\": \"scsi-hd\","
214 " \"id\": \"scsi-hd\","
215 " \"drive\": \"drv1\""
216 "}}");
218 g_assert(response);
219 g_assert(!qdict_haskey(response, "error"));
220 QDECREF(response);
222 response = qmp("{\"execute\": \"device_del\","
223 " \"arguments\": {"
224 " \"id\": \"scsi-hd\""
225 "}}");
227 g_assert(response);
228 g_assert(!qdict_haskey(response, "error"));
229 g_assert(qdict_haskey(response, "event"));
230 g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
231 QDECREF(response);
232 qvirtio_scsi_stop();
235 /* Test WRITE SAME with the lba not aligned */
236 static void test_unaligned_write_same(void)
238 QVirtIOSCSI *vs;
239 uint8_t buf1[512] = { 0 };
240 uint8_t buf2[512] = { 1 };
241 const uint8_t write_same_cdb_1[CDB_SIZE] = { 0x41, 0x00, 0x00, 0x00, 0x00,
242 0x01, 0x00, 0x00, 0x02, 0x00 };
243 const uint8_t write_same_cdb_2[CDB_SIZE] = { 0x41, 0x00, 0x00, 0x00, 0x00,
244 0x01, 0x00, 0x33, 0x00, 0x00 };
246 qvirtio_scsi_start("-drive file=blkdebug::null-co://,if=none,id=dr1"
247 ",format=raw,file.align=4k "
248 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
249 vs = qvirtio_scsi_pci_init(PCI_SLOT);
251 g_assert_cmphex(0, ==,
252 virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512, NULL));
254 g_assert_cmphex(0, ==,
255 virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512, NULL));
257 qvirtio_scsi_pci_free(vs);
258 qvirtio_scsi_stop();
261 int main(int argc, char **argv)
263 int ret;
265 g_test_init(&argc, &argv, NULL);
266 qtest_add_func("/virtio/scsi/pci/nop", pci_nop);
267 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug);
268 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
269 test_unaligned_write_same);
271 ret = g_test_run();
273 return ret;