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 "block/scsi.h"
14 #include "libqos/virtio.h"
15 #include "libqos/virtio-pci.h"
16 #include "libqos/pci-pc.h"
17 #include "libqos/malloc.h"
18 #include "libqos/malloc-pc.h"
19 #include "libqos/malloc-generic.h"
23 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
26 #define MAX_NUM_QUEUES 64
30 QGuestAllocator
*alloc
;
33 QVirtQueue
*vq
[MAX_NUM_QUEUES
+ 2];
42 uint8_t cdb
[CDB_SIZE
];
43 } QEMU_PACKED QVirtIOSCSICmdReq
;
48 uint16_t status_qualifier
;
52 } QEMU_PACKED QVirtIOSCSICmdResp
;
54 static void qvirtio_scsi_start(const char *extra_opts
)
58 cmdline
= g_strdup_printf(
59 "-drive id=drv0,if=none,file=/dev/null,format=raw "
60 "-device virtio-scsi-pci,id=vs0 "
61 "-device scsi-hd,bus=vs0.0,drive=drv0 %s",
67 static void qvirtio_scsi_stop(void)
72 static void qvirtio_scsi_pci_free(QVirtIOSCSI
*vs
)
76 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
77 guest_free(vs
->alloc
, vs
->vq
[i
]->desc
);
79 pc_alloc_uninit(vs
->alloc
);
80 qvirtio_pci_device_disable(container_of(vs
->dev
, QVirtioPCIDevice
, vdev
));
82 qpci_free_pc(vs
->bus
);
85 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI
*vs
, size_t alloc_size
,
90 addr
= guest_alloc(vs
->alloc
, alloc_size
);
92 memwrite(addr
, data
, alloc_size
);
98 static uint8_t virtio_scsi_do_command(QVirtIOSCSI
*vs
, const uint8_t *cdb
,
99 const uint8_t *data_in
,
101 uint8_t *data_out
, size_t data_out_len
,
102 QVirtIOSCSICmdResp
*resp_out
)
105 QVirtIOSCSICmdReq req
= { { 0 } };
106 QVirtIOSCSICmdResp resp
= { .response
= 0xff, .status
= 0xff };
107 uint64_t req_addr
, resp_addr
, data_in_addr
= 0, data_out_addr
= 0;
113 req
.lun
[0] = 1; /* Select LUN */
114 req
.lun
[1] = 1; /* Select target 1 */
115 memcpy(req
.cdb
, cdb
, CDB_SIZE
);
117 /* XXX: Fix endian if any multi-byte field in req/resp is used */
119 /* Add request header */
120 req_addr
= qvirtio_scsi_alloc(vs
, sizeof(req
), &req
);
121 free_head
= qvirtqueue_add(vq
, req_addr
, sizeof(req
), false, true);
124 data_out_addr
= qvirtio_scsi_alloc(vs
, data_out_len
, data_out
);
125 qvirtqueue_add(vq
, data_out_addr
, data_out_len
, false, true);
128 /* Add response header */
129 resp_addr
= qvirtio_scsi_alloc(vs
, sizeof(resp
), &resp
);
130 qvirtqueue_add(vq
, resp_addr
, sizeof(resp
), true, !!data_in_len
);
133 data_in_addr
= qvirtio_scsi_alloc(vs
, data_in_len
, data_in
);
134 qvirtqueue_add(vq
, data_in_addr
, data_in_len
, true, false);
137 qvirtqueue_kick(&qvirtio_pci
, vs
->dev
, vq
, free_head
);
138 qvirtio_wait_queue_isr(&qvirtio_pci
, vs
->dev
, vq
, QVIRTIO_SCSI_TIMEOUT_US
);
140 response
= readb(resp_addr
+ offsetof(QVirtIOSCSICmdResp
, response
));
143 memread(resp_addr
, resp_out
, sizeof(*resp_out
));
146 guest_free(vs
->alloc
, req_addr
);
147 guest_free(vs
->alloc
, resp_addr
);
148 guest_free(vs
->alloc
, data_in_addr
);
149 guest_free(vs
->alloc
, data_out_addr
);
153 static QVirtIOSCSI
*qvirtio_scsi_pci_init(int slot
)
155 const uint8_t test_unit_ready_cdb
[CDB_SIZE
] = {};
157 QVirtioPCIDevice
*dev
;
158 QVirtIOSCSICmdResp resp
;
162 vs
= g_new0(QVirtIOSCSI
, 1);
163 vs
->alloc
= pc_alloc_init();
164 vs
->bus
= qpci_init_pc();
166 dev
= qvirtio_pci_device_find(vs
->bus
, QVIRTIO_SCSI_DEVICE_ID
);
167 vs
->dev
= (QVirtioDevice
*)dev
;
168 g_assert(dev
!= NULL
);
169 g_assert_cmphex(vs
->dev
->device_type
, ==, QVIRTIO_SCSI_DEVICE_ID
);
171 qvirtio_pci_device_enable(dev
);
172 qvirtio_reset(&qvirtio_pci
, vs
->dev
);
173 qvirtio_set_acknowledge(&qvirtio_pci
, vs
->dev
);
174 qvirtio_set_driver(&qvirtio_pci
, vs
->dev
);
176 addr
= dev
->addr
+ QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX
;
177 vs
->num_queues
= qvirtio_config_readl(&qvirtio_pci
, vs
->dev
,
178 (uint64_t)(uintptr_t)addr
);
180 g_assert_cmpint(vs
->num_queues
, <, MAX_NUM_QUEUES
);
182 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
183 vs
->vq
[i
] = qvirtqueue_setup(&qvirtio_pci
, vs
->dev
, vs
->alloc
, i
);
186 /* Clear the POWER ON OCCURRED unit attention */
187 g_assert_cmpint(virtio_scsi_do_command(vs
, test_unit_ready_cdb
,
188 NULL
, 0, NULL
, 0, &resp
),
190 g_assert_cmpint(resp
.status
, ==, CHECK_CONDITION
);
191 g_assert_cmpint(resp
.sense
[0], ==, 0x70); /* Fixed format sense buffer */
192 g_assert_cmpint(resp
.sense
[2], ==, UNIT_ATTENTION
);
193 g_assert_cmpint(resp
.sense
[12], ==, 0x29); /* POWER ON */
194 g_assert_cmpint(resp
.sense
[13], ==, 0x00);
199 /* Tests only initialization so far. TODO: Replace with functional tests */
200 static void pci_nop(void)
202 qvirtio_scsi_start(NULL
);
206 static void hotplug(void)
210 qvirtio_scsi_start("-drive id=drv1,if=none,file=/dev/null,format=raw");
211 response
= qmp("{\"execute\": \"device_add\","
213 " \"driver\": \"scsi-hd\","
214 " \"id\": \"scsi-hd\","
215 " \"drive\": \"drv1\""
219 g_assert(!qdict_haskey(response
, "error"));
222 response
= qmp("{\"execute\": \"device_del\","
224 " \"id\": \"scsi-hd\""
228 g_assert(!qdict_haskey(response
, "error"));
229 g_assert(qdict_haskey(response
, "event"));
230 g_assert(!strcmp(qdict_get_str(response
, "event"), "DEVICE_DELETED"));
235 /* Test WRITE SAME with the lba not aligned */
236 static void test_unaligned_write_same(void)
239 uint8_t buf1
[512] = { 0 };
240 uint8_t buf2
[512] = { 1 };
241 const uint8_t write_same_cdb_1
[CDB_SIZE
] = { 0x41, 0x00, 0x00, 0x00, 0x00,
242 0x01, 0x00, 0x00, 0x02, 0x00 };
243 const uint8_t write_same_cdb_2
[CDB_SIZE
] = { 0x41, 0x00, 0x00, 0x00, 0x00,
244 0x01, 0x00, 0x33, 0x00, 0x00 };
246 qvirtio_scsi_start("-drive file=blkdebug::null-co://,if=none,id=dr1"
247 ",format=raw,file.align=4k "
248 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
249 vs
= qvirtio_scsi_pci_init(PCI_SLOT
);
251 g_assert_cmphex(0, ==,
252 virtio_scsi_do_command(vs
, write_same_cdb_1
, NULL
, 0, buf1
, 512, NULL
));
254 g_assert_cmphex(0, ==,
255 virtio_scsi_do_command(vs
, write_same_cdb_2
, NULL
, 0, buf2
, 512, NULL
));
257 qvirtio_scsi_pci_free(vs
);
261 int main(int argc
, char **argv
)
265 g_test_init(&argc
, &argv
, NULL
);
266 qtest_add_func("/virtio/scsi/pci/nop", pci_nop
);
267 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug
);
268 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
269 test_unaligned_write_same
);