Pull request

- Jeuk Kim's emulated UFS device
 - Fabiano Rosas' IOThread GSource "name" debugging aid
 -----BEGIN PGP SIGNATURE-----
 
 iQEzBAABCAAdFiEEhpWov9P5fNqsNXdanKSrs4Grc8gFAmT6E3YACgkQnKSrs4Gr
 c8g3NwgAxVSzxS6hGCs6hKjRJ6f8HIlBWWbeO273XWbuAsMUbbYYAw7Gx6bzrbnb
 NFdFZ4F3M0lOfIaGFONasA4Wo2rIuiWVfj6Es4pcYjnexF0OmhAib2RLLEtuDlyc
 EJB/s4Mo2Xk+aw8LzQoYJVvDYi+hTZMmaHaQmNAsYLyQmsN/zg5iEE/vMDqVCNOs
 trv9rMGOJBblzJ4bqgItJ80nSVaEbwErpiU1+NTY77dpTeIt/3nq7yGAo+h4rVpc
 Tv4IuH8y6Rb9xjlwSANL2YKS2VOuJc5J/G5vpM63T4O2yzYSosTsU+ET/ZqB2U1H
 v0uT9K2akfsTOFR8rUaroFsXha8IMQ==
 =6SqP
 -----END PGP SIGNATURE-----

Merge tag 'block-pull-request' of https://gitlab.com/stefanha/qemu into staging

Pull request

- Jeuk Kim's emulated UFS device
- Fabiano Rosas' IOThread GSource "name" debugging aid

# -----BEGIN PGP SIGNATURE-----
#
# iQEzBAABCAAdFiEEhpWov9P5fNqsNXdanKSrs4Grc8gFAmT6E3YACgkQnKSrs4Gr
# c8g3NwgAxVSzxS6hGCs6hKjRJ6f8HIlBWWbeO273XWbuAsMUbbYYAw7Gx6bzrbnb
# NFdFZ4F3M0lOfIaGFONasA4Wo2rIuiWVfj6Es4pcYjnexF0OmhAib2RLLEtuDlyc
# EJB/s4Mo2Xk+aw8LzQoYJVvDYi+hTZMmaHaQmNAsYLyQmsN/zg5iEE/vMDqVCNOs
# trv9rMGOJBblzJ4bqgItJ80nSVaEbwErpiU1+NTY77dpTeIt/3nq7yGAo+h4rVpc
# Tv4IuH8y6Rb9xjlwSANL2YKS2VOuJc5J/G5vpM63T4O2yzYSosTsU+ET/ZqB2U1H
# v0uT9K2akfsTOFR8rUaroFsXha8IMQ==
# =6SqP
# -----END PGP SIGNATURE-----
# gpg: Signature made Thu 07 Sep 2023 14:16:22 EDT
# gpg:                using RSA key 8695A8BFD3F97CDAAC35775A9CA4ABB381AB73C8
# gpg: Good signature from "Stefan Hajnoczi <stefanha@redhat.com>" [ultimate]
# gpg:                 aka "Stefan Hajnoczi <stefanha@gmail.com>" [ultimate]
# Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35  775A 9CA4 ABB3 81AB 73C8

* tag 'block-pull-request' of https://gitlab.com/stefanha/qemu:
  tests/qtest: Introduce tests for UFS
  hw/ufs: Support for UFS logical unit
  hw/ufs: Support for Query Transfer Requests
  hw/ufs: Initial commit for emulated Universal-Flash-Storage
  iothread: Set the GSource "name" field

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Stefan Hajnoczi 2023-09-08 07:09:04 -04:00
commit 2f352bc227
19 changed files with 4843 additions and 6 deletions

View File

@ -2248,6 +2248,13 @@ F: tests/qtest/nvme-test.c
F: docs/system/devices/nvme.rst
T: git git://git.infradead.org/qemu-nvme.git nvme-next
ufs
M: Jeuk Kim <jeuk20.kim@samsung.com>
S: Supported
F: hw/ufs/*
F: include/block/ufs.h
F: tests/qtest/ufs-test.c
megasas
M: Hannes Reinecke <hare@suse.com>
L: qemu-block@nongnu.org

View File

@ -92,6 +92,8 @@ PCI devices (other than virtio):
PCI PVPanic device (``-device pvpanic-pci``)
1b36:0012
PCI ACPI ERST device (``-device acpi-erst``)
1b36:0013
PCI UFS device (``-device ufs``)
All these devices are documented in :doc:`index`.

View File

@ -38,6 +38,7 @@ source smbios/Kconfig
source ssi/Kconfig
source timer/Kconfig
source tpm/Kconfig
source ufs/Kconfig
source usb/Kconfig
source virtio/Kconfig
source vfio/Kconfig

View File

@ -37,6 +37,7 @@ subdir('smbios')
subdir('ssi')
subdir('timer')
subdir('tpm')
subdir('ufs')
subdir('usb')
subdir('vfio')
subdir('virtio')

4
hw/ufs/Kconfig Normal file
View File

@ -0,0 +1,4 @@
config UFS_PCI
bool
default y if PCI_DEVICES
depends on PCI

1445
hw/ufs/lu.c Normal file

File diff suppressed because it is too large Load Diff

1
hw/ufs/meson.build Normal file
View File

@ -0,0 +1 @@
system_ss.add(when: 'CONFIG_UFS_PCI', if_true: files('ufs.c', 'lu.c'))

58
hw/ufs/trace-events Normal file
View File

@ -0,0 +1,58 @@
# ufs.c
ufs_irq_raise(void) "INTx"
ufs_irq_lower(void) "INTx"
ufs_mmio_read(uint64_t addr, uint64_t data, unsigned size) "addr 0x%"PRIx64" data 0x%"PRIx64" size %d"
ufs_mmio_write(uint64_t addr, uint64_t data, unsigned size) "addr 0x%"PRIx64" data 0x%"PRIx64" size %d"
ufs_process_db(uint32_t slot) "UTRLDBR slot %"PRIu32""
ufs_process_req(uint32_t slot) "UTRLDBR slot %"PRIu32""
ufs_complete_req(uint32_t slot) "UTRLDBR slot %"PRIu32""
ufs_sendback_req(uint32_t slot) "UTRLDBR slot %"PRIu32""
ufs_exec_nop_cmd(uint32_t slot) "UTRLDBR slot %"PRIu32""
ufs_exec_scsi_cmd(uint32_t slot, uint8_t lun, uint8_t opcode) "slot %"PRIu32", lun 0x%"PRIx8", opcode 0x%"PRIx8""
ufs_exec_query_cmd(uint32_t slot, uint8_t opcode) "slot %"PRIu32", opcode 0x%"PRIx8""
ufs_process_uiccmd(uint32_t uiccmd, uint32_t ucmdarg1, uint32_t ucmdarg2, uint32_t ucmdarg3) "uiccmd 0x%"PRIx32", ucmdarg1 0x%"PRIx32", ucmdarg2 0x%"PRIx32", ucmdarg3 0x%"PRIx32""
# lu.c
ufs_scsi_check_condition(uint32_t tag, uint8_t key, uint8_t asc, uint8_t ascq) "Command complete tag=0x%x sense=%d/%d/%d"
ufs_scsi_read_complete(uint32_t tag, size_t size) "Data ready tag=0x%x len=%zd"
ufs_scsi_read_data_count(uint32_t sector_count) "Read sector_count=%d"
ufs_scsi_read_data_invalid(void) "Data transfer direction invalid"
ufs_scsi_write_complete_noio(uint32_t tag, size_t size) "Write complete tag=0x%x more=%zd"
ufs_scsi_write_data_invalid(void) "Data transfer direction invalid"
ufs_scsi_emulate_vpd_page_00(size_t xfer) "Inquiry EVPD[Supported pages] buffer size %zd"
ufs_scsi_emulate_vpd_page_80_not_supported(void) "Inquiry EVPD[Serial number] not supported"
ufs_scsi_emulate_vpd_page_80(size_t xfer) "Inquiry EVPD[Serial number] buffer size %zd"
ufs_scsi_emulate_vpd_page_87(size_t xfer) "Inquiry EVPD[Mode Page Policy] buffer size %zd"
ufs_scsi_emulate_mode_sense(int cmd, int page, size_t xfer, int control) "Mode Sense(%d) (page %d, xfer %zd, page_control %d)"
ufs_scsi_emulate_read_data(int buflen) "Read buf_len=%d"
ufs_scsi_emulate_write_data(int buflen) "Write buf_len=%d"
ufs_scsi_emulate_command_START_STOP(void) "START STOP UNIT"
ufs_scsi_emulate_command_FORMAT_UNIT(void) "FORMAT UNIT"
ufs_scsi_emulate_command_SEND_DIAGNOSTIC(void) "SEND DIAGNOSTIC"
ufs_scsi_emulate_command_SAI_16(void) "SAI READ CAPACITY(16)"
ufs_scsi_emulate_command_SAI_unsupported(void) "Unsupported Service Action In"
ufs_scsi_emulate_command_MODE_SELECT_10(size_t xfer) "Mode Select(10) (len %zd)"
ufs_scsi_emulate_command_VERIFY(int bytchk) "Verify (bytchk %d)"
ufs_scsi_emulate_command_UNKNOWN(int cmd, const char *name) "Unknown SCSI command (0x%2.2x=%s)"
ufs_scsi_dma_command_READ(uint64_t lba, uint32_t len) "Read (block %" PRIu64 ", count %u)"
ufs_scsi_dma_command_WRITE(uint64_t lba, int len) "Write (block %" PRIu64 ", count %u)"
# error condition
ufs_err_dma_read_utrd(uint32_t slot, uint64_t addr) "failed to read utrd. UTRLDBR slot %"PRIu32", UTRD dma addr %"PRIu64""
ufs_err_dma_read_req_upiu(uint32_t slot, uint64_t addr) "failed to read req upiu. UTRLDBR slot %"PRIu32", request upiu addr %"PRIu64""
ufs_err_dma_read_prdt(uint32_t slot, uint64_t addr) "failed to read prdt. UTRLDBR slot %"PRIu32", prdt addr %"PRIu64""
ufs_err_dma_write_utrd(uint32_t slot, uint64_t addr) "failed to write utrd. UTRLDBR slot %"PRIu32", UTRD dma addr %"PRIu64""
ufs_err_dma_write_rsp_upiu(uint32_t slot, uint64_t addr) "failed to write rsp upiu. UTRLDBR slot %"PRIu32", response upiu addr %"PRIu64""
ufs_err_utrl_slot_error(uint32_t slot) "UTRLDBR slot %"PRIu32" is in error"
ufs_err_utrl_slot_busy(uint32_t slot) "UTRLDBR slot %"PRIu32" is busy"
ufs_err_unsupport_register_offset(uint32_t offset) "Register offset 0x%"PRIx32" is not yet supported"
ufs_err_invalid_register_offset(uint32_t offset) "Register offset 0x%"PRIx32" is invalid"
ufs_err_scsi_cmd_invalid_lun(uint8_t lun) "scsi command has invalid lun: 0x%"PRIx8""
ufs_err_query_flag_not_readable(uint8_t idn) "query flag idn 0x%"PRIx8" is denied to read"
ufs_err_query_flag_not_writable(uint8_t idn) "query flag idn 0x%"PRIx8" is denied to write"
ufs_err_query_attr_not_readable(uint8_t idn) "query attribute idn 0x%"PRIx8" is denied to read"
ufs_err_query_attr_not_writable(uint8_t idn) "query attribute idn 0x%"PRIx8" is denied to write"
ufs_err_query_invalid_opcode(uint8_t opcode) "query request has invalid opcode. opcode: 0x%"PRIx8""
ufs_err_query_invalid_idn(uint8_t opcode, uint8_t idn) "query request has invalid idn. opcode: 0x%"PRIx8", idn 0x%"PRIx8""
ufs_err_query_invalid_index(uint8_t opcode, uint8_t index) "query request has invalid index. opcode: 0x%"PRIx8", index 0x%"PRIx8""
ufs_err_invalid_trans_code(uint32_t slot, uint8_t trans_code) "request upiu has invalid transaction code. slot: %"PRIu32", trans_code: 0x%"PRIx8""

1
hw/ufs/trace.h Normal file
View File

@ -0,0 +1 @@
#include "trace/trace-hw_ufs.h"

1502
hw/ufs/ufs.c Normal file

File diff suppressed because it is too large Load Diff

131
hw/ufs/ufs.h Normal file
View File

@ -0,0 +1,131 @@
/*
* QEMU UFS
*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
*
* Written by Jeuk Kim <jeuk20.kim@samsung.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef HW_UFS_UFS_H
#define HW_UFS_UFS_H
#include "hw/pci/pci_device.h"
#include "hw/scsi/scsi.h"
#include "block/ufs.h"
#define UFS_MAX_LUS 32
#define UFS_BLOCK_SIZE 4096
typedef struct UfsBusClass {
BusClass parent_class;
bool (*parent_check_address)(BusState *bus, DeviceState *dev, Error **errp);
} UfsBusClass;
typedef struct UfsBus {
SCSIBus parent_bus;
} UfsBus;
#define TYPE_UFS_BUS "ufs-bus"
DECLARE_OBJ_CHECKERS(UfsBus, UfsBusClass, UFS_BUS, TYPE_UFS_BUS)
typedef enum UfsRequestState {
UFS_REQUEST_IDLE = 0,
UFS_REQUEST_READY = 1,
UFS_REQUEST_RUNNING = 2,
UFS_REQUEST_COMPLETE = 3,
UFS_REQUEST_ERROR = 4,
} UfsRequestState;
typedef enum UfsReqResult {
UFS_REQUEST_SUCCESS = 0,
UFS_REQUEST_FAIL = 1,
UFS_REQUEST_NO_COMPLETE = 2,
} UfsReqResult;
typedef struct UfsRequest {
struct UfsHc *hc;
UfsRequestState state;
int slot;
UtpTransferReqDesc utrd;
UtpUpiuReq req_upiu;
UtpUpiuRsp rsp_upiu;
/* for scsi command */
QEMUSGList *sg;
} UfsRequest;
typedef struct UfsLu {
SCSIDevice qdev;
uint8_t lun;
UnitDescriptor unit_desc;
} UfsLu;
typedef struct UfsWLu {
SCSIDevice qdev;
uint8_t lun;
} UfsWLu;
typedef struct UfsParams {
char *serial;
uint8_t nutrs; /* Number of UTP Transfer Request Slots */
uint8_t nutmrs; /* Number of UTP Task Management Request Slots */
} UfsParams;
typedef struct UfsHc {
PCIDevice parent_obj;
UfsBus bus;
MemoryRegion iomem;
UfsReg reg;
UfsParams params;
uint32_t reg_size;
UfsRequest *req_list;
UfsLu *lus[UFS_MAX_LUS];
UfsWLu *report_wlu;
UfsWLu *dev_wlu;
UfsWLu *boot_wlu;
UfsWLu *rpmb_wlu;
DeviceDescriptor device_desc;
GeometryDescriptor geometry_desc;
Attributes attributes;
Flags flags;
qemu_irq irq;
QEMUBH *doorbell_bh;
QEMUBH *complete_bh;
} UfsHc;
#define TYPE_UFS "ufs"
#define UFS(obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS)
#define TYPE_UFS_LU "ufs-lu"
#define UFSLU(obj) OBJECT_CHECK(UfsLu, (obj), TYPE_UFS_LU)
#define TYPE_UFS_WLU "ufs-wlu"
#define UFSWLU(obj) OBJECT_CHECK(UfsWLu, (obj), TYPE_UFS_WLU)
typedef enum UfsQueryFlagPerm {
UFS_QUERY_FLAG_NONE = 0x0,
UFS_QUERY_FLAG_READ = 0x1,
UFS_QUERY_FLAG_SET = 0x2,
UFS_QUERY_FLAG_CLEAR = 0x4,
UFS_QUERY_FLAG_TOGGLE = 0x8,
} UfsQueryFlagPerm;
typedef enum UfsQueryAttrPerm {
UFS_QUERY_ATTR_NONE = 0x0,
UFS_QUERY_ATTR_READ = 0x1,
UFS_QUERY_ATTR_WRITE = 0x2,
} UfsQueryAttrPerm;
static inline bool is_wlun(uint8_t lun)
{
return (lun == UFS_UPIU_REPORT_LUNS_WLUN ||
lun == UFS_UPIU_UFS_DEVICE_WLUN || lun == UFS_UPIU_BOOT_WLUN ||
lun == UFS_UPIU_RPMB_WLUN);
}
#endif /* HW_UFS_UFS_H */

1090
include/block/ufs.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -114,6 +114,7 @@ extern bool pci_available;
#define PCI_DEVICE_ID_REDHAT_NVME 0x0010
#define PCI_DEVICE_ID_REDHAT_PVPANIC 0x0011
#define PCI_DEVICE_ID_REDHAT_ACPI_ERST 0x0012
#define PCI_DEVICE_ID_REDHAT_UFS 0x0013
#define PCI_DEVICE_ID_REDHAT_QXL 0x0100
#define FMT_PCIBUS PRIx64

View File

@ -26,6 +26,7 @@
#define PCI_CLASS_STORAGE_SATA 0x0106
#define PCI_CLASS_STORAGE_SAS 0x0107
#define PCI_CLASS_STORAGE_EXPRESS 0x0108
#define PCI_CLASS_STORAGE_UFS 0x0109
#define PCI_CLASS_STORAGE_OTHER 0x0180
#define PCI_BASE_CLASS_NETWORK 0x02

View File

@ -231,6 +231,7 @@
#define MODE_PAGE_FLEXIBLE_DISK_GEOMETRY 0x05
#define MODE_PAGE_CACHING 0x08
#define MODE_PAGE_AUDIO_CTL 0x0e
#define MODE_PAGE_CONTROL 0x0a
#define MODE_PAGE_POWER 0x1a
#define MODE_PAGE_FAULT_FAIL 0x1c
#define MODE_PAGE_TO_PROTECT 0x1d

View File

@ -138,12 +138,14 @@ static void iothread_instance_finalize(Object *obj)
qemu_sem_destroy(&iothread->init_done_sem);
}
static void iothread_init_gcontext(IOThread *iothread)
static void iothread_init_gcontext(IOThread *iothread, const char *thread_name)
{
GSource *source;
g_autofree char *name = g_strdup_printf("%s aio-context", thread_name);
iothread->worker_context = g_main_context_new();
source = aio_get_g_source(iothread_get_aio_context(iothread));
g_source_set_name(source, name);
g_source_attach(source, iothread->worker_context);
g_source_unref(source);
iothread->main_loop = g_main_loop_new(iothread->worker_context, TRUE);
@ -180,7 +182,7 @@ static void iothread_init(EventLoopBase *base, Error **errp)
{
Error *local_error = NULL;
IOThread *iothread = IOTHREAD(base);
char *thread_name;
g_autofree char *thread_name = NULL;
iothread->stopping = false;
iothread->running = true;
@ -189,11 +191,14 @@ static void iothread_init(EventLoopBase *base, Error **errp)
return;
}
thread_name = g_strdup_printf("IO %s",
object_get_canonical_path_component(OBJECT(base)));
/*
* Init one GMainContext for the iothread unconditionally, even if
* it's not used
*/
iothread_init_gcontext(iothread);
iothread_init_gcontext(iothread, thread_name);
iothread_set_aio_context_params(base, &local_error);
if (local_error) {
@ -206,11 +211,8 @@ static void iothread_init(EventLoopBase *base, Error **errp)
/* This assumes we are called from a thread with useful CPU affinity for us
* to inherit.
*/
thread_name = g_strdup_printf("IO %s",
object_get_canonical_path_component(OBJECT(base)));
qemu_thread_create(&iothread->thread, thread_name, iothread_run,
iothread, QEMU_THREAD_JOINABLE);
g_free(thread_name);
/* Wait for initialization to complete */
while (iothread->thread_id == -1) {

View File

@ -3287,6 +3287,7 @@ if have_system
'hw/ssi',
'hw/timer',
'hw/tpm',
'hw/ufs',
'hw/usb',
'hw/vfio',
'hw/virtio',

View File

@ -269,6 +269,7 @@ qos_test_ss.add(
'virtio-iommu-test.c',
'vmxnet3-test.c',
'igb-test.c',
'ufs-test.c',
)
if config_all_devices.has_key('CONFIG_VIRTIO_SERIAL')

587
tests/qtest/ufs-test.c Normal file
View File

@ -0,0 +1,587 @@
/*
* QTest testcase for UFS
*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qemu/module.h"
#include "qemu/units.h"
#include "libqtest.h"
#include "libqos/qgraph.h"
#include "libqos/pci.h"
#include "scsi/constants.h"
#include "include/block/ufs.h"
/* Test images sizes in Bytes */
#define TEST_IMAGE_SIZE (64 * 1024 * 1024)
/* Timeout for various operations, in seconds. */
#define TIMEOUT_SECONDS 10
/* Maximum PRD entry count */
#define MAX_PRD_ENTRY_COUNT 10
#define PRD_ENTRY_DATA_SIZE 4096
/* Constants to build upiu */
#define UTP_COMMAND_DESCRIPTOR_SIZE 4096
#define UTP_RESPONSE_UPIU_OFFSET 1024
#define UTP_PRDT_UPIU_OFFSET 2048
typedef struct QUfs QUfs;
struct QUfs {
QOSGraphObject obj;
QPCIDevice dev;
QPCIBar bar;
uint64_t utrlba;
uint64_t utmrlba;
uint64_t cmd_desc_addr;
uint64_t data_buffer_addr;
bool enabled;
};
static inline uint32_t ufs_rreg(QUfs *ufs, size_t offset)
{
return qpci_io_readl(&ufs->dev, ufs->bar, offset);
}
static inline void ufs_wreg(QUfs *ufs, size_t offset, uint32_t value)
{
qpci_io_writel(&ufs->dev, ufs->bar, offset, value);
}
static void ufs_wait_for_irq(QUfs *ufs)
{
uint64_t end_time;
uint32_t is;
/* Wait for device to reset as the linux driver does. */
end_time = g_get_monotonic_time() + TIMEOUT_SECONDS * G_TIME_SPAN_SECOND;
do {
qtest_clock_step(ufs->dev.bus->qts, 100);
is = ufs_rreg(ufs, A_IS);
} while (is == 0 && g_get_monotonic_time() < end_time);
}
static UtpTransferReqDesc ufs_build_req_utrd(uint64_t cmd_desc_addr,
uint8_t slot,
uint32_t data_direction,
uint16_t prd_table_length)
{
UtpTransferReqDesc req = { 0 };
uint64_t command_desc_base_addr =
cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE;
req.header.dword_0 =
cpu_to_le32(1 << 28 | data_direction | UFS_UTP_REQ_DESC_INT_CMD);
req.header.dword_2 = cpu_to_le32(UFS_OCS_INVALID_COMMAND_STATUS);
req.command_desc_base_addr_hi = cpu_to_le32(command_desc_base_addr >> 32);
req.command_desc_base_addr_lo =
cpu_to_le32(command_desc_base_addr & 0xffffffff);
req.response_upiu_offset =
cpu_to_le16(UTP_RESPONSE_UPIU_OFFSET / sizeof(uint32_t));
req.response_upiu_length = cpu_to_le16(sizeof(UtpUpiuRsp));
req.prd_table_offset = cpu_to_le16(UTP_PRDT_UPIU_OFFSET / sizeof(uint32_t));
req.prd_table_length = cpu_to_le16(prd_table_length);
return req;
}
static void ufs_send_nop_out(QUfs *ufs, uint8_t slot,
UtpTransferReqDesc *utrd_out, UtpUpiuRsp *rsp_out)
{
/* Build up utp transfer request descriptor */
UtpTransferReqDesc utrd = ufs_build_req_utrd(ufs->cmd_desc_addr, slot,
UFS_UTP_NO_DATA_TRANSFER, 0);
uint64_t utrd_addr = ufs->utrlba + slot * sizeof(UtpTransferReqDesc);
uint64_t req_upiu_addr =
ufs->cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE;
uint64_t rsp_upiu_addr = req_upiu_addr + UTP_RESPONSE_UPIU_OFFSET;
qtest_memwrite(ufs->dev.bus->qts, utrd_addr, &utrd, sizeof(utrd));
/* Build up request upiu */
UtpUpiuReq req_upiu = { 0 };
req_upiu.header.trans_type = UFS_UPIU_TRANSACTION_NOP_OUT;
req_upiu.header.task_tag = slot;
qtest_memwrite(ufs->dev.bus->qts, req_upiu_addr, &req_upiu,
sizeof(req_upiu));
/* Ring Doorbell */
ufs_wreg(ufs, A_UTRLDBR, 1);
ufs_wait_for_irq(ufs);
g_assert_true(FIELD_EX32(ufs_rreg(ufs, A_IS), IS, UTRCS));
ufs_wreg(ufs, A_IS, FIELD_DP32(0, IS, UTRCS, 1));
qtest_memread(ufs->dev.bus->qts, utrd_addr, utrd_out, sizeof(*utrd_out));
qtest_memread(ufs->dev.bus->qts, rsp_upiu_addr, rsp_out, sizeof(*rsp_out));
}
static void ufs_send_query(QUfs *ufs, uint8_t slot, uint8_t query_function,
uint8_t query_opcode, uint8_t idn, uint8_t index,
UtpTransferReqDesc *utrd_out, UtpUpiuRsp *rsp_out)
{
/* Build up utp transfer request descriptor */
UtpTransferReqDesc utrd = ufs_build_req_utrd(ufs->cmd_desc_addr, slot,
UFS_UTP_NO_DATA_TRANSFER, 0);
uint64_t utrd_addr = ufs->utrlba + slot * sizeof(UtpTransferReqDesc);
uint64_t req_upiu_addr =
ufs->cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE;
uint64_t rsp_upiu_addr = req_upiu_addr + UTP_RESPONSE_UPIU_OFFSET;
qtest_memwrite(ufs->dev.bus->qts, utrd_addr, &utrd, sizeof(utrd));
/* Build up request upiu */
UtpUpiuReq req_upiu = { 0 };
req_upiu.header.trans_type = UFS_UPIU_TRANSACTION_QUERY_REQ;
req_upiu.header.query_func = query_function;
req_upiu.header.task_tag = slot;
/*
* QEMU UFS does not currently support Write descriptor and Write attribute,
* so the value of data_segment_length is always 0.
*/
req_upiu.header.data_segment_length = 0;
req_upiu.qr.opcode = query_opcode;
req_upiu.qr.idn = idn;
req_upiu.qr.index = index;
qtest_memwrite(ufs->dev.bus->qts, req_upiu_addr, &req_upiu,
sizeof(req_upiu));
/* Ring Doorbell */
ufs_wreg(ufs, A_UTRLDBR, 1);
ufs_wait_for_irq(ufs);
g_assert_true(FIELD_EX32(ufs_rreg(ufs, A_IS), IS, UTRCS));
ufs_wreg(ufs, A_IS, FIELD_DP32(0, IS, UTRCS, 1));
qtest_memread(ufs->dev.bus->qts, utrd_addr, utrd_out, sizeof(*utrd_out));
qtest_memread(ufs->dev.bus->qts, rsp_upiu_addr, rsp_out, sizeof(*rsp_out));
}
static void ufs_send_scsi_command(QUfs *ufs, uint8_t slot, uint8_t lun,
const uint8_t *cdb, const uint8_t *data_in,
size_t data_in_len, uint8_t *data_out,
size_t data_out_len,
UtpTransferReqDesc *utrd_out,
UtpUpiuRsp *rsp_out)
{
/* Build up PRDT */
UfshcdSgEntry entries[MAX_PRD_ENTRY_COUNT] = {
0,
};
uint8_t flags;
uint16_t prd_table_length, i;
uint32_t data_direction, data_len;
uint64_t req_upiu_addr =
ufs->cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE;
uint64_t prdt_addr = req_upiu_addr + UTP_PRDT_UPIU_OFFSET;
g_assert_true(data_in_len < MAX_PRD_ENTRY_COUNT * PRD_ENTRY_DATA_SIZE);
g_assert_true(data_out_len < MAX_PRD_ENTRY_COUNT * PRD_ENTRY_DATA_SIZE);
if (data_in_len > 0) {
g_assert_nonnull(data_in);
data_direction = UFS_UTP_HOST_TO_DEVICE;
data_len = data_in_len;
flags = UFS_UPIU_CMD_FLAGS_WRITE;
} else if (data_out_len > 0) {
g_assert_nonnull(data_out);
data_direction = UFS_UTP_DEVICE_TO_HOST;
data_len = data_out_len;
flags = UFS_UPIU_CMD_FLAGS_READ;
} else {
data_direction = UFS_UTP_NO_DATA_TRANSFER;
data_len = 0;
flags = UFS_UPIU_CMD_FLAGS_NONE;
}
prd_table_length = DIV_ROUND_UP(data_len, PRD_ENTRY_DATA_SIZE);
qtest_memset(ufs->dev.bus->qts, ufs->data_buffer_addr, 0,
MAX_PRD_ENTRY_COUNT * PRD_ENTRY_DATA_SIZE);
if (data_in_len) {
qtest_memwrite(ufs->dev.bus->qts, ufs->data_buffer_addr, data_in,
data_in_len);
}
for (i = 0; i < prd_table_length; i++) {
entries[i].addr =
cpu_to_le64(ufs->data_buffer_addr + i * sizeof(UfshcdSgEntry));
if (i + 1 != prd_table_length) {
entries[i].size = cpu_to_le32(PRD_ENTRY_DATA_SIZE - 1);
} else {
entries[i].size = cpu_to_le32(
data_len - (PRD_ENTRY_DATA_SIZE * (prd_table_length - 1)) - 1);
}
}
qtest_memwrite(ufs->dev.bus->qts, prdt_addr, entries,
prd_table_length * sizeof(UfshcdSgEntry));
/* Build up utp transfer request descriptor */
UtpTransferReqDesc utrd = ufs_build_req_utrd(
ufs->cmd_desc_addr, slot, data_direction, prd_table_length);
uint64_t utrd_addr = ufs->utrlba + slot * sizeof(UtpTransferReqDesc);
uint64_t rsp_upiu_addr = req_upiu_addr + UTP_RESPONSE_UPIU_OFFSET;
qtest_memwrite(ufs->dev.bus->qts, utrd_addr, &utrd, sizeof(utrd));
/* Build up request upiu */
UtpUpiuReq req_upiu = { 0 };
req_upiu.header.trans_type = UFS_UPIU_TRANSACTION_COMMAND;
req_upiu.header.flags = flags;
req_upiu.header.lun = lun;
req_upiu.header.task_tag = slot;
req_upiu.sc.exp_data_transfer_len = cpu_to_be32(data_len);
memcpy(req_upiu.sc.cdb, cdb, UFS_CDB_SIZE);
qtest_memwrite(ufs->dev.bus->qts, req_upiu_addr, &req_upiu,
sizeof(req_upiu));
/* Ring Doorbell */
ufs_wreg(ufs, A_UTRLDBR, 1);
ufs_wait_for_irq(ufs);
g_assert_true(FIELD_EX32(ufs_rreg(ufs, A_IS), IS, UTRCS));
ufs_wreg(ufs, A_IS, FIELD_DP32(0, IS, UTRCS, 1));
qtest_memread(ufs->dev.bus->qts, utrd_addr, utrd_out, sizeof(*utrd_out));
qtest_memread(ufs->dev.bus->qts, rsp_upiu_addr, rsp_out, sizeof(*rsp_out));
if (data_out_len) {
qtest_memread(ufs->dev.bus->qts, ufs->data_buffer_addr, data_out,
data_out_len);
}
}
/**
* Initialize Ufs host controller and logical unit.
* After running this function, you can make a transfer request to the UFS.
*/
static void ufs_init(QUfs *ufs, QGuestAllocator *alloc)
{
uint64_t end_time;
uint32_t nutrs, nutmrs;
uint32_t hcs, is, ucmdarg2, cap;
uint32_t hce = 0, ie = 0;
UtpTransferReqDesc utrd;
UtpUpiuRsp rsp_upiu;
ufs->bar = qpci_iomap(&ufs->dev, 0, NULL);
qpci_device_enable(&ufs->dev);
/* Start host controller initialization */
hce = FIELD_DP32(hce, HCE, HCE, 1);
ufs_wreg(ufs, A_HCE, hce);
/* Wait for device to reset */
end_time = g_get_monotonic_time() + TIMEOUT_SECONDS * G_TIME_SPAN_SECOND;
do {
qtest_clock_step(ufs->dev.bus->qts, 100);
hce = FIELD_EX32(ufs_rreg(ufs, A_HCE), HCE, HCE);
} while (hce == 0 && g_get_monotonic_time() < end_time);
g_assert_cmpuint(hce, ==, 1);
/* Enable interrupt */
ie = FIELD_DP32(ie, IE, UCCE, 1);
ie = FIELD_DP32(ie, IE, UHESE, 1);
ie = FIELD_DP32(ie, IE, UHXSE, 1);
ie = FIELD_DP32(ie, IE, UPMSE, 1);
ufs_wreg(ufs, A_IE, ie);
/* Send DME_LINK_STARTUP uic command */
hcs = ufs_rreg(ufs, A_HCS);
g_assert_true(FIELD_EX32(hcs, HCS, UCRDY));
ufs_wreg(ufs, A_UCMDARG1, 0);
ufs_wreg(ufs, A_UCMDARG2, 0);
ufs_wreg(ufs, A_UCMDARG3, 0);
ufs_wreg(ufs, A_UICCMD, UFS_UIC_CMD_DME_LINK_STARTUP);
is = ufs_rreg(ufs, A_IS);
g_assert_true(FIELD_EX32(is, IS, UCCS));
ufs_wreg(ufs, A_IS, FIELD_DP32(0, IS, UCCS, 1));
ucmdarg2 = ufs_rreg(ufs, A_UCMDARG2);
g_assert_cmpuint(ucmdarg2, ==, 0);
is = ufs_rreg(ufs, A_IS);
g_assert_cmpuint(is, ==, 0);
hcs = ufs_rreg(ufs, A_HCS);
g_assert_true(FIELD_EX32(hcs, HCS, DP));
g_assert_true(FIELD_EX32(hcs, HCS, UTRLRDY));
g_assert_true(FIELD_EX32(hcs, HCS, UTMRLRDY));
g_assert_true(FIELD_EX32(hcs, HCS, UCRDY));
/* Enable all interrupt functions */
ie = FIELD_DP32(ie, IE, UTRCE, 1);
ie = FIELD_DP32(ie, IE, UEE, 1);
ie = FIELD_DP32(ie, IE, UPMSE, 1);
ie = FIELD_DP32(ie, IE, UHXSE, 1);
ie = FIELD_DP32(ie, IE, UHESE, 1);
ie = FIELD_DP32(ie, IE, UTMRCE, 1);
ie = FIELD_DP32(ie, IE, UCCE, 1);
ie = FIELD_DP32(ie, IE, DFEE, 1);
ie = FIELD_DP32(ie, IE, HCFEE, 1);
ie = FIELD_DP32(ie, IE, SBFEE, 1);
ie = FIELD_DP32(ie, IE, CEFEE, 1);
ufs_wreg(ufs, A_IE, ie);
ufs_wreg(ufs, A_UTRIACR, 0);
/* Enable tranfer request and task management request */
cap = ufs_rreg(ufs, A_CAP);
nutrs = FIELD_EX32(cap, CAP, NUTRS) + 1;
nutmrs = FIELD_EX32(cap, CAP, NUTMRS) + 1;
ufs->cmd_desc_addr =
guest_alloc(alloc, nutrs * UTP_COMMAND_DESCRIPTOR_SIZE);
ufs->data_buffer_addr =
guest_alloc(alloc, MAX_PRD_ENTRY_COUNT * PRD_ENTRY_DATA_SIZE);
ufs->utrlba = guest_alloc(alloc, nutrs * sizeof(UtpTransferReqDesc));
ufs->utmrlba = guest_alloc(alloc, nutmrs * sizeof(UtpTaskReqDesc));
ufs_wreg(ufs, A_UTRLBA, ufs->utrlba & 0xffffffff);
ufs_wreg(ufs, A_UTRLBAU, ufs->utrlba >> 32);
ufs_wreg(ufs, A_UTMRLBA, ufs->utmrlba & 0xffffffff);
ufs_wreg(ufs, A_UTMRLBAU, ufs->utmrlba >> 32);
ufs_wreg(ufs, A_UTRLRSR, 1);
ufs_wreg(ufs, A_UTMRLRSR, 1);
/* Send nop out to test transfer request */
ufs_send_nop_out(ufs, 0, &utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
/* Set fDeviceInit flag via query request */
ufs_send_query(ufs, 0, UFS_UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST,
UFS_UPIU_QUERY_OPCODE_SET_FLAG,
UFS_QUERY_FLAG_IDN_FDEVICEINIT, 0, &utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
/* Wait for device to reset */
end_time = g_get_monotonic_time() + TIMEOUT_SECONDS * G_TIME_SPAN_SECOND;
do {
qtest_clock_step(ufs->dev.bus->qts, 100);
ufs_send_query(ufs, 0, UFS_UPIU_QUERY_FUNC_STANDARD_READ_REQUEST,
UFS_UPIU_QUERY_OPCODE_READ_FLAG,
UFS_QUERY_FLAG_IDN_FDEVICEINIT, 0, &utrd, &rsp_upiu);
} while (be32_to_cpu(rsp_upiu.qr.value) != 0 &&
g_get_monotonic_time() < end_time);
g_assert_cmpuint(be32_to_cpu(rsp_upiu.qr.value), ==, 0);
ufs->enabled = true;
}
static void ufs_exit(QUfs *ufs, QGuestAllocator *alloc)
{
if (ufs->enabled) {
guest_free(alloc, ufs->utrlba);
guest_free(alloc, ufs->utmrlba);
guest_free(alloc, ufs->cmd_desc_addr);
guest_free(alloc, ufs->data_buffer_addr);
}
qpci_iounmap(&ufs->dev, ufs->bar);
}
static void *ufs_get_driver(void *obj, const char *interface)
{
QUfs *ufs = obj;
if (!g_strcmp0(interface, "pci-device")) {
return &ufs->dev;
}
fprintf(stderr, "%s not present in ufs\n", interface);
g_assert_not_reached();
}
static void *ufs_create(void *pci_bus, QGuestAllocator *alloc, void *addr)
{
QUfs *ufs = g_new0(QUfs, 1);
QPCIBus *bus = pci_bus;
qpci_device_init(&ufs->dev, bus, addr);
ufs->obj.get_driver = ufs_get_driver;
return &ufs->obj;
}
static void ufstest_reg_read(void *obj, void *data, QGuestAllocator *alloc)
{
QUfs *ufs = obj;
uint32_t cap;
ufs->bar = qpci_iomap(&ufs->dev, 0, NULL);
qpci_device_enable(&ufs->dev);
cap = ufs_rreg(ufs, A_CAP);
g_assert_cmpuint(FIELD_EX32(cap, CAP, NUTRS), ==, 31);
g_assert_cmpuint(FIELD_EX32(cap, CAP, NUTMRS), ==, 7);
g_assert_cmpuint(FIELD_EX32(cap, CAP, 64AS), ==, 1);
qpci_iounmap(&ufs->dev, ufs->bar);
}
static void ufstest_init(void *obj, void *data, QGuestAllocator *alloc)
{
QUfs *ufs = obj;
uint8_t buf[4096] = { 0 };
const uint8_t report_luns_cdb[UFS_CDB_SIZE] = {
/* allocation length 4096 */
REPORT_LUNS, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x00, 0x00, 0x00
};
const uint8_t test_unit_ready_cdb[UFS_CDB_SIZE] = {
TEST_UNIT_READY,
};
UtpTransferReqDesc utrd;
UtpUpiuRsp rsp_upiu;
ufs_init(ufs, alloc);
/* Check REPORT_LUNS */
ufs_send_scsi_command(ufs, 0, 0, report_luns_cdb, NULL, 0, buf, sizeof(buf),
&utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
g_assert_cmpuint(rsp_upiu.header.scsi_status, ==, GOOD);
/* LUN LIST LENGTH should be 8, in big endian */
g_assert_cmpuint(buf[3], ==, 8);
/* There is one logical unit whose lun is 0 */
g_assert_cmpuint(buf[9], ==, 0);
/* Check TEST_UNIT_READY */
ufs_send_scsi_command(ufs, 0, 0, test_unit_ready_cdb, NULL, 0, NULL, 0,
&utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
g_assert_cmpuint(rsp_upiu.header.scsi_status, ==, GOOD);
ufs_exit(ufs, alloc);
}
static void ufstest_read_write(void *obj, void *data, QGuestAllocator *alloc)
{
QUfs *ufs = obj;
uint8_t read_buf[4096] = { 0 };
uint8_t write_buf[4096] = { 0 };
const uint8_t read_capacity_cdb[UFS_CDB_SIZE] = {
/* allocation length 4096 */
SERVICE_ACTION_IN_16,
SAI_READ_CAPACITY_16,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x10,
0x00,
0x00,
0x00
};
const uint8_t read_cdb[UFS_CDB_SIZE] = {
/* READ(10) to LBA 0, transfer length 1 */
READ_10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
};
const uint8_t write_cdb[UFS_CDB_SIZE] = {
/* WRITE(10) to LBA 0, transfer length 1 */
WRITE_10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
};
uint32_t block_size;
UtpTransferReqDesc utrd;
UtpUpiuRsp rsp_upiu;
ufs_init(ufs, alloc);
/* Read capacity */
ufs_send_scsi_command(ufs, 0, 1, read_capacity_cdb, NULL, 0, read_buf,
sizeof(read_buf), &utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
g_assert_cmpuint(rsp_upiu.header.scsi_status, ==,
UFS_COMMAND_RESULT_SUCESS);
block_size = ldl_be_p(&read_buf[8]);
g_assert_cmpuint(block_size, ==, 4096);
/* Write data */
memset(write_buf, rand() % 255 + 1, block_size);
ufs_send_scsi_command(ufs, 0, 1, write_cdb, write_buf, block_size, NULL, 0,
&utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
g_assert_cmpuint(rsp_upiu.header.scsi_status, ==,
UFS_COMMAND_RESULT_SUCESS);
/* Read data and verify */
ufs_send_scsi_command(ufs, 0, 1, read_cdb, NULL, 0, read_buf, block_size,
&utrd, &rsp_upiu);
g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), ==, UFS_OCS_SUCCESS);
g_assert_cmpuint(rsp_upiu.header.scsi_status, ==,
UFS_COMMAND_RESULT_SUCESS);
g_assert_cmpint(memcmp(read_buf, write_buf, block_size), ==, 0);
ufs_exit(ufs, alloc);
}
static void drive_destroy(void *path)
{
unlink(path);
g_free(path);
qos_invalidate_command_line();
}
static char *drive_create(void)
{
int fd, ret;
char *t_path;
/* Create a temporary raw image */
fd = g_file_open_tmp("qtest-ufs.XXXXXX", &t_path, NULL);
g_assert_cmpint(fd, >=, 0);
ret = ftruncate(fd, TEST_IMAGE_SIZE);
g_assert_cmpint(ret, ==, 0);
close(fd);
g_test_queue_destroy(drive_destroy, t_path);
return t_path;
}
static void *ufs_blk_test_setup(GString *cmd_line, void *arg)
{
char *tmp_path = drive_create();
g_string_append_printf(cmd_line,
" -blockdev file,filename=%s,node-name=drv1 "
"-device ufs-lu,bus=ufs0,drive=drv1,lun=1 ",
tmp_path);
return arg;
}
static void ufs_register_nodes(void)
{
const char *arch;
QOSGraphEdgeOptions edge_opts = {
.before_cmd_line = "-blockdev null-co,node-name=drv0,read-zeroes=on",
.after_cmd_line = "-device ufs-lu,bus=ufs0,drive=drv0,lun=0",
.extra_device_opts = "addr=04.0,id=ufs0,nutrs=32,nutmrs=8"
};
QOSGraphTestOptions io_test_opts = {
.before = ufs_blk_test_setup,
};
add_qpci_address(&edge_opts, &(QPCIAddress){ .devfn = QPCI_DEVFN(4, 0) });
qos_node_create_driver("ufs", ufs_create);
qos_node_consumes("ufs", "pci-bus", &edge_opts);
qos_node_produces("ufs", "pci-device");
qos_add_test("reg-read", "ufs", ufstest_reg_read, NULL);
/*
* Check architecture
* TODO: Enable ufs io tests for ppc64
*/
arch = qtest_get_arch();
if (!strcmp(arch, "ppc64")) {
g_test_message("Skipping ufs io tests for ppc64");
return;
}
qos_add_test("init", "ufs", ufstest_init, NULL);
qos_add_test("read-write", "ufs", ufstest_read_write, &io_test_opts);
}
libqos_init(ufs_register_nodes);