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.
14 #include "qemu/osdep.h"
16 #include "block/scsi.h"
17 #include "libqos/virtio.h"
18 #include "libqos/virtio-pci.h"
19 #include "libqos/pci-pc.h"
20 #include "libqos/malloc.h"
21 #include "libqos/malloc-pc.h"
22 #include "libqos/malloc-generic.h"
26 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
29 #define MAX_NUM_QUEUES 64
33 QGuestAllocator
*alloc
;
36 QVirtQueue
*vq
[MAX_NUM_QUEUES
+ 2];
45 uint8_t cdb
[CDB_SIZE
];
46 } QEMU_PACKED QVirtIOSCSICmdReq
;
51 uint16_t status_qualifier
;
55 } QEMU_PACKED QVirtIOSCSICmdResp
;
57 static void qvirtio_scsi_start(const char *extra_opts
)
61 cmdline
= g_strdup_printf(
62 "-drive id=drv0,if=none,file=/dev/null,format=raw "
63 "-device virtio-scsi-pci,id=vs0 "
64 "-device scsi-hd,bus=vs0.0,drive=drv0 %s",
70 static void qvirtio_scsi_stop(void)
75 static void qvirtio_scsi_pci_free(QVirtIOSCSI
*vs
)
79 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
80 guest_free(vs
->alloc
, vs
->vq
[i
]->desc
);
82 pc_alloc_uninit(vs
->alloc
);
83 qvirtio_pci_device_disable(container_of(vs
->dev
, QVirtioPCIDevice
, vdev
));
85 qpci_free_pc(vs
->bus
);
88 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI
*vs
, size_t alloc_size
,
93 addr
= guest_alloc(vs
->alloc
, alloc_size
);
95 memwrite(addr
, data
, alloc_size
);
101 static uint8_t virtio_scsi_do_command(QVirtIOSCSI
*vs
, const uint8_t *cdb
,
102 const uint8_t *data_in
,
104 uint8_t *data_out
, size_t data_out_len
,
105 QVirtIOSCSICmdResp
*resp_out
)
108 QVirtIOSCSICmdReq req
= { { 0 } };
109 QVirtIOSCSICmdResp resp
= { .response
= 0xff, .status
= 0xff };
110 uint64_t req_addr
, resp_addr
, data_in_addr
= 0, data_out_addr
= 0;
116 req
.lun
[0] = 1; /* Select LUN */
117 req
.lun
[1] = 1; /* Select target 1 */
118 memcpy(req
.cdb
, cdb
, CDB_SIZE
);
120 /* XXX: Fix endian if any multi-byte field in req/resp is used */
122 /* Add request header */
123 req_addr
= qvirtio_scsi_alloc(vs
, sizeof(req
), &req
);
124 free_head
= qvirtqueue_add(vq
, req_addr
, sizeof(req
), false, true);
127 data_out_addr
= qvirtio_scsi_alloc(vs
, data_out_len
, data_out
);
128 qvirtqueue_add(vq
, data_out_addr
, data_out_len
, false, true);
131 /* Add response header */
132 resp_addr
= qvirtio_scsi_alloc(vs
, sizeof(resp
), &resp
);
133 qvirtqueue_add(vq
, resp_addr
, sizeof(resp
), true, !!data_in_len
);
136 data_in_addr
= qvirtio_scsi_alloc(vs
, data_in_len
, data_in
);
137 qvirtqueue_add(vq
, data_in_addr
, data_in_len
, true, false);
140 qvirtqueue_kick(&qvirtio_pci
, vs
->dev
, vq
, free_head
);
141 qvirtio_wait_queue_isr(&qvirtio_pci
, vs
->dev
, vq
, QVIRTIO_SCSI_TIMEOUT_US
);
143 response
= readb(resp_addr
+ offsetof(QVirtIOSCSICmdResp
, response
));
146 memread(resp_addr
, resp_out
, sizeof(*resp_out
));
149 guest_free(vs
->alloc
, req_addr
);
150 guest_free(vs
->alloc
, resp_addr
);
151 guest_free(vs
->alloc
, data_in_addr
);
152 guest_free(vs
->alloc
, data_out_addr
);
156 static QVirtIOSCSI
*qvirtio_scsi_pci_init(int slot
)
158 const uint8_t test_unit_ready_cdb
[CDB_SIZE
] = {};
160 QVirtioPCIDevice
*dev
;
161 QVirtIOSCSICmdResp resp
;
165 vs
= g_new0(QVirtIOSCSI
, 1);
166 vs
->alloc
= pc_alloc_init();
167 vs
->bus
= qpci_init_pc();
169 dev
= qvirtio_pci_device_find(vs
->bus
, QVIRTIO_SCSI_DEVICE_ID
);
170 vs
->dev
= (QVirtioDevice
*)dev
;
171 g_assert(dev
!= NULL
);
172 g_assert_cmphex(vs
->dev
->device_type
, ==, QVIRTIO_SCSI_DEVICE_ID
);
174 qvirtio_pci_device_enable(dev
);
175 qvirtio_reset(&qvirtio_pci
, vs
->dev
);
176 qvirtio_set_acknowledge(&qvirtio_pci
, vs
->dev
);
177 qvirtio_set_driver(&qvirtio_pci
, vs
->dev
);
179 addr
= dev
->addr
+ QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX
;
180 vs
->num_queues
= qvirtio_config_readl(&qvirtio_pci
, vs
->dev
,
181 (uint64_t)(uintptr_t)addr
);
183 g_assert_cmpint(vs
->num_queues
, <, MAX_NUM_QUEUES
);
185 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
186 vs
->vq
[i
] = qvirtqueue_setup(&qvirtio_pci
, vs
->dev
, vs
->alloc
, i
);
189 /* Clear the POWER ON OCCURRED unit attention */
190 g_assert_cmpint(virtio_scsi_do_command(vs
, test_unit_ready_cdb
,
191 NULL
, 0, NULL
, 0, &resp
),
193 g_assert_cmpint(resp
.status
, ==, CHECK_CONDITION
);
194 g_assert_cmpint(resp
.sense
[0], ==, 0x70); /* Fixed format sense buffer */
195 g_assert_cmpint(resp
.sense
[2], ==, UNIT_ATTENTION
);
196 g_assert_cmpint(resp
.sense
[12], ==, 0x29); /* POWER ON */
197 g_assert_cmpint(resp
.sense
[13], ==, 0x00);
202 /* Tests only initialization so far. TODO: Replace with functional tests */
203 static void pci_nop(void)
205 qvirtio_scsi_start(NULL
);
209 static void hotplug(void)
213 qvirtio_scsi_start("-drive id=drv1,if=none,file=/dev/null,format=raw");
214 response
= qmp("{\"execute\": \"device_add\","
216 " \"driver\": \"scsi-hd\","
217 " \"id\": \"scsi-hd\","
218 " \"drive\": \"drv1\""
222 g_assert(!qdict_haskey(response
, "error"));
225 response
= qmp("{\"execute\": \"device_del\","
227 " \"id\": \"scsi-hd\""
231 g_assert(!qdict_haskey(response
, "error"));
232 g_assert(qdict_haskey(response
, "event"));
233 g_assert(!strcmp(qdict_get_str(response
, "event"), "DEVICE_DELETED"));
238 /* Test WRITE SAME with the lba not aligned */
239 static void test_unaligned_write_same(void)
242 uint8_t buf1
[512] = { 0 };
243 uint8_t buf2
[512] = { 1 };
244 const uint8_t write_same_cdb_1
[CDB_SIZE
] = { 0x41, 0x00, 0x00, 0x00, 0x00,
245 0x01, 0x00, 0x00, 0x02, 0x00 };
246 const uint8_t write_same_cdb_2
[CDB_SIZE
] = { 0x41, 0x00, 0x00, 0x00, 0x00,
247 0x01, 0x00, 0x33, 0x00, 0x00 };
249 qvirtio_scsi_start("-drive file=blkdebug::null-co://,if=none,id=dr1"
250 ",format=raw,file.align=4k "
251 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
252 vs
= qvirtio_scsi_pci_init(PCI_SLOT
);
254 g_assert_cmphex(0, ==,
255 virtio_scsi_do_command(vs
, write_same_cdb_1
, NULL
, 0, buf1
, 512, NULL
));
257 g_assert_cmphex(0, ==,
258 virtio_scsi_do_command(vs
, write_same_cdb_2
, NULL
, 0, buf2
, 512, NULL
));
260 qvirtio_scsi_pci_free(vs
);
264 int main(int argc
, char **argv
)
268 g_test_init(&argc
, &argv
, NULL
);
269 qtest_add_func("/virtio/scsi/pci/nop", pci_nop
);
270 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug
);
271 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
272 test_unaligned_write_same
);