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 "libqos/virtio.h"
17 #include "libqos/virtio-pci.h"
18 #include "libqos/pci-pc.h"
19 #include "libqos/malloc.h"
20 #include "libqos/malloc-pc.h"
21 #include "libqos/malloc-generic.h"
25 #define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
28 #define MAX_NUM_QUEUES 64
32 QGuestAllocator
*alloc
;
35 QVirtQueue
*vq
[MAX_NUM_QUEUES
+ 2];
44 uint8_t cdb
[CDB_SIZE
];
45 } QEMU_PACKED QVirtIOSCSICmdReq
;
50 uint16_t status_qualifier
;
54 } QEMU_PACKED QVirtIOSCSICmdResp
;
56 static void qvirtio_scsi_start(const char *extra_opts
)
60 cmdline
= g_strdup_printf(
61 "-drive id=drv0,if=none,file=/dev/null,format=raw "
62 "-device virtio-scsi-pci,id=vs0 "
63 "-device scsi-hd,bus=vs0.0,drive=drv0 %s",
69 static void qvirtio_scsi_stop(void)
74 static QVirtIOSCSI
*qvirtio_scsi_pci_init(int slot
)
77 QVirtioPCIDevice
*dev
;
81 vs
= g_new0(QVirtIOSCSI
, 1);
82 vs
->alloc
= pc_alloc_init();
83 vs
->bus
= qpci_init_pc();
85 dev
= qvirtio_pci_device_find(vs
->bus
, QVIRTIO_SCSI_DEVICE_ID
);
86 vs
->dev
= (QVirtioDevice
*)dev
;
87 g_assert(dev
!= NULL
);
88 g_assert_cmphex(vs
->dev
->device_type
, ==, QVIRTIO_SCSI_DEVICE_ID
);
90 qvirtio_pci_device_enable(dev
);
91 qvirtio_reset(&qvirtio_pci
, vs
->dev
);
92 qvirtio_set_acknowledge(&qvirtio_pci
, vs
->dev
);
93 qvirtio_set_driver(&qvirtio_pci
, vs
->dev
);
95 addr
= dev
->addr
+ QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX
;
96 vs
->num_queues
= qvirtio_config_readl(&qvirtio_pci
, vs
->dev
,
97 (uint64_t)(uintptr_t)addr
);
99 g_assert_cmpint(vs
->num_queues
, <, MAX_NUM_QUEUES
);
101 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
102 vs
->vq
[i
] = qvirtqueue_setup(&qvirtio_pci
, vs
->dev
, vs
->alloc
, i
);
108 static void qvirtio_scsi_pci_free(QVirtIOSCSI
*vs
)
112 for (i
= 0; i
< vs
->num_queues
+ 2; i
++) {
113 guest_free(vs
->alloc
, vs
->vq
[i
]->desc
);
115 pc_alloc_uninit(vs
->alloc
);
116 qvirtio_pci_device_disable(container_of(vs
->dev
, QVirtioPCIDevice
, vdev
));
118 qpci_free_pc(vs
->bus
);
121 static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI
*vs
, size_t alloc_size
,
126 addr
= guest_alloc(vs
->alloc
, alloc_size
);
128 memwrite(addr
, data
, alloc_size
);
134 static uint8_t virtio_scsi_do_command(QVirtIOSCSI
*vs
, const uint8_t *cdb
,
135 const uint8_t *data_in
,
137 uint8_t *data_out
, size_t data_out_len
)
140 QVirtIOSCSICmdReq req
= { { 0 } };
141 QVirtIOSCSICmdResp resp
= { .response
= 0xff, .status
= 0xff };
142 uint64_t req_addr
, resp_addr
, data_in_addr
= 0, data_out_addr
= 0;
148 req
.lun
[0] = 1; /* Select LUN */
149 req
.lun
[1] = 1; /* Select target 1 */
150 memcpy(req
.cdb
, cdb
, CDB_SIZE
);
152 /* XXX: Fix endian if any multi-byte field in req/resp is used */
154 /* Add request header */
155 req_addr
= qvirtio_scsi_alloc(vs
, sizeof(req
), &req
);
156 free_head
= qvirtqueue_add(vq
, req_addr
, sizeof(req
), false, true);
159 data_out_addr
= qvirtio_scsi_alloc(vs
, data_out_len
, data_out
);
160 qvirtqueue_add(vq
, data_out_addr
, data_out_len
, false, true);
163 /* Add response header */
164 resp_addr
= qvirtio_scsi_alloc(vs
, sizeof(resp
), &resp
);
165 qvirtqueue_add(vq
, resp_addr
, sizeof(resp
), true, !!data_in_len
);
168 data_in_addr
= qvirtio_scsi_alloc(vs
, data_in_len
, data_in
);
169 qvirtqueue_add(vq
, data_in_addr
, data_in_len
, true, false);
172 qvirtqueue_kick(&qvirtio_pci
, vs
->dev
, vq
, free_head
);
173 qvirtio_wait_queue_isr(&qvirtio_pci
, vs
->dev
, vq
, QVIRTIO_SCSI_TIMEOUT_US
);
175 response
= readb(resp_addr
+ offsetof(QVirtIOSCSICmdResp
, response
));
177 guest_free(vs
->alloc
, req_addr
);
178 guest_free(vs
->alloc
, resp_addr
);
179 guest_free(vs
->alloc
, data_in_addr
);
180 guest_free(vs
->alloc
, data_out_addr
);
184 /* Tests only initialization so far. TODO: Replace with functional tests */
185 static void pci_nop(void)
187 qvirtio_scsi_start(NULL
);
191 static void hotplug(void)
195 qvirtio_scsi_start("-drive id=drv1,if=none,file=/dev/null,format=raw");
196 response
= qmp("{\"execute\": \"device_add\","
198 " \"driver\": \"scsi-hd\","
199 " \"id\": \"scsi-hd\","
200 " \"drive\": \"drv1\""
204 g_assert(!qdict_haskey(response
, "error"));
207 response
= qmp("{\"execute\": \"device_del\","
209 " \"id\": \"scsi-hd\""
213 g_assert(!qdict_haskey(response
, "error"));
214 g_assert(qdict_haskey(response
, "event"));
215 g_assert(!strcmp(qdict_get_str(response
, "event"), "DEVICE_DELETED"));
220 /* Test WRITE SAME with the lba not aligned */
221 static void test_unaligned_write_same(void)
224 uint8_t buf
[512] = { 0 };
225 const uint8_t write_same_cdb
[CDB_SIZE
] = { 0x41, 0x00, 0x00, 0x00, 0x00,
226 0x01, 0x00, 0x00, 0x02, 0x00 };
228 qvirtio_scsi_start("-drive file=blkdebug::null-co://,if=none,id=dr1"
229 ",format=raw,file.align=4k "
230 "-device scsi-disk,drive=dr1,lun=0,scsi-id=1");
231 vs
= qvirtio_scsi_pci_init(PCI_SLOT
);
233 g_assert_cmphex(0, ==,
234 virtio_scsi_do_command(vs
, write_same_cdb
, NULL
, 0, buf
, 512));
236 qvirtio_scsi_pci_free(vs
);
240 int main(int argc
, char **argv
)
244 g_test_init(&argc
, &argv
, NULL
);
245 qtest_add_func("/virtio/scsi/pci/nop", pci_nop
);
246 qtest_add_func("/virtio/scsi/pci/hotplug", hotplug
);
247 qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
248 test_unaligned_write_same
);