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.
11 #include "qemu/osdep.h"
13 #include "scsi/constants.h"
14 #include "libqos/libqos-pc.h"
15 #include "libqos/libqos-spapr.h"
16 #include "libqos/virtio.h"
17 #include "libqos/virtio-pci.h"
18 #include "standard-headers/linux/virtio_ids.h"
19 #include "standard-headers/linux/virtio_pci.h"
20 #include "standard-headers/linux/virtio_scsi.h"
21 #include "libqos/virtio-scsi.h"
22 #include "libqos/qgraph.h"
26 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
28 #define MAX_NUM_QUEUES 64
33 QVirtQueue
*vq
[MAX_NUM_QUEUES
+ 2];
36 static QGuestAllocator
*alloc
;
38 static void qvirtio_scsi_pci_free(QVirtioSCSIQueues
*vs
)
42 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
43 qvirtqueue_cleanup(vs
->dev
->bus
, vs
->vq
[i
], alloc
);
48 static uint64_t qvirtio_scsi_alloc(QVirtioSCSIQueues
*vs
, size_t alloc_size
,
53 addr
= guest_alloc(alloc
, alloc_size
);
55 memwrite(addr
, data
, alloc_size
);
61 static uint8_t virtio_scsi_do_command(QVirtioSCSIQueues
*vs
,
63 const uint8_t *data_in
,
65 uint8_t *data_out
, size_t data_out_len
,
66 struct virtio_scsi_cmd_resp
*resp_out
)
69 struct virtio_scsi_cmd_req req
= { { 0 } };
70 struct virtio_scsi_cmd_resp resp
= { .response
= 0xff, .status
= 0xff };
71 uint64_t req_addr
, resp_addr
, data_in_addr
= 0, data_out_addr
= 0;
77 req
.lun
[0] = 1; /* Select LUN */
78 req
.lun
[1] = 1; /* Select target 1 */
79 memcpy(req
.cdb
, cdb
, VIRTIO_SCSI_CDB_SIZE
);
81 /* XXX: Fix endian if any multi-byte field in req/resp is used */
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);
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);
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
);
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);
101 qvirtqueue_kick(vs
->dev
, vq
, free_head
);
102 qvirtio_wait_used_elem(vs
->dev
, vq
, free_head
, NULL
,
103 QVIRTIO_SCSI_TIMEOUT_US
);
105 response
= readb(resp_addr
+
106 offsetof(struct virtio_scsi_cmd_resp
, response
));
109 memread(resp_addr
, resp_out
, sizeof(*resp_out
));
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
);
119 static QVirtioSCSIQueues
*qvirtio_scsi_init(QVirtioDevice
*dev
)
121 QVirtioSCSIQueues
*vs
;
122 const uint8_t test_unit_ready_cdb
[VIRTIO_SCSI_CDB_SIZE
] = {};
123 struct virtio_scsi_cmd_resp resp
;
126 vs
= g_new0(QVirtioSCSIQueues
, 1);
128 vs
->num_queues
= qvirtio_config_readl(dev
, 0);
130 g_assert_cmpint(vs
->num_queues
, <, MAX_NUM_QUEUES
);
132 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
133 vs
->vq
[i
] = qvirtqueue_setup(dev
, alloc
, i
);
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
),
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);
149 static void hotplug(void *obj
, void *data
, QGuestAllocator
*alloc
)
151 qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drv1'}");
152 qtest_qmp_device_del("scsihd");
155 /* Test WRITE SAME with the lba not aligned */
156 static void test_unaligned_write_same(void *obj
, void *data
,
157 QGuestAllocator
*t_alloc
)
159 QVirtioSCSI
*scsi
= obj
;
160 QVirtioSCSIQueues
*vs
;
161 uint8_t buf1
[512] = { 0 };
162 uint8_t buf2
[512] = { 1 };
163 const uint8_t write_same_cdb_1
[VIRTIO_SCSI_CDB_SIZE
] = {
164 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
166 const uint8_t write_same_cdb_2
[VIRTIO_SCSI_CDB_SIZE
] = {
167 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
169 const uint8_t write_same_cdb_ndob
[VIRTIO_SCSI_CDB_SIZE
] = {
170 0x41, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
174 vs
= qvirtio_scsi_init(scsi
->vdev
);
176 g_assert_cmphex(0, ==,
177 virtio_scsi_do_command(vs
, write_same_cdb_1
, NULL
, 0, buf1
, 512,
180 g_assert_cmphex(0, ==,
181 virtio_scsi_do_command(vs
, write_same_cdb_2
, NULL
, 0, buf2
, 512,
184 g_assert_cmphex(0, ==,
185 virtio_scsi_do_command(vs
, write_same_cdb_ndob
, NULL
, 0, NULL
, 0,
188 qvirtio_scsi_pci_free(vs
);
191 static void test_iothread_attach_node(void *obj
, void *data
,
192 QGuestAllocator
*t_alloc
)
194 QVirtioSCSIPCI
*scsi_pci
= obj
;
195 QVirtioSCSI
*scsi
= &scsi_pci
->scsi
;
196 QVirtioSCSIQueues
*vs
;
197 char tmp_path
[] = "/tmp/qtest.XXXXXX";
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
208 vs
= qvirtio_scsi_init(scsi
->vdev
);
210 /* Create a temporary qcow2 overlay*/
211 fd
= mkstemp(tmp_path
);
215 if (!have_qemu_img()) {
216 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
217 "skipping snapshot test");
221 mkqcow2(tmp_path
, 64);
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
);
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);
234 qvirtio_scsi_pci_free(vs
);
238 static void *virtio_scsi_hotplug_setup(GString
*cmd_line
, void *arg
)
240 g_string_append(cmd_line
,
241 " -drive id=drv1,if=none,file=null-co://,format=raw");
245 static void *virtio_scsi_setup(GString
*cmd_line
, void *arg
)
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");
254 static void *virtio_scsi_setup_iothread(GString
*cmd_line
, void *arg
)
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");
263 static void register_virtio_scsi_test(void)
265 QOSGraphTestOptions opts
= { };
267 opts
.before
= virtio_scsi_hotplug_setup
;
268 qos_add_test("hotplug", "virtio-scsi", hotplug
, &opts
);
270 opts
.before
= virtio_scsi_setup
;
271 qos_add_test("unaligned-write-same", "virtio-scsi",
272 test_unaligned_write_same
, &opts
);
274 opts
.before
= virtio_scsi_setup_iothread
;
275 opts
.edge
= (QOSGraphEdgeOptions
) {
276 .extra_device_opts
= "iothread=thread0",
278 qos_add_test("iothread-attach-node", "virtio-scsi-pci",
279 test_iothread_attach_node
, &opts
);
282 libqos_init(register_virtio_scsi_test
);