cirrus: fix off-by-one in cirrus_bitblt_rop_bkwd_transp_*_16
[qemu.git] / tests / virtio-scsi-test.c
1 /*
2 * QTest testcase for VirtIO SCSI
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 * Copyright (c) 2015 Red Hat Inc.
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
11 #include "qemu/osdep.h"
12 #include "libqtest.h"
13 #include "block/scsi.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
22 #define PCI_SLOT 0x02
23 #define PCI_FN 0x00
24 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
25
26 #define MAX_NUM_QUEUES 64
27
28 typedef struct {
29 QVirtioDevice *dev;
30 QOSState *qs;
31 int num_queues;
32 QVirtQueue *vq[MAX_NUM_QUEUES + 2];
33 } QVirtIOSCSI;
34
35 static QOSState *qvirtio_scsi_start(const char *extra_opts)
36 {
37 const char *arch = qtest_get_arch();
38 const char *cmd = "-drive id=drv0,if=none,file=/dev/null,format=raw "
39 "-device virtio-scsi-pci,id=vs0 "
40 "-device scsi-hd,bus=vs0.0,drive=drv0 %s";
41
42 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
43 return qtest_pc_boot(cmd, extra_opts ? : "");
44 }
45 if (strcmp(arch, "ppc64") == 0) {
46 return qtest_spapr_boot(cmd, extra_opts ? : "");
47 }
48
49 g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
50 exit(EXIT_FAILURE);
51 }
52
53 static void qvirtio_scsi_stop(QOSState *qs)
54 {
55 qtest_shutdown(qs);
56 }
57
58 static void qvirtio_scsi_pci_free(QVirtIOSCSI *vs)
59 {
60 int i;
61
62 for (i = 0; i < vs->num_queues + 2; i++) {
63 qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], vs->qs->alloc);
64 }
65 qvirtio_pci_device_disable(container_of(vs->dev, QVirtioPCIDevice, vdev));
66 qvirtio_pci_device_free((QVirtioPCIDevice *)vs->dev);
67 qvirtio_scsi_stop(vs->qs);
68 g_free(vs);
69 }
70
71 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,
72 const void *data)
73 {
74 uint64_t addr;
75
76 addr = guest_alloc(vs->qs->alloc, alloc_size);
77 if (data) {
78 memwrite(addr, data, alloc_size);
79 }
80
81 return addr;
82 }
83
84 static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
85 const uint8_t *data_in,
86 size_t data_in_len,
87 uint8_t *data_out, size_t data_out_len,
88 struct virtio_scsi_cmd_resp *resp_out)
89 {
90 QVirtQueue *vq;
91 struct virtio_scsi_cmd_req req = { { 0 } };
92 struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
93 uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
94 uint8_t response;
95 uint32_t free_head;
96
97 vq = vs->vq[2];
98
99 req.lun[0] = 1; /* Select LUN */
100 req.lun[1] = 1; /* Select target 1 */
101 memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
102
103 /* XXX: Fix endian if any multi-byte field in req/resp is used */
104
105 /* Add request header */
106 req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
107 free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
108
109 if (data_out_len) {
110 data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
111 qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
112 }
113
114 /* Add response header */
115 resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
116 qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
117
118 if (data_in_len) {
119 data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
120 qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
121 }
122
123 qvirtqueue_kick(vs->dev, vq, free_head);
124 qvirtio_wait_queue_isr(vs->dev, vq, QVIRTIO_SCSI_TIMEOUT_US);
125
126 response = readb(resp_addr +
127 offsetof(struct virtio_scsi_cmd_resp, response));
128
129 if (resp_out) {
130 memread(resp_addr, resp_out, sizeof(*resp_out));
131 }
132
133 guest_free(vs->qs->alloc, req_addr);
134 guest_free(vs->qs->alloc, resp_addr);
135 guest_free(vs->qs->alloc, data_in_addr);
136 guest_free(vs->qs->alloc, data_out_addr);
137 return response;
138 }
139
140 static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
141 {
142 const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
143 QVirtIOSCSI *vs;
144 QVirtioPCIDevice *dev;
145 struct virtio_scsi_cmd_resp resp;
146 int i;
147
148 vs = g_new0(QVirtIOSCSI, 1);
149
150 vs->qs = qvirtio_scsi_start("-drive file=blkdebug::null-co://,"
151 "if=none,id=dr1,format=raw,file.align=4k "
152 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
153 dev = qvirtio_pci_device_find(vs->qs->pcibus, VIRTIO_ID_SCSI);
154 vs->dev = (QVirtioDevice *)dev;
155 g_assert(dev != NULL);
156 g_assert_cmphex(vs->dev->device_type, ==, VIRTIO_ID_SCSI);
157
158 qvirtio_pci_device_enable(dev);
159 qvirtio_reset(vs->dev);
160 qvirtio_set_acknowledge(vs->dev);
161 qvirtio_set_driver(vs->dev);
162
163 vs->num_queues = qvirtio_config_readl(vs->dev, 0);
164
165 g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
166
167 for (i = 0; i < vs->num_queues + 2; i++) {
168 vs->vq[i] = qvirtqueue_setup(vs->dev, vs->qs->alloc, i);
169 }
170
171 /* Clear the POWER ON OCCURRED unit attention */
172 g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
173 NULL, 0, NULL, 0, &resp),
174 ==, 0);
175 g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
176 g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
177 g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
178 g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
179 g_assert_cmpint(resp.sense[13], ==, 0x00);
180
181 return vs;
182 }
183
184 /* Tests only initialization so far. TODO: Replace with functional tests */
185 static void pci_nop(void)
186 {
187 QOSState *qs;
188
189 qs = qvirtio_scsi_start(NULL);
190 qvirtio_scsi_stop(qs);
191 }
192
193 static void hotplug(void)
194 {
195 QDict *response;
196 QOSState *qs;
197
198 qs = qvirtio_scsi_start("-drive id=drv1,if=none,file=/dev/null,format=raw");
199 response = qmp("{\"execute\": \"device_add\","
200 " \"arguments\": {"
201 " \"driver\": \"scsi-hd\","
202 " \"id\": \"scsi-hd\","
203 " \"drive\": \"drv1\""
204 "}}");
205
206 g_assert(response);
207 g_assert(!qdict_haskey(response, "error"));
208 QDECREF(response);
209
210 response = qmp("{\"execute\": \"device_del\","
211 " \"arguments\": {"
212 " \"id\": \"scsi-hd\""
213 "}}");
214
215 g_assert(response);
216 g_assert(!qdict_haskey(response, "error"));
217 g_assert(qdict_haskey(response, "event"));
218 g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
219 QDECREF(response);
220 qvirtio_scsi_stop(qs);
221 }
222
223 /* Test WRITE SAME with the lba not aligned */
224 static void test_unaligned_write_same(void)
225 {
226 QVirtIOSCSI *vs;
227 uint8_t buf1[512] = { 0 };
228 uint8_t buf2[512] = { 1 };
229 const uint8_t write_same_cdb_1[VIRTIO_SCSI_CDB_SIZE] = {
230 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
231 };
232 const uint8_t write_same_cdb_2[VIRTIO_SCSI_CDB_SIZE] = {
233 0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
234 };
235
236 vs = qvirtio_scsi_pci_init(PCI_SLOT);
237
238 g_assert_cmphex(0, ==,
239 virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512, NULL));
240
241 g_assert_cmphex(0, ==,
242 virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512, NULL));
243
244 qvirtio_scsi_pci_free(vs);
245 }
246
247 int main(int argc, char **argv)
248 {
249 g_test_init(&argc, &argv, NULL);
250 qtest_add_func("/virtio/scsi/pci/nop", pci_nop);
251 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug);
252 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
253 test_unaligned_write_same);
254
255 return g_test_run();
256 }