From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896374; cv=none; d=zohomail.com; s=zohoarc; b=jWJIrACkogEvPCWtK1ZfQVoEZtQB7Gv2Ic8aGFR8To9kHIbfGRBu8iKe2A2cEXGF5dKSOrHuP29tXhEomvP76U4S9aON1PNEudvZ3tZXbv7K/FNyQvHBKLH0KUULK64CcjVD8rI2GQ0qj529YaLVZgDn0L17+TVjSrm/Rg+K6jU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896374; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7hovzHLw7LTmBMrZldCkpepojPhJNTMuljgJwoCHRmg=; b=T2fzjH98jNaeQInvIRQWg+4tFU8fQrU7Qum3hGE/mi0NktHD0U2iOUVEHJfXaSbLxsr2OG3EswNdtNCO387IYCegAOmxdJ4+WbDttmZaPfGrPUZsU/8Df5U24/JMRQU52uRrhor8U/nMSj3X9Twh5Dzktb9ouwIu4aE9aNfncyE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896374233932.8675966669987; Thu, 24 Aug 2023 09:59:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfZ-0005uC-59; Thu, 24 Aug 2023 12:58:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfX-0005pg-6c for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfR-0003Oi-Ub for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:50 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-296-KFGPd0BAP96bCYtrCz6zxg-1; Thu, 24 Aug 2023 12:58:41 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9FD1F3C14AA5; Thu, 24 Aug 2023 16:58:40 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7562D140E962; Thu, 24 Aug 2023 16:58:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896325; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7hovzHLw7LTmBMrZldCkpepojPhJNTMuljgJwoCHRmg=; b=jKxSFOv0R7XvQXCmYkqutoA/eCffyefPou1WT4z/kJ7WIba6SmT3LezetKmgvylpLP4iZj hQKIGV18z8xxw5kgcwPx1KjivGuW8jC6Ftkrxquxrw36O6oEPI2efAuTPh+sMzb90/zmLY /MGca0O97FgvRWFY7Ga03emCgx0eQIg= X-MC-Unique: KFGPd0BAP96bCYtrCz6zxg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Jeuk Kim Subject: [PULL 1/8] hw/ufs: Initial commit for emulated Universal-Flash-Storage Date: Thu, 24 Aug 2023 12:58:26 -0400 Message-ID: <20230824165833.1689137-2-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896375238100001 Content-Type: text/plain; charset="utf-8" From: Jeuk Kim Universal Flash Storage (UFS) is a high-performance mass storage device with a serial interface. It is primarily used as a high-performance data storage device for embedded applications. This commit contains code for UFS device to be recognized as a UFS PCI device. Patches to handle UFS logical unit and Transfer Request will follow. Signed-off-by: Jeuk Kim Reviewed-by: Stefan Hajnoczi Message-id: 9f3db32fe1c708090a6bb764d456973b5abef55f.1691062912.git.jeuk20.= kim@samsung.com Signed-off-by: Stefan Hajnoczi --- MAINTAINERS | 6 + docs/specs/pci-ids.rst | 2 + meson.build | 1 + hw/ufs/trace.h | 1 + hw/ufs/ufs.h | 42 ++ include/block/ufs.h | 1090 ++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 1 + include/hw/pci/pci_ids.h | 1 + hw/ufs/ufs.c | 278 ++++++++++ hw/Kconfig | 1 + hw/meson.build | 1 + hw/ufs/Kconfig | 4 + hw/ufs/meson.build | 1 + hw/ufs/trace-events | 32 ++ 14 files changed, 1461 insertions(+) create mode 100644 hw/ufs/trace.h create mode 100644 hw/ufs/ufs.h create mode 100644 include/block/ufs.h create mode 100644 hw/ufs/ufs.c create mode 100644 hw/ufs/Kconfig create mode 100644 hw/ufs/meson.build create mode 100644 hw/ufs/trace-events diff --git a/MAINTAINERS b/MAINTAINERS index 6111b6b4d9..7da40dabc2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2256,6 +2256,12 @@ F: tests/qtest/nvme-test.c F: docs/system/devices/nvme.rst T: git git://git.infradead.org/qemu-nvme.git nvme-next =20 +ufs +M: Jeuk Kim +S: Supported +F: hw/ufs/* +F: include/block/ufs.h + megasas M: Hannes Reinecke L: qemu-block@nongnu.org diff --git a/docs/specs/pci-ids.rst b/docs/specs/pci-ids.rst index e302bea484..d6707fa069 100644 --- a/docs/specs/pci-ids.rst +++ b/docs/specs/pci-ids.rst @@ -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``) =20 All these devices are documented in :doc:`index`. =20 diff --git a/meson.build b/meson.build index 98e68ef0b1..76578b2bf2 100644 --- a/meson.build +++ b/meson.build @@ -3277,6 +3277,7 @@ if have_system 'hw/ssi', 'hw/timer', 'hw/tpm', + 'hw/ufs', 'hw/usb', 'hw/vfio', 'hw/virtio', diff --git a/hw/ufs/trace.h b/hw/ufs/trace.h new file mode 100644 index 0000000000..2dbd6397c3 --- /dev/null +++ b/hw/ufs/trace.h @@ -0,0 +1 @@ +#include "trace/trace-hw_ufs.h" diff --git a/hw/ufs/ufs.h b/hw/ufs/ufs.h new file mode 100644 index 0000000000..d9d195caec --- /dev/null +++ b/hw/ufs/ufs.h @@ -0,0 +1,42 @@ +/* + * QEMU UFS + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved. + * + * Written by Jeuk Kim + * + * 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 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; + MemoryRegion iomem; + UfsReg reg; + UfsParams params; + uint32_t reg_size; + + qemu_irq irq; + QEMUBH *doorbell_bh; + QEMUBH *complete_bh; +} UfsHc; + +#define TYPE_UFS "ufs" +#define UFS(obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS) + +#endif /* HW_UFS_UFS_H */ diff --git a/include/block/ufs.h b/include/block/ufs.h new file mode 100644 index 0000000000..e70ee23183 --- /dev/null +++ b/include/block/ufs.h @@ -0,0 +1,1090 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef BLOCK_UFS_H +#define BLOCK_UFS_H + +#include "hw/registerfields.h" + +typedef struct QEMU_PACKED UfsReg { + uint32_t cap; + uint32_t rsvd0; + uint32_t ver; + uint32_t rsvd1; + uint32_t hcpid; + uint32_t hcmid; + uint32_t ahit; + uint32_t rsvd2; + uint32_t is; + uint32_t ie; + uint32_t rsvd3[2]; + uint32_t hcs; + uint32_t hce; + uint32_t uecpa; + uint32_t uecdl; + uint32_t uecn; + uint32_t uect; + uint32_t uecdme; + uint32_t utriacr; + uint32_t utrlba; + uint32_t utrlbau; + uint32_t utrldbr; + uint32_t utrlclr; + uint32_t utrlrsr; + uint32_t utrlcnr; + uint32_t rsvd4[2]; + uint32_t utmrlba; + uint32_t utmrlbau; + uint32_t utmrldbr; + uint32_t utmrlclr; + uint32_t utmrlrsr; + uint32_t rsvd5[3]; + uint32_t uiccmd; + uint32_t ucmdarg1; + uint32_t ucmdarg2; + uint32_t ucmdarg3; + uint32_t rsvd6[4]; + uint32_t rsvd7[4]; + uint32_t rsvd8[16]; + uint32_t ccap; +} UfsReg; + +REG32(CAP, offsetof(UfsReg, cap)) + FIELD(CAP, NUTRS, 0, 5) + FIELD(CAP, RTT, 8, 8) + FIELD(CAP, NUTMRS, 16, 3) + FIELD(CAP, AUTOH8, 23, 1) + FIELD(CAP, 64AS, 24, 1) + FIELD(CAP, OODDS, 25, 1) + FIELD(CAP, UICDMETMS, 26, 1) + FIELD(CAP, CS, 28, 1) +REG32(VER, offsetof(UfsReg, ver)) +REG32(HCPID, offsetof(UfsReg, hcpid)) +REG32(HCMID, offsetof(UfsReg, hcmid)) +REG32(AHIT, offsetof(UfsReg, ahit)) +REG32(IS, offsetof(UfsReg, is)) + FIELD(IS, UTRCS, 0, 1) + FIELD(IS, UDEPRI, 1, 1) + FIELD(IS, UE, 2, 1) + FIELD(IS, UTMS, 3, 1) + FIELD(IS, UPMS, 4, 1) + FIELD(IS, UHXS, 5, 1) + FIELD(IS, UHES, 6, 1) + FIELD(IS, ULLS, 7, 1) + FIELD(IS, ULSS, 8, 1) + FIELD(IS, UTMRCS, 9, 1) + FIELD(IS, UCCS, 10, 1) + FIELD(IS, DFES, 11, 1) + FIELD(IS, UTPES, 12, 1) + FIELD(IS, HCFES, 16, 1) + FIELD(IS, SBFES, 17, 1) + FIELD(IS, CEFES, 18, 1) +REG32(IE, offsetof(UfsReg, ie)) + FIELD(IE, UTRCE, 0, 1) + FIELD(IE, UDEPRIE, 1, 1) + FIELD(IE, UEE, 2, 1) + FIELD(IE, UTMSE, 3, 1) + FIELD(IE, UPMSE, 4, 1) + FIELD(IE, UHXSE, 5, 1) + FIELD(IE, UHESE, 6, 1) + FIELD(IE, ULLSE, 7, 1) + FIELD(IE, ULSSE, 8, 1) + FIELD(IE, UTMRCE, 9, 1) + FIELD(IE, UCCE, 10, 1) + FIELD(IE, DFEE, 11, 1) + FIELD(IE, UTPEE, 12, 1) + FIELD(IE, HCFEE, 16, 1) + FIELD(IE, SBFEE, 17, 1) + FIELD(IE, CEFEE, 18, 1) +REG32(HCS, offsetof(UfsReg, hcs)) + FIELD(HCS, DP, 0, 1) + FIELD(HCS, UTRLRDY, 1, 1) + FIELD(HCS, UTMRLRDY, 2, 1) + FIELD(HCS, UCRDY, 3, 1) + FIELD(HCS, UPMCRS, 8, 3) +REG32(HCE, offsetof(UfsReg, hce)) + FIELD(HCE, HCE, 0, 1) + FIELD(HCE, CGE, 1, 1) +REG32(UECPA, offsetof(UfsReg, uecpa)) +REG32(UECDL, offsetof(UfsReg, uecdl)) +REG32(UECN, offsetof(UfsReg, uecn)) +REG32(UECT, offsetof(UfsReg, uect)) +REG32(UECDME, offsetof(UfsReg, uecdme)) +REG32(UTRIACR, offsetof(UfsReg, utriacr)) +REG32(UTRLBA, offsetof(UfsReg, utrlba)) + FIELD(UTRLBA, UTRLBA, 9, 22) +REG32(UTRLBAU, offsetof(UfsReg, utrlbau)) +REG32(UTRLDBR, offsetof(UfsReg, utrldbr)) +REG32(UTRLCLR, offsetof(UfsReg, utrlclr)) +REG32(UTRLRSR, offsetof(UfsReg, utrlrsr)) +REG32(UTRLCNR, offsetof(UfsReg, utrlcnr)) +REG32(UTMRLBA, offsetof(UfsReg, utmrlba)) + FIELD(UTMRLBA, UTMRLBA, 9, 22) +REG32(UTMRLBAU, offsetof(UfsReg, utmrlbau)) +REG32(UTMRLDBR, offsetof(UfsReg, utmrldbr)) +REG32(UTMRLCLR, offsetof(UfsReg, utmrlclr)) +REG32(UTMRLRSR, offsetof(UfsReg, utmrlrsr)) +REG32(UICCMD, offsetof(UfsReg, uiccmd)) +REG32(UCMDARG1, offsetof(UfsReg, ucmdarg1)) +REG32(UCMDARG2, offsetof(UfsReg, ucmdarg2)) +REG32(UCMDARG3, offsetof(UfsReg, ucmdarg3)) +REG32(CCAP, offsetof(UfsReg, ccap)) + +#define UFS_INTR_MASK \ + ((1 << R_IS_CEFES_SHIFT) | (1 << R_IS_SBFES_SHIFT) | \ + (1 << R_IS_HCFES_SHIFT) | (1 << R_IS_UTPES_SHIFT) | \ + (1 << R_IS_DFES_SHIFT) | (1 << R_IS_UCCS_SHIFT) | \ + (1 << R_IS_UTMRCS_SHIFT) | (1 << R_IS_ULSS_SHIFT) | \ + (1 << R_IS_ULLS_SHIFT) | (1 << R_IS_UHES_SHIFT) | \ + (1 << R_IS_UHXS_SHIFT) | (1 << R_IS_UPMS_SHIFT) | \ + (1 << R_IS_UTMS_SHIFT) | (1 << R_IS_UE_SHIFT) | \ + (1 << R_IS_UDEPRI_SHIFT) | (1 << R_IS_UTRCS_SHIFT)) + +#define UFS_UPIU_HEADER_TRANSACTION_TYPE_SHIFT 24 +#define UFS_UPIU_HEADER_TRANSACTION_TYPE_MASK 0xff +#define UFS_UPIU_HEADER_TRANSACTION_TYPE(dword0) \ + ((be32_to_cpu(dword0) >> UFS_UPIU_HEADER_TRANSACTION_TYPE_SHIFT) & \ + UFS_UPIU_HEADER_TRANSACTION_TYPE_MASK) + +#define UFS_UPIU_HEADER_QUERY_FUNC_SHIFT 16 +#define UFS_UPIU_HEADER_QUERY_FUNC_MASK 0xff +#define UFS_UPIU_HEADER_QUERY_FUNC(dword1) \ + ((be32_to_cpu(dword1) >> UFS_UPIU_HEADER_QUERY_FUNC_SHIFT) & \ + UFS_UPIU_HEADER_QUERY_FUNC_MASK) + +#define UFS_UPIU_HEADER_DATA_SEGMENT_LENGTH_SHIFT 0 +#define UFS_UPIU_HEADER_DATA_SEGMENT_LENGTH_MASK 0xffff +#define UFS_UPIU_HEADER_DATA_SEGMENT_LENGTH(dword2) \ + ((be32_to_cpu(dword2) >> UFS_UPIU_HEADER_DATA_SEGMENT_LENGTH_SHIFT) & \ + UFS_UPIU_HEADER_DATA_SEGMENT_LENGTH_MASK) + +typedef struct QEMU_PACKED DeviceDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint8_t device; + uint8_t device_class; + uint8_t device_sub_class; + uint8_t protocol; + uint8_t number_lu; + uint8_t number_wlu; + uint8_t boot_enable; + uint8_t descr_access_en; + uint8_t init_power_mode; + uint8_t high_priority_lun; + uint8_t secure_removal_type; + uint8_t security_lu; + uint8_t background_ops_term_lat; + uint8_t init_active_icc_level; + uint16_t spec_version; + uint16_t manufacture_date; + uint8_t manufacturer_name; + uint8_t product_name; + uint8_t serial_number; + uint8_t oem_id; + uint16_t manufacturer_id; + uint8_t ud_0_base_offset; + uint8_t ud_config_p_length; + uint8_t device_rtt_cap; + uint16_t periodic_rtc_update; + uint8_t ufs_features_support; + uint8_t ffu_timeout; + uint8_t queue_depth; + uint16_t device_version; + uint8_t num_secure_wp_area; + uint32_t psa_max_data_size; + uint8_t psa_state_timeout; + uint8_t product_revision_level; + uint8_t reserved[36]; + uint32_t extended_ufs_features_support; + uint8_t write_booster_buffer_preserve_user_space_en; + uint8_t write_booster_buffer_type; + uint32_t num_shared_write_booster_buffer_alloc_units; +} DeviceDescriptor; + +typedef struct QEMU_PACKED GeometryDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint8_t media_technology; + uint8_t reserved; + uint64_t total_raw_device_capacity; + uint8_t max_number_lu; + uint32_t segment_size; + uint8_t allocation_unit_size; + uint8_t min_addr_block_size; + uint8_t optimal_read_block_size; + uint8_t optimal_write_block_size; + uint8_t max_in_buffer_size; + uint8_t max_out_buffer_size; + uint8_t rpmb_read_write_size; + uint8_t dynamic_capacity_resource_policy; + uint8_t data_ordering; + uint8_t max_context_id_number; + uint8_t sys_data_tag_unit_size; + uint8_t sys_data_tag_res_size; + uint8_t supported_sec_r_types; + uint16_t supported_memory_types; + uint32_t system_code_max_n_alloc_u; + uint16_t system_code_cap_adj_fac; + uint32_t non_persist_max_n_alloc_u; + uint16_t non_persist_cap_adj_fac; + uint32_t enhanced_1_max_n_alloc_u; + uint16_t enhanced_1_cap_adj_fac; + uint32_t enhanced_2_max_n_alloc_u; + uint16_t enhanced_2_cap_adj_fac; + uint32_t enhanced_3_max_n_alloc_u; + uint16_t enhanced_3_cap_adj_fac; + uint32_t enhanced_4_max_n_alloc_u; + uint16_t enhanced_4_cap_adj_fac; + uint32_t optimal_logical_block_size; + uint8_t reserved2[7]; + uint32_t write_booster_buffer_max_n_alloc_units; + uint8_t device_max_write_booster_l_us; + uint8_t write_booster_buffer_cap_adj_fac; + uint8_t supported_write_booster_buffer_user_space_reduction_types; + uint8_t supported_write_booster_buffer_types; +} GeometryDescriptor; + +#define UFS_GEOMETRY_CAPACITY_SHIFT 9 + +typedef struct QEMU_PACKED UnitDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint8_t unit_index; + uint8_t lu_enable; + uint8_t boot_lun_id; + uint8_t lu_write_protect; + uint8_t lu_queue_depth; + uint8_t psa_sensitive; + uint8_t memory_type; + uint8_t data_reliability; + uint8_t logical_block_size; + uint64_t logical_block_count; + uint32_t erase_block_size; + uint8_t provisioning_type; + uint64_t phy_mem_resource_count; + uint16_t context_capabilities; + uint8_t large_unit_granularity_m1; + uint8_t reserved[6]; + uint32_t lu_num_write_booster_buffer_alloc_units; +} UnitDescriptor; + +typedef struct QEMU_PACKED RpmbUnitDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint8_t unit_index; + uint8_t lu_enable; + uint8_t boot_lun_id; + uint8_t lu_write_protect; + uint8_t lu_queue_depth; + uint8_t psa_sensitive; + uint8_t memory_type; + uint8_t reserved; + uint8_t logical_block_size; + uint64_t logical_block_count; + uint32_t erase_block_size; + uint8_t provisioning_type; + uint64_t phy_mem_resource_count; + uint8_t reserved2[3]; +} RpmbUnitDescriptor; + +typedef struct QEMU_PACKED PowerParametersDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint16_t active_icc_levels_vcc[16]; + uint16_t active_icc_levels_vccq[16]; + uint16_t active_icc_levels_vccq_2[16]; +} PowerParametersDescriptor; + +typedef struct QEMU_PACKED InterconnectDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint16_t bcd_unipro_version; + uint16_t bcd_mphy_version; +} InterconnectDescriptor; + +typedef struct QEMU_PACKED StringDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint16_t UC[126]; +} StringDescriptor; + +typedef struct QEMU_PACKED DeviceHealthDescriptor { + uint8_t length; + uint8_t descriptor_idn; + uint8_t pre_eol_info; + uint8_t device_life_time_est_a; + uint8_t device_life_time_est_b; + uint8_t vendor_prop_info[32]; + uint32_t refresh_total_count; + uint32_t refresh_progress; +} DeviceHealthDescriptor; + +typedef struct QEMU_PACKED Flags { + uint8_t reserved; + uint8_t device_init; + uint8_t permanent_wp_en; + uint8_t power_on_wp_en; + uint8_t background_ops_en; + uint8_t device_life_span_mode_en; + uint8_t purge_enable; + uint8_t refresh_enable; + uint8_t phy_resource_removal; + uint8_t busy_rtc; + uint8_t reserved2; + uint8_t permanently_disable_fw_update; + uint8_t reserved3[2]; + uint8_t wb_en; + uint8_t wb_buffer_flush_en; + uint8_t wb_buffer_flush_during_hibernate; + uint8_t reserved4[2]; +} Flags; + +typedef struct Attributes { + uint8_t boot_lun_en; + uint8_t reserved; + uint8_t current_power_mode; + uint8_t active_icc_level; + uint8_t out_of_order_data_en; + uint8_t background_op_status; + uint8_t purge_status; + uint8_t max_data_in_size; + uint8_t max_data_out_size; + uint32_t dyn_cap_needed; + uint8_t ref_clk_freq; + uint8_t config_descr_lock; + uint8_t max_num_of_rtt; + uint16_t exception_event_control; + uint16_t exception_event_status; + uint32_t seconds_passed; + uint16_t context_conf; + uint8_t device_ffu_status; + uint8_t psa_state; + uint32_t psa_data_size; + uint8_t ref_clk_gating_wait_time; + uint8_t device_case_rough_temperaure; + uint8_t device_too_high_temp_boundary; + uint8_t device_too_low_temp_boundary; + uint8_t throttling_status; + uint8_t wb_buffer_flush_status; + uint8_t available_wb_buffer_size; + uint8_t wb_buffer_life_time_est; + uint32_t current_wb_buffer_size; + uint8_t refresh_status; + uint8_t refresh_freq; + uint8_t refresh_unit; + uint8_t refresh_method; +} Attributes; + +#define UFS_TRANSACTION_SPECIFIC_FIELD_SIZE 20 +#define UFS_MAX_QUERY_DATA_SIZE 256 + +/* Command response result code */ +typedef enum CommandRespCode { + COMMAND_RESULT_SUCESS =3D 0x00, + COMMAND_RESULT_FAIL =3D 0x01, +} CommandRespCode; + +enum { + UFS_UPIU_FLAG_UNDERFLOW =3D 0x20, + UFS_UPIU_FLAG_OVERFLOW =3D 0x40, +}; + +typedef struct QEMU_PACKED UtpUpiuHeader { + uint8_t trans_type; + uint8_t flags; + uint8_t lun; + uint8_t task_tag; + uint8_t iid_cmd_set_type; + uint8_t query_func; + uint8_t response; + uint8_t scsi_status; + uint8_t ehs_len; + uint8_t device_inf; + uint16_t data_segment_length; +} UtpUpiuHeader; + +/* + * The code below is copied from the linux kernel + * ("include/uapi/scsi/scsi_bsg_ufs.h") and modified to fit the qemu style. + */ + +typedef struct QEMU_PACKED UtpUpiuQuery { + uint8_t opcode; + uint8_t idn; + uint8_t index; + uint8_t selector; + uint16_t reserved_osf; + uint16_t length; + uint32_t value; + uint32_t reserved[2]; + /* EHS length should be 0. We don't have to worry about EHS area. */ + uint8_t data[UFS_MAX_QUERY_DATA_SIZE]; +} UtpUpiuQuery; + +#define UFS_CDB_SIZE 16 + +/* + * struct UtpUpiuCmd - Command UPIU structure + * @data_transfer_len: Data Transfer Length DW-3 + * @cdb: Command Descriptor Block CDB DW-4 to DW-7 + */ +typedef struct QEMU_PACKED UtpUpiuCmd { + uint32_t exp_data_transfer_len; + uint8_t cdb[UFS_CDB_SIZE]; +} UtpUpiuCmd; + +/* + * struct UtpUpiuReq - general upiu request structure + * @header:UPIU header structure DW-0 to DW-2 + * @sc: fields structure for scsi command DW-3 to DW-7 + * @qr: fields structure for query request DW-3 to DW-7 + * @uc: use utp_upiu_query to host the 4 dwords of uic command + */ +typedef struct QEMU_PACKED UtpUpiuReq { + UtpUpiuHeader header; + union { + UtpUpiuCmd sc; + UtpUpiuQuery qr; + }; +} UtpUpiuReq; + +/* + * The code below is copied from the linux kernel ("include/ufs/ufshci.h")= and + * modified to fit the qemu style. + */ + +enum { + PWR_OK =3D 0x0, + PWR_LOCAL =3D 0x01, + PWR_REMOTE =3D 0x02, + PWR_BUSY =3D 0x03, + PWR_ERROR_CAP =3D 0x04, + PWR_FATAL_ERROR =3D 0x05, +}; + +/* UIC Commands */ +enum uic_cmd_dme { + UIC_CMD_DME_GET =3D 0x01, + UIC_CMD_DME_SET =3D 0x02, + UIC_CMD_DME_PEER_GET =3D 0x03, + UIC_CMD_DME_PEER_SET =3D 0x04, + UIC_CMD_DME_POWERON =3D 0x10, + UIC_CMD_DME_POWEROFF =3D 0x11, + UIC_CMD_DME_ENABLE =3D 0x12, + UIC_CMD_DME_RESET =3D 0x14, + UIC_CMD_DME_END_PT_RST =3D 0x15, + UIC_CMD_DME_LINK_STARTUP =3D 0x16, + UIC_CMD_DME_HIBER_ENTER =3D 0x17, + UIC_CMD_DME_HIBER_EXIT =3D 0x18, + UIC_CMD_DME_TEST_MODE =3D 0x1A, +}; + +/* UIC Config result code / Generic error code */ +enum { + UIC_CMD_RESULT_SUCCESS =3D 0x00, + UIC_CMD_RESULT_INVALID_ATTR =3D 0x01, + UIC_CMD_RESULT_FAILURE =3D 0x01, + UIC_CMD_RESULT_INVALID_ATTR_VALUE =3D 0x02, + UIC_CMD_RESULT_READ_ONLY_ATTR =3D 0x03, + UIC_CMD_RESULT_WRITE_ONLY_ATTR =3D 0x04, + UIC_CMD_RESULT_BAD_INDEX =3D 0x05, + UIC_CMD_RESULT_LOCKED_ATTR =3D 0x06, + UIC_CMD_RESULT_BAD_TEST_FEATURE_INDEX =3D 0x07, + UIC_CMD_RESULT_PEER_COMM_FAILURE =3D 0x08, + UIC_CMD_RESULT_BUSY =3D 0x09, + UIC_CMD_RESULT_DME_FAILURE =3D 0x0A, +}; + +#define MASK_UIC_COMMAND_RESULT 0xFF + +/* + * Request Descriptor Definitions + */ + +/* Transfer request command type */ +enum { + UTP_CMD_TYPE_SCSI =3D 0x0, + UTP_CMD_TYPE_UFS =3D 0x1, + UTP_CMD_TYPE_DEV_MANAGE =3D 0x2, +}; + +/* To accommodate UFS2.0 required Command type */ +enum { + UTP_CMD_TYPE_UFS_STORAGE =3D 0x1, +}; + +enum { + UTP_SCSI_COMMAND =3D 0x00000000, + UTP_NATIVE_UFS_COMMAND =3D 0x10000000, + UTP_DEVICE_MANAGEMENT_FUNCTION =3D 0x20000000, + UTP_REQ_DESC_INT_CMD =3D 0x01000000, + UTP_REQ_DESC_CRYPTO_ENABLE_CMD =3D 0x00800000, +}; + +/* UTP Transfer Request Data Direction (DD) */ +enum { + UTP_NO_DATA_TRANSFER =3D 0x00000000, + UTP_HOST_TO_DEVICE =3D 0x02000000, + UTP_DEVICE_TO_HOST =3D 0x04000000, +}; + +/* Overall command status values */ +enum UtpOcsCodes { + OCS_SUCCESS =3D 0x0, + OCS_INVALID_CMD_TABLE_ATTR =3D 0x1, + OCS_INVALID_PRDT_ATTR =3D 0x2, + OCS_MISMATCH_DATA_BUF_SIZE =3D 0x3, + OCS_MISMATCH_RESP_UPIU_SIZE =3D 0x4, + OCS_PEER_COMM_FAILURE =3D 0x5, + OCS_ABORTED =3D 0x6, + OCS_FATAL_ERROR =3D 0x7, + OCS_DEVICE_FATAL_ERROR =3D 0x8, + OCS_INVALID_CRYPTO_CONFIG =3D 0x9, + OCS_GENERAL_CRYPTO_ERROR =3D 0xa, + OCS_INVALID_COMMAND_STATUS =3D 0xf, +}; + +enum { + MASK_OCS =3D 0x0F, +}; + +/* + * struct UfshcdSgEntry - UFSHCI PRD Entry + * @addr: Physical address; DW-0 and DW-1. + * @reserved: Reserved for future use DW-2 + * @size: size of physical segment DW-3 + */ +typedef struct QEMU_PACKED UfshcdSgEntry { + uint64_t addr; + uint32_t reserved; + uint32_t size; + /* + * followed by variant-specific fields if + * CONFIG_SCSI_UFS_VARIABLE_SG_ENTRY_SIZE has been defined. + */ +} UfshcdSgEntry; + +/* + * struct RequestDescHeader - Descriptor Header common to both UTRD and UT= MRD + * @dword0: Descriptor Header DW0 + * @dword1: Descriptor Header DW1 + * @dword2: Descriptor Header DW2 + * @dword3: Descriptor Header DW3 + */ +typedef struct QEMU_PACKED RequestDescHeader { + uint32_t dword_0; + uint32_t dword_1; + uint32_t dword_2; + uint32_t dword_3; +} RequestDescHeader; + +/* + * struct UtpTransferReqDesc - UTP Transfer Request Descriptor (UTRD) + * @header: UTRD header DW-0 to DW-3 + * @command_desc_base_addr_lo: UCD base address low DW-4 + * @command_desc_base_addr_hi: UCD base address high DW-5 + * @response_upiu_length: response UPIU length DW-6 + * @response_upiu_offset: response UPIU offset DW-6 + * @prd_table_length: Physical region descriptor length DW-7 + * @prd_table_offset: Physical region descriptor offset DW-7 + */ +typedef struct QEMU_PACKED UtpTransferReqDesc { + /* DW 0-3 */ + RequestDescHeader header; + + /* DW 4-5*/ + uint32_t command_desc_base_addr_lo; + uint32_t command_desc_base_addr_hi; + + /* DW 6 */ + uint16_t response_upiu_length; + uint16_t response_upiu_offset; + + /* DW 7 */ + uint16_t prd_table_length; + uint16_t prd_table_offset; +} UtpTransferReqDesc; + +/* + * UTMRD structure. + */ +typedef struct QEMU_PACKED UtpTaskReqDesc { + /* DW 0-3 */ + RequestDescHeader header; + + /* DW 4-11 - Task request UPIU structure */ + struct { + UtpUpiuHeader req_header; + uint32_t input_param1; + uint32_t input_param2; + uint32_t input_param3; + uint32_t reserved1[2]; + } upiu_req; + + /* DW 12-19 - Task Management Response UPIU structure */ + struct { + UtpUpiuHeader rsp_header; + uint32_t output_param1; + uint32_t output_param2; + uint32_t reserved2[3]; + } upiu_rsp; +} UtpTaskReqDesc; + +/* + * The code below is copied from the linux kernel ("include/ufs/ufs.h") and + * modified to fit the qemu style. + */ + +#define GENERAL_UPIU_REQUEST_SIZE (sizeof(UtpUpiuReq)) +#define QUERY_DESC_MAX_SIZE 255 +#define QUERY_DESC_MIN_SIZE 2 +#define QUERY_DESC_HDR_SIZE 2 +#define QUERY_OSF_SIZE (GENERAL_UPIU_REQUEST_SIZE - (sizeof(UtpUpiuHeader)= )) +#define UFS_SENSE_SIZE 18 + +/* + * UFS device may have standard LUs and LUN id could be from 0x00 to + * 0x7F. Standard LUs use "Peripheral Device Addressing Format". + * UFS device may also have the Well Known LUs (also referred as W-LU) + * which again could be from 0x00 to 0x7F. For W-LUs, device only use + * the "Extended Addressing Format" which means the W-LUNs would be + * from 0xc100 (SCSI_W_LUN_BASE) onwards. + * This means max. LUN number reported from UFS device could be 0xC17F. + */ +#define UFS_UPIU_MAX_UNIT_NUM_ID 0x7F +#define UFS_UPIU_WLUN_ID (1 << 7) + +/* WriteBooster buffer is available only for the logical unit from 0 to 7 = */ +#define UFS_UPIU_MAX_WB_LUN_ID 8 + +/* + * WriteBooster buffer lifetime has a limit setted by vendor. + * If it is over the limit, WriteBooster feature will be disabled. + */ +#define UFS_WB_EXCEED_LIFETIME 0x0B + +/* + * In UFS Spec, the Extra Header Segment (EHS) starts from byte 32 in UPIU + * request/response packet + */ +#define EHS_OFFSET_IN_RESPONSE 32 + +/* Well known logical unit id in LUN field of UPIU */ +enum { + UFS_UPIU_REPORT_LUNS_WLUN =3D 0x81, + UFS_UPIU_UFS_DEVICE_WLUN =3D 0xD0, + UFS_UPIU_BOOT_WLUN =3D 0xB0, + UFS_UPIU_RPMB_WLUN =3D 0xC4, +}; + +/* + * UFS Protocol Information Unit related definitions + */ + +/* Task management functions */ +enum { + UFS_ABORT_TASK =3D 0x01, + UFS_ABORT_TASK_SET =3D 0x02, + UFS_CLEAR_TASK_SET =3D 0x04, + UFS_LOGICAL_RESET =3D 0x08, + UFS_QUERY_TASK =3D 0x80, + UFS_QUERY_TASK_SET =3D 0x81, +}; + +/* UTP UPIU Transaction Codes Initiator to Target */ +enum { + UPIU_TRANSACTION_NOP_OUT =3D 0x00, + UPIU_TRANSACTION_COMMAND =3D 0x01, + UPIU_TRANSACTION_DATA_OUT =3D 0x02, + UPIU_TRANSACTION_TASK_REQ =3D 0x04, + UPIU_TRANSACTION_QUERY_REQ =3D 0x16, +}; + +/* UTP UPIU Transaction Codes Target to Initiator */ +enum { + UPIU_TRANSACTION_NOP_IN =3D 0x20, + UPIU_TRANSACTION_RESPONSE =3D 0x21, + UPIU_TRANSACTION_DATA_IN =3D 0x22, + UPIU_TRANSACTION_TASK_RSP =3D 0x24, + UPIU_TRANSACTION_READY_XFER =3D 0x31, + UPIU_TRANSACTION_QUERY_RSP =3D 0x36, + UPIU_TRANSACTION_REJECT_UPIU =3D 0x3F, +}; + +/* UPIU Read/Write flags */ +enum { + UPIU_CMD_FLAGS_NONE =3D 0x00, + UPIU_CMD_FLAGS_WRITE =3D 0x20, + UPIU_CMD_FLAGS_READ =3D 0x40, +}; + +/* UPIU Task Attributes */ +enum { + UPIU_TASK_ATTR_SIMPLE =3D 0x00, + UPIU_TASK_ATTR_ORDERED =3D 0x01, + UPIU_TASK_ATTR_HEADQ =3D 0x02, + UPIU_TASK_ATTR_ACA =3D 0x03, +}; + +/* UPIU Query request function */ +enum { + UPIU_QUERY_FUNC_STANDARD_READ_REQUEST =3D 0x01, + UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST =3D 0x81, +}; + +/* Flag idn for Query Requests*/ +enum flag_idn { + QUERY_FLAG_IDN_FDEVICEINIT =3D 0x01, + QUERY_FLAG_IDN_PERMANENT_WPE =3D 0x02, + QUERY_FLAG_IDN_PWR_ON_WPE =3D 0x03, + QUERY_FLAG_IDN_BKOPS_EN =3D 0x04, + QUERY_FLAG_IDN_LIFE_SPAN_MODE_ENABLE =3D 0x05, + QUERY_FLAG_IDN_PURGE_ENABLE =3D 0x06, + QUERY_FLAG_IDN_REFRESH_ENABLE =3D 0x07, + QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL =3D 0x08, + QUERY_FLAG_IDN_BUSY_RTC =3D 0x09, + QUERY_FLAG_IDN_RESERVED3 =3D 0x0A, + QUERY_FLAG_IDN_PERMANENTLY_DISABLE_FW_UPDATE =3D 0x0B, + QUERY_FLAG_IDN_WB_EN =3D 0x0E, + QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN =3D 0x0F, + QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8 =3D 0x10, + QUERY_FLAG_IDN_HPB_RESET =3D 0x11, + QUERY_FLAG_IDN_HPB_EN =3D 0x12, + QUERY_FLAG_IDN_COUNT, +}; + +/* Attribute idn for Query requests */ +enum attr_idn { + QUERY_ATTR_IDN_BOOT_LU_EN =3D 0x00, + QUERY_ATTR_IDN_MAX_HPB_SINGLE_CMD =3D 0x01, + QUERY_ATTR_IDN_POWER_MODE =3D 0x02, + QUERY_ATTR_IDN_ACTIVE_ICC_LVL =3D 0x03, + QUERY_ATTR_IDN_OOO_DATA_EN =3D 0x04, + QUERY_ATTR_IDN_BKOPS_STATUS =3D 0x05, + QUERY_ATTR_IDN_PURGE_STATUS =3D 0x06, + QUERY_ATTR_IDN_MAX_DATA_IN =3D 0x07, + QUERY_ATTR_IDN_MAX_DATA_OUT =3D 0x08, + QUERY_ATTR_IDN_DYN_CAP_NEEDED =3D 0x09, + QUERY_ATTR_IDN_REF_CLK_FREQ =3D 0x0A, + QUERY_ATTR_IDN_CONF_DESC_LOCK =3D 0x0B, + QUERY_ATTR_IDN_MAX_NUM_OF_RTT =3D 0x0C, + QUERY_ATTR_IDN_EE_CONTROL =3D 0x0D, + QUERY_ATTR_IDN_EE_STATUS =3D 0x0E, + QUERY_ATTR_IDN_SECONDS_PASSED =3D 0x0F, + QUERY_ATTR_IDN_CNTX_CONF =3D 0x10, + QUERY_ATTR_IDN_CORR_PRG_BLK_NUM =3D 0x11, + QUERY_ATTR_IDN_RESERVED2 =3D 0x12, + QUERY_ATTR_IDN_RESERVED3 =3D 0x13, + QUERY_ATTR_IDN_FFU_STATUS =3D 0x14, + QUERY_ATTR_IDN_PSA_STATE =3D 0x15, + QUERY_ATTR_IDN_PSA_DATA_SIZE =3D 0x16, + QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME =3D 0x17, + QUERY_ATTR_IDN_CASE_ROUGH_TEMP =3D 0x18, + QUERY_ATTR_IDN_HIGH_TEMP_BOUND =3D 0x19, + QUERY_ATTR_IDN_LOW_TEMP_BOUND =3D 0x1A, + QUERY_ATTR_IDN_THROTTLING_STATUS =3D 0x1B, + QUERY_ATTR_IDN_WB_FLUSH_STATUS =3D 0x1C, + QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE =3D 0x1D, + QUERY_ATTR_IDN_WB_BUFF_LIFE_TIME_EST =3D 0x1E, + QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE =3D 0x1F, + QUERY_ATTR_IDN_REFRESH_STATUS =3D 0x2C, + QUERY_ATTR_IDN_REFRESH_FREQ =3D 0x2D, + QUERY_ATTR_IDN_REFRESH_UNIT =3D 0x2E, + QUERY_ATTR_IDN_COUNT, +}; + +/* Descriptor idn for Query requests */ +enum desc_idn { + QUERY_DESC_IDN_DEVICE =3D 0x0, + QUERY_DESC_IDN_CONFIGURATION =3D 0x1, + QUERY_DESC_IDN_UNIT =3D 0x2, + QUERY_DESC_IDN_RFU_0 =3D 0x3, + QUERY_DESC_IDN_INTERCONNECT =3D 0x4, + QUERY_DESC_IDN_STRING =3D 0x5, + QUERY_DESC_IDN_RFU_1 =3D 0x6, + QUERY_DESC_IDN_GEOMETRY =3D 0x7, + QUERY_DESC_IDN_POWER =3D 0x8, + QUERY_DESC_IDN_HEALTH =3D 0x9, + QUERY_DESC_IDN_MAX, +}; + +enum desc_header_offset { + QUERY_DESC_LENGTH_OFFSET =3D 0x00, + QUERY_DESC_DESC_TYPE_OFFSET =3D 0x01, +}; + +/* Unit descriptor parameters offsets in bytes*/ +enum unit_desc_param { + UNIT_DESC_PARAM_LEN =3D 0x0, + UNIT_DESC_PARAM_TYPE =3D 0x1, + UNIT_DESC_PARAM_UNIT_INDEX =3D 0x2, + UNIT_DESC_PARAM_LU_ENABLE =3D 0x3, + UNIT_DESC_PARAM_BOOT_LUN_ID =3D 0x4, + UNIT_DESC_PARAM_LU_WR_PROTECT =3D 0x5, + UNIT_DESC_PARAM_LU_Q_DEPTH =3D 0x6, + UNIT_DESC_PARAM_PSA_SENSITIVE =3D 0x7, + UNIT_DESC_PARAM_MEM_TYPE =3D 0x8, + UNIT_DESC_PARAM_DATA_RELIABILITY =3D 0x9, + UNIT_DESC_PARAM_LOGICAL_BLK_SIZE =3D 0xA, + UNIT_DESC_PARAM_LOGICAL_BLK_COUNT =3D 0xB, + UNIT_DESC_PARAM_ERASE_BLK_SIZE =3D 0x13, + UNIT_DESC_PARAM_PROVISIONING_TYPE =3D 0x17, + UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT =3D 0x18, + UNIT_DESC_PARAM_CTX_CAPABILITIES =3D 0x20, + UNIT_DESC_PARAM_LARGE_UNIT_SIZE_M1 =3D 0x22, + UNIT_DESC_PARAM_HPB_LU_MAX_ACTIVE_RGNS =3D 0x23, + UNIT_DESC_PARAM_HPB_PIN_RGN_START_OFF =3D 0x25, + UNIT_DESC_PARAM_HPB_NUM_PIN_RGNS =3D 0x27, + UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS =3D 0x29, +}; + +/* RPMB Unit descriptor parameters offsets in bytes*/ +enum rpmb_unit_desc_param { + RPMB_UNIT_DESC_PARAM_LEN =3D 0x0, + RPMB_UNIT_DESC_PARAM_TYPE =3D 0x1, + RPMB_UNIT_DESC_PARAM_UNIT_INDEX =3D 0x2, + RPMB_UNIT_DESC_PARAM_LU_ENABLE =3D 0x3, + RPMB_UNIT_DESC_PARAM_BOOT_LUN_ID =3D 0x4, + RPMB_UNIT_DESC_PARAM_LU_WR_PROTECT =3D 0x5, + RPMB_UNIT_DESC_PARAM_LU_Q_DEPTH =3D 0x6, + RPMB_UNIT_DESC_PARAM_PSA_SENSITIVE =3D 0x7, + RPMB_UNIT_DESC_PARAM_MEM_TYPE =3D 0x8, + RPMB_UNIT_DESC_PARAM_REGION_EN =3D 0x9, + RPMB_UNIT_DESC_PARAM_LOGICAL_BLK_SIZE =3D 0xA, + RPMB_UNIT_DESC_PARAM_LOGICAL_BLK_COUNT =3D 0xB, + RPMB_UNIT_DESC_PARAM_REGION0_SIZE =3D 0x13, + RPMB_UNIT_DESC_PARAM_REGION1_SIZE =3D 0x14, + RPMB_UNIT_DESC_PARAM_REGION2_SIZE =3D 0x15, + RPMB_UNIT_DESC_PARAM_REGION3_SIZE =3D 0x16, + RPMB_UNIT_DESC_PARAM_PROVISIONING_TYPE =3D 0x17, + RPMB_UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT =3D 0x18, +}; + +/* Device descriptor parameters offsets in bytes*/ +enum device_desc_param { + DEVICE_DESC_PARAM_LEN =3D 0x0, + DEVICE_DESC_PARAM_TYPE =3D 0x1, + DEVICE_DESC_PARAM_DEVICE_TYPE =3D 0x2, + DEVICE_DESC_PARAM_DEVICE_CLASS =3D 0x3, + DEVICE_DESC_PARAM_DEVICE_SUB_CLASS =3D 0x4, + DEVICE_DESC_PARAM_PRTCL =3D 0x5, + DEVICE_DESC_PARAM_NUM_LU =3D 0x6, + DEVICE_DESC_PARAM_NUM_WLU =3D 0x7, + DEVICE_DESC_PARAM_BOOT_ENBL =3D 0x8, + DEVICE_DESC_PARAM_DESC_ACCSS_ENBL =3D 0x9, + DEVICE_DESC_PARAM_INIT_PWR_MODE =3D 0xA, + DEVICE_DESC_PARAM_HIGH_PR_LUN =3D 0xB, + DEVICE_DESC_PARAM_SEC_RMV_TYPE =3D 0xC, + DEVICE_DESC_PARAM_SEC_LU =3D 0xD, + DEVICE_DESC_PARAM_BKOP_TERM_LT =3D 0xE, + DEVICE_DESC_PARAM_ACTVE_ICC_LVL =3D 0xF, + DEVICE_DESC_PARAM_SPEC_VER =3D 0x10, + DEVICE_DESC_PARAM_MANF_DATE =3D 0x12, + DEVICE_DESC_PARAM_MANF_NAME =3D 0x14, + DEVICE_DESC_PARAM_PRDCT_NAME =3D 0x15, + DEVICE_DESC_PARAM_SN =3D 0x16, + DEVICE_DESC_PARAM_OEM_ID =3D 0x17, + DEVICE_DESC_PARAM_MANF_ID =3D 0x18, + DEVICE_DESC_PARAM_UD_OFFSET =3D 0x1A, + DEVICE_DESC_PARAM_UD_LEN =3D 0x1B, + DEVICE_DESC_PARAM_RTT_CAP =3D 0x1C, + DEVICE_DESC_PARAM_FRQ_RTC =3D 0x1D, + DEVICE_DESC_PARAM_UFS_FEAT =3D 0x1F, + DEVICE_DESC_PARAM_FFU_TMT =3D 0x20, + DEVICE_DESC_PARAM_Q_DPTH =3D 0x21, + DEVICE_DESC_PARAM_DEV_VER =3D 0x22, + DEVICE_DESC_PARAM_NUM_SEC_WPA =3D 0x24, + DEVICE_DESC_PARAM_PSA_MAX_DATA =3D 0x25, + DEVICE_DESC_PARAM_PSA_TMT =3D 0x29, + DEVICE_DESC_PARAM_PRDCT_REV =3D 0x2A, + DEVICE_DESC_PARAM_HPB_VER =3D 0x40, + DEVICE_DESC_PARAM_HPB_CONTROL =3D 0x42, + DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP =3D 0x4F, + DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN =3D 0x53, + DEVICE_DESC_PARAM_WB_TYPE =3D 0x54, + DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS =3D 0x55, +}; + +/* Interconnect descriptor parameters offsets in bytes*/ +enum interconnect_desc_param { + INTERCONNECT_DESC_PARAM_LEN =3D 0x0, + INTERCONNECT_DESC_PARAM_TYPE =3D 0x1, + INTERCONNECT_DESC_PARAM_UNIPRO_VER =3D 0x2, + INTERCONNECT_DESC_PARAM_MPHY_VER =3D 0x4, +}; + +/* Geometry descriptor parameters offsets in bytes*/ +enum geometry_desc_param { + GEOMETRY_DESC_PARAM_LEN =3D 0x0, + GEOMETRY_DESC_PARAM_TYPE =3D 0x1, + GEOMETRY_DESC_PARAM_DEV_CAP =3D 0x4, + GEOMETRY_DESC_PARAM_MAX_NUM_LUN =3D 0xC, + GEOMETRY_DESC_PARAM_SEG_SIZE =3D 0xD, + GEOMETRY_DESC_PARAM_ALLOC_UNIT_SIZE =3D 0x11, + GEOMETRY_DESC_PARAM_MIN_BLK_SIZE =3D 0x12, + GEOMETRY_DESC_PARAM_OPT_RD_BLK_SIZE =3D 0x13, + GEOMETRY_DESC_PARAM_OPT_WR_BLK_SIZE =3D 0x14, + GEOMETRY_DESC_PARAM_MAX_IN_BUF_SIZE =3D 0x15, + GEOMETRY_DESC_PARAM_MAX_OUT_BUF_SIZE =3D 0x16, + GEOMETRY_DESC_PARAM_RPMB_RW_SIZE =3D 0x17, + GEOMETRY_DESC_PARAM_DYN_CAP_RSRC_PLC =3D 0x18, + GEOMETRY_DESC_PARAM_DATA_ORDER =3D 0x19, + GEOMETRY_DESC_PARAM_MAX_NUM_CTX =3D 0x1A, + GEOMETRY_DESC_PARAM_TAG_UNIT_SIZE =3D 0x1B, + GEOMETRY_DESC_PARAM_TAG_RSRC_SIZE =3D 0x1C, + GEOMETRY_DESC_PARAM_SEC_RM_TYPES =3D 0x1D, + GEOMETRY_DESC_PARAM_MEM_TYPES =3D 0x1E, + GEOMETRY_DESC_PARAM_SCM_MAX_NUM_UNITS =3D 0x20, + GEOMETRY_DESC_PARAM_SCM_CAP_ADJ_FCTR =3D 0x24, + GEOMETRY_DESC_PARAM_NPM_MAX_NUM_UNITS =3D 0x26, + GEOMETRY_DESC_PARAM_NPM_CAP_ADJ_FCTR =3D 0x2A, + GEOMETRY_DESC_PARAM_ENM1_MAX_NUM_UNITS =3D 0x2C, + GEOMETRY_DESC_PARAM_ENM1_CAP_ADJ_FCTR =3D 0x30, + GEOMETRY_DESC_PARAM_ENM2_MAX_NUM_UNITS =3D 0x32, + GEOMETRY_DESC_PARAM_ENM2_CAP_ADJ_FCTR =3D 0x36, + GEOMETRY_DESC_PARAM_ENM3_MAX_NUM_UNITS =3D 0x38, + GEOMETRY_DESC_PARAM_ENM3_CAP_ADJ_FCTR =3D 0x3C, + GEOMETRY_DESC_PARAM_ENM4_MAX_NUM_UNITS =3D 0x3E, + GEOMETRY_DESC_PARAM_ENM4_CAP_ADJ_FCTR =3D 0x42, + GEOMETRY_DESC_PARAM_OPT_LOG_BLK_SIZE =3D 0x44, + GEOMETRY_DESC_PARAM_HPB_REGION_SIZE =3D 0x48, + GEOMETRY_DESC_PARAM_HPB_NUMBER_LU =3D 0x49, + GEOMETRY_DESC_PARAM_HPB_SUBREGION_SIZE =3D 0x4A, + GEOMETRY_DESC_PARAM_HPB_MAX_ACTIVE_REGS =3D 0x4B, + GEOMETRY_DESC_PARAM_WB_MAX_ALLOC_UNITS =3D 0x4F, + GEOMETRY_DESC_PARAM_WB_MAX_WB_LUNS =3D 0x53, + GEOMETRY_DESC_PARAM_WB_BUFF_CAP_ADJ =3D 0x54, + GEOMETRY_DESC_PARAM_WB_SUP_RED_TYPE =3D 0x55, + GEOMETRY_DESC_PARAM_WB_SUP_WB_TYPE =3D 0x56, +}; + +/* Health descriptor parameters offsets in bytes*/ +enum health_desc_param { + HEALTH_DESC_PARAM_LEN =3D 0x0, + HEALTH_DESC_PARAM_TYPE =3D 0x1, + HEALTH_DESC_PARAM_EOL_INFO =3D 0x2, + HEALTH_DESC_PARAM_LIFE_TIME_EST_A =3D 0x3, + HEALTH_DESC_PARAM_LIFE_TIME_EST_B =3D 0x4, +}; + +/* WriteBooster buffer mode */ +enum { + WB_BUF_MODE_LU_DEDICATED =3D 0x0, + WB_BUF_MODE_SHARED =3D 0x1, +}; + +/* + * Logical Unit Write Protect + * 00h: LU not write protected + * 01h: LU write protected when fPowerOnWPEn =3D1 + * 02h: LU permanently write protected when fPermanentWPEn =3D1 + */ +enum ufs_lu_wp_type { + UFS_LU_NO_WP =3D 0x00, + UFS_LU_POWER_ON_WP =3D 0x01, + UFS_LU_PERM_WP =3D 0x02, +}; + +/* UTP QUERY Transaction Specific Fields OpCode */ +enum query_opcode { + UPIU_QUERY_OPCODE_NOP =3D 0x0, + UPIU_QUERY_OPCODE_READ_DESC =3D 0x1, + UPIU_QUERY_OPCODE_WRITE_DESC =3D 0x2, + UPIU_QUERY_OPCODE_READ_ATTR =3D 0x3, + UPIU_QUERY_OPCODE_WRITE_ATTR =3D 0x4, + UPIU_QUERY_OPCODE_READ_FLAG =3D 0x5, + UPIU_QUERY_OPCODE_SET_FLAG =3D 0x6, + UPIU_QUERY_OPCODE_CLEAR_FLAG =3D 0x7, + UPIU_QUERY_OPCODE_TOGGLE_FLAG =3D 0x8, +}; + +/* Query response result code */ +typedef enum QueryRespCode { + QUERY_RESULT_SUCCESS =3D 0x00, + QUERY_RESULT_NOT_READABLE =3D 0xF6, + QUERY_RESULT_NOT_WRITEABLE =3D 0xF7, + QUERY_RESULT_ALREADY_WRITTEN =3D 0xF8, + QUERY_RESULT_INVALID_LENGTH =3D 0xF9, + QUERY_RESULT_INVALID_VALUE =3D 0xFA, + QUERY_RESULT_INVALID_SELECTOR =3D 0xFB, + QUERY_RESULT_INVALID_INDEX =3D 0xFC, + QUERY_RESULT_INVALID_IDN =3D 0xFD, + QUERY_RESULT_INVALID_OPCODE =3D 0xFE, + QUERY_RESULT_GENERAL_FAILURE =3D 0xFF, +} QueryRespCode; + +/* UTP Transfer Request Command Type (CT) */ +enum { + UPIU_COMMAND_SET_TYPE_SCSI =3D 0x0, + UPIU_COMMAND_SET_TYPE_UFS =3D 0x1, + UPIU_COMMAND_SET_TYPE_QUERY =3D 0x2, +}; + +/* Task management service response */ +enum { + UPIU_TASK_MANAGEMENT_FUNC_COMPL =3D 0x00, + UPIU_TASK_MANAGEMENT_FUNC_NOT_SUPPORTED =3D 0x04, + UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED =3D 0x08, + UPIU_TASK_MANAGEMENT_FUNC_FAILED =3D 0x05, + UPIU_INCORRECT_LOGICAL_UNIT_NO =3D 0x09, +}; + +/* UFS device power modes */ +enum ufs_dev_pwr_mode { + UFS_ACTIVE_PWR_MODE =3D 1, + UFS_SLEEP_PWR_MODE =3D 2, + UFS_POWERDOWN_PWR_MODE =3D 3, + UFS_DEEPSLEEP_PWR_MODE =3D 4, +}; + +/* + * struct UtpCmdRsp - Response UPIU structure + * @residual_transfer_count: Residual transfer count DW-3 + * @reserved: Reserved double words DW-4 to DW-7 + * @sense_data_len: Sense data length DW-8 U16 + * @sense_data: Sense data field DW-8 to DW-12 + */ +typedef struct QEMU_PACKED UtpCmdRsp { + uint32_t residual_transfer_count; + uint32_t reserved[4]; + uint16_t sense_data_len; + uint8_t sense_data[UFS_SENSE_SIZE]; +} UtpCmdRsp; + +/* + * struct UtpUpiuRsp - general upiu response structure + * @header: UPIU header structure DW-0 to DW-2 + * @sr: fields structure for scsi command DW-3 to DW-12 + * @qr: fields structure for query request DW-3 to DW-7 + */ +typedef struct QEMU_PACKED UtpUpiuRsp { + UtpUpiuHeader header; + union { + UtpCmdRsp sr; + UtpUpiuQuery qr; + }; +} UtpUpiuRsp; + +static inline void _ufs_check_size(void) +{ + QEMU_BUILD_BUG_ON(sizeof(UfsReg) !=3D 0x104); + QEMU_BUILD_BUG_ON(sizeof(DeviceDescriptor) !=3D 89); + QEMU_BUILD_BUG_ON(sizeof(GeometryDescriptor) !=3D 87); + QEMU_BUILD_BUG_ON(sizeof(UnitDescriptor) !=3D 45); + QEMU_BUILD_BUG_ON(sizeof(RpmbUnitDescriptor) !=3D 35); + QEMU_BUILD_BUG_ON(sizeof(PowerParametersDescriptor) !=3D 98); + QEMU_BUILD_BUG_ON(sizeof(InterconnectDescriptor) !=3D 6); + QEMU_BUILD_BUG_ON(sizeof(StringDescriptor) !=3D 254); + QEMU_BUILD_BUG_ON(sizeof(DeviceHealthDescriptor) !=3D 45); + QEMU_BUILD_BUG_ON(sizeof(Flags) !=3D 0x13); + QEMU_BUILD_BUG_ON(sizeof(UtpUpiuHeader) !=3D 12); + QEMU_BUILD_BUG_ON(sizeof(UtpUpiuQuery) !=3D 276); + QEMU_BUILD_BUG_ON(sizeof(UtpUpiuCmd) !=3D 20); + QEMU_BUILD_BUG_ON(sizeof(UtpUpiuReq) !=3D 288); + QEMU_BUILD_BUG_ON(sizeof(UfshcdSgEntry) !=3D 16); + QEMU_BUILD_BUG_ON(sizeof(RequestDescHeader) !=3D 16); + QEMU_BUILD_BUG_ON(sizeof(UtpTransferReqDesc) !=3D 32); + QEMU_BUILD_BUG_ON(sizeof(UtpTaskReqDesc) !=3D 80); + QEMU_BUILD_BUG_ON(sizeof(UtpCmdRsp) !=3D 40); + QEMU_BUILD_BUG_ON(sizeof(UtpUpiuRsp) !=3D 288); +} +#endif diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index abdc1ef103..b70a0b95ff 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -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 =20 #define FMT_PCIBUS PRIx64 diff --git a/include/hw/pci/pci_ids.h b/include/hw/pci/pci_ids.h index e4386ebb20..85469b9b53 100644 --- a/include/hw/pci/pci_ids.h +++ b/include/hw/pci/pci_ids.h @@ -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 =20 #define PCI_BASE_CLASS_NETWORK 0x02 diff --git a/hw/ufs/ufs.c b/hw/ufs/ufs.c new file mode 100644 index 0000000000..101082a8a3 --- /dev/null +++ b/hw/ufs/ufs.c @@ -0,0 +1,278 @@ +/* + * QEMU Universal Flash Storage (UFS) Controller + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved. + * + * Written by Jeuk Kim + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "trace.h" +#include "ufs.h" + +/* The QEMU-UFS device follows spec version 3.1 */ +#define UFS_SPEC_VER 0x00000310 +#define UFS_MAX_NUTRS 32 +#define UFS_MAX_NUTMRS 8 + +static void ufs_irq_check(UfsHc *u) +{ + PCIDevice *pci =3D PCI_DEVICE(u); + + if ((u->reg.is & UFS_INTR_MASK) & u->reg.ie) { + trace_ufs_irq_raise(); + pci_irq_assert(pci); + } else { + trace_ufs_irq_lower(); + pci_irq_deassert(pci); + } +} + +static void ufs_process_uiccmd(UfsHc *u, uint32_t val) +{ + trace_ufs_process_uiccmd(val, u->reg.ucmdarg1, u->reg.ucmdarg2, + u->reg.ucmdarg3); + /* + * Only the essential uic commands for running drivers on Linux and Wi= ndows + * are implemented. + */ + switch (val) { + case UIC_CMD_DME_LINK_STARTUP: + u->reg.hcs =3D FIELD_DP32(u->reg.hcs, HCS, DP, 1); + u->reg.hcs =3D FIELD_DP32(u->reg.hcs, HCS, UTRLRDY, 1); + u->reg.hcs =3D FIELD_DP32(u->reg.hcs, HCS, UTMRLRDY, 1); + u->reg.ucmdarg2 =3D UIC_CMD_RESULT_SUCCESS; + break; + /* TODO: Revisit it when Power Management is implemented */ + case UIC_CMD_DME_HIBER_ENTER: + u->reg.is =3D FIELD_DP32(u->reg.is, IS, UHES, 1); + u->reg.hcs =3D FIELD_DP32(u->reg.hcs, HCS, UPMCRS, PWR_LOCAL); + u->reg.ucmdarg2 =3D UIC_CMD_RESULT_SUCCESS; + break; + case UIC_CMD_DME_HIBER_EXIT: + u->reg.is =3D FIELD_DP32(u->reg.is, IS, UHXS, 1); + u->reg.hcs =3D FIELD_DP32(u->reg.hcs, HCS, UPMCRS, PWR_LOCAL); + u->reg.ucmdarg2 =3D UIC_CMD_RESULT_SUCCESS; + break; + default: + u->reg.ucmdarg2 =3D UIC_CMD_RESULT_FAILURE; + } + + u->reg.is =3D FIELD_DP32(u->reg.is, IS, UCCS, 1); + + ufs_irq_check(u); +} + +static void ufs_write_reg(UfsHc *u, hwaddr offset, uint32_t data, unsigned= size) +{ + switch (offset) { + case A_IS: + u->reg.is &=3D ~data; + ufs_irq_check(u); + break; + case A_IE: + u->reg.ie =3D data; + ufs_irq_check(u); + break; + case A_HCE: + if (!FIELD_EX32(u->reg.hce, HCE, HCE) && FIELD_EX32(data, HCE, HCE= )) { + u->reg.hcs =3D FIELD_DP32(u->reg.hcs, HCS, UCRDY, 1); + u->reg.hce =3D FIELD_DP32(u->reg.hce, HCE, HCE, 1); + } else if (FIELD_EX32(u->reg.hce, HCE, HCE) && + !FIELD_EX32(data, HCE, HCE)) { + u->reg.hcs =3D 0; + u->reg.hce =3D FIELD_DP32(u->reg.hce, HCE, HCE, 0); + } + break; + case A_UTRLBA: + u->reg.utrlba =3D data & R_UTRLBA_UTRLBA_MASK; + break; + case A_UTRLBAU: + u->reg.utrlbau =3D data; + break; + case A_UTRLDBR: + /* Not yet supported */ + break; + case A_UTRLRSR: + u->reg.utrlrsr =3D data; + break; + case A_UTRLCNR: + u->reg.utrlcnr &=3D ~data; + break; + case A_UTMRLBA: + u->reg.utmrlba =3D data & R_UTMRLBA_UTMRLBA_MASK; + break; + case A_UTMRLBAU: + u->reg.utmrlbau =3D data; + break; + case A_UICCMD: + ufs_process_uiccmd(u, data); + break; + case A_UCMDARG1: + u->reg.ucmdarg1 =3D data; + break; + case A_UCMDARG2: + u->reg.ucmdarg2 =3D data; + break; + case A_UCMDARG3: + u->reg.ucmdarg3 =3D data; + break; + case A_UTRLCLR: + case A_UTMRLDBR: + case A_UTMRLCLR: + case A_UTMRLRSR: + trace_ufs_err_unsupport_register_offset(offset); + break; + default: + trace_ufs_err_invalid_register_offset(offset); + break; + } +} + +static uint64_t ufs_mmio_read(void *opaque, hwaddr addr, unsigned size) +{ + UfsHc *u =3D (UfsHc *)opaque; + uint8_t *ptr =3D (uint8_t *)&u->reg; + uint64_t value; + + if (addr > sizeof(u->reg) - size) { + trace_ufs_err_invalid_register_offset(addr); + return 0; + } + + value =3D *(uint32_t *)(ptr + addr); + trace_ufs_mmio_read(addr, value, size); + return value; +} + +static void ufs_mmio_write(void *opaque, hwaddr addr, uint64_t data, + unsigned size) +{ + UfsHc *u =3D (UfsHc *)opaque; + + if (addr > sizeof(u->reg) - size) { + trace_ufs_err_invalid_register_offset(addr); + return; + } + + trace_ufs_mmio_write(addr, data, size); + ufs_write_reg(u, addr, data, size); +} + +static const MemoryRegionOps ufs_mmio_ops =3D { + .read =3D ufs_mmio_read, + .write =3D ufs_mmio_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .impl =3D { + .min_access_size =3D 4, + .max_access_size =3D 4, + }, +}; + +static bool ufs_check_constraints(UfsHc *u, Error **errp) +{ + if (u->params.nutrs > UFS_MAX_NUTRS) { + error_setg(errp, "nutrs must be less than or equal to %d", + UFS_MAX_NUTRS); + return false; + } + + if (u->params.nutmrs > UFS_MAX_NUTMRS) { + error_setg(errp, "nutmrs must be less than or equal to %d", + UFS_MAX_NUTMRS); + return false; + } + + return true; +} + +static void ufs_init_pci(UfsHc *u, PCIDevice *pci_dev) +{ + uint8_t *pci_conf =3D pci_dev->config; + + pci_conf[PCI_INTERRUPT_PIN] =3D 1; + pci_config_set_prog_interface(pci_conf, 0x1); + + memory_region_init_io(&u->iomem, OBJECT(u), &ufs_mmio_ops, u, "ufs", + u->reg_size); + pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &u->iomem); + u->irq =3D pci_allocate_irq(pci_dev); +} + +static void ufs_init_hc(UfsHc *u) +{ + uint32_t cap =3D 0; + + u->reg_size =3D pow2ceil(sizeof(UfsReg)); + + memset(&u->reg, 0, sizeof(u->reg)); + cap =3D FIELD_DP32(cap, CAP, NUTRS, (u->params.nutrs - 1)); + cap =3D FIELD_DP32(cap, CAP, RTT, 2); + cap =3D FIELD_DP32(cap, CAP, NUTMRS, (u->params.nutmrs - 1)); + cap =3D FIELD_DP32(cap, CAP, AUTOH8, 0); + cap =3D FIELD_DP32(cap, CAP, 64AS, 1); + cap =3D FIELD_DP32(cap, CAP, OODDS, 0); + cap =3D FIELD_DP32(cap, CAP, UICDMETMS, 0); + cap =3D FIELD_DP32(cap, CAP, CS, 0); + u->reg.cap =3D cap; + u->reg.ver =3D UFS_SPEC_VER; +} + +static void ufs_realize(PCIDevice *pci_dev, Error **errp) +{ + UfsHc *u =3D UFS(pci_dev); + + if (!ufs_check_constraints(u, errp)) { + return; + } + + ufs_init_hc(u); + ufs_init_pci(u, pci_dev); +} + +static Property ufs_props[] =3D { + DEFINE_PROP_STRING("serial", UfsHc, params.serial), + DEFINE_PROP_UINT8("nutrs", UfsHc, params.nutrs, 32), + DEFINE_PROP_UINT8("nutmrs", UfsHc, params.nutmrs, 8), + DEFINE_PROP_END_OF_LIST(), +}; + +static const VMStateDescription ufs_vmstate =3D { + .name =3D "ufs", + .unmigratable =3D 1, +}; + +static void ufs_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + PCIDeviceClass *pc =3D PCI_DEVICE_CLASS(oc); + + pc->realize =3D ufs_realize; + pc->vendor_id =3D PCI_VENDOR_ID_REDHAT; + pc->device_id =3D PCI_DEVICE_ID_REDHAT_UFS; + pc->class_id =3D PCI_CLASS_STORAGE_UFS; + + set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); + dc->desc =3D "Universal Flash Storage"; + device_class_set_props(dc, ufs_props); + dc->vmsd =3D &ufs_vmstate; +} + +static const TypeInfo ufs_info =3D { + .name =3D TYPE_UFS, + .parent =3D TYPE_PCI_DEVICE, + .class_init =3D ufs_class_init, + .instance_size =3D sizeof(UfsHc), + .interfaces =3D (InterfaceInfo[]){ { INTERFACE_PCIE_DEVICE }, {} }, +}; + +static void ufs_register_types(void) +{ + type_register_static(&ufs_info); +} + +type_init(ufs_register_types) diff --git a/hw/Kconfig b/hw/Kconfig index ba62ff6417..9ca7b38c31 100644 --- a/hw/Kconfig +++ b/hw/Kconfig @@ -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 diff --git a/hw/meson.build b/hw/meson.build index c7ac7d3d75..f01fac4617 100644 --- a/hw/meson.build +++ b/hw/meson.build @@ -37,6 +37,7 @@ subdir('smbios') subdir('ssi') subdir('timer') subdir('tpm') +subdir('ufs') subdir('usb') subdir('vfio') subdir('virtio') diff --git a/hw/ufs/Kconfig b/hw/ufs/Kconfig new file mode 100644 index 0000000000..b7b3392e85 --- /dev/null +++ b/hw/ufs/Kconfig @@ -0,0 +1,4 @@ +config UFS_PCI + bool + default y if PCI_DEVICES + depends on PCI diff --git a/hw/ufs/meson.build b/hw/ufs/meson.build new file mode 100644 index 0000000000..eb5164bde9 --- /dev/null +++ b/hw/ufs/meson.build @@ -0,0 +1 @@ +system_ss.add(when: 'CONFIG_UFS_PCI', if_true: files('ufs.c')) diff --git a/hw/ufs/trace-events b/hw/ufs/trace-events new file mode 100644 index 0000000000..d1badcad10 --- /dev/null +++ b/hw/ufs/trace-events @@ -0,0 +1,32 @@ +# 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%"PRIx6= 4" data 0x%"PRIx64" size %d" +ufs_mmio_write(uint64_t addr, uint64_t data, unsigned size) "addr 0x%"PRIx= 64" 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 %"PRIu= 32", 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"" + +# 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 re= q 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_busy(uint32_t slot) "UTRLDBR slot %"PRIu32" is busy" +ufs_err_unsupport_register_offset(uint32_t offset) "Register offset 0x%"PR= Ix32" is not yet supported" +ufs_err_invalid_register_offset(uint32_t offset) "Register offset 0x%"PRIx= 32" is invalid" +ufs_err_scsi_cmd_invalid_lun(uint8_t lun) "scsi command has invalid lun: 0= x%"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%"PRIx= 8" is denied to read" +ufs_err_query_attr_not_writable(uint8_t idn) "query attribute idn 0x%"PRIx= 8" is denied to write" +ufs_err_query_invalid_opcode(uint8_t opcode) "query request has invalid op= code. 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 upi= u has invalid transaction code. slot: %"PRIu32", trans_code: 0x%"PRIx8"" --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896399; cv=none; d=zohomail.com; s=zohoarc; b=g0Spz4X5Ku/CEoxmJrA1M9mWN6Bn52Wu+OKZc4VXAyy+edpWmAyV7o+5z7UwNe5mZDx0wuxQPj2W+2CmpeINop9MGvJ5mRkkzV1yPoN3jbXzDkpnG4HyoNrbNTryXckwzS06+sQrWRAyqZ7xJyTvPaObAEvKLvuT0EWyQq8PlKg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896399; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=wmlANfPjFFuQKkPMw5IwWEWZLPYe7OFZTTiittuLTe4=; b=D6Uijipi+DW7nbkqvuXcPDkpHP+eowPwdnBhVanAG/J3n49gcSSl6QzE4x+xevSBOHOL59VVcDKdfNd9UU0b6QgxDMNjfl2mbIw/xX2IIoiWhT1sgDf/NdxVCLhCkJPqrjIloKt5Sqb1xf/7X60R7srpP805/tv6zN2nOssKN84= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896399424853.9723683822036; Thu, 24 Aug 2023 09:59:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfb-00066x-Og; Thu, 24 Aug 2023 12:58:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfZ-0005wY-K7 for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfU-0003PF-JR for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:53 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-546-3Y7FQRR9PnqGCo7DCtCdAA-1; Thu, 24 Aug 2023 12:58:44 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6BA4E3C14AA7; Thu, 24 Aug 2023 16:58:43 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id 30B96492C13; Thu, 24 Aug 2023 16:58:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896327; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wmlANfPjFFuQKkPMw5IwWEWZLPYe7OFZTTiittuLTe4=; b=hcE4mDono862E2kCsuRJ65o6eJXf1o5wJeK7kqOGsW/M7/CRQ33mt3W79eZB/MkRumhMWs 7J1tyTV6sG685Nususww/VUcq6vrtaWbRSIL0o9ZuKeBbzykbZuNeIRZTCbUWGy4aJDeGK heh188tVSbSv5CmQOX5iDDhWPmQDO68= X-MC-Unique: 3Y7FQRR9PnqGCo7DCtCdAA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Jeuk Kim Subject: [PULL 2/8] hw/ufs: Support for Query Transfer Requests Date: Thu, 24 Aug 2023 12:58:27 -0400 Message-ID: <20230824165833.1689137-3-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896400042100007 Content-Type: text/plain; charset="utf-8" From: Jeuk Kim This commit makes the UFS device support query and nop out transfer requests. The next patch would be support for UFS logical unit and scsi command transfer request. Signed-off-by: Jeuk Kim Reviewed-by: Stefan Hajnoczi Message-id: d06b440d660872092f70af1b8167bd5f4704c957.1691062912.git.jeuk20.= kim@samsung.com Signed-off-by: Stefan Hajnoczi --- hw/ufs/ufs.h | 46 +++ hw/ufs/ufs.c | 980 +++++++++++++++++++++++++++++++++++++++++++- hw/ufs/trace-events | 1 + 3 files changed, 1025 insertions(+), 2 deletions(-) diff --git a/hw/ufs/ufs.h b/hw/ufs/ufs.h index d9d195caec..3d1b2cff4e 100644 --- a/hw/ufs/ufs.h +++ b/hw/ufs/ufs.h @@ -18,6 +18,32 @@ #define UFS_MAX_LUS 32 #define UFS_BLOCK_SIZE 4096 =20 +typedef enum UfsRequestState { + UFS_REQUEST_IDLE =3D 0, + UFS_REQUEST_READY =3D 1, + UFS_REQUEST_RUNNING =3D 2, + UFS_REQUEST_COMPLETE =3D 3, + UFS_REQUEST_ERROR =3D 4, +} UfsRequestState; + +typedef enum UfsReqResult { + UFS_REQUEST_SUCCESS =3D 0, + UFS_REQUEST_FAIL =3D 1, +} 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 UfsParams { char *serial; uint8_t nutrs; /* Number of UTP Transfer Request Slots */ @@ -30,6 +56,12 @@ typedef struct UfsHc { UfsReg reg; UfsParams params; uint32_t reg_size; + UfsRequest *req_list; + + DeviceDescriptor device_desc; + GeometryDescriptor geometry_desc; + Attributes attributes; + Flags flags; =20 qemu_irq irq; QEMUBH *doorbell_bh; @@ -39,4 +71,18 @@ typedef struct UfsHc { #define TYPE_UFS "ufs" #define UFS(obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS) =20 +typedef enum UfsQueryFlagPerm { + UFS_QUERY_FLAG_NONE =3D 0x0, + UFS_QUERY_FLAG_READ =3D 0x1, + UFS_QUERY_FLAG_SET =3D 0x2, + UFS_QUERY_FLAG_CLEAR =3D 0x4, + UFS_QUERY_FLAG_TOGGLE =3D 0x8, +} UfsQueryFlagPerm; + +typedef enum UfsQueryAttrPerm { + UFS_QUERY_ATTR_NONE =3D 0x0, + UFS_QUERY_ATTR_READ =3D 0x1, + UFS_QUERY_ATTR_WRITE =3D 0x2, +} UfsQueryAttrPerm; + #endif /* HW_UFS_UFS_H */ diff --git a/hw/ufs/ufs.c b/hw/ufs/ufs.c index 101082a8a3..c771f8e0b4 100644 --- a/hw/ufs/ufs.c +++ b/hw/ufs/ufs.c @@ -15,10 +15,221 @@ #include "ufs.h" =20 /* The QEMU-UFS device follows spec version 3.1 */ -#define UFS_SPEC_VER 0x00000310 +#define UFS_SPEC_VER 0x0310 #define UFS_MAX_NUTRS 32 #define UFS_MAX_NUTMRS 8 =20 +static MemTxResult ufs_addr_read(UfsHc *u, hwaddr addr, void *buf, int siz= e) +{ + hwaddr hi =3D addr + size - 1; + + if (hi < addr) { + return MEMTX_DECODE_ERROR; + } + + if (!FIELD_EX32(u->reg.cap, CAP, 64AS) && (hi >> 32)) { + return MEMTX_DECODE_ERROR; + } + + return pci_dma_read(PCI_DEVICE(u), addr, buf, size); +} + +static MemTxResult ufs_addr_write(UfsHc *u, hwaddr addr, const void *buf, + int size) +{ + hwaddr hi =3D addr + size - 1; + if (hi < addr) { + return MEMTX_DECODE_ERROR; + } + + if (!FIELD_EX32(u->reg.cap, CAP, 64AS) && (hi >> 32)) { + return MEMTX_DECODE_ERROR; + } + + return pci_dma_write(PCI_DEVICE(u), addr, buf, size); +} + +static void ufs_complete_req(UfsRequest *req, UfsReqResult req_result); + +static inline hwaddr ufs_get_utrd_addr(UfsHc *u, uint32_t slot) +{ + hwaddr utrl_base_addr =3D (((hwaddr)u->reg.utrlbau) << 32) + u->reg.ut= rlba; + hwaddr utrd_addr =3D utrl_base_addr + slot * sizeof(UtpTransferReqDesc= ); + + return utrd_addr; +} + +static inline hwaddr ufs_get_req_upiu_base_addr(const UtpTransferReqDesc *= utrd) +{ + uint32_t cmd_desc_base_addr_lo =3D + le32_to_cpu(utrd->command_desc_base_addr_lo); + uint32_t cmd_desc_base_addr_hi =3D + le32_to_cpu(utrd->command_desc_base_addr_hi); + + return (((hwaddr)cmd_desc_base_addr_hi) << 32) + cmd_desc_base_addr_lo; +} + +static inline hwaddr ufs_get_rsp_upiu_base_addr(const UtpTransferReqDesc *= utrd) +{ + hwaddr req_upiu_base_addr =3D ufs_get_req_upiu_base_addr(utrd); + uint32_t rsp_upiu_byte_off =3D + le16_to_cpu(utrd->response_upiu_offset) * sizeof(uint32_t); + return req_upiu_base_addr + rsp_upiu_byte_off; +} + +static MemTxResult ufs_dma_read_utrd(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + hwaddr utrd_addr =3D ufs_get_utrd_addr(u, req->slot); + MemTxResult ret; + + ret =3D ufs_addr_read(u, utrd_addr, &req->utrd, sizeof(req->utrd)); + if (ret) { + trace_ufs_err_dma_read_utrd(req->slot, utrd_addr); + } + return ret; +} + +static MemTxResult ufs_dma_read_req_upiu(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + hwaddr req_upiu_base_addr =3D ufs_get_req_upiu_base_addr(&req->utrd); + UtpUpiuReq *req_upiu =3D &req->req_upiu; + uint32_t copy_size; + uint16_t data_segment_length; + MemTxResult ret; + + /* + * To know the size of the req_upiu, we need to read the + * data_segment_length in the header first. + */ + ret =3D ufs_addr_read(u, req_upiu_base_addr, &req_upiu->header, + sizeof(UtpUpiuHeader)); + if (ret) { + trace_ufs_err_dma_read_req_upiu(req->slot, req_upiu_base_addr); + return ret; + } + data_segment_length =3D be16_to_cpu(req_upiu->header.data_segment_leng= th); + + copy_size =3D sizeof(UtpUpiuHeader) + UFS_TRANSACTION_SPECIFIC_FIELD_S= IZE + + data_segment_length; + + ret =3D ufs_addr_read(u, req_upiu_base_addr, &req->req_upiu, copy_size= ); + if (ret) { + trace_ufs_err_dma_read_req_upiu(req->slot, req_upiu_base_addr); + } + return ret; +} + +static MemTxResult ufs_dma_read_prdt(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + uint16_t prdt_len =3D le16_to_cpu(req->utrd.prd_table_length); + uint16_t prdt_byte_off =3D + le16_to_cpu(req->utrd.prd_table_offset) * sizeof(uint32_t); + uint32_t prdt_size =3D prdt_len * sizeof(UfshcdSgEntry); + g_autofree UfshcdSgEntry *prd_entries =3D NULL; + hwaddr req_upiu_base_addr, prdt_base_addr; + int err; + + assert(!req->sg); + + if (prdt_size =3D=3D 0) { + return MEMTX_OK; + } + prd_entries =3D g_new(UfshcdSgEntry, prdt_size); + + req_upiu_base_addr =3D ufs_get_req_upiu_base_addr(&req->utrd); + prdt_base_addr =3D req_upiu_base_addr + prdt_byte_off; + + err =3D ufs_addr_read(u, prdt_base_addr, prd_entries, prdt_size); + if (err) { + trace_ufs_err_dma_read_prdt(req->slot, prdt_base_addr); + return err; + } + + req->sg =3D g_malloc0(sizeof(QEMUSGList)); + pci_dma_sglist_init(req->sg, PCI_DEVICE(u), prdt_len); + + for (uint16_t i =3D 0; i < prdt_len; ++i) { + hwaddr data_dma_addr =3D le64_to_cpu(prd_entries[i].addr); + uint32_t data_byte_count =3D le32_to_cpu(prd_entries[i].size) + 1; + qemu_sglist_add(req->sg, data_dma_addr, data_byte_count); + } + return MEMTX_OK; +} + +static MemTxResult ufs_dma_read_upiu(UfsRequest *req) +{ + MemTxResult ret; + + ret =3D ufs_dma_read_utrd(req); + if (ret) { + return ret; + } + + ret =3D ufs_dma_read_req_upiu(req); + if (ret) { + return ret; + } + + ret =3D ufs_dma_read_prdt(req); + if (ret) { + return ret; + } + + return 0; +} + +static MemTxResult ufs_dma_write_utrd(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + hwaddr utrd_addr =3D ufs_get_utrd_addr(u, req->slot); + MemTxResult ret; + + ret =3D ufs_addr_write(u, utrd_addr, &req->utrd, sizeof(req->utrd)); + if (ret) { + trace_ufs_err_dma_write_utrd(req->slot, utrd_addr); + } + return ret; +} + +static MemTxResult ufs_dma_write_rsp_upiu(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + hwaddr rsp_upiu_base_addr =3D ufs_get_rsp_upiu_base_addr(&req->utrd); + uint32_t rsp_upiu_byte_len =3D + le16_to_cpu(req->utrd.response_upiu_length) * sizeof(uint32_t); + uint16_t data_segment_length =3D + be16_to_cpu(req->rsp_upiu.header.data_segment_length); + uint32_t copy_size =3D sizeof(UtpUpiuHeader) + + UFS_TRANSACTION_SPECIFIC_FIELD_SIZE + + data_segment_length; + MemTxResult ret; + + if (copy_size > rsp_upiu_byte_len) { + copy_size =3D rsp_upiu_byte_len; + } + + ret =3D ufs_addr_write(u, rsp_upiu_base_addr, &req->rsp_upiu, copy_siz= e); + if (ret) { + trace_ufs_err_dma_write_rsp_upiu(req->slot, rsp_upiu_base_addr); + } + return ret; +} + +static MemTxResult ufs_dma_write_upiu(UfsRequest *req) +{ + MemTxResult ret; + + ret =3D ufs_dma_write_rsp_upiu(req); + if (ret) { + return ret; + } + + return ufs_dma_write_utrd(req); +} + static void ufs_irq_check(UfsHc *u) { PCIDevice *pci =3D PCI_DEVICE(u); @@ -32,6 +243,39 @@ static void ufs_irq_check(UfsHc *u) } } =20 +static void ufs_process_db(UfsHc *u, uint32_t val) +{ + uint32_t slot; + uint32_t nutrs =3D u->params.nutrs; + UfsRequest *req; + + val &=3D ~u->reg.utrldbr; + if (!val) { + return; + } + + slot =3D find_first_bit((unsigned long *)&val, nutrs); + + while (slot < nutrs) { + req =3D &u->req_list[slot]; + if (req->state =3D=3D UFS_REQUEST_ERROR) { + trace_ufs_err_utrl_slot_error(req->slot); + return; + } + + if (req->state !=3D UFS_REQUEST_IDLE) { + trace_ufs_err_utrl_slot_busy(req->slot); + return; + } + + trace_ufs_process_db(slot); + req->state =3D UFS_REQUEST_READY; + slot =3D find_next_bit((unsigned long *)&val, nutrs, slot + 1); + } + + qemu_bh_schedule(u->doorbell_bh); +} + static void ufs_process_uiccmd(UfsHc *u, uint32_t val) { trace_ufs_process_uiccmd(val, u->reg.ucmdarg1, u->reg.ucmdarg2, @@ -95,7 +339,8 @@ static void ufs_write_reg(UfsHc *u, hwaddr offset, uint3= 2_t data, unsigned size) u->reg.utrlbau =3D data; break; case A_UTRLDBR: - /* Not yet supported */ + ufs_process_db(u, data); + u->reg.utrldbr |=3D data; break; case A_UTRLRSR: u->reg.utrlrsr =3D data; @@ -173,6 +418,659 @@ static const MemoryRegionOps ufs_mmio_ops =3D { }, }; =20 +static void ufs_build_upiu_header(UfsRequest *req, uint8_t trans_type, + uint8_t flags, uint8_t response, + uint8_t scsi_status, + uint16_t data_segment_length) +{ + memcpy(&req->rsp_upiu.header, &req->req_upiu.header, sizeof(UtpUpiuHea= der)); + req->rsp_upiu.header.trans_type =3D trans_type; + req->rsp_upiu.header.flags =3D flags; + req->rsp_upiu.header.response =3D response; + req->rsp_upiu.header.scsi_status =3D scsi_status; + req->rsp_upiu.header.data_segment_length =3D cpu_to_be16(data_segment_= length); +} + +static UfsReqResult ufs_exec_nop_cmd(UfsRequest *req) +{ + trace_ufs_exec_nop_cmd(req->slot); + ufs_build_upiu_header(req, UPIU_TRANSACTION_NOP_IN, 0, 0, 0, 0); + return UFS_REQUEST_SUCCESS; +} + +/* + * This defines the permission of flags based on their IDN. There are some + * things that are declared read-only, which is inconsistent with the ufs = spec, + * because we want to return an error for features that are not yet suppor= ted. + */ +static const int flag_permission[QUERY_FLAG_IDN_COUNT] =3D { + [QUERY_FLAG_IDN_FDEVICEINIT] =3D UFS_QUERY_FLAG_READ | UFS_QUERY_FLAG_= SET, + /* Write protection is not supported */ + [QUERY_FLAG_IDN_PERMANENT_WPE] =3D UFS_QUERY_FLAG_READ, + [QUERY_FLAG_IDN_PWR_ON_WPE] =3D UFS_QUERY_FLAG_READ, + [QUERY_FLAG_IDN_BKOPS_EN] =3D UFS_QUERY_FLAG_READ | UFS_QUERY_FLAG_SET= | + UFS_QUERY_FLAG_CLEAR | UFS_QUERY_FLAG_TOGG= LE, + [QUERY_FLAG_IDN_LIFE_SPAN_MODE_ENABLE] =3D + UFS_QUERY_FLAG_READ | UFS_QUERY_FLAG_SET | UFS_QUERY_FLAG_CLEAR | + UFS_QUERY_FLAG_TOGGLE, + /* Purge Operation is not supported */ + [QUERY_FLAG_IDN_PURGE_ENABLE] =3D UFS_QUERY_FLAG_NONE, + /* Refresh Operation is not supported */ + [QUERY_FLAG_IDN_REFRESH_ENABLE] =3D UFS_QUERY_FLAG_NONE, + /* Physical Resource Removal is not supported */ + [QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL] =3D UFS_QUERY_FLAG_READ, + [QUERY_FLAG_IDN_BUSY_RTC] =3D UFS_QUERY_FLAG_READ, + [QUERY_FLAG_IDN_PERMANENTLY_DISABLE_FW_UPDATE] =3D UFS_QUERY_FLAG_READ, + /* Write Booster is not supported */ + [QUERY_FLAG_IDN_WB_EN] =3D UFS_QUERY_FLAG_READ, + [QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN] =3D UFS_QUERY_FLAG_READ, + [QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8] =3D UFS_QUERY_FLAG_READ, +}; + +static inline QueryRespCode ufs_flag_check_idn_valid(uint8_t idn, int op) +{ + if (idn >=3D QUERY_FLAG_IDN_COUNT) { + return QUERY_RESULT_INVALID_IDN; + } + + if (!(flag_permission[idn] & op)) { + if (op =3D=3D UFS_QUERY_FLAG_READ) { + trace_ufs_err_query_flag_not_readable(idn); + return QUERY_RESULT_NOT_READABLE; + } + trace_ufs_err_query_flag_not_writable(idn); + return QUERY_RESULT_NOT_WRITEABLE; + } + + return QUERY_RESULT_SUCCESS; +} + +static const int attr_permission[QUERY_ATTR_IDN_COUNT] =3D { + /* booting is not supported */ + [QUERY_ATTR_IDN_BOOT_LU_EN] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_POWER_MODE] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_ACTIVE_ICC_LVL] =3D + UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR_WRITE, + [QUERY_ATTR_IDN_OOO_DATA_EN] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_BKOPS_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_PURGE_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_MAX_DATA_IN] =3D UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR_= WRITE, + [QUERY_ATTR_IDN_MAX_DATA_OUT] =3D UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR= _WRITE, + [QUERY_ATTR_IDN_DYN_CAP_NEEDED] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_REF_CLK_FREQ] =3D UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR= _WRITE, + [QUERY_ATTR_IDN_CONF_DESC_LOCK] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_MAX_NUM_OF_RTT] =3D + UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR_WRITE, + [QUERY_ATTR_IDN_EE_CONTROL] =3D UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR_W= RITE, + [QUERY_ATTR_IDN_EE_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_SECONDS_PASSED] =3D UFS_QUERY_ATTR_WRITE, + [QUERY_ATTR_IDN_CNTX_CONF] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_FFU_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_PSA_STATE] =3D UFS_QUERY_ATTR_READ | UFS_QUERY_ATTR_WR= ITE, + [QUERY_ATTR_IDN_PSA_DATA_SIZE] =3D UFS_QUERY_ATTR_READ | UFS_QUERY_ATT= R_WRITE, + [QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_CASE_ROUGH_TEMP] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_HIGH_TEMP_BOUND] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_LOW_TEMP_BOUND] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_THROTTLING_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_WB_FLUSH_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_WB_BUFF_LIFE_TIME_EST] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE] =3D UFS_QUERY_ATTR_READ, + /* refresh operation is not supported */ + [QUERY_ATTR_IDN_REFRESH_STATUS] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_REFRESH_FREQ] =3D UFS_QUERY_ATTR_READ, + [QUERY_ATTR_IDN_REFRESH_UNIT] =3D UFS_QUERY_ATTR_READ, +}; + +static inline QueryRespCode ufs_attr_check_idn_valid(uint8_t idn, int op) +{ + if (idn >=3D QUERY_ATTR_IDN_COUNT) { + return QUERY_RESULT_INVALID_IDN; + } + + if (!(attr_permission[idn] & op)) { + if (op =3D=3D UFS_QUERY_ATTR_READ) { + trace_ufs_err_query_attr_not_readable(idn); + return QUERY_RESULT_NOT_READABLE; + } + trace_ufs_err_query_attr_not_writable(idn); + return QUERY_RESULT_NOT_WRITEABLE; + } + + return QUERY_RESULT_SUCCESS; +} + +static QueryRespCode ufs_exec_query_flag(UfsRequest *req, int op) +{ + UfsHc *u =3D req->hc; + uint8_t idn =3D req->req_upiu.qr.idn; + uint32_t value; + QueryRespCode ret; + + ret =3D ufs_flag_check_idn_valid(idn, op); + if (ret) { + return ret; + } + + if (idn =3D=3D QUERY_FLAG_IDN_FDEVICEINIT) { + value =3D 0; + } else if (op =3D=3D UFS_QUERY_FLAG_READ) { + value =3D *(((uint8_t *)&u->flags) + idn); + } else if (op =3D=3D UFS_QUERY_FLAG_SET) { + value =3D 1; + } else if (op =3D=3D UFS_QUERY_FLAG_CLEAR) { + value =3D 0; + } else if (op =3D=3D UFS_QUERY_FLAG_TOGGLE) { + value =3D *(((uint8_t *)&u->flags) + idn); + value =3D !value; + } else { + trace_ufs_err_query_invalid_opcode(op); + return QUERY_RESULT_INVALID_OPCODE; + } + + *(((uint8_t *)&u->flags) + idn) =3D value; + req->rsp_upiu.qr.value =3D cpu_to_be32(value); + return QUERY_RESULT_SUCCESS; +} + +static uint32_t ufs_read_attr_value(UfsHc *u, uint8_t idn) +{ + switch (idn) { + case QUERY_ATTR_IDN_BOOT_LU_EN: + return u->attributes.boot_lun_en; + case QUERY_ATTR_IDN_POWER_MODE: + return u->attributes.current_power_mode; + case QUERY_ATTR_IDN_ACTIVE_ICC_LVL: + return u->attributes.active_icc_level; + case QUERY_ATTR_IDN_OOO_DATA_EN: + return u->attributes.out_of_order_data_en; + case QUERY_ATTR_IDN_BKOPS_STATUS: + return u->attributes.background_op_status; + case QUERY_ATTR_IDN_PURGE_STATUS: + return u->attributes.purge_status; + case QUERY_ATTR_IDN_MAX_DATA_IN: + return u->attributes.max_data_in_size; + case QUERY_ATTR_IDN_MAX_DATA_OUT: + return u->attributes.max_data_out_size; + case QUERY_ATTR_IDN_DYN_CAP_NEEDED: + return be32_to_cpu(u->attributes.dyn_cap_needed); + case QUERY_ATTR_IDN_REF_CLK_FREQ: + return u->attributes.ref_clk_freq; + case QUERY_ATTR_IDN_CONF_DESC_LOCK: + return u->attributes.config_descr_lock; + case QUERY_ATTR_IDN_MAX_NUM_OF_RTT: + return u->attributes.max_num_of_rtt; + case QUERY_ATTR_IDN_EE_CONTROL: + return be16_to_cpu(u->attributes.exception_event_control); + case QUERY_ATTR_IDN_EE_STATUS: + return be16_to_cpu(u->attributes.exception_event_status); + case QUERY_ATTR_IDN_SECONDS_PASSED: + return be32_to_cpu(u->attributes.seconds_passed); + case QUERY_ATTR_IDN_CNTX_CONF: + return be16_to_cpu(u->attributes.context_conf); + case QUERY_ATTR_IDN_FFU_STATUS: + return u->attributes.device_ffu_status; + case QUERY_ATTR_IDN_PSA_STATE: + return be32_to_cpu(u->attributes.psa_state); + case QUERY_ATTR_IDN_PSA_DATA_SIZE: + return be32_to_cpu(u->attributes.psa_data_size); + case QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME: + return u->attributes.ref_clk_gating_wait_time; + case QUERY_ATTR_IDN_CASE_ROUGH_TEMP: + return u->attributes.device_case_rough_temperaure; + case QUERY_ATTR_IDN_HIGH_TEMP_BOUND: + return u->attributes.device_too_high_temp_boundary; + case QUERY_ATTR_IDN_LOW_TEMP_BOUND: + return u->attributes.device_too_low_temp_boundary; + case QUERY_ATTR_IDN_THROTTLING_STATUS: + return u->attributes.throttling_status; + case QUERY_ATTR_IDN_WB_FLUSH_STATUS: + return u->attributes.wb_buffer_flush_status; + case QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE: + return u->attributes.available_wb_buffer_size; + case QUERY_ATTR_IDN_WB_BUFF_LIFE_TIME_EST: + return u->attributes.wb_buffer_life_time_est; + case QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE: + return be32_to_cpu(u->attributes.current_wb_buffer_size); + case QUERY_ATTR_IDN_REFRESH_STATUS: + return u->attributes.refresh_status; + case QUERY_ATTR_IDN_REFRESH_FREQ: + return u->attributes.refresh_freq; + case QUERY_ATTR_IDN_REFRESH_UNIT: + return u->attributes.refresh_unit; + } + return 0; +} + +static void ufs_write_attr_value(UfsHc *u, uint8_t idn, uint32_t value) +{ + switch (idn) { + case QUERY_ATTR_IDN_ACTIVE_ICC_LVL: + u->attributes.active_icc_level =3D value; + break; + case QUERY_ATTR_IDN_MAX_DATA_IN: + u->attributes.max_data_in_size =3D value; + break; + case QUERY_ATTR_IDN_MAX_DATA_OUT: + u->attributes.max_data_out_size =3D value; + break; + case QUERY_ATTR_IDN_REF_CLK_FREQ: + u->attributes.ref_clk_freq =3D value; + break; + case QUERY_ATTR_IDN_MAX_NUM_OF_RTT: + u->attributes.max_num_of_rtt =3D value; + break; + case QUERY_ATTR_IDN_EE_CONTROL: + u->attributes.exception_event_control =3D cpu_to_be16(value); + break; + case QUERY_ATTR_IDN_SECONDS_PASSED: + u->attributes.seconds_passed =3D cpu_to_be32(value); + break; + case QUERY_ATTR_IDN_PSA_STATE: + u->attributes.psa_state =3D value; + break; + case QUERY_ATTR_IDN_PSA_DATA_SIZE: + u->attributes.psa_data_size =3D cpu_to_be32(value); + break; + } +} + +static QueryRespCode ufs_exec_query_attr(UfsRequest *req, int op) +{ + UfsHc *u =3D req->hc; + uint8_t idn =3D req->req_upiu.qr.idn; + uint32_t value; + QueryRespCode ret; + + ret =3D ufs_attr_check_idn_valid(idn, op); + if (ret) { + return ret; + } + + if (op =3D=3D UFS_QUERY_ATTR_READ) { + value =3D ufs_read_attr_value(u, idn); + } else { + value =3D be32_to_cpu(req->req_upiu.qr.value); + ufs_write_attr_value(u, idn, value); + } + + req->rsp_upiu.qr.value =3D cpu_to_be32(value); + return QUERY_RESULT_SUCCESS; +} + +static const RpmbUnitDescriptor rpmb_unit_desc =3D { + .length =3D sizeof(RpmbUnitDescriptor), + .descriptor_idn =3D 2, + .unit_index =3D UFS_UPIU_RPMB_WLUN, + .lu_enable =3D 0, +}; + +static QueryRespCode ufs_read_unit_desc(UfsRequest *req) +{ + uint8_t lun =3D req->req_upiu.qr.index; + + if (lun !=3D UFS_UPIU_RPMB_WLUN && lun > UFS_MAX_LUS) { + trace_ufs_err_query_invalid_index(req->req_upiu.qr.opcode, lun); + return QUERY_RESULT_INVALID_INDEX; + } + + if (lun =3D=3D UFS_UPIU_RPMB_WLUN) { + memcpy(&req->rsp_upiu.qr.data, &rpmb_unit_desc, rpmb_unit_desc.len= gth); + } else { + /* unit descriptor is not yet supported */ + return QUERY_RESULT_INVALID_INDEX; + } + + return QUERY_RESULT_SUCCESS; +} + +static inline StringDescriptor manufacturer_str_desc(void) +{ + StringDescriptor desc =3D { + .length =3D 0x12, + .descriptor_idn =3D QUERY_DESC_IDN_STRING, + }; + desc.UC[0] =3D cpu_to_be16('R'); + desc.UC[1] =3D cpu_to_be16('E'); + desc.UC[2] =3D cpu_to_be16('D'); + desc.UC[3] =3D cpu_to_be16('H'); + desc.UC[4] =3D cpu_to_be16('A'); + desc.UC[5] =3D cpu_to_be16('T'); + return desc; +} + +static inline StringDescriptor product_name_str_desc(void) +{ + StringDescriptor desc =3D { + .length =3D 0x22, + .descriptor_idn =3D QUERY_DESC_IDN_STRING, + }; + desc.UC[0] =3D cpu_to_be16('Q'); + desc.UC[1] =3D cpu_to_be16('E'); + desc.UC[2] =3D cpu_to_be16('M'); + desc.UC[3] =3D cpu_to_be16('U'); + desc.UC[4] =3D cpu_to_be16(' '); + desc.UC[5] =3D cpu_to_be16('U'); + desc.UC[6] =3D cpu_to_be16('F'); + desc.UC[7] =3D cpu_to_be16('S'); + return desc; +} + +static inline StringDescriptor product_rev_level_str_desc(void) +{ + StringDescriptor desc =3D { + .length =3D 0x0a, + .descriptor_idn =3D QUERY_DESC_IDN_STRING, + }; + desc.UC[0] =3D cpu_to_be16('0'); + desc.UC[1] =3D cpu_to_be16('0'); + desc.UC[2] =3D cpu_to_be16('0'); + desc.UC[3] =3D cpu_to_be16('1'); + return desc; +} + +static const StringDescriptor null_str_desc =3D { + .length =3D 0x02, + .descriptor_idn =3D QUERY_DESC_IDN_STRING, +}; + +static QueryRespCode ufs_read_string_desc(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + uint8_t index =3D req->req_upiu.qr.index; + StringDescriptor desc; + + if (index =3D=3D u->device_desc.manufacturer_name) { + desc =3D manufacturer_str_desc(); + memcpy(&req->rsp_upiu.qr.data, &desc, desc.length); + } else if (index =3D=3D u->device_desc.product_name) { + desc =3D product_name_str_desc(); + memcpy(&req->rsp_upiu.qr.data, &desc, desc.length); + } else if (index =3D=3D u->device_desc.serial_number) { + memcpy(&req->rsp_upiu.qr.data, &null_str_desc, null_str_desc.lengt= h); + } else if (index =3D=3D u->device_desc.oem_id) { + memcpy(&req->rsp_upiu.qr.data, &null_str_desc, null_str_desc.lengt= h); + } else if (index =3D=3D u->device_desc.product_revision_level) { + desc =3D product_rev_level_str_desc(); + memcpy(&req->rsp_upiu.qr.data, &desc, desc.length); + } else { + trace_ufs_err_query_invalid_index(req->req_upiu.qr.opcode, index); + return QUERY_RESULT_INVALID_INDEX; + } + return QUERY_RESULT_SUCCESS; +} + +static inline InterconnectDescriptor interconnect_desc(void) +{ + InterconnectDescriptor desc =3D { + .length =3D sizeof(InterconnectDescriptor), + .descriptor_idn =3D QUERY_DESC_IDN_INTERCONNECT, + }; + desc.bcd_unipro_version =3D cpu_to_be16(0x180); + desc.bcd_mphy_version =3D cpu_to_be16(0x410); + return desc; +} + +static QueryRespCode ufs_read_desc(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + QueryRespCode status; + uint8_t idn =3D req->req_upiu.qr.idn; + uint16_t length =3D be16_to_cpu(req->req_upiu.qr.length); + InterconnectDescriptor desc; + + switch (idn) { + case QUERY_DESC_IDN_DEVICE: + memcpy(&req->rsp_upiu.qr.data, &u->device_desc, sizeof(u->device_d= esc)); + status =3D QUERY_RESULT_SUCCESS; + break; + case QUERY_DESC_IDN_UNIT: + status =3D ufs_read_unit_desc(req); + break; + case QUERY_DESC_IDN_GEOMETRY: + memcpy(&req->rsp_upiu.qr.data, &u->geometry_desc, + sizeof(u->geometry_desc)); + status =3D QUERY_RESULT_SUCCESS; + break; + case QUERY_DESC_IDN_INTERCONNECT: { + desc =3D interconnect_desc(); + memcpy(&req->rsp_upiu.qr.data, &desc, sizeof(InterconnectDescripto= r)); + status =3D QUERY_RESULT_SUCCESS; + break; + } + case QUERY_DESC_IDN_STRING: + status =3D ufs_read_string_desc(req); + break; + case QUERY_DESC_IDN_POWER: + /* mocking of power descriptor is not supported */ + memset(&req->rsp_upiu.qr.data, 0, sizeof(PowerParametersDescriptor= )); + req->rsp_upiu.qr.data[0] =3D sizeof(PowerParametersDescriptor); + req->rsp_upiu.qr.data[1] =3D QUERY_DESC_IDN_POWER; + status =3D QUERY_RESULT_SUCCESS; + break; + case QUERY_DESC_IDN_HEALTH: + /* mocking of health descriptor is not supported */ + memset(&req->rsp_upiu.qr.data, 0, sizeof(DeviceHealthDescriptor)); + req->rsp_upiu.qr.data[0] =3D sizeof(DeviceHealthDescriptor); + req->rsp_upiu.qr.data[1] =3D QUERY_DESC_IDN_HEALTH; + status =3D QUERY_RESULT_SUCCESS; + break; + default: + length =3D 0; + trace_ufs_err_query_invalid_idn(req->req_upiu.qr.opcode, idn); + status =3D QUERY_RESULT_INVALID_IDN; + } + + if (length > req->rsp_upiu.qr.data[0]) { + length =3D req->rsp_upiu.qr.data[0]; + } + req->rsp_upiu.qr.opcode =3D req->req_upiu.qr.opcode; + req->rsp_upiu.qr.idn =3D req->req_upiu.qr.idn; + req->rsp_upiu.qr.index =3D req->req_upiu.qr.index; + req->rsp_upiu.qr.selector =3D req->req_upiu.qr.selector; + req->rsp_upiu.qr.length =3D cpu_to_be16(length); + + return status; +} + +static QueryRespCode ufs_exec_query_read(UfsRequest *req) +{ + QueryRespCode status; + switch (req->req_upiu.qr.opcode) { + case UPIU_QUERY_OPCODE_NOP: + status =3D QUERY_RESULT_SUCCESS; + break; + case UPIU_QUERY_OPCODE_READ_DESC: + status =3D ufs_read_desc(req); + break; + case UPIU_QUERY_OPCODE_READ_ATTR: + status =3D ufs_exec_query_attr(req, UFS_QUERY_ATTR_READ); + break; + case UPIU_QUERY_OPCODE_READ_FLAG: + status =3D ufs_exec_query_flag(req, UFS_QUERY_FLAG_READ); + break; + default: + trace_ufs_err_query_invalid_opcode(req->req_upiu.qr.opcode); + status =3D QUERY_RESULT_INVALID_OPCODE; + break; + } + + return status; +} + +static QueryRespCode ufs_exec_query_write(UfsRequest *req) +{ + QueryRespCode status; + switch (req->req_upiu.qr.opcode) { + case UPIU_QUERY_OPCODE_NOP: + status =3D QUERY_RESULT_SUCCESS; + break; + case UPIU_QUERY_OPCODE_WRITE_DESC: + /* write descriptor is not supported */ + status =3D QUERY_RESULT_NOT_WRITEABLE; + break; + case UPIU_QUERY_OPCODE_WRITE_ATTR: + status =3D ufs_exec_query_attr(req, UFS_QUERY_ATTR_WRITE); + break; + case UPIU_QUERY_OPCODE_SET_FLAG: + status =3D ufs_exec_query_flag(req, UFS_QUERY_FLAG_SET); + break; + case UPIU_QUERY_OPCODE_CLEAR_FLAG: + status =3D ufs_exec_query_flag(req, UFS_QUERY_FLAG_CLEAR); + break; + case UPIU_QUERY_OPCODE_TOGGLE_FLAG: + status =3D ufs_exec_query_flag(req, UFS_QUERY_FLAG_TOGGLE); + break; + default: + trace_ufs_err_query_invalid_opcode(req->req_upiu.qr.opcode); + status =3D QUERY_RESULT_INVALID_OPCODE; + break; + } + + return status; +} + +static UfsReqResult ufs_exec_query_cmd(UfsRequest *req) +{ + uint8_t query_func =3D req->req_upiu.header.query_func; + uint16_t data_segment_length; + QueryRespCode status; + + trace_ufs_exec_query_cmd(req->slot, req->req_upiu.qr.opcode); + if (query_func =3D=3D UPIU_QUERY_FUNC_STANDARD_READ_REQUEST) { + status =3D ufs_exec_query_read(req); + } else if (query_func =3D=3D UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST) { + status =3D ufs_exec_query_write(req); + } else { + status =3D QUERY_RESULT_GENERAL_FAILURE; + } + + data_segment_length =3D be16_to_cpu(req->rsp_upiu.qr.length); + ufs_build_upiu_header(req, UPIU_TRANSACTION_QUERY_RSP, 0, status, 0, + data_segment_length); + + if (status !=3D QUERY_RESULT_SUCCESS) { + return UFS_REQUEST_FAIL; + } + return UFS_REQUEST_SUCCESS; +} + +static void ufs_exec_req(UfsRequest *req) +{ + UfsReqResult req_result; + + if (ufs_dma_read_upiu(req)) { + return; + } + + switch (req->req_upiu.header.trans_type) { + case UPIU_TRANSACTION_NOP_OUT: + req_result =3D ufs_exec_nop_cmd(req); + break; + case UPIU_TRANSACTION_COMMAND: + /* Not yet implemented */ + req_result =3D UFS_REQUEST_FAIL; + break; + case UPIU_TRANSACTION_QUERY_REQ: + req_result =3D ufs_exec_query_cmd(req); + break; + default: + trace_ufs_err_invalid_trans_code(req->slot, + req->req_upiu.header.trans_type); + req_result =3D UFS_REQUEST_FAIL; + } + + ufs_complete_req(req, req_result); +} + +static void ufs_process_req(void *opaque) +{ + UfsHc *u =3D opaque; + UfsRequest *req; + int slot; + + for (slot =3D 0; slot < u->params.nutrs; slot++) { + req =3D &u->req_list[slot]; + + if (req->state !=3D UFS_REQUEST_READY) { + continue; + } + trace_ufs_process_req(slot); + req->state =3D UFS_REQUEST_RUNNING; + + ufs_exec_req(req); + } +} + +static void ufs_complete_req(UfsRequest *req, UfsReqResult req_result) +{ + UfsHc *u =3D req->hc; + assert(req->state =3D=3D UFS_REQUEST_RUNNING); + + if (req_result =3D=3D UFS_REQUEST_SUCCESS) { + req->utrd.header.dword_2 =3D cpu_to_le32(OCS_SUCCESS); + } else { + req->utrd.header.dword_2 =3D cpu_to_le32(OCS_INVALID_CMD_TABLE_ATT= R); + } + + trace_ufs_complete_req(req->slot); + req->state =3D UFS_REQUEST_COMPLETE; + qemu_bh_schedule(u->complete_bh); +} + +static void ufs_clear_req(UfsRequest *req) +{ + if (req->sg !=3D NULL) { + qemu_sglist_destroy(req->sg); + g_free(req->sg); + req->sg =3D NULL; + } + + memset(&req->utrd, 0, sizeof(req->utrd)); + memset(&req->req_upiu, 0, sizeof(req->req_upiu)); + memset(&req->rsp_upiu, 0, sizeof(req->rsp_upiu)); +} + +static void ufs_sendback_req(void *opaque) +{ + UfsHc *u =3D opaque; + UfsRequest *req; + int slot; + + for (slot =3D 0; slot < u->params.nutrs; slot++) { + req =3D &u->req_list[slot]; + + if (req->state !=3D UFS_REQUEST_COMPLETE) { + continue; + } + + if (ufs_dma_write_upiu(req)) { + req->state =3D UFS_REQUEST_ERROR; + continue; + } + + /* + * TODO: UTP Transfer Request Interrupt Aggregation Control is not= yet + * supported + */ + if (le32_to_cpu(req->utrd.header.dword_2) !=3D OCS_SUCCESS || + le32_to_cpu(req->utrd.header.dword_0) & UTP_REQ_DESC_INT_CMD) { + u->reg.is =3D FIELD_DP32(u->reg.is, IS, UTRCS, 1); + } + + u->reg.utrldbr &=3D ~(1 << slot); + u->reg.utrlcnr |=3D (1 << slot); + + trace_ufs_sendback_req(req->slot); + + ufs_clear_req(req); + req->state =3D UFS_REQUEST_IDLE; + } + + ufs_irq_check(u); +} + static bool ufs_check_constraints(UfsHc *u, Error **errp) { if (u->params.nutrs > UFS_MAX_NUTRS) { @@ -203,6 +1101,23 @@ static void ufs_init_pci(UfsHc *u, PCIDevice *pci_dev) u->irq =3D pci_allocate_irq(pci_dev); } =20 +static void ufs_init_state(UfsHc *u) +{ + u->req_list =3D g_new0(UfsRequest, u->params.nutrs); + + for (int i =3D 0; i < u->params.nutrs; i++) { + u->req_list[i].hc =3D u; + u->req_list[i].slot =3D i; + u->req_list[i].sg =3D NULL; + u->req_list[i].state =3D UFS_REQUEST_IDLE; + } + + u->doorbell_bh =3D qemu_bh_new_guarded(ufs_process_req, u, + &DEVICE(u)->mem_reentrancy_guard); + u->complete_bh =3D qemu_bh_new_guarded(ufs_sendback_req, u, + &DEVICE(u)->mem_reentrancy_guard); +} + static void ufs_init_hc(UfsHc *u) { uint32_t cap =3D 0; @@ -220,6 +1135,52 @@ static void ufs_init_hc(UfsHc *u) cap =3D FIELD_DP32(cap, CAP, CS, 0); u->reg.cap =3D cap; u->reg.ver =3D UFS_SPEC_VER; + + memset(&u->device_desc, 0, sizeof(DeviceDescriptor)); + u->device_desc.length =3D sizeof(DeviceDescriptor); + u->device_desc.descriptor_idn =3D QUERY_DESC_IDN_DEVICE; + u->device_desc.device_sub_class =3D 0x01; + u->device_desc.number_lu =3D 0x00; + u->device_desc.number_wlu =3D 0x04; + /* TODO: Revisit it when Power Management is implemented */ + u->device_desc.init_power_mode =3D 0x01; /* Active Mode */ + u->device_desc.high_priority_lun =3D 0x7F; /* Same Priority */ + u->device_desc.spec_version =3D cpu_to_be16(UFS_SPEC_VER); + u->device_desc.manufacturer_name =3D 0x00; + u->device_desc.product_name =3D 0x01; + u->device_desc.serial_number =3D 0x02; + u->device_desc.oem_id =3D 0x03; + u->device_desc.ud_0_base_offset =3D 0x16; + u->device_desc.ud_config_p_length =3D 0x1A; + u->device_desc.device_rtt_cap =3D 0x02; + u->device_desc.queue_depth =3D u->params.nutrs; + u->device_desc.product_revision_level =3D 0x04; + + memset(&u->geometry_desc, 0, sizeof(GeometryDescriptor)); + u->geometry_desc.length =3D sizeof(GeometryDescriptor); + u->geometry_desc.descriptor_idn =3D QUERY_DESC_IDN_GEOMETRY; + u->geometry_desc.max_number_lu =3D (UFS_MAX_LUS =3D=3D 32) ? 0x1 : 0x0; + u->geometry_desc.segment_size =3D cpu_to_be32(0x2000); /* 4KB */ + u->geometry_desc.allocation_unit_size =3D 0x1; /* 4KB */ + u->geometry_desc.min_addr_block_size =3D 0x8; /* 4KB */ + u->geometry_desc.max_in_buffer_size =3D 0x8; + u->geometry_desc.max_out_buffer_size =3D 0x8; + u->geometry_desc.rpmb_read_write_size =3D 0x40; + u->geometry_desc.data_ordering =3D + 0x0; /* out-of-order data transfer is not supported */ + u->geometry_desc.max_context_id_number =3D 0x5; + u->geometry_desc.supported_memory_types =3D cpu_to_be16(0x8001); + + memset(&u->attributes, 0, sizeof(u->attributes)); + u->attributes.max_data_in_size =3D 0x08; + u->attributes.max_data_out_size =3D 0x08; + u->attributes.ref_clk_freq =3D 0x01; /* 26 MHz */ + /* configure descriptor is not supported */ + u->attributes.config_descr_lock =3D 0x01; + u->attributes.max_num_of_rtt =3D 0x02; + + memset(&u->flags, 0, sizeof(u->flags)); + u->flags.permanently_disable_fw_update =3D 1; } =20 static void ufs_realize(PCIDevice *pci_dev, Error **errp) @@ -230,10 +1191,24 @@ static void ufs_realize(PCIDevice *pci_dev, Error **= errp) return; } =20 + ufs_init_state(u); ufs_init_hc(u); ufs_init_pci(u, pci_dev); } =20 +static void ufs_exit(PCIDevice *pci_dev) +{ + UfsHc *u =3D UFS(pci_dev); + + qemu_bh_delete(u->doorbell_bh); + qemu_bh_delete(u->complete_bh); + + for (int i =3D 0; i < u->params.nutrs; i++) { + ufs_clear_req(&u->req_list[i]); + } + g_free(u->req_list); +} + static Property ufs_props[] =3D { DEFINE_PROP_STRING("serial", UfsHc, params.serial), DEFINE_PROP_UINT8("nutrs", UfsHc, params.nutrs, 32), @@ -252,6 +1227,7 @@ static void ufs_class_init(ObjectClass *oc, void *data) PCIDeviceClass *pc =3D PCI_DEVICE_CLASS(oc); =20 pc->realize =3D ufs_realize; + pc->exit =3D ufs_exit; pc->vendor_id =3D PCI_VENDOR_ID_REDHAT; pc->device_id =3D PCI_DEVICE_ID_REDHAT_UFS; pc->class_id =3D PCI_CLASS_STORAGE_UFS; diff --git a/hw/ufs/trace-events b/hw/ufs/trace-events index d1badcad10..665e1a942b 100644 --- a/hw/ufs/trace-events +++ b/hw/ufs/trace-events @@ -18,6 +18,7 @@ ufs_err_dma_read_req_upiu(uint32_t slot, uint64_t addr) "= failed to read req upiu 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%"PR= Ix32" is not yet supported" ufs_err_invalid_register_offset(uint32_t offset) "Register offset 0x%"PRIx= 32" is invalid" --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896475; cv=none; d=zohomail.com; s=zohoarc; b=cW426kdb2PV+DvtbmhCjD85A6sWB+obyPhoHVZTasvyo6Y9Keyk8mIQ2bzFbC2vObOQYpvYDQ1PvRXa1bBpRNoq50ZQ6Sv7UWPYdDBXS/hJnYL8uCVQJl68ggqr421A3BxeVMhvZElC1gcB1/9tTNzgTC03Z+9FfTJXzyt2EF8U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896475; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3xtzoQNezClJZxHjgG8xqio1GPqeqMBPqZAYQS2sOJk=; b=bjHPBVZ0O2cu6qxcMBULqeD9zugwCCrzGg3B31nudct5DOVrGKzNRt2rELTksa8r/hncGdqfZ/yzFgo7+SelN5ENTIDCUCGrdNi/9oc1to34OHdQU4gS1SihwfL6bLLLS2PneJVkD9bKmySFhWn3y4IepQuSf213FFdPlUmH4nY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896475671606.3475908351091; Thu, 24 Aug 2023 10:01:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfd-0006AT-Cn; Thu, 24 Aug 2023 12:58:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfc-000682-DF for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:56 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfW-0003Pd-VB for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:56 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-387-8tV01WUxOoW5jbYcla2uoQ-1; Thu, 24 Aug 2023 12:58:46 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F362C185A794; Thu, 24 Aug 2023 16:58:45 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id BCBF5C1602B; Thu, 24 Aug 2023 16:58:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896330; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3xtzoQNezClJZxHjgG8xqio1GPqeqMBPqZAYQS2sOJk=; b=HRjNTYbCFA3AfHmdZq/FASZQm5QHKfVdWq12SFuC5MvoH0UTprPT6axVUF6pZnFZuiuV4A BpEVpQPqgDo9vr0oB4inw1jI+sG6fjU9GJpKn6XHxwKQd7tG7mTXowph/ovQupd8o3MeO9 EXjmTXmQtqoKUfFQE5E1NZYRLMzW2Ew= X-MC-Unique: 8tV01WUxOoW5jbYcla2uoQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Jeuk Kim Subject: [PULL 3/8] hw/ufs: Support for UFS logical unit Date: Thu, 24 Aug 2023 12:58:28 -0400 Message-ID: <20230824165833.1689137-4-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896484098100001 Content-Type: text/plain; charset="utf-8" From: Jeuk Kim This commit adds support for ufs logical unit. The LU handles processing for the SCSI command, unit descriptor query request. This commit enables the UFS device to process IO requests. Signed-off-by: Jeuk Kim Reviewed-by: Stefan Hajnoczi Message-id: 898cea923e819dc21a99597bf045a12d7983be28.1691062912.git.jeuk20.= kim@samsung.com Signed-off-by: Stefan Hajnoczi --- hw/ufs/ufs.h | 43 ++ include/scsi/constants.h | 1 + hw/ufs/lu.c | 1445 ++++++++++++++++++++++++++++++++++++++ hw/ufs/ufs.c | 252 ++++++- hw/ufs/meson.build | 2 +- hw/ufs/trace-events | 25 + 6 files changed, 1761 insertions(+), 7 deletions(-) create mode 100644 hw/ufs/lu.c diff --git a/hw/ufs/ufs.h b/hw/ufs/ufs.h index 3d1b2cff4e..f244228617 100644 --- a/hw/ufs/ufs.h +++ b/hw/ufs/ufs.h @@ -18,6 +18,18 @@ #define UFS_MAX_LUS 32 #define UFS_BLOCK_SIZE 4096 =20 +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 =3D 0, UFS_REQUEST_READY =3D 1, @@ -29,6 +41,7 @@ typedef enum UfsRequestState { typedef enum UfsReqResult { UFS_REQUEST_SUCCESS =3D 0, UFS_REQUEST_FAIL =3D 1, + UFS_REQUEST_NO_COMPLETE =3D 2, } UfsReqResult; =20 typedef struct UfsRequest { @@ -44,6 +57,17 @@ typedef struct UfsRequest { QEMUSGList *sg; } UfsRequest; =20 +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 */ @@ -52,12 +76,18 @@ typedef struct UfsParams { =20 typedef struct UfsHc { PCIDevice parent_obj; + UfsBus bus; MemoryRegion iomem; UfsReg reg; UfsParams params; uint32_t reg_size; UfsRequest *req_list; =20 + 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; @@ -71,6 +101,12 @@ typedef struct UfsHc { #define TYPE_UFS "ufs" #define UFS(obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS) =20 +#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 =3D 0x0, UFS_QUERY_FLAG_READ =3D 0x1, @@ -85,4 +121,11 @@ typedef enum UfsQueryAttrPerm { UFS_QUERY_ATTR_WRITE =3D 0x2, } UfsQueryAttrPerm; =20 +static inline bool is_wlun(uint8_t lun) +{ + return (lun =3D=3D UFS_UPIU_REPORT_LUNS_WLUN || + lun =3D=3D UFS_UPIU_UFS_DEVICE_WLUN || lun =3D=3D UFS_UPIU_BOO= T_WLUN || + lun =3D=3D UFS_UPIU_RPMB_WLUN); +} + #endif /* HW_UFS_UFS_H */ diff --git a/include/scsi/constants.h b/include/scsi/constants.h index 6a8bad556a..9b98451912 100644 --- a/include/scsi/constants.h +++ b/include/scsi/constants.h @@ -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 diff --git a/hw/ufs/lu.c b/hw/ufs/lu.c new file mode 100644 index 0000000000..ec8932ff86 --- /dev/null +++ b/hw/ufs/lu.c @@ -0,0 +1,1445 @@ +/* + * QEMU UFS Logical Unit + * + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved. + * + * Written by Jeuk Kim + * + * This code is licensed under the GNU GPL v2 or later. + */ + +#include "qemu/osdep.h" +#include "qemu/units.h" +#include "qapi/error.h" +#include "qemu/memalign.h" +#include "hw/scsi/scsi.h" +#include "scsi/constants.h" +#include "sysemu/block-backend.h" +#include "qemu/cutils.h" +#include "trace.h" +#include "ufs.h" + +/* + * The code below handling SCSI commands is copied from hw/scsi/scsi-disk.= c, + * with minor adjustments to make it work for UFS. + */ + +#define SCSI_DMA_BUF_SIZE (128 * KiB) +#define SCSI_MAX_INQUIRY_LEN 256 +#define SCSI_INQUIRY_DATA_SIZE 36 +#define SCSI_MAX_MODE_LEN 256 + +typedef struct UfsSCSIReq { + SCSIRequest req; + /* Both sector and sector_count are in terms of BDRV_SECTOR_SIZE bytes= . */ + uint64_t sector; + uint32_t sector_count; + uint32_t buflen; + bool started; + bool need_fua_emulation; + struct iovec iov; + QEMUIOVector qiov; + BlockAcctCookie acct; +} UfsSCSIReq; + +static void ufs_scsi_free_request(SCSIRequest *req) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + + qemu_vfree(r->iov.iov_base); +} + +static void scsi_check_condition(UfsSCSIReq *r, SCSISense sense) +{ + trace_ufs_scsi_check_condition(r->req.tag, sense.key, sense.asc, + sense.ascq); + scsi_req_build_sense(&r->req, sense); + scsi_req_complete(&r->req, CHECK_CONDITION); +} + +static int ufs_scsi_emulate_vpd_page(SCSIRequest *req, uint8_t *outbuf, + uint32_t outbuf_len) +{ + UfsHc *u =3D UFS(req->bus->qbus.parent); + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, req->dev); + uint8_t page_code =3D req->cmd.buf[2]; + int start, buflen =3D 0; + + if (outbuf_len < SCSI_INQUIRY_DATA_SIZE) { + return -1; + } + + outbuf[buflen++] =3D lu->qdev.type & 0x1f; + outbuf[buflen++] =3D page_code; + outbuf[buflen++] =3D 0x00; + outbuf[buflen++] =3D 0x00; + start =3D buflen; + + switch (page_code) { + case 0x00: /* Supported page codes, mandatory */ + { + trace_ufs_scsi_emulate_vpd_page_00(req->cmd.xfer); + outbuf[buflen++] =3D 0x00; /* list of supported pages (this page) = */ + if (u->params.serial) { + outbuf[buflen++] =3D 0x80; /* unit serial number */ + } + outbuf[buflen++] =3D 0x87; /* mode page policy */ + break; + } + case 0x80: /* Device serial number, optional */ + { + int l; + + if (!u->params.serial) { + trace_ufs_scsi_emulate_vpd_page_80_not_supported(); + return -1; + } + + l =3D strlen(u->params.serial); + if (l > SCSI_INQUIRY_DATA_SIZE) { + l =3D SCSI_INQUIRY_DATA_SIZE; + } + + trace_ufs_scsi_emulate_vpd_page_80(req->cmd.xfer); + memcpy(outbuf + buflen, u->params.serial, l); + buflen +=3D l; + break; + } + case 0x87: /* Mode Page Policy, mandatory */ + { + trace_ufs_scsi_emulate_vpd_page_87(req->cmd.xfer); + outbuf[buflen++] =3D 0x3f; /* apply to all mode pages and subpages= */ + outbuf[buflen++] =3D 0xff; + outbuf[buflen++] =3D 0; /* shared */ + outbuf[buflen++] =3D 0; + break; + } + default: + return -1; + } + /* done with EVPD */ + assert(buflen - start <=3D 255); + outbuf[start - 1] =3D buflen - start; + return buflen; +} + +static int ufs_scsi_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf, + uint32_t outbuf_len) +{ + int buflen =3D 0; + + if (outbuf_len < SCSI_INQUIRY_DATA_SIZE) { + return -1; + } + + if (req->cmd.buf[1] & 0x1) { + /* Vital product data */ + return ufs_scsi_emulate_vpd_page(req, outbuf, outbuf_len); + } + + /* Standard INQUIRY data */ + if (req->cmd.buf[2] !=3D 0) { + return -1; + } + + /* PAGE CODE =3D=3D 0 */ + buflen =3D req->cmd.xfer; + if (buflen > SCSI_MAX_INQUIRY_LEN) { + buflen =3D SCSI_MAX_INQUIRY_LEN; + } + + if (is_wlun(req->lun)) { + outbuf[0] =3D TYPE_WLUN; + } else { + outbuf[0] =3D 0; + } + outbuf[1] =3D 0; + + strpadcpy((char *)&outbuf[16], 16, "QEMU UFS", ' '); + strpadcpy((char *)&outbuf[8], 8, "QEMU", ' '); + + memset(&outbuf[32], 0, 4); + + outbuf[2] =3D 0x06; /* SPC-4 */ + outbuf[3] =3D 0x2; + + if (buflen > SCSI_INQUIRY_DATA_SIZE) { + outbuf[4] =3D buflen - 5; /* Additional Length =3D (Len - 1) - 4 */ + } else { + /* + * If the allocation length of CDB is too small, the additional + * length is not adjusted + */ + outbuf[4] =3D SCSI_INQUIRY_DATA_SIZE - 5; + } + + /* Support TCQ. */ + outbuf[7] =3D req->bus->info->tcq ? 0x02 : 0; + return buflen; +} + +static int mode_sense_page(UfsLu *lu, int page, uint8_t **p_outbuf, + int page_control) +{ + static const int mode_sense_valid[0x3f] =3D { + [MODE_PAGE_CACHING] =3D 1, + [MODE_PAGE_R_W_ERROR] =3D 1, + [MODE_PAGE_CONTROL] =3D 1, + }; + + uint8_t *p =3D *p_outbuf + 2; + int length; + + assert(page < ARRAY_SIZE(mode_sense_valid)); + if ((mode_sense_valid[page]) =3D=3D 0) { + return -1; + } + + /* + * If Changeable Values are requested, a mask denoting those mode para= meters + * that are changeable shall be returned. As we currently don't support + * parameter changes via MODE_SELECT all bits are returned set to zero. + * The buffer was already memset to zero by the caller of this functio= n. + */ + switch (page) { + case MODE_PAGE_CACHING: + length =3D 0x12; + if (page_control =3D=3D 1 || /* Changeable Values */ + blk_enable_write_cache(lu->qdev.conf.blk)) { + p[0] =3D 4; /* WCE */ + } + break; + + case MODE_PAGE_R_W_ERROR: + length =3D 10; + if (page_control =3D=3D 1) { /* Changeable Values */ + break; + } + p[0] =3D 0x80; /* Automatic Write Reallocation Enabled */ + break; + + case MODE_PAGE_CONTROL: + length =3D 10; + if (page_control =3D=3D 1) { /* Changeable Values */ + break; + } + p[1] =3D 0x10; /* Queue Algorithm modifier */ + p[8] =3D 0xff; /* Busy Timeout Period */ + p[9] =3D 0xff; + break; + + default: + return -1; + } + + assert(length < 256); + (*p_outbuf)[0] =3D page; + (*p_outbuf)[1] =3D length; + *p_outbuf +=3D length + 2; + return length + 2; +} + +static int ufs_scsi_emulate_mode_sense(UfsSCSIReq *r, uint8_t *outbuf) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + bool dbd; + int page, buflen, ret, page_control; + uint8_t *p; + uint8_t dev_specific_param =3D 0; + + dbd =3D (r->req.cmd.buf[1] & 0x8) !=3D 0; + if (!dbd) { + return -1; + } + + page =3D r->req.cmd.buf[2] & 0x3f; + page_control =3D (r->req.cmd.buf[2] & 0xc0) >> 6; + + trace_ufs_scsi_emulate_mode_sense((r->req.cmd.buf[0] =3D=3D MODE_SENSE= ) ? 6 : + = 10, + page, r->req.cmd.xfer, page_control); + memset(outbuf, 0, r->req.cmd.xfer); + p =3D outbuf; + + if (!blk_is_writable(lu->qdev.conf.blk)) { + dev_specific_param |=3D 0x80; /* Readonly. */ + } + + p[2] =3D 0; /* Medium type. */ + p[3] =3D dev_specific_param; + p[6] =3D p[7] =3D 0; /* Block descriptor length. */ + p +=3D 8; + + if (page_control =3D=3D 3) { + /* Saved Values */ + scsi_check_condition(r, SENSE_CODE(SAVING_PARAMS_NOT_SUPPORTED)); + return -1; + } + + if (page =3D=3D 0x3f) { + for (page =3D 0; page <=3D 0x3e; page++) { + mode_sense_page(lu, page, &p, page_control); + } + } else { + ret =3D mode_sense_page(lu, page, &p, page_control); + if (ret =3D=3D -1) { + return -1; + } + } + + buflen =3D p - outbuf; + /* + * The mode data length field specifies the length in bytes of the + * following data that is available to be transferred. The mode data + * length does not include itself. + */ + outbuf[0] =3D ((buflen - 2) >> 8) & 0xff; + outbuf[1] =3D (buflen - 2) & 0xff; + return buflen; +} + +/* + * scsi_handle_rw_error has two return values. False means that the error + * must be ignored, true means that the error has been processed and the + * caller should not do anything else for this request. Note that + * scsi_handle_rw_error always manages its reference counts, independent + * of the return value. + */ +static bool scsi_handle_rw_error(UfsSCSIReq *r, int ret, bool acct_failed) +{ + bool is_read =3D (r->req.cmd.mode =3D=3D SCSI_XFER_FROM_DEV); + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + SCSISense sense =3D SENSE_CODE(NO_SENSE); + int error =3D 0; + bool req_has_sense =3D false; + BlockErrorAction action; + int status; + + if (ret < 0) { + status =3D scsi_sense_from_errno(-ret, &sense); + error =3D -ret; + } else { + /* A passthrough command has completed with nonzero status. */ + status =3D ret; + if (status =3D=3D CHECK_CONDITION) { + req_has_sense =3D true; + error =3D scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.= sense)); + } else { + error =3D EINVAL; + } + } + + /* + * Check whether the error has to be handled by the guest or should + * rather follow the rerror=3D/werror=3D settings. Guest-handled erro= rs + * are usually retried immediately, so do not post them to QMP and + * do not account them as failed I/O. + */ + if (req_has_sense && scsi_sense_buf_is_guest_recoverable( + r->req.sense, sizeof(r->req.sense))) { + action =3D BLOCK_ERROR_ACTION_REPORT; + acct_failed =3D false; + } else { + action =3D blk_get_error_action(lu->qdev.conf.blk, is_read, error); + blk_error_action(lu->qdev.conf.blk, action, is_read, error); + } + + switch (action) { + case BLOCK_ERROR_ACTION_REPORT: + if (acct_failed) { + block_acct_failed(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } + if (!req_has_sense && status =3D=3D CHECK_CONDITION) { + scsi_req_build_sense(&r->req, sense); + } + scsi_req_complete(&r->req, status); + return true; + + case BLOCK_ERROR_ACTION_IGNORE: + return false; + + case BLOCK_ERROR_ACTION_STOP: + scsi_req_retry(&r->req); + return true; + + default: + g_assert_not_reached(); + } +} + +static bool ufs_scsi_req_check_error(UfsSCSIReq *r, int ret, bool acct_fai= led) +{ + if (r->req.io_canceled) { + scsi_req_cancel_complete(&r->req); + return true; + } + + if (ret < 0) { + return scsi_handle_rw_error(r, ret, acct_failed); + } + + return false; +} + +static void scsi_aio_complete(void *opaque, int ret) +{ + UfsSCSIReq *r =3D (UfsSCSIReq *)opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + aio_context_acquire(blk_get_aio_context(lu->qdev.conf.blk)); + if (ufs_scsi_req_check_error(r, ret, true)) { + goto done; + } + + block_acct_done(blk_get_stats(lu->qdev.conf.blk), &r->acct); + scsi_req_complete(&r->req, GOOD); + +done: + aio_context_release(blk_get_aio_context(lu->qdev.conf.blk)); + scsi_req_unref(&r->req); +} + +static int32_t ufs_scsi_emulate_command(SCSIRequest *req, uint8_t *buf) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, req->dev); + uint32_t last_block =3D 0; + uint8_t *outbuf; + int buflen; + + switch (req->cmd.buf[0]) { + case INQUIRY: + case MODE_SENSE_10: + case START_STOP: + case REQUEST_SENSE: + break; + + default: + if (!blk_is_available(lu->qdev.conf.blk)) { + scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); + return 0; + } + break; + } + + /* + * FIXME: we shouldn't return anything bigger than 4k, but the code + * requires the buffer to be as big as req->cmd.xfer in several + * places. So, do not allow CDBs with a very large ALLOCATION + * LENGTH. The real fix would be to modify scsi_read_data and + * dma_buf_read, so that they return data beyond the buflen + * as all zeros. + */ + if (req->cmd.xfer > 65536) { + goto illegal_request; + } + r->buflen =3D MAX(4096, req->cmd.xfer); + + if (!r->iov.iov_base) { + r->iov.iov_base =3D blk_blockalign(lu->qdev.conf.blk, r->buflen); + } + + outbuf =3D r->iov.iov_base; + memset(outbuf, 0, r->buflen); + switch (req->cmd.buf[0]) { + case TEST_UNIT_READY: + assert(blk_is_available(lu->qdev.conf.blk)); + break; + case INQUIRY: + buflen =3D ufs_scsi_emulate_inquiry(req, outbuf, r->buflen); + if (buflen < 0) { + goto illegal_request; + } + break; + case MODE_SENSE_10: + buflen =3D ufs_scsi_emulate_mode_sense(r, outbuf); + if (buflen < 0) { + goto illegal_request; + } + break; + case READ_CAPACITY_10: + /* The normal LEN field for this command is zero. */ + memset(outbuf, 0, 8); + if (lu->qdev.max_lba > 0) { + last_block =3D lu->qdev.max_lba - 1; + }; + outbuf[0] =3D (last_block >> 24) & 0xff; + outbuf[1] =3D (last_block >> 16) & 0xff; + outbuf[2] =3D (last_block >> 8) & 0xff; + outbuf[3] =3D last_block & 0xff; + outbuf[4] =3D (lu->qdev.blocksize >> 24) & 0xff; + outbuf[5] =3D (lu->qdev.blocksize >> 16) & 0xff; + outbuf[6] =3D (lu->qdev.blocksize >> 8) & 0xff; + outbuf[7] =3D lu->qdev.blocksize & 0xff; + break; + case REQUEST_SENSE: + /* Just return "NO SENSE". */ + buflen =3D scsi_convert_sense(NULL, 0, outbuf, r->buflen, + (req->cmd.buf[1] & 1) =3D=3D 0); + if (buflen < 0) { + goto illegal_request; + } + break; + case SYNCHRONIZE_CACHE: + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + block_acct_start(blk_get_stats(lu->qdev.conf.blk), &r->acct, 0, + BLOCK_ACCT_FLUSH); + r->req.aiocb =3D blk_aio_flush(lu->qdev.conf.blk, scsi_aio_complet= e, r); + return 0; + case VERIFY_10: + trace_ufs_scsi_emulate_command_VERIFY((req->cmd.buf[1] >> 1) & 3); + if (req->cmd.buf[1] & 6) { + goto illegal_request; + } + break; + case SERVICE_ACTION_IN_16: + /* Service Action In subcommands. */ + if ((req->cmd.buf[1] & 31) =3D=3D SAI_READ_CAPACITY_16) { + trace_ufs_scsi_emulate_command_SAI_16(); + memset(outbuf, 0, req->cmd.xfer); + + if (lu->qdev.max_lba > 0) { + last_block =3D lu->qdev.max_lba - 1; + }; + outbuf[0] =3D 0; + outbuf[1] =3D 0; + outbuf[2] =3D 0; + outbuf[3] =3D 0; + outbuf[4] =3D (last_block >> 24) & 0xff; + outbuf[5] =3D (last_block >> 16) & 0xff; + outbuf[6] =3D (last_block >> 8) & 0xff; + outbuf[7] =3D last_block & 0xff; + outbuf[8] =3D (lu->qdev.blocksize >> 24) & 0xff; + outbuf[9] =3D (lu->qdev.blocksize >> 16) & 0xff; + outbuf[10] =3D (lu->qdev.blocksize >> 8) & 0xff; + outbuf[11] =3D lu->qdev.blocksize & 0xff; + outbuf[12] =3D 0; + outbuf[13] =3D get_physical_block_exp(&lu->qdev.conf); + + if (lu->unit_desc.provisioning_type =3D=3D 2 || + lu->unit_desc.provisioning_type =3D=3D 3) { + outbuf[14] =3D 0x80; + } + /* Protection, exponent and lowest lba field left blank. */ + break; + } + trace_ufs_scsi_emulate_command_SAI_unsupported(); + goto illegal_request; + case MODE_SELECT_10: + trace_ufs_scsi_emulate_command_MODE_SELECT_10(r->req.cmd.xfer); + break; + case START_STOP: + /* + * TODO: START_STOP is not yet implemented. It always returns succ= ess. + * Revisit it when ufs power management is implemented. + */ + trace_ufs_scsi_emulate_command_START_STOP(); + break; + case FORMAT_UNIT: + trace_ufs_scsi_emulate_command_FORMAT_UNIT(); + break; + case SEND_DIAGNOSTIC: + trace_ufs_scsi_emulate_command_SEND_DIAGNOSTIC(); + break; + default: + trace_ufs_scsi_emulate_command_UNKNOWN(buf[0], + scsi_command_name(buf[0])); + scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE)); + return 0; + } + assert(!r->req.aiocb); + r->iov.iov_len =3D MIN(r->buflen, req->cmd.xfer); + if (r->iov.iov_len =3D=3D 0) { + scsi_req_complete(&r->req, GOOD); + } + if (r->req.cmd.mode =3D=3D SCSI_XFER_TO_DEV) { + assert(r->iov.iov_len =3D=3D req->cmd.xfer); + return -r->iov.iov_len; + } else { + return r->iov.iov_len; + } + +illegal_request: + if (r->req.status =3D=3D -1) { + scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); + } + return 0; +} + +static void ufs_scsi_emulate_read_data(SCSIRequest *req) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + int buflen =3D r->iov.iov_len; + + if (buflen) { + trace_ufs_scsi_emulate_read_data(buflen); + r->iov.iov_len =3D 0; + r->started =3D true; + scsi_req_data(&r->req, buflen); + return; + } + + /* This also clears the sense buffer for REQUEST SENSE. */ + scsi_req_complete(&r->req, GOOD); +} + +static int ufs_scsi_check_mode_select(UfsLu *lu, int page, uint8_t *inbuf, + int inlen) +{ + uint8_t mode_current[SCSI_MAX_MODE_LEN]; + uint8_t mode_changeable[SCSI_MAX_MODE_LEN]; + uint8_t *p; + int len, expected_len, changeable_len, i; + + /* + * The input buffer does not include the page header, so it is + * off by 2 bytes. + */ + expected_len =3D inlen + 2; + if (expected_len > SCSI_MAX_MODE_LEN) { + return -1; + } + + /* MODE_PAGE_ALLS is only valid for MODE SENSE commands */ + if (page =3D=3D MODE_PAGE_ALLS) { + return -1; + } + + p =3D mode_current; + memset(mode_current, 0, inlen + 2); + len =3D mode_sense_page(lu, page, &p, 0); + if (len < 0 || len !=3D expected_len) { + return -1; + } + + p =3D mode_changeable; + memset(mode_changeable, 0, inlen + 2); + changeable_len =3D mode_sense_page(lu, page, &p, 1); + assert(changeable_len =3D=3D len); + + /* + * Check that unchangeable bits are the same as what MODE SENSE + * would return. + */ + for (i =3D 2; i < len; i++) { + if (((mode_current[i] ^ inbuf[i - 2]) & ~mode_changeable[i]) !=3D = 0) { + return -1; + } + } + return 0; +} + +static void ufs_scsi_apply_mode_select(UfsLu *lu, int page, uint8_t *p) +{ + switch (page) { + case MODE_PAGE_CACHING: + blk_set_enable_write_cache(lu->qdev.conf.blk, (p[0] & 4) !=3D 0); + break; + + default: + break; + } +} + +static int mode_select_pages(UfsSCSIReq *r, uint8_t *p, int len, bool chan= ge) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + while (len > 0) { + int page, page_len; + + page =3D p[0] & 0x3f; + if (p[0] & 0x40) { + goto invalid_param; + } else { + if (len < 2) { + goto invalid_param_len; + } + page_len =3D p[1]; + p +=3D 2; + len -=3D 2; + } + + if (page_len > len) { + goto invalid_param_len; + } + + if (!change) { + if (ufs_scsi_check_mode_select(lu, page, p, page_len) < 0) { + goto invalid_param; + } + } else { + ufs_scsi_apply_mode_select(lu, page, p); + } + + p +=3D page_len; + len -=3D page_len; + } + return 0; + +invalid_param: + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); + return -1; + +invalid_param_len: + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); + return -1; +} + +static void ufs_scsi_emulate_mode_select(UfsSCSIReq *r, uint8_t *inbuf) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + uint8_t *p =3D inbuf; + int len =3D r->req.cmd.xfer; + int hdr_len =3D 8; + int bd_len; + int pass; + + /* We only support PF=3D1, SP=3D0. */ + if ((r->req.cmd.buf[1] & 0x11) !=3D 0x10) { + goto invalid_field; + } + + if (len < hdr_len) { + goto invalid_param_len; + } + + bd_len =3D lduw_be_p(&p[6]); + if (bd_len !=3D 0) { + goto invalid_param; + } + + len -=3D hdr_len; + p +=3D hdr_len; + + /* Ensure no change is made if there is an error! */ + for (pass =3D 0; pass < 2; pass++) { + if (mode_select_pages(r, p, len, pass =3D=3D 1) < 0) { + assert(pass =3D=3D 0); + return; + } + } + + if (!blk_enable_write_cache(lu->qdev.conf.blk)) { + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + block_acct_start(blk_get_stats(lu->qdev.conf.blk), &r->acct, 0, + BLOCK_ACCT_FLUSH); + r->req.aiocb =3D blk_aio_flush(lu->qdev.conf.blk, scsi_aio_complet= e, r); + return; + } + + scsi_req_complete(&r->req, GOOD); + return; + +invalid_param: + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM)); + return; + +invalid_param_len: + scsi_check_condition(r, SENSE_CODE(INVALID_PARAM_LEN)); + return; + +invalid_field: + scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); +} + +/* block_num and nb_blocks expected to be in qdev blocksize */ +static inline bool check_lba_range(UfsLu *lu, uint64_t block_num, + uint32_t nb_blocks) +{ + /* + * The first line tests that no overflow happens when computing the la= st + * block. The second line tests that the last accessed block is in + * range. + * + * Careful, the computations should not underflow for nb_blocks =3D=3D= 0, + * and a 0-block read to the first LBA beyond the end of device is + * valid. + */ + return (block_num <=3D block_num + nb_blocks && + block_num + nb_blocks <=3D lu->qdev.max_lba + 1); +} + +static void ufs_scsi_emulate_write_data(SCSIRequest *req) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + + if (r->iov.iov_len) { + int buflen =3D r->iov.iov_len; + trace_ufs_scsi_emulate_write_data(buflen); + r->iov.iov_len =3D 0; + scsi_req_data(&r->req, buflen); + return; + } + + switch (req->cmd.buf[0]) { + case MODE_SELECT_10: + /* This also clears the sense buffer for REQUEST SENSE. */ + ufs_scsi_emulate_mode_select(r, r->iov.iov_base); + break; + default: + abort(); + } +} + +/* Return a pointer to the data buffer. */ +static uint8_t *ufs_scsi_get_buf(SCSIRequest *req) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + + return (uint8_t *)r->iov.iov_base; +} + +static int32_t ufs_scsi_dma_command(SCSIRequest *req, uint8_t *buf) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, req->dev); + uint32_t len; + uint8_t command; + + command =3D buf[0]; + + if (!blk_is_available(lu->qdev.conf.blk)) { + scsi_check_condition(r, SENSE_CODE(NO_MEDIUM)); + return 0; + } + + len =3D scsi_data_cdb_xfer(r->req.cmd.buf); + switch (command) { + case READ_6: + case READ_10: + trace_ufs_scsi_dma_command_READ(r->req.cmd.lba, len); + if (r->req.cmd.buf[1] & 0xe0) { + goto illegal_request; + } + if (!check_lba_range(lu, r->req.cmd.lba, len)) { + goto illegal_lba; + } + r->sector =3D r->req.cmd.lba * (lu->qdev.blocksize / BDRV_SECTOR_S= IZE); + r->sector_count =3D len * (lu->qdev.blocksize / BDRV_SECTOR_SIZE); + break; + case WRITE_6: + case WRITE_10: + trace_ufs_scsi_dma_command_WRITE(r->req.cmd.lba, len); + if (!blk_is_writable(lu->qdev.conf.blk)) { + scsi_check_condition(r, SENSE_CODE(WRITE_PROTECTED)); + return 0; + } + if (r->req.cmd.buf[1] & 0xe0) { + goto illegal_request; + } + if (!check_lba_range(lu, r->req.cmd.lba, len)) { + goto illegal_lba; + } + r->sector =3D r->req.cmd.lba * (lu->qdev.blocksize / BDRV_SECTOR_S= IZE); + r->sector_count =3D len * (lu->qdev.blocksize / BDRV_SECTOR_SIZE); + break; + default: + abort(); + illegal_request: + scsi_check_condition(r, SENSE_CODE(INVALID_FIELD)); + return 0; + illegal_lba: + scsi_check_condition(r, SENSE_CODE(LBA_OUT_OF_RANGE)); + return 0; + } + r->need_fua_emulation =3D ((r->req.cmd.buf[1] & 8) !=3D 0); + if (r->sector_count =3D=3D 0) { + scsi_req_complete(&r->req, GOOD); + } + assert(r->iov.iov_len =3D=3D 0); + if (r->req.cmd.mode =3D=3D SCSI_XFER_TO_DEV) { + return -r->sector_count * BDRV_SECTOR_SIZE; + } else { + return r->sector_count * BDRV_SECTOR_SIZE; + } +} + +static void scsi_write_do_fua(UfsSCSIReq *r) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb =3D=3D NULL); + assert(!r->req.io_canceled); + + if (r->need_fua_emulation) { + block_acct_start(blk_get_stats(lu->qdev.conf.blk), &r->acct, 0, + BLOCK_ACCT_FLUSH); + r->req.aiocb =3D blk_aio_flush(lu->qdev.conf.blk, scsi_aio_complet= e, r); + return; + } + + scsi_req_complete(&r->req, GOOD); + scsi_req_unref(&r->req); +} + +static void scsi_dma_complete_noio(UfsSCSIReq *r, int ret) +{ + assert(r->req.aiocb =3D=3D NULL); + if (ufs_scsi_req_check_error(r, ret, false)) { + goto done; + } + + r->sector +=3D r->sector_count; + r->sector_count =3D 0; + if (r->req.cmd.mode =3D=3D SCSI_XFER_TO_DEV) { + scsi_write_do_fua(r); + return; + } else { + scsi_req_complete(&r->req, GOOD); + } + +done: + scsi_req_unref(&r->req); +} + +static void scsi_dma_complete(void *opaque, int ret) +{ + UfsSCSIReq *r =3D (UfsSCSIReq *)opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + + aio_context_acquire(blk_get_aio_context(lu->qdev.conf.blk)); + if (ret < 0) { + block_acct_failed(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } else { + block_acct_done(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } + scsi_dma_complete_noio(r, ret); + aio_context_release(blk_get_aio_context(lu->qdev.conf.blk)); +} + +static BlockAIOCB *scsi_dma_readv(int64_t offset, QEMUIOVector *iov, + BlockCompletionFunc *cb, void *cb_opaque, + void *opaque) +{ + UfsSCSIReq *r =3D opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + return blk_aio_preadv(lu->qdev.conf.blk, offset, iov, 0, cb, cb_opaque= ); +} + +static void scsi_init_iovec(UfsSCSIReq *r, size_t size) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + if (!r->iov.iov_base) { + r->buflen =3D size; + r->iov.iov_base =3D blk_blockalign(lu->qdev.conf.blk, r->buflen); + } + r->iov.iov_len =3D MIN(r->sector_count * BDRV_SECTOR_SIZE, r->buflen); + qemu_iovec_init_external(&r->qiov, &r->iov, 1); +} + +static void scsi_read_complete_noio(UfsSCSIReq *r, int ret) +{ + uint32_t n; + + assert(r->req.aiocb =3D=3D NULL); + if (ufs_scsi_req_check_error(r, ret, false)) { + goto done; + } + + n =3D r->qiov.size / BDRV_SECTOR_SIZE; + r->sector +=3D n; + r->sector_count -=3D n; + scsi_req_data(&r->req, r->qiov.size); + +done: + scsi_req_unref(&r->req); +} + +static void scsi_read_complete(void *opaque, int ret) +{ + UfsSCSIReq *r =3D (UfsSCSIReq *)opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + trace_ufs_scsi_read_data_count(r->sector_count); + aio_context_acquire(blk_get_aio_context(lu->qdev.conf.blk)); + if (ret < 0) { + block_acct_failed(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } else { + block_acct_done(blk_get_stats(lu->qdev.conf.blk), &r->acct); + trace_ufs_scsi_read_complete(r->req.tag, r->qiov.size); + } + scsi_read_complete_noio(r, ret); + aio_context_release(blk_get_aio_context(lu->qdev.conf.blk)); +} + +/* Actually issue a read to the block device. */ +static void scsi_do_read(UfsSCSIReq *r, int ret) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb =3D=3D NULL); + if (ufs_scsi_req_check_error(r, ret, false)) { + goto done; + } + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + + if (r->req.sg) { + dma_acct_start(lu->qdev.conf.blk, &r->acct, r->req.sg, BLOCK_ACCT_= READ); + r->req.residual -=3D r->req.sg->size; + r->req.aiocb =3D dma_blk_io( + blk_get_aio_context(lu->qdev.conf.blk), r->req.sg, + r->sector << BDRV_SECTOR_BITS, BDRV_SECTOR_SIZE, scsi_dma_read= v, r, + scsi_dma_complete, r, DMA_DIRECTION_FROM_DEVICE); + } else { + scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); + block_acct_start(blk_get_stats(lu->qdev.conf.blk), &r->acct, + r->qiov.size, BLOCK_ACCT_READ); + r->req.aiocb =3D scsi_dma_readv(r->sector << BDRV_SECTOR_BITS, &r-= >qiov, + scsi_read_complete, r, r); + } + +done: + scsi_req_unref(&r->req); +} + +static void scsi_do_read_cb(void *opaque, int ret) +{ + UfsSCSIReq *r =3D (UfsSCSIReq *)opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + + aio_context_acquire(blk_get_aio_context(lu->qdev.conf.blk)); + if (ret < 0) { + block_acct_failed(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } else { + block_acct_done(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } + scsi_do_read(opaque, ret); + aio_context_release(blk_get_aio_context(lu->qdev.conf.blk)); +} + +/* Read more data from scsi device into buffer. */ +static void scsi_read_data(SCSIRequest *req) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + bool first; + + trace_ufs_scsi_read_data_count(r->sector_count); + if (r->sector_count =3D=3D 0) { + /* This also clears the sense buffer for REQUEST SENSE. */ + scsi_req_complete(&r->req, GOOD); + return; + } + + /* No data transfer may already be in progress */ + assert(r->req.aiocb =3D=3D NULL); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + if (r->req.cmd.mode =3D=3D SCSI_XFER_TO_DEV) { + trace_ufs_scsi_read_data_invalid(); + scsi_read_complete_noio(r, -EINVAL); + return; + } + + if (!blk_is_available(req->dev->conf.blk)) { + scsi_read_complete_noio(r, -ENOMEDIUM); + return; + } + + first =3D !r->started; + r->started =3D true; + if (first && r->need_fua_emulation) { + block_acct_start(blk_get_stats(lu->qdev.conf.blk), &r->acct, 0, + BLOCK_ACCT_FLUSH); + r->req.aiocb =3D blk_aio_flush(lu->qdev.conf.blk, scsi_do_read_cb,= r); + } else { + scsi_do_read(r, 0); + } +} + +static void scsi_write_complete_noio(UfsSCSIReq *r, int ret) +{ + uint32_t n; + + assert(r->req.aiocb =3D=3D NULL); + if (ufs_scsi_req_check_error(r, ret, false)) { + goto done; + } + + n =3D r->qiov.size / BDRV_SECTOR_SIZE; + r->sector +=3D n; + r->sector_count -=3D n; + if (r->sector_count =3D=3D 0) { + scsi_write_do_fua(r); + return; + } else { + scsi_init_iovec(r, SCSI_DMA_BUF_SIZE); + trace_ufs_scsi_write_complete_noio(r->req.tag, r->qiov.size); + scsi_req_data(&r->req, r->qiov.size); + } + +done: + scsi_req_unref(&r->req); +} + +static void scsi_write_complete(void *opaque, int ret) +{ + UfsSCSIReq *r =3D (UfsSCSIReq *)opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + assert(r->req.aiocb !=3D NULL); + r->req.aiocb =3D NULL; + + aio_context_acquire(blk_get_aio_context(lu->qdev.conf.blk)); + if (ret < 0) { + block_acct_failed(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } else { + block_acct_done(blk_get_stats(lu->qdev.conf.blk), &r->acct); + } + scsi_write_complete_noio(r, ret); + aio_context_release(blk_get_aio_context(lu->qdev.conf.blk)); +} + +static BlockAIOCB *scsi_dma_writev(int64_t offset, QEMUIOVector *iov, + BlockCompletionFunc *cb, void *cb_opaqu= e, + void *opaque) +{ + UfsSCSIReq *r =3D opaque; + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + return blk_aio_pwritev(lu->qdev.conf.blk, offset, iov, 0, cb, cb_opaqu= e); +} + +static void scsi_write_data(SCSIRequest *req) +{ + UfsSCSIReq *r =3D DO_UPCAST(UfsSCSIReq, req, req); + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, r->req.dev); + + /* No data transfer may already be in progress */ + assert(r->req.aiocb =3D=3D NULL); + + /* The request is used as the AIO opaque value, so add a ref. */ + scsi_req_ref(&r->req); + if (r->req.cmd.mode !=3D SCSI_XFER_TO_DEV) { + trace_ufs_scsi_write_data_invalid(); + scsi_write_complete_noio(r, -EINVAL); + return; + } + + if (!r->req.sg && !r->qiov.size) { + /* Called for the first time. Ask the driver to send us more data= . */ + r->started =3D true; + scsi_write_complete_noio(r, 0); + return; + } + if (!blk_is_available(req->dev->conf.blk)) { + scsi_write_complete_noio(r, -ENOMEDIUM); + return; + } + + if (r->req.sg) { + dma_acct_start(lu->qdev.conf.blk, &r->acct, r->req.sg, + BLOCK_ACCT_WRITE); + r->req.residual -=3D r->req.sg->size; + r->req.aiocb =3D dma_blk_io( + blk_get_aio_context(lu->qdev.conf.blk), r->req.sg, + r->sector << BDRV_SECTOR_BITS, BDRV_SECTOR_SIZE, scsi_dma_writ= ev, r, + scsi_dma_complete, r, DMA_DIRECTION_TO_DEVICE); + } else { + block_acct_start(blk_get_stats(lu->qdev.conf.blk), &r->acct, + r->qiov.size, BLOCK_ACCT_WRITE); + r->req.aiocb =3D scsi_dma_writev(r->sector << BDRV_SECTOR_BITS, &r= ->qiov, + scsi_write_complete, r, r); + } +} + +static const SCSIReqOps ufs_scsi_emulate_reqops =3D { + .size =3D sizeof(UfsSCSIReq), + .free_req =3D ufs_scsi_free_request, + .send_command =3D ufs_scsi_emulate_command, + .read_data =3D ufs_scsi_emulate_read_data, + .write_data =3D ufs_scsi_emulate_write_data, + .get_buf =3D ufs_scsi_get_buf, +}; + +static const SCSIReqOps ufs_scsi_dma_reqops =3D { + .size =3D sizeof(UfsSCSIReq), + .free_req =3D ufs_scsi_free_request, + .send_command =3D ufs_scsi_dma_command, + .read_data =3D scsi_read_data, + .write_data =3D scsi_write_data, + .get_buf =3D ufs_scsi_get_buf, +}; + +/* + * Following commands are not yet supported + * PRE_FETCH(10), + * UNMAP, + * WRITE_BUFFER, READ_BUFFER, + * SECURITY_PROTOCOL_IN, SECURITY_PROTOCOL_OUT + */ +static const SCSIReqOps *const ufs_scsi_reqops_dispatch[256] =3D { + [TEST_UNIT_READY] =3D &ufs_scsi_emulate_reqops, + [INQUIRY] =3D &ufs_scsi_emulate_reqops, + [MODE_SENSE_10] =3D &ufs_scsi_emulate_reqops, + [START_STOP] =3D &ufs_scsi_emulate_reqops, + [READ_CAPACITY_10] =3D &ufs_scsi_emulate_reqops, + [REQUEST_SENSE] =3D &ufs_scsi_emulate_reqops, + [SYNCHRONIZE_CACHE] =3D &ufs_scsi_emulate_reqops, + [MODE_SELECT_10] =3D &ufs_scsi_emulate_reqops, + [VERIFY_10] =3D &ufs_scsi_emulate_reqops, + [FORMAT_UNIT] =3D &ufs_scsi_emulate_reqops, + [SERVICE_ACTION_IN_16] =3D &ufs_scsi_emulate_reqops, + [SEND_DIAGNOSTIC] =3D &ufs_scsi_emulate_reqops, + + [READ_6] =3D &ufs_scsi_dma_reqops, + [READ_10] =3D &ufs_scsi_dma_reqops, + [WRITE_6] =3D &ufs_scsi_dma_reqops, + [WRITE_10] =3D &ufs_scsi_dma_reqops, +}; + +static SCSIRequest *scsi_new_request(SCSIDevice *dev, uint32_t tag, + uint32_t lun, uint8_t *buf, + void *hba_private) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, dev); + SCSIRequest *req; + const SCSIReqOps *ops; + uint8_t command; + + command =3D buf[0]; + ops =3D ufs_scsi_reqops_dispatch[command]; + if (!ops) { + ops =3D &ufs_scsi_emulate_reqops; + } + req =3D scsi_req_alloc(ops, &lu->qdev, tag, lun, hba_private); + + return req; +} + +static Property ufs_lu_props[] =3D { + DEFINE_PROP_DRIVE("drive", UfsLu, qdev.conf.blk), + DEFINE_PROP_END_OF_LIST(), +}; + +static bool ufs_lu_brdv_init(UfsLu *lu, Error **errp) +{ + SCSIDevice *dev =3D &lu->qdev; + bool read_only; + + if (!lu->qdev.conf.blk) { + error_setg(errp, "drive property not set"); + return false; + } + + if (!blkconf_blocksizes(&lu->qdev.conf, errp)) { + return false; + } + + if (blk_get_aio_context(lu->qdev.conf.blk) !=3D qemu_get_aio_context()= && + !lu->qdev.hba_supports_iothread) { + error_setg(errp, "HBA does not support iothreads"); + return false; + } + + read_only =3D !blk_supports_write_perm(lu->qdev.conf.blk); + + if (!blkconf_apply_backend_options(&dev->conf, read_only, + dev->type =3D=3D TYPE_DISK, errp)) { + return false; + } + + if (blk_is_sg(lu->qdev.conf.blk)) { + error_setg(errp, "unwanted /dev/sg*"); + return false; + } + + blk_iostatus_enable(lu->qdev.conf.blk); + return true; +} + +static bool ufs_add_lu(UfsHc *u, UfsLu *lu, Error **errp) +{ + BlockBackend *blk =3D lu->qdev.conf.blk; + int64_t brdv_len =3D blk_getlength(blk); + uint64_t raw_dev_cap =3D + be64_to_cpu(u->geometry_desc.total_raw_device_capacity); + + if (u->device_desc.number_lu >=3D UFS_MAX_LUS) { + error_setg(errp, "ufs host controller has too many logical units."= ); + return false; + } + + if (u->lus[lu->lun] !=3D NULL) { + error_setg(errp, "ufs logical unit %d already exists.", lu->lun); + return false; + } + + u->lus[lu->lun] =3D lu; + u->device_desc.number_lu++; + raw_dev_cap +=3D (brdv_len >> UFS_GEOMETRY_CAPACITY_SHIFT); + u->geometry_desc.total_raw_device_capacity =3D cpu_to_be64(raw_dev_cap= ); + return true; +} + +static inline uint8_t ufs_log2(uint64_t input) +{ + int log =3D 0; + while (input >>=3D 1) { + log++; + } + return log; +} + +static void ufs_init_lu(UfsLu *lu) +{ + BlockBackend *blk =3D lu->qdev.conf.blk; + int64_t brdv_len =3D blk_getlength(blk); + + lu->lun =3D lu->qdev.lun; + memset(&lu->unit_desc, 0, sizeof(lu->unit_desc)); + lu->unit_desc.length =3D sizeof(UnitDescriptor); + lu->unit_desc.descriptor_idn =3D QUERY_DESC_IDN_UNIT; + lu->unit_desc.lu_enable =3D 0x01; + lu->unit_desc.logical_block_size =3D ufs_log2(lu->qdev.blocksize); + lu->unit_desc.unit_index =3D lu->qdev.lun; + lu->unit_desc.logical_block_count =3D + cpu_to_be64(brdv_len / (1 << lu->unit_desc.logical_block_size)); +} + +static bool ufs_lu_check_constraints(UfsLu *lu, Error **errp) +{ + if (!lu->qdev.conf.blk) { + error_setg(errp, "drive property not set"); + return false; + } + + if (lu->qdev.channel !=3D 0) { + error_setg(errp, "ufs logical unit does not support channel"); + return false; + } + + if (lu->qdev.lun >=3D UFS_MAX_LUS) { + error_setg(errp, "lun must be between 1 and %d", UFS_MAX_LUS - 1); + return false; + } + + return true; +} + +static void ufs_lu_realize(SCSIDevice *dev, Error **errp) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, dev); + BusState *s =3D qdev_get_parent_bus(&dev->qdev); + UfsHc *u =3D UFS(s->parent); + AioContext *ctx =3D NULL; + uint64_t nb_sectors, nb_blocks; + + if (!ufs_lu_check_constraints(lu, errp)) { + return; + } + + if (lu->qdev.conf.blk) { + ctx =3D blk_get_aio_context(lu->qdev.conf.blk); + aio_context_acquire(ctx); + if (!blkconf_blocksizes(&lu->qdev.conf, errp)) { + goto out; + } + } + lu->qdev.blocksize =3D UFS_BLOCK_SIZE; + blk_get_geometry(lu->qdev.conf.blk, &nb_sectors); + nb_blocks =3D nb_sectors / (lu->qdev.blocksize / BDRV_SECTOR_SIZE); + if (nb_blocks > UINT32_MAX) { + nb_blocks =3D UINT32_MAX; + } + lu->qdev.max_lba =3D nb_blocks; + lu->qdev.type =3D TYPE_DISK; + + ufs_init_lu(lu); + if (!ufs_add_lu(u, lu, errp)) { + goto out; + } + + ufs_lu_brdv_init(lu, errp); +out: + if (ctx) { + aio_context_release(ctx); + } +} + +static void ufs_lu_unrealize(SCSIDevice *dev) +{ + UfsLu *lu =3D DO_UPCAST(UfsLu, qdev, dev); + + blk_drain(lu->qdev.conf.blk); +} + +static void ufs_wlu_realize(DeviceState *qdev, Error **errp) +{ + UfsWLu *wlu =3D UFSWLU(qdev); + SCSIDevice *dev =3D &wlu->qdev; + + if (!is_wlun(dev->lun)) { + error_setg(errp, "not well-known logical unit number"); + return; + } + + QTAILQ_INIT(&dev->requests); +} + +static void ufs_lu_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + SCSIDeviceClass *sc =3D SCSI_DEVICE_CLASS(oc); + + sc->realize =3D ufs_lu_realize; + sc->unrealize =3D ufs_lu_unrealize; + sc->alloc_req =3D scsi_new_request; + dc->bus_type =3D TYPE_UFS_BUS; + device_class_set_props(dc, ufs_lu_props); + dc->desc =3D "Virtual UFS logical unit"; +} + +static void ufs_wlu_class_init(ObjectClass *oc, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(oc); + SCSIDeviceClass *sc =3D SCSI_DEVICE_CLASS(oc); + + /* + * The realize() function of TYPE_SCSI_DEVICE causes a segmentation fa= ult + * if a block drive does not exist. Define a new realize function for + * well-known LUs that do not have a block drive. + */ + dc->realize =3D ufs_wlu_realize; + sc->alloc_req =3D scsi_new_request; + dc->bus_type =3D TYPE_UFS_BUS; + dc->desc =3D "Virtual UFS well-known logical unit"; +} + +static const TypeInfo ufs_lu_info =3D { + .name =3D TYPE_UFS_LU, + .parent =3D TYPE_SCSI_DEVICE, + .class_init =3D ufs_lu_class_init, + .instance_size =3D sizeof(UfsLu), +}; + +static const TypeInfo ufs_wlu_info =3D { + .name =3D TYPE_UFS_WLU, + .parent =3D TYPE_SCSI_DEVICE, + .class_init =3D ufs_wlu_class_init, + .instance_size =3D sizeof(UfsWLu), +}; + +static void ufs_lu_register_types(void) +{ + type_register_static(&ufs_lu_info); + type_register_static(&ufs_wlu_info); +} + +type_init(ufs_lu_register_types) diff --git a/hw/ufs/ufs.c b/hw/ufs/ufs.c index c771f8e0b4..af57ba6df0 100644 --- a/hw/ufs/ufs.c +++ b/hw/ufs/ufs.c @@ -8,6 +8,19 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ =20 +/** + * Reference Specs: https://www.jedec.org/, 3.1 + * + * Usage + * ----- + * + * Add options: + * -drive file=3D,if=3Dnone,id=3D + * -device ufs,serial=3D,id=3D, \ + * nutrs=3D,nutmrs=3D + * -device ufs-lu,drive=3D,bus=3D + */ + #include "qemu/osdep.h" #include "qapi/error.h" #include "migration/vmstate.h" @@ -418,6 +431,19 @@ static const MemoryRegionOps ufs_mmio_ops =3D { }, }; =20 +static QEMUSGList *ufs_get_sg_list(SCSIRequest *scsi_req) +{ + UfsRequest *req =3D scsi_req->hba_private; + return req->sg; +} + +static void ufs_build_upiu_sense_data(UfsRequest *req, SCSIRequest *scsi_r= eq) +{ + req->rsp_upiu.sr.sense_data_len =3D cpu_to_be16(scsi_req->sense_len); + assert(scsi_req->sense_len <=3D SCSI_SENSE_LEN); + memcpy(req->rsp_upiu.sr.sense_data, scsi_req->sense, scsi_req->sense_l= en); +} + static void ufs_build_upiu_header(UfsRequest *req, uint8_t trans_type, uint8_t flags, uint8_t response, uint8_t scsi_status, @@ -431,6 +457,98 @@ static void ufs_build_upiu_header(UfsRequest *req, uin= t8_t trans_type, req->rsp_upiu.header.data_segment_length =3D cpu_to_be16(data_segment_= length); } =20 +static void ufs_scsi_command_complete(SCSIRequest *scsi_req, size_t resid) +{ + UfsRequest *req =3D scsi_req->hba_private; + int16_t status =3D scsi_req->status; + uint32_t expected_len =3D be32_to_cpu(req->req_upiu.sc.exp_data_transf= er_len); + uint32_t transfered_len =3D scsi_req->cmd.xfer - resid; + uint8_t flags =3D 0, response =3D COMMAND_RESULT_SUCESS; + uint16_t data_segment_length; + + if (expected_len > transfered_len) { + req->rsp_upiu.sr.residual_transfer_count =3D + cpu_to_be32(expected_len - transfered_len); + flags |=3D UFS_UPIU_FLAG_UNDERFLOW; + } else if (expected_len < transfered_len) { + req->rsp_upiu.sr.residual_transfer_count =3D + cpu_to_be32(transfered_len - expected_len); + flags |=3D UFS_UPIU_FLAG_OVERFLOW; + } + + if (status !=3D 0) { + ufs_build_upiu_sense_data(req, scsi_req); + response =3D COMMAND_RESULT_FAIL; + } + + data_segment_length =3D cpu_to_be16(scsi_req->sense_len + + sizeof(req->rsp_upiu.sr.sense_data_l= en)); + ufs_build_upiu_header(req, UPIU_TRANSACTION_RESPONSE, flags, response, + status, data_segment_length); + + ufs_complete_req(req, UFS_REQUEST_SUCCESS); + + scsi_req->hba_private =3D NULL; + scsi_req_unref(scsi_req); +} + +static const struct SCSIBusInfo ufs_scsi_info =3D { + .tcq =3D true, + .max_target =3D 0, + .max_lun =3D UFS_MAX_LUS, + .max_channel =3D 0, + + .get_sg_list =3D ufs_get_sg_list, + .complete =3D ufs_scsi_command_complete, +}; + +static UfsReqResult ufs_exec_scsi_cmd(UfsRequest *req) +{ + UfsHc *u =3D req->hc; + uint8_t lun =3D req->req_upiu.header.lun; + uint8_t task_tag =3D req->req_upiu.header.task_tag; + SCSIDevice *dev =3D NULL; + + trace_ufs_exec_scsi_cmd(req->slot, lun, req->req_upiu.sc.cdb[0]); + + if (!is_wlun(lun)) { + if (lun >=3D u->device_desc.number_lu) { + trace_ufs_err_scsi_cmd_invalid_lun(lun); + return UFS_REQUEST_FAIL; + } else if (u->lus[lun] =3D=3D NULL) { + trace_ufs_err_scsi_cmd_invalid_lun(lun); + return UFS_REQUEST_FAIL; + } + } + + switch (lun) { + case UFS_UPIU_REPORT_LUNS_WLUN: + dev =3D &u->report_wlu->qdev; + break; + case UFS_UPIU_UFS_DEVICE_WLUN: + dev =3D &u->dev_wlu->qdev; + break; + case UFS_UPIU_BOOT_WLUN: + dev =3D &u->boot_wlu->qdev; + break; + case UFS_UPIU_RPMB_WLUN: + dev =3D &u->rpmb_wlu->qdev; + break; + default: + dev =3D &u->lus[lun]->qdev; + } + + SCSIRequest *scsi_req =3D scsi_req_new( + dev, task_tag, lun, req->req_upiu.sc.cdb, UFS_CDB_SIZE, req); + + uint32_t len =3D scsi_req_enqueue(scsi_req); + if (len) { + scsi_req_continue(scsi_req); + } + + return UFS_REQUEST_NO_COMPLETE; +} + static UfsReqResult ufs_exec_nop_cmd(UfsRequest *req) { trace_ufs_exec_nop_cmd(req->slot); @@ -708,9 +826,11 @@ static const RpmbUnitDescriptor rpmb_unit_desc =3D { =20 static QueryRespCode ufs_read_unit_desc(UfsRequest *req) { + UfsHc *u =3D req->hc; uint8_t lun =3D req->req_upiu.qr.index; =20 - if (lun !=3D UFS_UPIU_RPMB_WLUN && lun > UFS_MAX_LUS) { + if (lun !=3D UFS_UPIU_RPMB_WLUN && + (lun > UFS_MAX_LUS || u->lus[lun] =3D=3D NULL)) { trace_ufs_err_query_invalid_index(req->req_upiu.qr.opcode, lun); return QUERY_RESULT_INVALID_INDEX; } @@ -718,8 +838,8 @@ static QueryRespCode ufs_read_unit_desc(UfsRequest *req) if (lun =3D=3D UFS_UPIU_RPMB_WLUN) { memcpy(&req->rsp_upiu.qr.data, &rpmb_unit_desc, rpmb_unit_desc.len= gth); } else { - /* unit descriptor is not yet supported */ - return QUERY_RESULT_INVALID_INDEX; + memcpy(&req->rsp_upiu.qr.data, &u->lus[lun]->unit_desc, + sizeof(u->lus[lun]->unit_desc)); } =20 return QUERY_RESULT_SUCCESS; @@ -969,8 +1089,7 @@ static void ufs_exec_req(UfsRequest *req) req_result =3D ufs_exec_nop_cmd(req); break; case UPIU_TRANSACTION_COMMAND: - /* Not yet implemented */ - req_result =3D UFS_REQUEST_FAIL; + req_result =3D ufs_exec_scsi_cmd(req); break; case UPIU_TRANSACTION_QUERY_REQ: req_result =3D ufs_exec_query_cmd(req); @@ -981,7 +1100,14 @@ static void ufs_exec_req(UfsRequest *req) req_result =3D UFS_REQUEST_FAIL; } =20 - ufs_complete_req(req, req_result); + /* + * The ufs_complete_req for scsi commands is handled by the + * ufs_scsi_command_complete() callback function. Therefore, to avoid + * duplicate processing, ufs_complete_req() is not called for scsi com= mands. + */ + if (req_result !=3D UFS_REQUEST_NO_COMPLETE) { + ufs_complete_req(req, req_result); + } } =20 static void ufs_process_req(void *opaque) @@ -1183,6 +1309,28 @@ static void ufs_init_hc(UfsHc *u) u->flags.permanently_disable_fw_update =3D 1; } =20 +static bool ufs_init_wlu(UfsHc *u, UfsWLu **wlu, uint8_t wlun, Error **err= p) +{ + UfsWLu *new_wlu =3D UFSWLU(qdev_new(TYPE_UFS_WLU)); + + qdev_prop_set_uint32(DEVICE(new_wlu), "lun", wlun); + + /* + * The well-known lu shares the same bus as the normal lu. If the well= -known + * lu writes the same channel value as the normal lu, the report will = be + * made not only for the normal lu but also for the well-known lu at + * REPORT_LUN time. To prevent this, the channel value of normal lu is= fixed + * to 0 and the channel value of well-known lu is fixed to 1. + */ + qdev_prop_set_uint32(DEVICE(new_wlu), "channel", 1); + if (!qdev_realize_and_unref(DEVICE(new_wlu), BUS(&u->bus), errp)) { + return false; + } + + *wlu =3D new_wlu; + return true; +} + static void ufs_realize(PCIDevice *pci_dev, Error **errp) { UfsHc *u =3D UFS(pci_dev); @@ -1191,15 +1339,55 @@ static void ufs_realize(PCIDevice *pci_dev, Error *= *errp) return; } =20 + qbus_init(&u->bus, sizeof(UfsBus), TYPE_UFS_BUS, &pci_dev->qdev, + u->parent_obj.qdev.id); + u->bus.parent_bus.info =3D &ufs_scsi_info; + ufs_init_state(u); ufs_init_hc(u); ufs_init_pci(u, pci_dev); + + if (!ufs_init_wlu(u, &u->report_wlu, UFS_UPIU_REPORT_LUNS_WLUN, errp))= { + return; + } + + if (!ufs_init_wlu(u, &u->dev_wlu, UFS_UPIU_UFS_DEVICE_WLUN, errp)) { + return; + } + + if (!ufs_init_wlu(u, &u->boot_wlu, UFS_UPIU_BOOT_WLUN, errp)) { + return; + } + + if (!ufs_init_wlu(u, &u->rpmb_wlu, UFS_UPIU_RPMB_WLUN, errp)) { + return; + } } =20 static void ufs_exit(PCIDevice *pci_dev) { UfsHc *u =3D UFS(pci_dev); =20 + if (u->dev_wlu) { + object_unref(OBJECT(u->dev_wlu)); + u->dev_wlu =3D NULL; + } + + if (u->report_wlu) { + object_unref(OBJECT(u->report_wlu)); + u->report_wlu =3D NULL; + } + + if (u->rpmb_wlu) { + object_unref(OBJECT(u->rpmb_wlu)); + u->rpmb_wlu =3D NULL; + } + + if (u->boot_wlu) { + object_unref(OBJECT(u->boot_wlu)); + u->boot_wlu =3D NULL; + } + qemu_bh_delete(u->doorbell_bh); qemu_bh_delete(u->complete_bh); =20 @@ -1238,6 +1426,49 @@ static void ufs_class_init(ObjectClass *oc, void *da= ta) dc->vmsd =3D &ufs_vmstate; } =20 +static bool ufs_bus_check_address(BusState *qbus, DeviceState *qdev, + Error **errp) +{ + SCSIDevice *dev =3D SCSI_DEVICE(qdev); + UfsBusClass *ubc =3D UFS_BUS_GET_CLASS(qbus); + UfsHc *u =3D UFS(qbus->parent); + + if (strcmp(object_get_typename(OBJECT(dev)), TYPE_UFS_WLU) =3D=3D 0) { + if (dev->lun !=3D UFS_UPIU_REPORT_LUNS_WLUN && + dev->lun !=3D UFS_UPIU_UFS_DEVICE_WLUN && + dev->lun !=3D UFS_UPIU_BOOT_WLUN && dev->lun !=3D UFS_UPIU_RPM= B_WLUN) { + error_setg(errp, "bad well-known lun: %d", dev->lun); + return false; + } + + if ((dev->lun =3D=3D UFS_UPIU_REPORT_LUNS_WLUN && u->report_wlu != =3D NULL) || + (dev->lun =3D=3D UFS_UPIU_UFS_DEVICE_WLUN && u->dev_wlu !=3D N= ULL) || + (dev->lun =3D=3D UFS_UPIU_BOOT_WLUN && u->boot_wlu !=3D NULL) = || + (dev->lun =3D=3D UFS_UPIU_RPMB_WLUN && u->rpmb_wlu !=3D NULL))= { + error_setg(errp, "well-known lun %d already exists", dev->lun); + return false; + } + + return true; + } + + if (strcmp(object_get_typename(OBJECT(dev)), TYPE_UFS_LU) !=3D 0) { + error_setg(errp, "%s cannot be connected to ufs-bus", + object_get_typename(OBJECT(dev))); + return false; + } + + return ubc->parent_check_address(qbus, qdev, errp); +} + +static void ufs_bus_class_init(ObjectClass *class, void *data) +{ + BusClass *bc =3D BUS_CLASS(class); + UfsBusClass *ubc =3D UFS_BUS_CLASS(class); + ubc->parent_check_address =3D bc->check_address; + bc->check_address =3D ufs_bus_check_address; +} + static const TypeInfo ufs_info =3D { .name =3D TYPE_UFS, .parent =3D TYPE_PCI_DEVICE, @@ -1246,9 +1477,18 @@ static const TypeInfo ufs_info =3D { .interfaces =3D (InterfaceInfo[]){ { INTERFACE_PCIE_DEVICE }, {} }, }; =20 +static const TypeInfo ufs_bus_info =3D { + .name =3D TYPE_UFS_BUS, + .parent =3D TYPE_SCSI_BUS, + .class_init =3D ufs_bus_class_init, + .class_size =3D sizeof(UfsBusClass), + .instance_size =3D sizeof(UfsBus), +}; + static void ufs_register_types(void) { type_register_static(&ufs_info); + type_register_static(&ufs_bus_info); } =20 type_init(ufs_register_types) diff --git a/hw/ufs/meson.build b/hw/ufs/meson.build index eb5164bde9..6e68328b93 100644 --- a/hw/ufs/meson.build +++ b/hw/ufs/meson.build @@ -1 +1 @@ -system_ss.add(when: 'CONFIG_UFS_PCI', if_true: files('ufs.c')) +system_ss.add(when: 'CONFIG_UFS_PCI', if_true: files('ufs.c', 'lu.c')) diff --git a/hw/ufs/trace-events b/hw/ufs/trace-events index 665e1a942b..1e55fb0d08 100644 --- a/hw/ufs/trace-events +++ b/hw/ufs/trace-events @@ -12,6 +12,31 @@ ufs_exec_scsi_cmd(uint32_t slot, uint8_t lun, uint8_t op= code) "slot %"PRIu32", l 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"" =20 +# lu.c +ufs_scsi_check_condition(uint32_t tag, uint8_t key, uint8_t asc, uint8_t a= scq) "Command complete tag=3D0x%x sense=3D%d/%d/%d" +ufs_scsi_read_complete(uint32_t tag, size_t size) "Data ready tag=3D0x%x l= en=3D%zd" +ufs_scsi_read_data_count(uint32_t sector_count) "Read sector_count=3D%d" +ufs_scsi_read_data_invalid(void) "Data transfer direction invalid" +ufs_scsi_write_complete_noio(uint32_t tag, size_t size) "Write complete ta= g=3D0x%x more=3D%zd" +ufs_scsi_write_data_invalid(void) "Data transfer direction invalid" +ufs_scsi_emulate_vpd_page_00(size_t xfer) "Inquiry EVPD[Supported pages] b= uffer size %zd" +ufs_scsi_emulate_vpd_page_80_not_supported(void) "Inquiry EVPD[Serial numb= er] not supported" +ufs_scsi_emulate_vpd_page_80(size_t xfer) "Inquiry EVPD[Serial number] buf= fer 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=3D%d" +ufs_scsi_emulate_write_data(int buflen) "Write buf_len=3D%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=3D%s)" +ufs_scsi_dma_command_READ(uint64_t lba, uint32_t len) "Read (block %" PRIu= 64 ", 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 re= q upiu. UTRLDBR slot %"PRIu32", request upiu addr %"PRIu64"" --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896364; cv=none; d=zohomail.com; s=zohoarc; b=kuZX11YBAxdB/CwL+1xRToY1oBoUEtM7CFHJuSM/3TkEFlMGv3UniM4hDUK2b5WkKDIiGlpkEqhLIUxKN9gXZ0jIUVTPkDZm5ulgvHLwnBKHuWYNJmTB9RaGKI2a/yvC3SDG1oM/osaQlV+8W/VzkJKlGoI9POyo9PD+vmFkOM8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896364; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iiVpmCXFRKc+jQE2ip6nn/ClN8JNbpHHjuTF06H6kAE=; b=NvS5U1no3UxypwNEtLKnlYwABzH91wz/xrFyHX8SMa0GTvKCuzk4/nbXFuxl/S+TNFUl6mNFdiPC6LBqlP7lZQpTqLyA4HSP5FQfgubPqaJxzEhYg5cp+m4PNHLQbDpZDdfpsAVsLUTb+dUrHcbMW0NUbe7fI1SNYOYaTmwt2S0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896364063886.6001701553499; Thu, 24 Aug 2023 09:59:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDff-0006EM-RG; Thu, 24 Aug 2023 12:58:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfd-0006Az-MC for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:57 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfZ-0003Qa-TY for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:57 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-612-vAQs7npbNl2DOX9GJNVDWQ-1; Thu, 24 Aug 2023 12:58:50 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 76C5A858EED; Thu, 24 Aug 2023 16:58:49 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7BB042166B26; Thu, 24 Aug 2023 16:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896333; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iiVpmCXFRKc+jQE2ip6nn/ClN8JNbpHHjuTF06H6kAE=; b=MarhA4TRY9/btfKyIYHpeTWkgOBsuUjrwUOd32dqBwdJEd2PCwUJQE+PbEdvWJDuMVIcPk SkKF8OjqGcEO28QfegTNmzE4goRejugx7kPa6rVAMQuZAqvaLFbBMqteGv6EFmqYkbahDJ PBYCmvWthwjibeqXTdsrXYbZ33F9JlI= X-MC-Unique: vAQs7npbNl2DOX9GJNVDWQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Jeuk Kim Subject: [PULL 4/8] tests/qtest: Introduce tests for UFS Date: Thu, 24 Aug 2023 12:58:29 -0400 Message-ID: <20230824165833.1689137-5-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896366717100003 Content-Type: text/plain; charset="utf-8" From: Jeuk Kim This patch includes the following tests Test mmio read Test ufs device initialization and ufs-lu recognition Test I/O (Performs a write followed by a read to verify) Signed-off-by: Jeuk Kim Acked-by: Thomas Huth Reviewed-by: Stefan Hajnoczi Message-id: af6b8d54c049490b3533a784a0aeac4798bb9217.1691062912.git.jeuk20.= kim@samsung.com Signed-off-by: Stefan Hajnoczi --- MAINTAINERS | 1 + tests/qtest/ufs-test.c | 584 ++++++++++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 1 + 3 files changed, 586 insertions(+) create mode 100644 tests/qtest/ufs-test.c diff --git a/MAINTAINERS b/MAINTAINERS index 7da40dabc2..a45bac20c9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2261,6 +2261,7 @@ M: Jeuk Kim S: Supported F: hw/ufs/* F: include/block/ufs.h +F: tests/qtest/ufs-test.c =20 megasas M: Hannes Reinecke diff --git a/tests/qtest/ufs-test.c b/tests/qtest/ufs-test.c new file mode 100644 index 0000000000..34c97bdf1b --- /dev/null +++ b/tests/qtest/ufs-test.c @@ -0,0 +1,584 @@ +/* + * 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 =3D g_get_monotonic_time() + TIMEOUT_SECONDS * G_TIME_SPAN_SE= COND; + do { + qtest_clock_step(ufs->dev.bus->qts, 100); + is =3D ufs_rreg(ufs, A_IS); + } while (is =3D=3D 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 =3D { 0 }; + uint64_t command_desc_base_addr =3D + cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE; + + req.header.dword_0 =3D + cpu_to_le32(1 << 28 | data_direction | UTP_REQ_DESC_INT_CMD); + req.header.dword_2 =3D cpu_to_le32(OCS_INVALID_COMMAND_STATUS); + + req.command_desc_base_addr_hi =3D cpu_to_le32(command_desc_base_addr >= > 32); + req.command_desc_base_addr_lo =3D + cpu_to_le32(command_desc_base_addr & 0xffffffff); + req.response_upiu_offset =3D + cpu_to_le16(UTP_RESPONSE_UPIU_OFFSET / sizeof(uint32_t)); + req.response_upiu_length =3D cpu_to_le16(sizeof(UtpUpiuRsp)); + req.prd_table_offset =3D cpu_to_le16(UTP_PRDT_UPIU_OFFSET / sizeof(uin= t32_t)); + req.prd_table_length =3D 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 =3D + ufs_build_req_utrd(ufs->cmd_desc_addr, slot, UTP_NO_DATA_TRANSFER,= 0); + uint64_t utrd_addr =3D ufs->utrlba + slot * sizeof(UtpTransferReqDesc); + uint64_t req_upiu_addr =3D + ufs->cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE; + uint64_t rsp_upiu_addr =3D 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 =3D { 0 }; + req_upiu.header.trans_type =3D UPIU_TRANSACTION_NOP_OUT; + req_upiu.header.task_tag =3D 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_o= ut)); +} + +static void ufs_send_query(QUfs *ufs, uint8_t slot, uint8_t query_function, + uint8_t query_opcode, uint8_t idn, uint8_t inde= x, + UtpTransferReqDesc *utrd_out, UtpUpiuRsp *rsp_o= ut) +{ + /* Build up utp transfer request descriptor */ + UtpTransferReqDesc utrd =3D + ufs_build_req_utrd(ufs->cmd_desc_addr, slot, UTP_NO_DATA_TRANSFER,= 0); + uint64_t utrd_addr =3D ufs->utrlba + slot * sizeof(UtpTransferReqDesc); + uint64_t req_upiu_addr =3D + ufs->cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE; + uint64_t rsp_upiu_addr =3D 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 =3D { 0 }; + req_upiu.header.trans_type =3D UPIU_TRANSACTION_QUERY_REQ; + req_upiu.header.query_func =3D query_function; + req_upiu.header.task_tag =3D slot; + /* + * QEMU UFS does not currently support Write descriptor and Write attr= ibute, + * so the value of data_segment_length is always 0. + */ + req_upiu.header.data_segment_length =3D 0; + req_upiu.qr.opcode =3D query_opcode; + req_upiu.qr.idn =3D idn; + req_upiu.qr.index =3D 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_o= ut)); +} + +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] =3D { + 0, + }; + uint8_t flags; + uint16_t prd_table_length, i; + uint32_t data_direction, data_len; + uint64_t req_upiu_addr =3D + ufs->cmd_desc_addr + slot * UTP_COMMAND_DESCRIPTOR_SIZE; + uint64_t prdt_addr =3D 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 =3D UTP_HOST_TO_DEVICE; + data_len =3D data_in_len; + flags =3D UPIU_CMD_FLAGS_WRITE; + } else if (data_out_len > 0) { + g_assert_nonnull(data_out); + data_direction =3D UTP_DEVICE_TO_HOST; + data_len =3D data_out_len; + flags =3D UPIU_CMD_FLAGS_READ; + } else { + data_direction =3D UTP_NO_DATA_TRANSFER; + data_len =3D 0; + flags =3D UPIU_CMD_FLAGS_NONE; + } + prd_table_length =3D 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 =3D 0; i < prd_table_length; i++) { + entries[i].addr =3D + cpu_to_le64(ufs->data_buffer_addr + i * sizeof(UfshcdSgEntry)); + if (i + 1 !=3D prd_table_length) { + entries[i].size =3D cpu_to_le32(PRD_ENTRY_DATA_SIZE - 1); + } else { + entries[i].size =3D 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 =3D ufs_build_req_utrd( + ufs->cmd_desc_addr, slot, data_direction, prd_table_length); + uint64_t utrd_addr =3D ufs->utrlba + slot * sizeof(UtpTransferReqDesc); + uint64_t rsp_upiu_addr =3D 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 =3D { 0 }; + req_upiu.header.trans_type =3D UPIU_TRANSACTION_COMMAND; + req_upiu.header.flags =3D flags; + req_upiu.header.lun =3D lun; + req_upiu.header.task_tag =3D slot; + req_upiu.sc.exp_data_transfer_len =3D 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_o= ut)); + 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 =3D 0, ie =3D 0; + UtpTransferReqDesc utrd; + UtpUpiuRsp rsp_upiu; + + ufs->bar =3D qpci_iomap(&ufs->dev, 0, NULL); + qpci_device_enable(&ufs->dev); + + /* Start host controller initialization */ + hce =3D FIELD_DP32(hce, HCE, HCE, 1); + ufs_wreg(ufs, A_HCE, hce); + + /* Wait for device to reset */ + end_time =3D g_get_monotonic_time() + TIMEOUT_SECONDS * G_TIME_SPAN_SE= COND; + do { + qtest_clock_step(ufs->dev.bus->qts, 100); + hce =3D FIELD_EX32(ufs_rreg(ufs, A_HCE), HCE, HCE); + } while (hce =3D=3D 0 && g_get_monotonic_time() < end_time); + g_assert_cmpuint(hce, =3D=3D, 1); + + /* Enable interrupt */ + ie =3D FIELD_DP32(ie, IE, UCCE, 1); + ie =3D FIELD_DP32(ie, IE, UHESE, 1); + ie =3D FIELD_DP32(ie, IE, UHXSE, 1); + ie =3D FIELD_DP32(ie, IE, UPMSE, 1); + ufs_wreg(ufs, A_IE, ie); + + /* Send DME_LINK_STARTUP uic command */ + hcs =3D 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, UIC_CMD_DME_LINK_STARTUP); + + is =3D 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 =3D ufs_rreg(ufs, A_UCMDARG2); + g_assert_cmpuint(ucmdarg2, =3D=3D, 0); + is =3D ufs_rreg(ufs, A_IS); + g_assert_cmpuint(is, =3D=3D, 0); + hcs =3D 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 =3D FIELD_DP32(ie, IE, UTRCE, 1); + ie =3D FIELD_DP32(ie, IE, UEE, 1); + ie =3D FIELD_DP32(ie, IE, UPMSE, 1); + ie =3D FIELD_DP32(ie, IE, UHXSE, 1); + ie =3D FIELD_DP32(ie, IE, UHESE, 1); + ie =3D FIELD_DP32(ie, IE, UTMRCE, 1); + ie =3D FIELD_DP32(ie, IE, UCCE, 1); + ie =3D FIELD_DP32(ie, IE, DFEE, 1); + ie =3D FIELD_DP32(ie, IE, HCFEE, 1); + ie =3D FIELD_DP32(ie, IE, SBFEE, 1); + ie =3D 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 =3D ufs_rreg(ufs, A_CAP); + nutrs =3D FIELD_EX32(cap, CAP, NUTRS) + 1; + nutmrs =3D FIELD_EX32(cap, CAP, NUTMRS) + 1; + ufs->cmd_desc_addr =3D + guest_alloc(alloc, nutrs * UTP_COMMAND_DESCRIPTOR_SIZE); + ufs->data_buffer_addr =3D + guest_alloc(alloc, MAX_PRD_ENTRY_COUNT * PRD_ENTRY_DATA_SIZE); + ufs->utrlba =3D guest_alloc(alloc, nutrs * sizeof(UtpTransferReqDesc)); + ufs->utmrlba =3D 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), =3D=3D, OCS_SUCCESS= ); + + /* Set fDeviceInit flag via query request */ + ufs_send_query(ufs, 0, UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST, + UPIU_QUERY_OPCODE_SET_FLAG, QUERY_FLAG_IDN_FDEVICEINIT,= 0, + &utrd, &rsp_upiu); + g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), =3D=3D, OCS_SUCCESS= ); + + /* Wait for device to reset */ + end_time =3D g_get_monotonic_time() + TIMEOUT_SECONDS * G_TIME_SPAN_SE= COND; + do { + qtest_clock_step(ufs->dev.bus->qts, 100); + ufs_send_query(ufs, 0, UPIU_QUERY_FUNC_STANDARD_READ_REQUEST, + UPIU_QUERY_OPCODE_READ_FLAG, QUERY_FLAG_IDN_FDEVICE= INIT, + 0, &utrd, &rsp_upiu); + } while (be32_to_cpu(rsp_upiu.qr.value) !=3D 0 && + g_get_monotonic_time() < end_time); + g_assert_cmpuint(be32_to_cpu(rsp_upiu.qr.value), =3D=3D, 0); + + ufs->enabled =3D 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 =3D 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 =3D g_new0(QUfs, 1); + QPCIBus *bus =3D pci_bus; + + qpci_device_init(&ufs->dev, bus, addr); + ufs->obj.get_driver =3D ufs_get_driver; + + return &ufs->obj; +} + +static void ufstest_reg_read(void *obj, void *data, QGuestAllocator *alloc) +{ + QUfs *ufs =3D obj; + uint32_t cap; + + ufs->bar =3D qpci_iomap(&ufs->dev, 0, NULL); + qpci_device_enable(&ufs->dev); + + cap =3D ufs_rreg(ufs, A_CAP); + g_assert_cmpuint(FIELD_EX32(cap, CAP, NUTRS), =3D=3D, 31); + g_assert_cmpuint(FIELD_EX32(cap, CAP, NUTMRS), =3D=3D, 7); + g_assert_cmpuint(FIELD_EX32(cap, CAP, 64AS), =3D=3D, 1); + + qpci_iounmap(&ufs->dev, ufs->bar); +} + +static void ufstest_init(void *obj, void *data, QGuestAllocator *alloc) +{ + QUfs *ufs =3D obj; + + uint8_t buf[4096] =3D { 0 }; + const uint8_t report_luns_cdb[UFS_CDB_SIZE] =3D { + /* 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] =3D { + 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), =3D=3D, OCS_SUCCESS= ); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + /* LUN LIST LENGTH should be 8, in big endian */ + g_assert_cmpuint(buf[3], =3D=3D, 8); + /* There is one logical unit whose lun is 0 */ + g_assert_cmpuint(buf[9], =3D=3D, 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), =3D=3D, OCS_SUCCESS= ); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_exit(ufs, alloc); +} + +static void ufstest_read_write(void *obj, void *data, QGuestAllocator *all= oc) +{ + QUfs *ufs =3D obj; + uint8_t read_buf[4096] =3D { 0 }; + uint8_t write_buf[4096] =3D { 0 }; + const uint8_t read_capacity_cdb[UFS_CDB_SIZE] =3D { + /* 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] =3D { + /* 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] =3D { + /* 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), =3D=3D, OCS_SUCCESS= ); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, COMMAND_RESULT_S= UCESS); + block_size =3D ldl_be_p(&read_buf[8]); + g_assert_cmpuint(block_size, =3D=3D, 4096); + + /* Write data */ + memset(write_buf, rand() % 255 + 1, block_size); + ufs_send_scsi_command(ufs, 0, 1, write_cdb, write_buf, block_size, NUL= L, 0, + &utrd, &rsp_upiu); + g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), =3D=3D, OCS_SUCCESS= ); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, COMMAND_RESULT_S= UCESS); + + /* Read data and verify */ + ufs_send_scsi_command(ufs, 0, 1, read_cdb, NULL, 0, read_buf, block_si= ze, + &utrd, &rsp_upiu); + g_assert_cmpuint(le32_to_cpu(utrd.header.dword_2), =3D=3D, OCS_SUCCESS= ); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, COMMAND_RESULT_S= UCESS); + g_assert_cmpint(memcmp(read_buf, write_buf, block_size), =3D=3D, 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 =3D g_file_open_tmp("qtest-ufs.XXXXXX", &t_path, NULL); + g_assert_cmpint(fd, >=3D, 0); + ret =3D ftruncate(fd, TEST_IMAGE_SIZE); + g_assert_cmpint(ret, =3D=3D, 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 =3D drive_create(); + + g_string_append_printf(cmd_line, + " -blockdev file,filename=3D%s,node-name=3Ddrv1= " + "-device ufs-lu,bus=3Dufs0,drive=3Ddrv1,lun=3D1= ", + tmp_path); + + return arg; +} + +static void ufs_register_nodes(void) +{ + const char *arch; + QOSGraphEdgeOptions edge_opts =3D { + .before_cmd_line =3D "-blockdev null-co,node-name=3Ddrv0,read-zero= es=3Don", + .after_cmd_line =3D "-device ufs-lu,bus=3Dufs0,drive=3Ddrv0,lun=3D= 0", + .extra_device_opts =3D "addr=3D04.0,id=3Dufs0,nutrs=3D32,nutmrs=3D= 8" + }; + + QOSGraphTestOptions io_test_opts =3D { + .before =3D ufs_blk_test_setup, + }; + + add_qpci_address(&edge_opts, &(QPCIAddress){ .devfn =3D 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 =3D 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); diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index b071d400b3..2b1d589a87 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -269,6 +269,7 @@ qos_test_ss.add( 'virtio-iommu-test.c', 'vmxnet3-test.c', 'igb-test.c', + 'ufs-test.c', ) =20 if config_all_devices.has_key('CONFIG_VIRTIO_SERIAL') --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896483; cv=none; d=zohomail.com; s=zohoarc; b=lDzg8sIG+x1xu+suTI8m7XqvwFz97cSAS1qczJ3ksA0L12sogB3yD6IP0mzqPVldnF6vCelP89pZUHlmLFR/EHPvz455ywls8RvdxbU6m5QKVViDxuCUaChTNF3yfqoFMkghvmfijw5JhePUSuEPliiaxXoC3wZGEOTaDkTvmHg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896483; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2mrbL2L5n4dlKXoxPhQ4bdIS4/YMrsyeedolVZYfUKA=; b=nduICxiMR8l+dyGcDkPVGY+W0DOmDn8CCKphEFbB6z4+EjuYKosh2wxsgo98rkOWSu9+WRfZhnEwKvlygM+H+E0DK/IPr2sqtMD9W11UHTmho7OtzaQyNGBacfDokPYo5o/b3pqHed7mbMCMuHa3IbGnUY/jdLoEsG5aHDr2oRE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896483351623.1020984249053; Thu, 24 Aug 2023 10:01:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfg-0006Ed-Ez; Thu, 24 Aug 2023 12:59:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDff-0006DY-7E for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfc-0003Rc-U9 for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:58:58 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-94-QIbCbPBhNry-bs2ukGotlQ-1; Thu, 24 Aug 2023 12:58:53 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 52AEE3C14AA1; Thu, 24 Aug 2023 16:58:51 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id C45BEC1602B; Thu, 24 Aug 2023 16:58:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896336; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2mrbL2L5n4dlKXoxPhQ4bdIS4/YMrsyeedolVZYfUKA=; b=dO6BQYjl129L4vFLL5YGqJ81RZD1Ztq5lw3iwgTgCktNq9eohFY9HhqPetrZZ+JcjCKySw yijjfCfggyhhd4xQa/VLGy4f8/BeXF+JOAs9nYpjaD8RPkOP51FR6r+cfQuY1B6Z2aM3tg ceivskMKHrczisT+DYKDagAlIuLuycU= X-MC-Unique: QIbCbPBhNry-bs2ukGotlQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Fabiano Rosas Subject: [PULL 5/8] block-migration: Ensure we don't crash during migration cleanup Date: Thu, 24 Aug 2023 12:58:30 -0400 Message-ID: <20230824165833.1689137-6-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896489372100001 Content-Type: text/plain; charset="utf-8" From: Fabiano Rosas We can fail the blk_insert_bs() at init_blk_migration(), leaving the BlkMigDevState without a dirty_bitmap and BlockDriverState. Account for the possibly missing elements when doing cleanup. Fix the following crashes: Thread 1 "qemu-system-x86" received signal SIGSEGV, Segmentation fault. 0x0000555555ec83ef in bdrv_release_dirty_bitmap (bitmap=3D0x0) at ../block/= dirty-bitmap.c:359 359 BlockDriverState *bs =3D bitmap->bs; #0 0x0000555555ec83ef in bdrv_release_dirty_bitmap (bitmap=3D0x0) at ../b= lock/dirty-bitmap.c:359 #1 0x0000555555bba331 in unset_dirty_tracking () at ../migration/block.c:= 371 #2 0x0000555555bbad98 in block_migration_cleanup_bmds () at ../migration/= block.c:681 Thread 1 "qemu-system-x86" received signal SIGSEGV, Segmentation fault. 0x0000555555e971ff in bdrv_op_unblock (bs=3D0x0, op=3DBLOCK_OP_TYPE_BACKUP_= SOURCE, reason=3D0x0) at ../block.c:7073 7073 QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { #0 0x0000555555e971ff in bdrv_op_unblock (bs=3D0x0, op=3DBLOCK_OP_TYPE_BA= CKUP_SOURCE, reason=3D0x0) at ../block.c:7073 #1 0x0000555555e9734a in bdrv_op_unblock_all (bs=3D0x0, reason=3D0x0) at = ../block.c:7095 #2 0x0000555555bbae13 in block_migration_cleanup_bmds () at ../migration/= block.c:690 Signed-off-by: Fabiano Rosas Message-id: 20230731203338.27581-1-farosas@suse.de Signed-off-by: Stefan Hajnoczi --- migration/block.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/migration/block.c b/migration/block.c index b9580a6c7e..86c2256a2b 100644 --- a/migration/block.c +++ b/migration/block.c @@ -368,7 +368,9 @@ static void unset_dirty_tracking(void) BlkMigDevState *bmds; =20 QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { - bdrv_release_dirty_bitmap(bmds->dirty_bitmap); + if (bmds->dirty_bitmap) { + bdrv_release_dirty_bitmap(bmds->dirty_bitmap); + } } } =20 @@ -676,13 +678,18 @@ static int64_t get_remaining_dirty(void) static void block_migration_cleanup_bmds(void) { BlkMigDevState *bmds; + BlockDriverState *bs; AioContext *ctx; =20 unset_dirty_tracking(); =20 while ((bmds =3D QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) !=3D NULL= ) { QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry); - bdrv_op_unblock_all(blk_bs(bmds->blk), bmds->blocker); + + bs =3D blk_bs(bmds->blk); + if (bs) { + bdrv_op_unblock_all(bs, bmds->blocker); + } error_free(bmds->blocker); =20 /* Save ctx, because bmds->blk can disappear during blk_unref. */ --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896464; cv=none; d=zohomail.com; s=zohoarc; b=JGgcp7MZl54NobgB4+Sn0WZqwieZUVFVQz7erDbgBuqehGTXk0WmEt813BpcaxSnN7zz+UaSiAcSfKGlFvpLwvJUpg6jQXH8nF+MJoV8d/5okJVjVSMPTNwG5/qL/fqmdEhaakezynI08sNsaWwCG602LxpctrCS8userlOO+4Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896464; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=EJD2LIajjQtoGeGPnYi+7JdF7CSvrKKy9cQB+BYrwtY=; b=ElTXgC8lG9//W16y063dbMxdwSNIS1zQrJEj92+9UBSfkTYmC5p6RfuS5JUZSM+d9isXA6Yl00ll2oJH5I1QEbGE9tgyefBLmIhxE+LRf7Fv1XFpWgj5wacwdIVhNz1uIW7Tk4xgJydO52Lg3TPAowbhLOkgEZL1yfuLIVOOKN0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896464473202.12729238739598; Thu, 24 Aug 2023 10:01:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfj-0006H4-6x; Thu, 24 Aug 2023 12:59:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfh-0006F0-1C for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:59:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfe-0003S2-G0 for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:59:00 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-76-z2cB3R2PPDmWc3PD1CgTFg-1; Thu, 24 Aug 2023 12:58:53 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 39AE0101A528; Thu, 24 Aug 2023 16:58:53 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9CA11140E962; Thu, 24 Aug 2023 16:58:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896337; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EJD2LIajjQtoGeGPnYi+7JdF7CSvrKKy9cQB+BYrwtY=; b=D5HVb0WUE9L7nwUVJrws87gG1zP/BY6ZKOeoy/KF8xRMDDbDJErAmv3GPiqw79uzngK9EO FHOWuqEiLIUeQV2WdPSlAHl1OMgWmE39QtqR9L11VP2BwJaCqsjwlAFKyYkJ3FF4646kPL 7ubI+HeG3/co4hhrYukxXnDCBdfjdDQ= X-MC-Unique: z2cB3R2PPDmWc3PD1CgTFg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Eric Blake , "Denis V . Lunev" , Andrey Drobyshev Subject: [PULL 6/8] block: add subcluster_size field to BlockDriverInfo Date: Thu, 24 Aug 2023 12:58:31 -0400 Message-ID: <20230824165833.1689137-7-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896469675100001 Content-Type: text/plain; charset="utf-8" From: Andrey Drobyshev via This is going to be used in the subsequent commit as requests alignment (in particular, during copy-on-read). This value only makes sense for the formats which support subclusters (currently QCOW2 only). If this field isn't set by driver's own bdrv_get_info() implementation, we simply set it equal to the cluster size thus treating each cluster as having a single subcluster. Reviewed-by: Eric Blake Reviewed-by: Denis V. Lunev Signed-off-by: Andrey Drobyshev Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Stefan Hajnoczi Message-ID: <20230711172553.234055-2-andrey.drobyshev@virtuozzo.com> --- include/block/block-common.h | 5 +++++ block.c | 7 +++++++ block/qcow2.c | 1 + 3 files changed, 13 insertions(+) diff --git a/include/block/block-common.h b/include/block/block-common.h index e15395f2cb..df5ffc8d09 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -132,6 +132,11 @@ typedef struct BlockZoneWps { typedef struct BlockDriverInfo { /* in bytes, 0 if irrelevant */ int cluster_size; + /* + * A fraction of cluster_size, if supported (currently QCOW2 only); if + * disabled or unsupported, set equal to cluster_size. + */ + int subcluster_size; /* offset at which the VM state can be saved (0 if not possible) */ int64_t vm_state_offset; bool is_dirty; diff --git a/block.c b/block.c index a307c151a8..0af890f647 100644 --- a/block.c +++ b/block.c @@ -6480,6 +6480,13 @@ int coroutine_fn bdrv_co_get_info(BlockDriverState *= bs, BlockDriverInfo *bdi) } memset(bdi, 0, sizeof(*bdi)); ret =3D drv->bdrv_co_get_info(bs, bdi); + if (bdi->subcluster_size =3D=3D 0) { + /* + * If the driver left this unset, subclusters are not supported. + * Then it is safe to treat each cluster as having only one subclu= ster. + */ + bdi->subcluster_size =3D bdi->cluster_size; + } if (ret < 0) { return ret; } diff --git a/block/qcow2.c b/block/qcow2.c index c51388e99d..b48cd9ce63 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -5197,6 +5197,7 @@ qcow2_co_get_info(BlockDriverState *bs, BlockDriverIn= fo *bdi) { BDRVQcow2State *s =3D bs->opaque; bdi->cluster_size =3D s->cluster_size; + bdi->subcluster_size =3D s->subcluster_size; bdi->vm_state_offset =3D qcow2_vm_state_offset(s); bdi->is_dirty =3D s->incompatible_features & QCOW2_INCOMPAT_DIRTY; return 0; --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896383; cv=none; d=zohomail.com; s=zohoarc; b=SoEDYyabrV2yC44GHILZqg0dKu79mUWU1VLhpFtsGKzKn5138f2+kEjeWc+M2dkZvPGTCO/vKiQ2FVHO8xNGe2dw2dQ4BxvBi/P7SjY/Dywlcrj7/7gCchK/3ZuPqlc5vGEYNx07BwhjSyZ/qtmQ92xfgFgJiUlKjm6kC1cDidI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896383; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=uK+WA16ReGOvmkg0hGc5DHjqdsVD7frQI0UjHGrI+sM=; b=GanXF7Kt9o0XNoNS/DZxF13dwDaRLYchrdSXSObgDihR45tovvk7zQIG6Fgm3y+C2kmk8kX3kdrdDmRNRYOvkx/guZgVtISGvj44th2/Myg0XTUOJNb88af2UASEfwLvSaM1wgpix50T6rg0YAM7GaiXkoMSHdAW+oKGyWXs4mw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896383714387.51674053722627; Thu, 24 Aug 2023 09:59:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfj-0006HC-Fx; Thu, 24 Aug 2023 12:59:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfh-0006Fh-VS for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:59:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfe-0003Ry-8v for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:59:01 -0400 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-532-9PAJ_60DOPuAgVa9-6VMHA-1; Thu, 24 Aug 2023 12:58:55 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5AF8B3C14AA5; Thu, 24 Aug 2023 16:58:55 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id BE2232026D68; Thu, 24 Aug 2023 16:58:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896337; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uK+WA16ReGOvmkg0hGc5DHjqdsVD7frQI0UjHGrI+sM=; b=D95RfLlKetZyR9RStHurw5MZ1ECTPbEqgN1SWaqe9tjYDIcBxFPD3wpxE4Stjt2ncNtv/y 5Z69DyoirWFF0qSbWITc2r52bprII04A7xvXPlXlhKvaSeAwn/Ip4Eu2Oc4lzIiQoQ4uQG HXSzMUR0h0qJMfothHo4NUYLeHnm5HI= X-MC-Unique: 9PAJ_60DOPuAgVa9-6VMHA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Eric Blake , "Denis V . Lunev" , Andrey Drobyshev Subject: [PULL 7/8] block/io: align requests to subcluster_size Date: Thu, 24 Aug 2023 12:58:32 -0400 Message-ID: <20230824165833.1689137-8-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896386197100003 Content-Type: text/plain; charset="utf-8" From: Andrey Drobyshev via When target image is using subclusters, and we align the request during copy-on-read, it makes sense to align to subcluster_size rather than cluster_size. Otherwise we end up with unnecessary allocations. This commit renames bdrv_round_to_clusters() to bdrv_round_to_subclusters() and utilizes subcluster_size field of BlockDriverInfo to make necessary alignments. It affects copy-on-read as well as mirror job (which is using bdrv_round_to_clusters()). This change also fixes the following bug with failing assert (covered by the test in the subsequent commit): qemu-img create -f qcow2 base.qcow2 64K qemu-img create -f qcow2 -o extended_l2=3Don,backing_file=3Dbase.qcow2,back= ing_fmt=3Dqcow2 img.qcow2 64K qemu-io -c "write -P 0xaa 0 2K" img.qcow2 qemu-io -C -c "read -P 0x00 2K 62K" img.qcow2 qemu-io: ../block/io.c:1236: bdrv_co_do_copy_on_readv: Assertion `skip_byte= s < pnum' failed. Reviewed-by: Eric Blake Reviewed-by: Denis V. Lunev Signed-off-by: Andrey Drobyshev Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Stefan Hajnoczi Message-ID: <20230711172553.234055-3-andrey.drobyshev@virtuozzo.com> --- include/block/block-io.h | 8 +++---- block/io.c | 50 ++++++++++++++++++++-------------------- block/mirror.c | 8 +++---- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/include/block/block-io.h b/include/block/block-io.h index 4415506e40..6db48f2d35 100644 --- a/include/block/block-io.h +++ b/include/block/block-io.h @@ -189,10 +189,10 @@ bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *= bdi); ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs, Error **errp); BlockStatsSpecific *bdrv_get_specific_stats(BlockDriverState *bs); -void bdrv_round_to_clusters(BlockDriverState *bs, - int64_t offset, int64_t bytes, - int64_t *cluster_offset, - int64_t *cluster_bytes); +void bdrv_round_to_subclusters(BlockDriverState *bs, + int64_t offset, int64_t bytes, + int64_t *cluster_offset, + int64_t *cluster_bytes); =20 void bdrv_get_backing_filename(BlockDriverState *bs, char *filename, int filename_size); diff --git a/block/io.c b/block/io.c index 055fcf7438..76e7df18d8 100644 --- a/block/io.c +++ b/block/io.c @@ -728,21 +728,21 @@ BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_req= uest(BlockDriverState *bs) } =20 /** - * Round a region to cluster boundaries + * Round a region to subcluster (if supported) or cluster boundaries */ void coroutine_fn GRAPH_RDLOCK -bdrv_round_to_clusters(BlockDriverState *bs, int64_t offset, int64_t bytes, - int64_t *cluster_offset, int64_t *cluster_bytes) +bdrv_round_to_subclusters(BlockDriverState *bs, int64_t offset, int64_t by= tes, + int64_t *align_offset, int64_t *align_bytes) { BlockDriverInfo bdi; IO_CODE(); - if (bdrv_co_get_info(bs, &bdi) < 0 || bdi.cluster_size =3D=3D 0) { - *cluster_offset =3D offset; - *cluster_bytes =3D bytes; + if (bdrv_co_get_info(bs, &bdi) < 0 || bdi.subcluster_size =3D=3D 0) { + *align_offset =3D offset; + *align_bytes =3D bytes; } else { - int64_t c =3D bdi.cluster_size; - *cluster_offset =3D QEMU_ALIGN_DOWN(offset, c); - *cluster_bytes =3D QEMU_ALIGN_UP(offset - *cluster_offset + bytes,= c); + int64_t c =3D bdi.subcluster_size; + *align_offset =3D QEMU_ALIGN_DOWN(offset, c); + *align_bytes =3D QEMU_ALIGN_UP(offset - *align_offset + bytes, c); } } =20 @@ -1168,8 +1168,8 @@ bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t of= fset, int64_t bytes, void *bounce_buffer =3D NULL; =20 BlockDriver *drv =3D bs->drv; - int64_t cluster_offset; - int64_t cluster_bytes; + int64_t align_offset; + int64_t align_bytes; int64_t skip_bytes; int ret; int max_transfer =3D MIN_NON_ZERO(bs->bl.max_transfer, @@ -1203,28 +1203,28 @@ bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t = offset, int64_t bytes, * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which * is one reason we loop rather than doing it all at once. */ - bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_by= tes); - skip_bytes =3D offset - cluster_offset; + bdrv_round_to_subclusters(bs, offset, bytes, &align_offset, &align_byt= es); + skip_bytes =3D offset - align_offset; =20 trace_bdrv_co_do_copy_on_readv(bs, offset, bytes, - cluster_offset, cluster_bytes); + align_offset, align_bytes); =20 - while (cluster_bytes) { + while (align_bytes) { int64_t pnum; =20 if (skip_write) { ret =3D 1; /* "already allocated", so nothing will be copied */ - pnum =3D MIN(cluster_bytes, max_transfer); + pnum =3D MIN(align_bytes, max_transfer); } else { - ret =3D bdrv_is_allocated(bs, cluster_offset, - MIN(cluster_bytes, max_transfer), &pnu= m); + ret =3D bdrv_is_allocated(bs, align_offset, + MIN(align_bytes, max_transfer), &pnum); if (ret < 0) { /* * Safe to treat errors in querying allocation as if * unallocated; we'll probably fail again soon on the * read, but at least that will set a decent errno. */ - pnum =3D MIN(cluster_bytes, max_transfer); + pnum =3D MIN(align_bytes, max_transfer); } =20 /* Stop at EOF if the image ends in the middle of the cluster = */ @@ -1242,7 +1242,7 @@ bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t of= fset, int64_t bytes, /* Must copy-on-read; use the bounce buffer */ pnum =3D MIN(pnum, MAX_BOUNCE_BUFFER); if (!bounce_buffer) { - int64_t max_we_need =3D MAX(pnum, cluster_bytes - pnum); + int64_t max_we_need =3D MAX(pnum, align_bytes - pnum); int64_t max_allowed =3D MIN(max_transfer, MAX_BOUNCE_BUFFE= R); int64_t bounce_buffer_len =3D MIN(max_we_need, max_allowed= ); =20 @@ -1254,7 +1254,7 @@ bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t of= fset, int64_t bytes, } qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum); =20 - ret =3D bdrv_driver_preadv(bs, cluster_offset, pnum, + ret =3D bdrv_driver_preadv(bs, align_offset, pnum, &local_qiov, 0, 0); if (ret < 0) { goto err; @@ -1266,13 +1266,13 @@ bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t = offset, int64_t bytes, /* FIXME: Should we (perhaps conditionally) be setting * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy * that still correctly reads as zero? */ - ret =3D bdrv_co_do_pwrite_zeroes(bs, cluster_offset, pnum, + ret =3D bdrv_co_do_pwrite_zeroes(bs, align_offset, pnum, BDRV_REQ_WRITE_UNCHANGED); } else { /* This does not change the data on the disk, it is not * necessary to flush even in cache=3Dwritethrough mode. */ - ret =3D bdrv_driver_pwritev(bs, cluster_offset, pnum, + ret =3D bdrv_driver_pwritev(bs, align_offset, pnum, &local_qiov, 0, BDRV_REQ_WRITE_UNCHANGED); } @@ -1301,8 +1301,8 @@ bdrv_co_do_copy_on_readv(BdrvChild *child, int64_t of= fset, int64_t bytes, } } =20 - cluster_offset +=3D pnum; - cluster_bytes -=3D pnum; + align_offset +=3D pnum; + align_bytes -=3D pnum; progress +=3D pnum - skip_bytes; skip_bytes =3D 0; } diff --git a/block/mirror.c b/block/mirror.c index d3cacd1708..e213a892db 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -283,8 +283,8 @@ static int coroutine_fn mirror_cow_align(MirrorBlockJob= *s, int64_t *offset, need_cow |=3D !test_bit((*offset + *bytes - 1) / s->granularity, s->cow_bitmap); if (need_cow) { - bdrv_round_to_clusters(blk_bs(s->target), *offset, *bytes, - &align_offset, &align_bytes); + bdrv_round_to_subclusters(blk_bs(s->target), *offset, *bytes, + &align_offset, &align_bytes); } =20 if (align_bytes > max_bytes) { @@ -576,8 +576,8 @@ static void coroutine_fn mirror_iteration(MirrorBlockJo= b *s) int64_t target_offset; int64_t target_bytes; WITH_GRAPH_RDLOCK_GUARD() { - bdrv_round_to_clusters(blk_bs(s->target), offset, io_bytes, - &target_offset, &target_bytes); + bdrv_round_to_subclusters(blk_bs(s->target), offset, io_by= tes, + &target_offset, &target_bytes); } if (target_offset =3D=3D offset && target_bytes =3D=3D io_bytes) { --=20 2.41.0 From nobody Thu Nov 28 08:35:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1692896474; cv=none; d=zohomail.com; s=zohoarc; b=miP2yjLfrQr7Lmia+himnXrPkeoBiHzJEvNe3Rkn/kXDbz+6aPP3LJCs5TBCMnTZDd5saR3GUczFjC2a6s5Wvat41gIai32gDFcW1HyZk5AKSL7H4V8cuIkMQqG5wOVhm++lHQo9DN2MiHQGUXGbLIg4AmElb+JhVqdReFiVQ5s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692896474; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+hcJ8z8+yEaQ9eqGuIoLyqdUrwj59cqGQM8mVF2k39U=; b=HlWnnz6sC+/W3y83pYTPpbN+vJZd6OEbgvmcNxGi1+dr4cPbzwcfwTlI/6mtQENuN1kGMyq/QfabujRP1BnwNdTP0k4e9emJmDrina6MoQHsfwB/EEJrYokNKAJYUqnCrv2gGOET5KojT8U9abCVMSu8JGNAbFDc7xNqXvegnqI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692896474704952.9502698187155; Thu, 24 Aug 2023 10:01:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZDfn-0006RL-5w; Thu, 24 Aug 2023 12:59:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfl-0006IT-9p for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:59:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZDfj-0003UZ-1g for qemu-devel@nongnu.org; Thu, 24 Aug 2023 12:59:05 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-557--83RO6jLM3uCu0oRXzXFYw-1; Thu, 24 Aug 2023 12:58:58 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 504998D40A2; Thu, 24 Aug 2023 16:58:57 +0000 (UTC) Received: from localhost (unknown [10.39.194.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id B04596B2B6; Thu, 24 Aug 2023 16:58:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692896342; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+hcJ8z8+yEaQ9eqGuIoLyqdUrwj59cqGQM8mVF2k39U=; b=Lm92vFhLpJOwKcu71IO8rkHy4ZGYJ8lfIBD+zt8EE7QQicnM7aUdtkE6jlqbek6t6F3BA9 uA5Wn2fO9lMpp3idCp/+8YiV7KNN5ihHtO1H+6+gE9AB2UsLh7axqIwQtVm/djy1+Uz+FP e2M9d+uxABjuCy8+6mrr+2cAuuP7NfE= X-MC-Unique: -83RO6jLM3uCu0oRXzXFYw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Peter Xu , Thomas Huth , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Kevin Wolf , Juan Quintela , Fam Zheng , Paolo Bonzini , Laurent Vivier , Stefan Hajnoczi , Marcel Apfelbaum , John Snow , Jeuk Kim , Hanna Reitz , Leonardo Bras , qemu-block@nongnu.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , "Michael S. Tsirkin" , Eric Blake , "Denis V . Lunev" , Andrey Drobyshev Subject: [PULL 8/8] tests/qemu-iotests/197: add testcase for CoR with subclusters Date: Thu, 24 Aug 2023 12:58:33 -0400 Message-ID: <20230824165833.1689137-9-stefanha@redhat.com> In-Reply-To: <20230824165833.1689137-1-stefanha@redhat.com> References: <20230824165833.1689137-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692896477123100001 Content-Type: text/plain; charset="utf-8" From: Andrey Drobyshev via Add testcase which checks that allocations during copy-on-read are performed on the subcluster basis when subclusters are enabled in target image. This testcase also triggers the following assert with previous commit not being applied, so we check that as well: qemu-io: ../block/io.c:1236: bdrv_co_do_copy_on_readv: Assertion `skip_byte= s < pnum' failed. Reviewed-by: Eric Blake Reviewed-by: Denis V. Lunev Signed-off-by: Andrey Drobyshev Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Stefan Hajnoczi Message-ID: <20230711172553.234055-4-andrey.drobyshev@virtuozzo.com> --- tests/qemu-iotests/197 | 29 +++++++++++++++++++++++++++++ tests/qemu-iotests/197.out | 24 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/tests/qemu-iotests/197 b/tests/qemu-iotests/197 index a2547bc280..f07a9da136 100755 --- a/tests/qemu-iotests/197 +++ b/tests/qemu-iotests/197 @@ -122,6 +122,35 @@ $QEMU_IO -f qcow2 -C -c 'read 0 1024' "$TEST_WRAP" | _= filter_qemu_io $QEMU_IO -f qcow2 -c map "$TEST_WRAP" _check_test_img =20 +echo +echo '=3D=3D=3D Copy-on-read with subclusters =3D=3D=3D' +echo + +# Create base and top images 64K (1 cluster) each. Make subclusters enabl= ed +# for the top image +_make_test_img 64K +IMGPROTO=3Dfile IMGFMT=3Dqcow2 TEST_IMG_FILE=3D"$TEST_WRAP" \ + _make_test_img --no-opts -o extended_l2=3Dtrue -F "$IMGFMT" -b "$TEST_= IMG" \ + 64K | _filter_img_create + +$QEMU_IO -c "write -P 0xaa 0 64k" "$TEST_IMG" | _filter_qemu_io + +# Allocate individual subclusters in the top image, and not the whole clus= ter +$QEMU_IO -c "write -P 0xbb 28K 2K" -c "write -P 0xcc 34K 2K" "$TEST_WRAP" \ + | _filter_qemu_io + +# Only 2 subclusters should be allocated in the top image at this point +$QEMU_IMG map "$TEST_WRAP" | _filter_qemu_img_map + +# Actual copy-on-read operation +$QEMU_IO -C -c "read -P 0xaa 30K 4K" "$TEST_WRAP" | _filter_qemu_io + +# And here we should have 4 subclusters allocated right in the middle of t= he +# top image. Make sure the whole cluster remains unallocated +$QEMU_IMG map "$TEST_WRAP" | _filter_qemu_img_map + +_check_test_img + # success, all done echo '*** done' status=3D0 diff --git a/tests/qemu-iotests/197.out b/tests/qemu-iotests/197.out index ad414c3b0e..8f34a30afe 100644 --- a/tests/qemu-iotests/197.out +++ b/tests/qemu-iotests/197.out @@ -31,4 +31,28 @@ read 1024/1024 bytes at offset 0 1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) 1 KiB (0x400) bytes allocated at offset 0 bytes (0x0) No errors were found on the image. + +=3D=3D=3D Copy-on-read with subclusters =3D=3D=3D + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D65536 +Formatting 'TEST_DIR/t.wrap.IMGFMT', fmt=3DIMGFMT size=3D65536 backing_fil= e=3DTEST_DIR/t.IMGFMT backing_fmt=3DIMGFMT +wrote 65536/65536 bytes at offset 0 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 2048/2048 bytes at offset 28672 +2 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +wrote 2048/2048 bytes at offset 34816 +2 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Offset Length File +0 0x7000 TEST_DIR/t.IMGFMT +0x7000 0x800 TEST_DIR/t.wrap.IMGFMT +0x7800 0x1000 TEST_DIR/t.IMGFMT +0x8800 0x800 TEST_DIR/t.wrap.IMGFMT +0x9000 0x7000 TEST_DIR/t.IMGFMT +read 4096/4096 bytes at offset 30720 +4 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Offset Length File +0 0x7000 TEST_DIR/t.IMGFMT +0x7000 0x2000 TEST_DIR/t.wrap.IMGFMT +0x9000 0x7000 TEST_DIR/t.IMGFMT +No errors were found on the image. *** done --=20 2.41.0