2014-02-21 16:42:15 +01:00
|
|
|
/*
|
|
|
|
* QTest testcase for VirtIO SCSI
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 SUSE LINUX Products GmbH
|
2015-04-24 13:35:20 +02:00
|
|
|
* Copyright (c) 2015 Red Hat Inc.
|
2014-02-21 16:42:15 +01: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-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-09-03 07:50:26 +02:00
|
|
|
#include "libqtest-single.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2017-08-22 09:23:55 +02:00
|
|
|
#include "scsi/constants.h"
|
2016-10-17 12:30:23 +02:00
|
|
|
#include "libqos/libqos-pc.h"
|
2016-10-17 12:30:24 +02:00
|
|
|
#include "libqos/libqos-spapr.h"
|
2015-04-24 13:35:20 +02:00
|
|
|
#include "libqos/virtio.h"
|
|
|
|
#include "libqos/virtio-pci.h"
|
2016-05-09 14:47:34 +02:00
|
|
|
#include "standard-headers/linux/virtio_ids.h"
|
2016-05-09 14:47:41 +02:00
|
|
|
#include "standard-headers/linux/virtio_pci.h"
|
2016-05-09 14:47:40 +02:00
|
|
|
#include "standard-headers/linux/virtio_scsi.h"
|
2018-07-31 11:19:06 +02:00
|
|
|
#include "libqos/virtio-scsi.h"
|
|
|
|
#include "libqos/qgraph.h"
|
2015-04-24 13:35:20 +02: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;
|
|
|
|
int num_queues;
|
|
|
|
QVirtQueue *vq[MAX_NUM_QUEUES + 2];
|
2018-07-31 11:19:06 +02:00
|
|
|
} QVirtioSCSIQueues;
|
2015-04-24 13:35:20 +02:00
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static QGuestAllocator *alloc;
|
2015-04-24 13:35:19 +02:00
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static void qvirtio_scsi_pci_free(QVirtioSCSIQueues *vs)
|
2015-04-24 13:35:20 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < vs->num_queues + 2; i++) {
|
2018-07-31 11:19:06 +02:00
|
|
|
qvirtqueue_cleanup(vs->dev->bus, vs->vq[i], alloc);
|
2015-04-24 13:35:20 +02:00
|
|
|
}
|
2016-10-17 12:30:19 +02:00
|
|
|
g_free(vs);
|
2015-04-24 13:35:20 +02:00
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static uint64_t qvirtio_scsi_alloc(QVirtioSCSIQueues *vs, size_t alloc_size,
|
2015-04-24 13:35:20 +02:00
|
|
|
const void *data)
|
|
|
|
{
|
|
|
|
uint64_t addr;
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
addr = guest_alloc(alloc, alloc_size);
|
2015-04-24 13:35:20 +02:00
|
|
|
if (data) {
|
|
|
|
memwrite(addr, data, alloc_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static uint8_t virtio_scsi_do_command(QVirtioSCSIQueues *vs,
|
|
|
|
const uint8_t *cdb,
|
2015-04-24 13:35:20 +02:00
|
|
|
const uint8_t *data_in,
|
|
|
|
size_t data_in_len,
|
2015-07-30 15:16:13 +02:00
|
|
|
uint8_t *data_out, size_t data_out_len,
|
2016-05-09 14:47:40 +02:00
|
|
|
struct virtio_scsi_cmd_resp *resp_out)
|
2015-04-24 13:35:20 +02:00
|
|
|
{
|
|
|
|
QVirtQueue *vq;
|
2016-05-09 14:47:40 +02:00
|
|
|
struct virtio_scsi_cmd_req req = { { 0 } };
|
|
|
|
struct virtio_scsi_cmd_resp resp = { .response = 0xff, .status = 0xff };
|
2015-04-24 13:35:20 +02:00
|
|
|
uint64_t req_addr, resp_addr, data_in_addr = 0, data_out_addr = 0;
|
|
|
|
uint8_t response;
|
|
|
|
uint32_t free_head;
|
2019-05-18 10:23:24 +02:00
|
|
|
QTestState *qts = global_qtest;
|
2015-04-24 13:35:20 +02:00
|
|
|
|
|
|
|
vq = vs->vq[2];
|
|
|
|
|
|
|
|
req.lun[0] = 1; /* Select LUN */
|
|
|
|
req.lun[1] = 1; /* Select target 1 */
|
2016-05-09 14:47:40 +02:00
|
|
|
memcpy(req.cdb, cdb, VIRTIO_SCSI_CDB_SIZE);
|
2015-04-24 13:35:20 +02: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);
|
2019-05-18 10:23:24 +02:00
|
|
|
free_head = qvirtqueue_add(qts, vq, req_addr, sizeof(req), false, true);
|
2015-04-24 13:35:20 +02:00
|
|
|
|
|
|
|
if (data_out_len) {
|
|
|
|
data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
|
2019-05-18 10:23:24 +02:00
|
|
|
qvirtqueue_add(qts, vq, data_out_addr, data_out_len, false, true);
|
2015-04-24 13:35:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add response header */
|
|
|
|
resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
|
2019-05-18 10:23:24 +02:00
|
|
|
qvirtqueue_add(qts, vq, resp_addr, sizeof(resp), true, !!data_in_len);
|
2015-04-24 13:35:20 +02:00
|
|
|
|
|
|
|
if (data_in_len) {
|
|
|
|
data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
|
2019-05-18 10:23:24 +02:00
|
|
|
qvirtqueue_add(qts, vq, data_in_addr, data_in_len, true, false);
|
2015-04-24 13:35:20 +02:00
|
|
|
}
|
|
|
|
|
2019-05-18 10:23:24 +02:00
|
|
|
qvirtqueue_kick(qts, vs->dev, vq, free_head);
|
|
|
|
qvirtio_wait_used_elem(qts, vs->dev, vq, free_head, NULL,
|
2018-02-01 21:21:28 +01:00
|
|
|
QVIRTIO_SCSI_TIMEOUT_US);
|
2015-04-24 13:35:20 +02:00
|
|
|
|
2016-05-09 14:47:40 +02:00
|
|
|
response = readb(resp_addr +
|
|
|
|
offsetof(struct virtio_scsi_cmd_resp, response));
|
2015-04-24 13:35:20 +02:00
|
|
|
|
2015-07-30 15:16:13 +02:00
|
|
|
if (resp_out) {
|
|
|
|
memread(resp_addr, resp_out, sizeof(*resp_out));
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
guest_free(alloc, req_addr);
|
|
|
|
guest_free(alloc, resp_addr);
|
|
|
|
guest_free(alloc, data_in_addr);
|
|
|
|
guest_free(alloc, data_out_addr);
|
2015-04-24 13:35:20 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static QVirtioSCSIQueues *qvirtio_scsi_init(QVirtioDevice *dev)
|
2015-07-30 15:16:13 +02:00
|
|
|
{
|
2018-07-31 11:19:06 +02:00
|
|
|
QVirtioSCSIQueues *vs;
|
2016-05-09 14:47:40 +02:00
|
|
|
const uint8_t test_unit_ready_cdb[VIRTIO_SCSI_CDB_SIZE] = {};
|
|
|
|
struct virtio_scsi_cmd_resp resp;
|
2019-10-23 12:04:13 +02:00
|
|
|
uint64_t features;
|
2015-07-30 15:16:13 +02:00
|
|
|
int i;
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
vs = g_new0(QVirtioSCSIQueues, 1);
|
|
|
|
vs->dev = dev;
|
2019-10-23 12:04:13 +02:00
|
|
|
|
|
|
|
features = qvirtio_get_features(dev);
|
|
|
|
features &= ~(QVIRTIO_F_BAD_FEATURE | (1ull << VIRTIO_RING_F_EVENT_IDX));
|
|
|
|
qvirtio_set_features(dev, features);
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
vs->num_queues = qvirtio_config_readl(dev, 0);
|
2015-07-30 15:16:13 +02:00
|
|
|
|
|
|
|
g_assert_cmpint(vs->num_queues, <, MAX_NUM_QUEUES);
|
|
|
|
|
|
|
|
for (i = 0; i < vs->num_queues + 2; i++) {
|
2018-07-31 11:19:06 +02:00
|
|
|
vs->vq[i] = qvirtqueue_setup(dev, alloc, i);
|
2015-07-30 15:16:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 12:04:13 +02:00
|
|
|
qvirtio_set_driver_ok(dev);
|
|
|
|
|
2015-07-30 15:16:13 +02: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;
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static void hotplug(void *obj, void *data, QGuestAllocator *alloc)
|
2014-09-26 11:28:06 +02:00
|
|
|
{
|
2019-07-22 17:10:55 +02:00
|
|
|
QTestState *qts = global_qtest;
|
|
|
|
|
|
|
|
qtest_qmp_device_add(qts, "scsi-hd", "scsihd", "{'drive': 'drv1'}");
|
|
|
|
qtest_qmp_device_del(qts, "scsihd");
|
2014-09-26 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 13:35:20 +02:00
|
|
|
/* Test WRITE SAME with the lba not aligned */
|
2018-07-31 11:19:06 +02:00
|
|
|
static void test_unaligned_write_same(void *obj, void *data,
|
|
|
|
QGuestAllocator *t_alloc)
|
2015-04-24 13:35:20 +02:00
|
|
|
{
|
2018-07-31 11:19:06 +02:00
|
|
|
QVirtioSCSI *scsi = obj;
|
|
|
|
QVirtioSCSIQueues *vs;
|
2015-07-29 10:45:12 +02:00
|
|
|
uint8_t buf1[512] = { 0 };
|
|
|
|
uint8_t buf2[512] = { 1 };
|
2016-05-09 14:47:40 +02: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
|
|
|
|
};
|
2018-03-08 16:17:59 +01:00
|
|
|
const uint8_t write_same_cdb_ndob[VIRTIO_SCSI_CDB_SIZE] = {
|
|
|
|
0x41, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x33, 0x00, 0x00
|
|
|
|
};
|
2015-04-24 13:35:20 +02:00
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
alloc = t_alloc;
|
|
|
|
vs = qvirtio_scsi_init(scsi->vdev);
|
2015-04-24 13:35:20 +02:00
|
|
|
|
|
|
|
g_assert_cmphex(0, ==,
|
2018-07-31 11:19:06 +02:00
|
|
|
virtio_scsi_do_command(vs, write_same_cdb_1, NULL, 0, buf1, 512,
|
|
|
|
NULL));
|
2015-07-29 10:45:12 +02:00
|
|
|
|
|
|
|
g_assert_cmphex(0, ==,
|
2018-07-31 11:19:06 +02:00
|
|
|
virtio_scsi_do_command(vs, write_same_cdb_2, NULL, 0, buf2, 512,
|
|
|
|
NULL));
|
2015-04-24 13:35:20 +02:00
|
|
|
|
2018-03-08 16:17:59 +01:00
|
|
|
g_assert_cmphex(0, ==,
|
2018-07-31 11:19:06 +02:00
|
|
|
virtio_scsi_do_command(vs, write_same_cdb_ndob, NULL, 0, NULL, 0,
|
|
|
|
NULL));
|
2018-03-08 16:17:59 +01:00
|
|
|
|
2015-04-24 13:35:20 +02:00
|
|
|
qvirtio_scsi_pci_free(vs);
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:34:48 +01:00
|
|
|
static void test_write_to_cdrom(void *obj, void *data,
|
|
|
|
QGuestAllocator *t_alloc)
|
|
|
|
{
|
|
|
|
QVirtioSCSI *scsi = obj;
|
|
|
|
QVirtioSCSIQueues *vs;
|
|
|
|
uint8_t buf[2048] = { 0 };
|
|
|
|
const uint8_t write_cdb[VIRTIO_SCSI_CDB_SIZE] = {
|
|
|
|
/* WRITE(10) to LBA 0, transfer length 1 */
|
|
|
|
0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
|
|
|
|
};
|
|
|
|
struct virtio_scsi_cmd_resp resp;
|
|
|
|
|
|
|
|
alloc = t_alloc;
|
|
|
|
vs = qvirtio_scsi_init(scsi->vdev);
|
|
|
|
|
|
|
|
virtio_scsi_do_command(vs, write_cdb, NULL, 0, buf, 2048, &resp);
|
|
|
|
g_assert_cmphex(resp.response, ==, 0);
|
|
|
|
g_assert_cmphex(resp.status, ==, CHECK_CONDITION);
|
|
|
|
g_assert_cmphex(resp.sense[0], ==, 0x70);
|
|
|
|
g_assert_cmphex(resp.sense[2], ==, DATA_PROTECT);
|
|
|
|
g_assert_cmphex(resp.sense[12], ==, 0x27); /* WRITE PROTECTED */
|
|
|
|
g_assert_cmphex(resp.sense[13], ==, 0x00); /* WRITE PROTECTED */
|
|
|
|
|
|
|
|
qvirtio_scsi_pci_free(vs);
|
|
|
|
}
|
|
|
|
|
2019-05-08 11:58:45 +02:00
|
|
|
static void test_iothread_attach_node(void *obj, void *data,
|
|
|
|
QGuestAllocator *t_alloc)
|
|
|
|
{
|
|
|
|
QVirtioSCSIPCI *scsi_pci = obj;
|
|
|
|
QVirtioSCSI *scsi = &scsi_pci->scsi;
|
|
|
|
QVirtioSCSIQueues *vs;
|
|
|
|
char tmp_path[] = "/tmp/qtest.XXXXXX";
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
uint8_t buf[512] = { 0 };
|
|
|
|
const uint8_t write_cdb[VIRTIO_SCSI_CDB_SIZE] = {
|
|
|
|
/* WRITE(10) to LBA 0, transfer length 1 */
|
|
|
|
0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
|
|
|
|
};
|
|
|
|
|
|
|
|
alloc = t_alloc;
|
|
|
|
vs = qvirtio_scsi_init(scsi->vdev);
|
|
|
|
|
|
|
|
/* Create a temporary qcow2 overlay*/
|
|
|
|
fd = mkstemp(tmp_path);
|
|
|
|
g_assert(fd >= 0);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (!have_qemu_img()) {
|
|
|
|
g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
|
|
|
|
"skipping snapshot test");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkqcow2(tmp_path, 64);
|
|
|
|
|
|
|
|
/* Attach the overlay to the null0 node */
|
2019-07-23 21:22:39 +02:00
|
|
|
qtest_qmp_assert_success(scsi_pci->pci_vdev.pdev->bus->qts,
|
|
|
|
"{'execute': 'blockdev-add', 'arguments': {"
|
|
|
|
" 'driver': 'qcow2', 'node-name': 'overlay',"
|
|
|
|
" 'backing': 'null0', 'file': {"
|
|
|
|
" 'driver': 'file', 'filename': %s}}}",
|
|
|
|
tmp_path);
|
2019-05-08 11:58:45 +02:00
|
|
|
|
|
|
|
/* Send a request to see if the AioContext is still right */
|
|
|
|
ret = virtio_scsi_do_command(vs, write_cdb, NULL, 0, buf, 512, NULL);
|
|
|
|
g_assert_cmphex(ret, ==, 0);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
qvirtio_scsi_pci_free(vs);
|
|
|
|
unlink(tmp_path);
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static void *virtio_scsi_hotplug_setup(GString *cmd_line, void *arg)
|
2014-02-21 16:42:15 +01:00
|
|
|
{
|
2018-07-31 11:19:06 +02:00
|
|
|
g_string_append(cmd_line,
|
2019-07-29 14:46:00 +02:00
|
|
|
" -drive id=drv1,if=none,file=null-co://,"
|
|
|
|
"file.read-zeroes=on,format=raw");
|
2018-07-31 11:19:06 +02:00
|
|
|
return arg;
|
|
|
|
}
|
2014-02-21 16:42:15 +01:00
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static void *virtio_scsi_setup(GString *cmd_line, void *arg)
|
|
|
|
{
|
|
|
|
g_string_append(cmd_line,
|
|
|
|
" -drive file=blkdebug::null-co://,"
|
2019-07-29 14:46:00 +02:00
|
|
|
"file.image.read-zeroes=on,"
|
2018-07-31 11:19:06 +02:00
|
|
|
"if=none,id=dr1,format=raw,file.align=4k "
|
|
|
|
"-device scsi-hd,drive=dr1,lun=0,scsi-id=1");
|
|
|
|
return arg;
|
2014-02-21 16:42:15 +01:00
|
|
|
}
|
2018-07-31 11:19:06 +02:00
|
|
|
|
2021-01-18 13:34:48 +01:00
|
|
|
static void *virtio_scsi_setup_cd(GString *cmd_line, void *arg)
|
|
|
|
{
|
|
|
|
g_string_append(cmd_line,
|
|
|
|
" -drive file=null-co://,"
|
|
|
|
"file.read-zeroes=on,"
|
|
|
|
"if=none,id=dr1,format=raw "
|
|
|
|
"-device scsi-cd,drive=dr1,lun=0,scsi-id=1");
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2019-05-08 11:58:45 +02:00
|
|
|
static void *virtio_scsi_setup_iothread(GString *cmd_line, void *arg)
|
|
|
|
{
|
|
|
|
g_string_append(cmd_line,
|
|
|
|
" -object iothread,id=thread0"
|
2019-07-29 14:46:00 +02:00
|
|
|
" -blockdev driver=null-co,read-zeroes=on,node-name=null0"
|
2019-05-08 11:58:45 +02:00
|
|
|
" -device scsi-hd,drive=null0");
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
2018-07-31 11:19:06 +02:00
|
|
|
static void register_virtio_scsi_test(void)
|
|
|
|
{
|
|
|
|
QOSGraphTestOptions opts = { };
|
|
|
|
|
|
|
|
opts.before = virtio_scsi_hotplug_setup;
|
|
|
|
qos_add_test("hotplug", "virtio-scsi", hotplug, &opts);
|
|
|
|
|
|
|
|
opts.before = virtio_scsi_setup;
|
|
|
|
qos_add_test("unaligned-write-same", "virtio-scsi",
|
|
|
|
test_unaligned_write_same, &opts);
|
2019-05-08 11:58:45 +02:00
|
|
|
|
2021-01-18 13:34:48 +01:00
|
|
|
opts.before = virtio_scsi_setup_cd;
|
|
|
|
qos_add_test("write-to-cdrom", "virtio-scsi", test_write_to_cdrom, &opts);
|
|
|
|
|
2019-05-08 11:58:45 +02:00
|
|
|
opts.before = virtio_scsi_setup_iothread;
|
|
|
|
opts.edge = (QOSGraphEdgeOptions) {
|
|
|
|
.extra_device_opts = "iothread=thread0",
|
|
|
|
};
|
|
|
|
qos_add_test("iothread-attach-node", "virtio-scsi-pci",
|
|
|
|
test_iothread_attach_node, &opts);
|
2018-07-31 11:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
libqos_init(register_virtio_scsi_test);
|