ppc/xive: export the TIMA memory accessors
[qemu.git] / tests / virtio-scsi-test.c
blob162b31c88d6b9c1e4a6645e07541e2212a2e7d01
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 "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"
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 int num_queues;
33 QVirtQueue *vq[MAX_NUM_QUEUES + 2];
34 } QVirtioSCSIQueues;
36 static QGuestAllocator *alloc;
38 static void qvirtio_scsi_pci_free(QVirtioSCSIQueues *vs)
40 int i;
42 for (i = 0; i < vs->num_queues + 2; i++) {
43 qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], alloc);
45 g_free(vs);
48 static uint64_t qvirtio_scsi_alloc(QVirtioSCSIQueues *vs, size_t alloc_size,
49 const void *data)
51 uint64_t addr;
53 addr = guest_alloc(alloc, alloc_size);
54 if (data) {
55 memwrite(addr, data, alloc_size);
58 return addr;
61 static uint8_t virtio_scsi_do_command(QVirtioSCSIQueues *vs,
62 const uint8_t *cdb,
63 const uint8_t *data_in,
64 size_t data_in_len,
65 uint8_t *data_out, size_t data_out_len,
66 struct virtio_scsi_cmd_resp *resp_out)
68 QVirtQueue *vq;
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;
72 uint8_t response;
73 uint32_t free_head;
75 vq = vs->vq[2];
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);
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);
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);
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);
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));
108 if (resp_out) {
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);
116 return response;
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;
124 int i;
126 vs = g_new0(QVirtioSCSIQueues, 1);
127 vs->dev = dev;
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),
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);
146 return vs;
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
173 alloc = t_alloc;
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,
178 NULL));
180 g_assert_cmphex(0, ==,
181 virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512,
182 NULL));
184 g_assert_cmphex(0, ==,
185 virtio_scsi_do_command(vs, write_same_cdb_ndob, NULL, 0, NULL, 0,
186 NULL));
188 qvirtio_scsi_pci_free(vs);
191 static void *virtio_scsi_hotplug_setup(GString *cmd_line, void *arg)
193 g_string_append(cmd_line,
194 " -drive id=drv1,if=none,file=null-co://,format=raw");
195 return arg;
198 static void *virtio_scsi_setup(GString *cmd_line, void *arg)
200 g_string_append(cmd_line,
201 " -drive file=blkdebug::null-co://,"
202 "if=none,id=dr1,format=raw,file.align=4k "
203 "-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
204 return arg;
207 static void register_virtio_scsi_test(void)
209 QOSGraphTestOptions opts = { };
211 opts.before = virtio_scsi_hotplug_setup;
212 qos_add_test("hotplug", "virtio-scsi", hotplug, &opts);
214 opts.before = virtio_scsi_setup;
215 qos_add_test("unaligned-write-same", "virtio-scsi",
216 test_unaligned_write_same, &opts);
219 libqos_init(register_virtio_scsi_test);