]> git.proxmox.com Git - mirror_qemu.git/blame - tests/virtio-scsi-test.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[mirror_qemu.git] / tests / virtio-scsi-test.c
CommitLineData
26c9a015
AF
1/*
2 * QTest testcase for VirtIO SCSI
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
397c767b 5 * Copyright (c) 2015 Red Hat Inc.
26c9a015
AF
6 *
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 */
10
681c28a3 11#include "qemu/osdep.h"
26c9a015 12#include "libqtest.h"
08e2c9f1 13#include "scsi/constants.h"
a980f7f2 14#include "libqos/libqos-pc.h"
30ca440e 15#include "libqos/libqos-spapr.h"
397c767b
FZ
16#include "libqos/virtio.h"
17#include "libqos/virtio-pci.h"
8ac9e205 18#include "standard-headers/linux/virtio_ids.h"
c75f4c06 19#include "standard-headers/linux/virtio_pci.h"
74f079a7 20#include "standard-headers/linux/virtio_scsi.h"
4e200798
EGE
21#include "libqos/virtio-scsi.h"
22#include "libqos/qgraph.h"
397c767b
FZ
23
24#define PCI_SLOT 0x02
25#define PCI_FN 0x00
26#define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
397c767b
FZ
27
28#define MAX_NUM_QUEUES 64
29
30typedef struct {
31 QVirtioDevice *dev;
397c767b
FZ
32 int num_queues;
33 QVirtQueue *vq[MAX_NUM_QUEUES + 2];
4e200798 34} QVirtioSCSIQueues;
397c767b 35
4e200798 36static QGuestAllocator *alloc;
06b008d9 37
4e200798 38static void qvirtio_scsi_pci_free(QVirtioSCSIQueues *vs)
397c767b
FZ
39{
40 int i;
41
42 for (i = 0; i < vs->num_queues + 2; i++) {
4e200798 43 qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], alloc);
397c767b 44 }
f62e0bbb 45 g_free(vs);
397c767b
FZ
46}
47
4e200798 48static uint64_t qvirtio_scsi_alloc(QVirtioSCSIQueues *vs, size_t alloc_size,
397c767b
FZ
49 const void *data)
50{
51 uint64_t addr;
52
4e200798 53 addr = guest_alloc(alloc, alloc_size);
397c767b
FZ
54 if (data) {
55 memwrite(addr, data, alloc_size);
56 }
57
58 return addr;
59}
60
4e200798
EGE
61static uint8_t virtio_scsi_do_command(QVirtioSCSIQueues *vs,
62 const uint8_t *cdb,
397c767b
FZ
63 const uint8_t *data_in,
64 size_t data_in_len,
4bb7b0da 65 uint8_t *data_out, size_t data_out_len,
74f079a7 66 struct virtio_scsi_cmd_resp *resp_out)
397c767b
FZ
67{
68 QVirtQueue *vq;
74f079a7
SH
69 struct virtio_scsi_cmd_req req = { { 0 } };
70 struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
397c767b
FZ
71 uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
72 uint8_t response;
73 uint32_t free_head;
74
75 vq = vs->vq[2];
76
77 req.lun[0] = 1; /* Select LUN */
78 req.lun[1] = 1; /* Select target 1 */
74f079a7 79 memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
397c767b
FZ
80
81 /* XXX: Fix endian if any multi-byte field in req/resp is used */
82
83 /* Add request header */
84 req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
85 free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
86
87 if (data_out_len) {
88 data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
89 qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
90 }
91
92 /* Add response header */
93 resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
94 qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
95
96 if (data_in_len) {
97 data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
98 qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
99 }
100
6b9cdf4c 101 qvirtqueue_kick(vs->dev, vq, free_head);
be3a6781
GK
102 qvirtio_wait_used_elem(vs->dev, vq, free_head, NULL,
103 QVIRTIO_SCSI_TIMEOUT_US);
397c767b 104
74f079a7
SH
105 response = readb(resp_addr +
106 offsetof(struct virtio_scsi_cmd_resp, response));
397c767b 107
4bb7b0da
SH
108 if (resp_out) {
109 memread(resp_addr, resp_out, sizeof(*resp_out));
110 }
111
4e200798
EGE
112 guest_free(alloc, req_addr);
113 guest_free(alloc, resp_addr);
114 guest_free(alloc, data_in_addr);
115 guest_free(alloc, data_out_addr);
397c767b
FZ
116 return response;
117}
118
4e200798 119static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev)
4bb7b0da 120{
4e200798 121 QVirtioSCSIQueues *vs;
74f079a7 122 const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
74f079a7 123 struct virtio_scsi_cmd_resp resp;
4bb7b0da
SH
124 int i;
125
4e200798
EGE
126 vs = g_new0(QVirtioSCSIQueues, 1);
127 vs->dev = dev;
128 vs->num_queues = qvirtio_config_readl(dev, 0);
4bb7b0da
SH
129
130 g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
131
132 for (i = 0; i < vs->num_queues + 2; i++) {
4e200798 133 vs->vq[i] = qvirtqueue_setup(dev, alloc, i);
4bb7b0da
SH
134 }
135
136 /* Clear the POWER ON OCCURRED unit attention */
137 g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
138 NULL, 0, NULL, 0, &resp),
139 ==, 0);
140 g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
141 g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
142 g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
143 g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
144 g_assert_cmpint(resp.sense[13], ==, 0x00);
145
146 return vs;
147}
148
4e200798 149static void hotplug(void *obj, void *data, QGuestAllocator *alloc)
ac2c4946 150{
82cab70b 151 qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drv1'}");
acd80015 152 qtest_qmp_device_del("scsihd");
ac2c4946
IM
153}
154
397c767b 155/* Test WRITE SAME with the lba not aligned */
4e200798
EGE
156static void test_unaligned_write_same(void *obj, void *data,
157 QGuestAllocator *t_alloc)
397c767b 158{
4e200798
EGE
159 QVirtioSCSI *scsi = obj;
160 QVirtioSCSIQueues *vs;
975b6655
FZ
161 uint8_t buf1[512] = { 0 };
162 uint8_t buf2[512] = { 1 };
74f079a7
SH
163 const uint8_t write_same_cdb_1[VIRTIO_SCSI_CDB_SIZE] = {
164 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
165 };
166 const uint8_t write_same_cdb_2[VIRTIO_SCSI_CDB_SIZE] = {
167 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
168 };
4397a018
PB
169 const uint8_t write_same_cdb_ndob[VIRTIO_SCSI_CDB_SIZE] = {
170 0x41, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
171 };
397c767b 172
4e200798
EGE
173 alloc = t_alloc;
174 vs = qvirtio_scsi_init(scsi->vdev);
397c767b
FZ
175
176 g_assert_cmphex(0, ==,
4e200798
EGE
177 virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512,
178 NULL));
975b6655
FZ
179
180 g_assert_cmphex(0, ==,
4e200798
EGE
181 virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512,
182 NULL));
397c767b 183
4397a018 184 g_assert_cmphex(0, ==,
4e200798
EGE
185 virtio_scsi_do_command(vs, write_same_cdb_ndob, NULL, 0, NULL, 0,
186 NULL));
4397a018 187
397c767b 188 qvirtio_scsi_pci_free(vs);
397c767b
FZ
189}
190
edbe36ad
KW
191static void test_iothread_attach_node(void *obj, void *data,
192 QGuestAllocator *t_alloc)
193{
194 QVirtioSCSIPCI *scsi_pci = obj;
195 QVirtioSCSI *scsi = &scsi_pci->scsi;
196 QVirtioSCSIQueues *vs;
197 char tmp_path[] = "/tmp/qtest.XXXXXX";
198 int fd;
199 int ret;
200
201 uint8_t buf[512] = { 0 };
202 const uint8_t write_cdb[VIRTIO_SCSI_CDB_SIZE] = {
203 /* WRITE(10) to LBA 0, transfer length 1 */
204 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
205 };
206
207 alloc = t_alloc;
208 vs = qvirtio_scsi_init(scsi->vdev);
209
210 /* Create a temporary qcow2 overlay*/
211 fd = mkstemp(tmp_path);
212 g_assert(fd >= 0);
213 close(fd);
214
215 if (!have_qemu_img()) {
216 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
217 "skipping snapshot test");
218 goto fail;
219 }
220
221 mkqcow2(tmp_path, 64);
222
223 /* Attach the overlay to the null0 node */
224 qmp_assert_success("{'execute': 'blockdev-add', 'arguments': {"
225 " 'driver': 'qcow2', 'node-name': 'overlay',"
226 " 'backing': 'null0', 'file': {"
227 " 'driver': 'file', 'filename': %s}}}", tmp_path);
228
229 /* Send a request to see if the AioContext is still right */
230 ret = virtio_scsi_do_command(vs, write_cdb, NULL, 0, buf, 512, NULL);
231 g_assert_cmphex(ret, ==, 0);
232
233fail:
234 qvirtio_scsi_pci_free(vs);
235 unlink(tmp_path);
236}
237
4e200798 238static void *virtio_scsi_hotplug_setup(GString *cmd_line, void *arg)
26c9a015 239{
4e200798
EGE
240 g_string_append(cmd_line,
241 " -drive id=drv1,if=none,file=null-co://,format=raw");
242 return arg;
243}
26c9a015 244
4e200798
EGE
245static void *virtio_scsi_setup(GString *cmd_line, void *arg)
246{
247 g_string_append(cmd_line,
248 " -drive file=blkdebug::null-co://,"
249 "if=none,id=dr1,format=raw,file.align=4k "
250 "-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
251 return arg;
26c9a015 252}
4e200798 253
edbe36ad
KW
254static void *virtio_scsi_setup_iothread(GString *cmd_line, void *arg)
255{
256 g_string_append(cmd_line,
257 " -object iothread,id=thread0"
258 " -blockdev driver=null-co,node-name=null0"
259 " -device scsi-hd,drive=null0");
260 return arg;
261}
262
4e200798
EGE
263static void register_virtio_scsi_test(void)
264{
265 QOSGraphTestOptions opts = { };
266
267 opts.before = virtio_scsi_hotplug_setup;
268 qos_add_test("hotplug", "virtio-scsi", hotplug, &opts);
269
270 opts.before = virtio_scsi_setup;
271 qos_add_test("unaligned-write-same", "virtio-scsi",
272 test_unaligned_write_same, &opts);
edbe36ad
KW
273
274 opts.before = virtio_scsi_setup_iothread;
275 opts.edge = (QOSGraphEdgeOptions) {
276 .extra_device_opts = "iothread=thread0",
277 };
278 qos_add_test("iothread-attach-node", "virtio-scsi-pci",
279 test_iothread_attach_node, &opts);
4e200798
EGE
280}
281
282libqos_init(register_virtio_scsi_test);