2014-02-21 23:42:15 +08:00
|
|
|
/*
|
|
|
|
* QTest testcase for VirtIO SCSI
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 SUSE LINUX Products GmbH
|
2015-04-24 19:35:20 +08:00
|
|
|
* Copyright (c) 2015 Red Hat Inc.
|
2014-02-21 23:42:15 +08:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-09 02:08:51 +08:00
|
|
|
#include "qemu/osdep.h"
|
2014-02-21 23:42:15 +08:00
|
|
|
#include "libqtest.h"
|
2017-08-22 15:23:55 +08:00
|
|
|
#include "scsi/constants.h"
|
2016-10-17 18:30:23 +08:00
|
|
|
#include "libqos/libqos-pc.h"
|
2016-10-17 18:30:24 +08:00
|
|
|
#include "libqos/libqos-spapr.h"
|
2015-04-24 19:35:20 +08:00
|
|
|
#include "libqos/virtio.h"
|
|
|
|
#include "libqos/virtio-pci.h"
|
2016-05-09 20:47:34 +08:00
|
|
|
#include "standard-headers/linux/virtio_ids.h"
|
2016-05-09 20:47:41 +08:00
|
|
|
#include "standard-headers/linux/virtio_pci.h"
|
2016-05-09 20:47:40 +08:00
|
|
|
#include "standard-headers/linux/virtio_scsi.h"
|
2015-04-24 19:35:20 +08:00
|
|
|
|
|
|
|
#define PCI_SLOT 0x02
|
|
|
|
#define PCI_FN 0x00
|
|
|
|
#define QVIRTIO_SCSI_TIMEOUT_US (1 * 1000 * 1000)
|
|
|
|
|
|
|
|
#define MAX_NUM_QUEUES 64
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
QVirtioDevice *dev;
|
2016-10-17 18:30:23 +08:00
|
|
|
QOSState *qs;
|
2015-04-24 19:35:20 +08:00
|
|
|
int num_queues;
|
|
|
|
QVirtQueue *vq[MAX_NUM_QUEUES + 2];
|
|
|
|
} QVirtIOSCSI;
|
|
|
|
|
2016-10-17 18:30:23 +08:00
|
|
|
static QOSState *qvirtio_scsi_start(const char *extra_opts)
|
2015-04-24 19:35:19 +08:00
|
|
|
{
|
2016-10-17 18:30:24 +08:00
|
|
|
const char *arch = qtest_get_arch();
|
2017-05-03 00:35:49 +08:00
|
|
|
const char *cmd = "-drive id=drv0,if=none,file=null-co://,format=raw "
|
2016-10-17 18:30:23 +08:00
|
|
|
"-device virtio-scsi-pci,id=vs0 "
|
|
|
|
"-device scsi-hd,bus=vs0.0,drive=drv0 %s";
|
|
|
|
|
2016-10-17 18:30:24 +08:00
|
|
|
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
|
|
|
|
return qtest_pc_boot(cmd, extra_opts ? : "");
|
|
|
|
}
|
|
|
|
if (strcmp(arch, "ppc64") == 0) {
|
|
|
|
return qtest_spapr_boot(cmd, extra_opts ? : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_printerr("virtio-scsi tests are only available on x86 or ppc64\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2015-04-24 19:35:19 +08:00
|
|
|
}
|
|
|
|
|
2016-10-17 18:30:23 +08:00
|
|
|
static void qvirtio_scsi_stop(QOSState *qs)
|
2015-04-24 19:35:19 +08:00
|
|
|
{
|
2016-10-17 18:30:23 +08:00
|
|
|
qtest_shutdown(qs);
|
2015-04-24 19:35:19 +08:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:35:20 +08:00
|
|
|
static void qvirtio_scsi_pci_free(QVirtIOSCSI *vs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < vs->num_queues + 2; i++) {
|
2016-10-17 18:30:23 +08:00
|
|
|
qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], vs->qs->alloc);
|
2015-04-24 19:35:20 +08:00
|
|
|
}
|
|
|
|
qvirtio_pci_device_disable(container_of(vs->dev, QVirtioPCIDevice, vdev));
|
2017-02-06 17:21:19 +08:00
|
|
|
qvirtio_pci_device_free((QVirtioPCIDevice *)vs->dev);
|
2016-10-17 18:30:23 +08:00
|
|
|
qvirtio_scsi_stop(vs->qs);
|
2016-10-17 18:30:19 +08:00
|
|
|
g_free(vs);
|
2015-04-24 19:35:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t qvirtio_scsi_alloc(QVirtIOSCSI *vs, size_t alloc_size,
|
|
|
|
const void *data)
|
|
|
|
{
|
|
|
|
uint64_t addr;
|
|
|
|
|
2016-10-17 18:30:23 +08:00
|
|
|
addr = guest_alloc(vs->qs->alloc, alloc_size);
|
2015-04-24 19:35:20 +08:00
|
|
|
if (data) {
|
|
|
|
memwrite(addr, data, alloc_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
|
|
|
|
const uint8_t *data_in,
|
|
|
|
size_t data_in_len,
|
2015-07-30 21:16:13 +08:00
|
|
|
uint8_t *data_out, size_t data_out_len,
|
2016-05-09 20:47:40 +08:00
|
|
|
struct virtio_scsi_cmd_resp *resp_out)
|
2015-04-24 19:35:20 +08:00
|
|
|
{
|
|
|
|
QVirtQueue *vq;
|
2016-05-09 20:47:40 +08:00
|
|
|
struct virtio_scsi_cmd_req req = { { 0 } };
|
|
|
|
struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
|
2015-04-24 19:35:20 +08:00
|
|
|
uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
|
|
|
|
uint8_t response;
|
|
|
|
uint32_t free_head;
|
|
|
|
|
|
|
|
vq = vs->vq[2];
|
|
|
|
|
|
|
|
req.lun[0] = 1; /* Select LUN */
|
|
|
|
req.lun[1] = 1; /* Select target 1 */
|
2016-05-09 20:47:40 +08:00
|
|
|
memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
|
2015-04-24 19:35:20 +08:00
|
|
|
|
|
|
|
/* XXX: Fix endian if any multi-byte field in req/resp is used */
|
|
|
|
|
|
|
|
/* Add request header */
|
|
|
|
req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
|
|
|
|
free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
|
|
|
|
|
|
|
|
if (data_out_len) {
|
|
|
|
data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
|
|
|
|
qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add response header */
|
|
|
|
resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
|
|
|
|
qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
|
|
|
|
|
|
|
|
if (data_in_len) {
|
|
|
|
data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
|
|
|
|
qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
|
|
|
|
}
|
|
|
|
|
2016-10-17 18:30:21 +08:00
|
|
|
qvirtqueue_kick(vs->dev, vq, free_head);
|
2017-06-29 02:47:21 +08:00
|
|
|
qvirtio_wait_used_elem(vs->dev, vq, free_head, QVIRTIO_SCSI_TIMEOUT_US);
|
2015-04-24 19:35:20 +08:00
|
|
|
|
2016-05-09 20:47:40 +08:00
|
|
|
response = readb(resp_addr +
|
|
|
|
offsetof(struct virtio_scsi_cmd_resp, response));
|
2015-04-24 19:35:20 +08:00
|
|
|
|
2015-07-30 21:16:13 +08:00
|
|
|
if (resp_out) {
|
|
|
|
memread(resp_addr, resp_out, sizeof(*resp_out));
|
|
|
|
}
|
|
|
|
|
2016-10-17 18:30:23 +08:00
|
|
|
guest_free(vs->qs->alloc, req_addr);
|
|
|
|
guest_free(vs->qs->alloc, resp_addr);
|
|
|
|
guest_free(vs->qs->alloc, data_in_addr);
|
|
|
|
guest_free(vs->qs->alloc, data_out_addr);
|
2015-04-24 19:35:20 +08:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:16:13 +08:00
|
|
|
static QVirtIOSCSI *qvirtio_scsi_pci_init(int slot)
|
|
|
|
{
|
2016-05-09 20:47:40 +08:00
|
|
|
const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
|
2015-07-30 21:16:13 +08:00
|
|
|
QVirtIOSCSI *vs;
|
|
|
|
QVirtioPCIDevice *dev;
|
2016-05-09 20:47:40 +08:00
|
|
|
struct virtio_scsi_cmd_resp resp;
|
2015-07-30 21:16:13 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
vs = g_new0(QVirtIOSCSI, 1);
|
|
|
|
|
2016-10-17 18:30:23 +08:00
|
|
|
vs->qs = qvirtio_scsi_start("-drive file=blkdebug::null-co://,"
|
|
|
|
"if=none,id=dr1,format=raw,file.align=4k "
|
2017-05-09 18:56:02 +08:00
|
|
|
"-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
|
2016-10-17 18:30:23 +08:00
|
|
|
dev = qvirtio_pci_device_find(vs->qs->pcibus, VIRTIO_ID_SCSI);
|
2015-07-30 21:16:13 +08:00
|
|
|
vs->dev = (QVirtioDevice *)dev;
|
|
|
|
g_assert(dev != NULL);
|
2016-05-09 20:47:34 +08:00
|
|
|
g_assert_cmphex(vs->dev->device_type, ==, VIRTIO_ID_SCSI);
|
2015-07-30 21:16:13 +08:00
|
|
|
|
|
|
|
qvirtio_pci_device_enable(dev);
|
2016-10-17 18:30:21 +08:00
|
|
|
qvirtio_reset(vs->dev);
|
|
|
|
qvirtio_set_acknowledge(vs->dev);
|
|
|
|
qvirtio_set_driver(vs->dev);
|
2015-07-30 21:16:13 +08:00
|
|
|
|
2016-10-20 11:08:07 +08:00
|
|
|
vs->num_queues = qvirtio_config_readl(vs->dev, 0);
|
2015-07-30 21:16:13 +08:00
|
|
|
|
|
|
|
g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
|
|
|
|
|
|
|
|
for (i = 0; i < vs->num_queues + 2; i++) {
|
2016-10-17 18:30:23 +08:00
|
|
|
vs->vq[i] = qvirtqueue_setup(vs->dev, vs->qs->alloc, i);
|
2015-07-30 21:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the POWER ON OCCURRED unit attention */
|
|
|
|
g_assert_cmpint(virtio_scsi_do_command(vs, test_unit_ready_cdb,
|
|
|
|
NULL, 0, NULL, 0, &resp),
|
|
|
|
==, 0);
|
|
|
|
g_assert_cmpint(resp.status, ==, CHECK_CONDITION);
|
|
|
|
g_assert_cmpint(resp.sense[0], ==, 0x70); /* Fixed format sense buffer */
|
|
|
|
g_assert_cmpint(resp.sense[2], ==, UNIT_ATTENTION);
|
|
|
|
g_assert_cmpint(resp.sense[12], ==, 0x29); /* POWER ON */
|
|
|
|
g_assert_cmpint(resp.sense[13], ==, 0x00);
|
|
|
|
|
|
|
|
return vs;
|
|
|
|
}
|
|
|
|
|
2014-02-21 23:42:15 +08:00
|
|
|
/* Tests only initialization so far. TODO: Replace with functional tests */
|
|
|
|
static void pci_nop(void)
|
|
|
|
{
|
2016-10-17 18:30:23 +08:00
|
|
|
QOSState *qs;
|
|
|
|
|
|
|
|
qs = qvirtio_scsi_start(NULL);
|
|
|
|
qvirtio_scsi_stop(qs);
|
2014-02-21 23:42:15 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 17:28:06 +08:00
|
|
|
static void hotplug(void)
|
|
|
|
{
|
2016-10-17 18:30:23 +08:00
|
|
|
QOSState *qs;
|
2014-09-26 17:28:06 +08:00
|
|
|
|
2017-05-03 00:35:49 +08:00
|
|
|
qs = qvirtio_scsi_start(
|
|
|
|
"-drive id=drv1,if=none,file=null-co://,format=raw");
|
2017-08-15 14:58:54 +08:00
|
|
|
qtest_qmp_device_add("scsi-hd", "scsihd", "'drive': 'drv1'");
|
|
|
|
qtest_qmp_device_del("scsihd");
|
2016-10-17 18:30:23 +08:00
|
|
|
qvirtio_scsi_stop(qs);
|
2014-09-26 17:28:06 +08:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:35:20 +08:00
|
|
|
/* Test WRITE SAME with the lba not aligned */
|
|
|
|
static void test_unaligned_write_same(void)
|
|
|
|
{
|
|
|
|
QVirtIOSCSI *vs;
|
2015-07-29 16:45:12 +08:00
|
|
|
uint8_t buf1[512] = { 0 };
|
|
|
|
uint8_t buf2[512] = { 1 };
|
2016-05-09 20:47:40 +08:00
|
|
|
const uint8_t write_same_cdb_1[VIRTIO_SCSI_CDB_SIZE] = {
|
|
|
|
0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00
|
|
|
|
};
|
|
|
|
const uint8_t write_same_cdb_2[VIRTIO_SCSI_CDB_SIZE] = {
|
|
|
|
0x41, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
|
|
|
|
};
|
2015-04-24 19:35:20 +08:00
|
|
|
|
|
|
|
vs = qvirtio_scsi_pci_init(PCI_SLOT);
|
|
|
|
|
|
|
|
g_assert_cmphex(0, ==,
|
2015-07-29 16:45:12 +08:00
|
|
|
virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512, NULL));
|
|
|
|
|
|
|
|
g_assert_cmphex(0, ==,
|
|
|
|
virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512, NULL));
|
2015-04-24 19:35:20 +08:00
|
|
|
|
|
|
|
qvirtio_scsi_pci_free(vs);
|
|
|
|
}
|
|
|
|
|
2014-02-21 23:42:15 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
qtest_add_func("/virtio/scsi/pci/nop", pci_nop);
|
2014-09-26 17:28:06 +08:00
|
|
|
qtest_add_func("/virtio/scsi/pci/hotplug", hotplug);
|
2015-04-24 19:35:20 +08:00
|
|
|
qtest_add_func("/virtio/scsi/pci/scsi-disk/unaligned-write-same",
|
|
|
|
test_unaligned_write_same);
|
2014-02-21 23:42:15 +08:00
|
|
|
|
2016-06-14 05:57:58 +08:00
|
|
|
return g_test_run();
|
2014-02-21 23:42:15 +08:00
|
|
|
}
|