rcu: Use automatic rc_read unlock in core memory/exec code
[qemu/ar7.git] / tests / virtio-scsi-test.c
blob7c8f9b27f8d6cf575adf6dbbedc59623d41c554d
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-single.h"
13 #include "qemu/module.h"
14 #include "scsi/constants.h"
15 #include "libqos/libqos-pc.h"
16 #include "libqos/libqos-spapr.h"
17 #include "libqos/virtio.h"
18 #include "libqos/virtio-pci.h"
19 #include "standard-headers/linux/virtio_ids.h"
20 #include "standard-headers/linux/virtio_pci.h"
21 #include "standard-headers/linux/virtio_scsi.h"
22 #include "libqos/virtio-scsi.h"
23 #include "libqos/qgraph.h"
25 #define PCI_SLOT 0x02
26 #define PCI_FN 0x00
27 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
29 #define MAX_NUM_QUEUES 64
31 typedef struct {
32 QVirtioDevice *dev;
33 int num_queues;
34 QVirtQueue *vq[MAX_NUM_QUEUES + 2];
35 } QVirtioSCSIQueues;
37 static QGuestAllocator *alloc;
39 static void qvirtio_scsi_pci_free(QVirtioSCSIQueues *vs)
41 int i;
43 for (i = 0; i < vs->num_queues + 2; i++) {
44 qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], alloc);
46 g_free(vs);
49 static uint64_t qvirtio_scsi_alloc(QVirtioSCSIQueues *vs, size_t alloc_size,
50 const void *data)
52 uint64_t addr;
54 addr = guest_alloc(alloc, alloc_size);
55 if (data) {
56 memwrite(addr, data, alloc_size);
59 return addr;
62 static uint8_t virtio_scsi_do_command(QVirtioSCSIQueues *vs,
63 const uint8_t *cdb,
64 const uint8_t *data_in,
65 size_t data_in_len,
66 uint8_t *data_out, size_t data_out_len,
67 struct virtio_scsi_cmd_resp *resp_out)
69 QVirtQueue *vq;
70 struct virtio_scsi_cmd_req req = { { 0 } };
71 struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
72 uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
73 uint8_t response;
74 uint32_t free_head;
75 QTestState *qts = global_qtest;
77 vq = vs->vq[2];
79 req.lun[0] = 1; /* Select LUN */
80 req.lun[1] = 1; /* Select target 1 */
81 memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
83 /* XXX: Fix endian if any multi-byte field in req/resp is used */
85 /* Add request header */
86 req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
87 free_head = qvirtqueue_add(qts, vq, req_addr, sizeof(req), false, true);
89 if (data_out_len) {
90 data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
91 qvirtqueue_add(qts, vq, data_out_addr, data_out_len, false, true);
94 /* Add response header */
95 resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
96 qvirtqueue_add(qts, vq, resp_addr, sizeof(resp), true, !!data_in_len);
98 if (data_in_len) {
99 data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
100 qvirtqueue_add(qts, vq, data_in_addr, data_in_len, true, false);
103 qvirtqueue_kick(qts, vs->dev, vq, free_head);
104 qvirtio_wait_used_elem(qts, vs->dev, vq, free_head, NULL,
105 QVIRTIO_SCSI_TIMEOUT_US);
107 response = readb(resp_addr +
108 offsetof(struct virtio_scsi_cmd_resp, response));
110 if (resp_out) {
111 memread(resp_addr, resp_out, sizeof(*resp_out));
114 guest_free(alloc, req_addr);
115 guest_free(alloc, resp_addr);
116 guest_free(alloc, data_in_addr);
117 guest_free(alloc, data_out_addr);
118 return response;
121 static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev)
123 QVirtioSCSIQueues *vs;
124 const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
125 struct virtio_scsi_cmd_resp resp;
126 int i;
128 vs = g_new0(QVirtioSCSIQueues, 1);
129 vs->dev = dev;
130 vs->num_queues = qvirtio_config_readl(dev, 0);
132 g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
134 for (i = 0; i < vs->num_queues + 2; i++) {
135 vs->vq[i] = qvirtqueue_setup(dev, alloc, i);
138 /* Clear the POWER ON OCCURRED unit attention */
139 g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
140 NULL, 0, NULL, 0, &resp),
141 ==, 0);
142 g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
143 g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
144 g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
145 g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
146 g_assert_cmpint(resp.sense[13], ==, 0x00);
148 return vs;
151 static void hotplug(void *obj, void *data, QGuestAllocator *alloc)
153 QTestState *qts = global_qtest;
155 qtest_qmp_device_add(qts, "scsi-hd", "scsihd", "{'drive': 'drv1'}");
156 qtest_qmp_device_del(qts, "scsihd");
159 /* Test WRITE SAME with the lba not aligned */
160 static void test_unaligned_write_same(void *obj, void *data,
161 QGuestAllocator *t_alloc)
163 QVirtioSCSI *scsi = obj;
164 QVirtioSCSIQueues *vs;
165 uint8_t buf1[512] = { 0 };
166 uint8_t buf2[512] = { 1 };
167 const uint8_t write_same_cdb_1[VIRTIO_SCSI_CDB_SIZE] = {
168 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
170 const uint8_t write_same_cdb_2[VIRTIO_SCSI_CDB_SIZE] = {
171 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
173 const uint8_t write_same_cdb_ndob[VIRTIO_SCSI_CDB_SIZE] = {
174 0x41, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
177 alloc = t_alloc;
178 vs = qvirtio_scsi_init(scsi->vdev);
180 g_assert_cmphex(0, ==,
181 virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512,
182 NULL));
184 g_assert_cmphex(0, ==,
185 virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512,
186 NULL));
188 g_assert_cmphex(0, ==,
189 virtio_scsi_do_command(vs, write_same_cdb_ndob, NULL, 0, NULL, 0,
190 NULL));
192 qvirtio_scsi_pci_free(vs);
195 static void test_iothread_attach_node(void *obj, void *data,
196 QGuestAllocator *t_alloc)
198 QVirtioSCSIPCI *scsi_pci = obj;
199 QVirtioSCSI *scsi = &scsi_pci->scsi;
200 QVirtioSCSIQueues *vs;
201 char tmp_path[] = "/tmp/qtest.XXXXXX";
202 int fd;
203 int ret;
205 uint8_t buf[512] = { 0 };
206 const uint8_t write_cdb[VIRTIO_SCSI_CDB_SIZE] = {
207 /* WRITE(10) to LBA 0, transfer length 1 */
208 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
211 alloc = t_alloc;
212 vs = qvirtio_scsi_init(scsi->vdev);
214 /* Create a temporary qcow2 overlay*/
215 fd = mkstemp(tmp_path);
216 g_assert(fd >= 0);
217 close(fd);
219 if (!have_qemu_img()) {
220 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
221 "skipping snapshot test");
222 goto fail;
225 mkqcow2(tmp_path, 64);
227 /* Attach the overlay to the null0 node */
228 qtest_qmp_assert_success(scsi_pci->pci_vdev.pdev->bus->qts,
229 "{'execute': 'blockdev-add', 'arguments': {"
230 " 'driver': 'qcow2', 'node-name': 'overlay',"
231 " 'backing': 'null0', 'file': {"
232 " 'driver': 'file', 'filename': %s}}}",
233 tmp_path);
235 /* Send a request to see if the AioContext is still right */
236 ret = virtio_scsi_do_command(vs, write_cdb, NULL, 0, buf, 512, NULL);
237 g_assert_cmphex(ret, ==, 0);
239 fail:
240 qvirtio_scsi_pci_free(vs);
241 unlink(tmp_path);
244 static void *virtio_scsi_hotplug_setup(GString *cmd_line, void *arg)
246 g_string_append(cmd_line,
247 " -drive id=drv1,if=none,file=null-co://,"
248 "file.read-zeroes=on,format=raw");
249 return arg;
252 static void *virtio_scsi_setup(GString *cmd_line, void *arg)
254 g_string_append(cmd_line,
255 " -drive file=blkdebug::null-co://,"
256 "file.image.read-zeroes=on,"
257 "if=none,id=dr1,format=raw,file.align=4k "
258 "-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
259 return arg;
262 static void *virtio_scsi_setup_iothread(GString *cmd_line, void *arg)
264 g_string_append(cmd_line,
265 " -object iothread,id=thread0"
266 " -blockdev driver=null-co,read-zeroes=on,node-name=null0"
267 " -device scsi-hd,drive=null0");
268 return arg;
271 static void register_virtio_scsi_test(void)
273 QOSGraphTestOptions opts = { };
275 opts.before = virtio_scsi_hotplug_setup;
276 qos_add_test("hotplug", "virtio-scsi", hotplug, &opts);
278 opts.before = virtio_scsi_setup;
279 qos_add_test("unaligned-write-same", "virtio-scsi",
280 test_unaligned_write_same, &opts);
282 opts.before = virtio_scsi_setup_iothread;
283 opts.edge = (QOSGraphEdgeOptions) {
284 .extra_device_opts = "iothread=thread0",
286 qos_add_test("iothread-attach-node", "virtio-scsi-pci",
287 test_iothread_attach_node, &opts);
290 libqos_init(register_virtio_scsi_test);