From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.251.105.195 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795100800; b=Pkg7NZ9Qdt0zEKO9BNqCawUvpWHDxxFWeCFznpkGS8HWUYL5mk4+Pqq/Oq/MNT8JsPkE 3BdJOUbM/r6SVAwfc53CenfRDE6OOUNbBrw29YFwRilBrEvvJJkfGvF1DWFDlAuyC/7wN OPzOoWNAMtIqZKFoogbj628u75xKqB1Wj85nao1m9mwzUEdDVCixNZTTNGqGV5O8IybX0 vqD0diJ0i4LAOxMfahLmghUsB/ZGd0WlWpb0ZQP0lHbtPh8oNV/xfR4nrqzlmXMsj1eSp 4uYsj+PMKuTLqSA8Ihmv0RAV900goo6NILRUbGcmodHFiKeMQX9oxbOtmaPcqzJlsnA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795100800; h=DMARC-Filter:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key; bh=lJvjE0QKKlysiS5Vt3rC/Uk9Y914ARmvYAmFvU36Zlk=; b=lp1rLU2SuPO5WBUpQSggDsAdex2L9jKbiFARg1SblXrZi3EtMarq9V9Wv/HgtggAswoq 9xRJfjZGfnvqN1SGtvv9TQrHx1NfPkCjmOOr+hIkIqtCsXQtWYvIQw6YCxYu0yw7W8DxT j2NrgBfdkovIjY6ySPdIH61ypTmMC0bjhUfKLLRsicCqmPfvVOdA9Kza8+dVOtmg6ErC3 3i3httmdQMC+25oP6UBWNQU5NY+kKQLH1J0TMz2W5u1KItJoGzvtAAHVcZrbulYwYLLNo qQvD/6R/PPpO9GgreE88r4AcdkFD9mmmh+xObGKnTfCpUNdB0COlRayTm15ZErlsMcw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=QatQXqoS; arc=none smtp.remote-ip=148.251.105.195 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 774AB606A339; Thu, 19 Nov 2026 15:06:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 774AB606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=QatQXqoS DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1795100798; bh=gQOgtDWFwYXFCju+OzcHEQ02ByTMfKQ/IpSCmXKrqjY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=QatQXqoSh36Bn4fqQyXf8QIjuPmAzN1p+Vjvu8ntO2XpDNyAY7UTkU0BzZNcTXOGn WGVnv71uKwaTDk0qQedhNVknLlCyutI//jELAuFoxpqS+JEelCjW+SOJe0AZs1W6zR Fv0Q48PLq60kh6pcu5six0Y+SwvZzoHzeJgNzpO9V5pNzTJ8/qQCPXMq1a3Ot97rpr Dp7Bxqb61eTpsFUKiIZ2DHBKAO8+xfbe3M2i3HCAXpEGdwuGZcyKLafB8a/ID9mGe7 wl46nVd/ejr4GDU+JEJ8jwKTCq8dpqy0w9k60cIqgMmi7lFRxDKH1io3EwhzInAE/h JK9t32XgN0paw== Received: from yukiji.home (unknown [100.64.0.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: laeyraud) by bali.collaboradmins.com (Postfix) with ESMTPSA id 079A517E047C; Thu, 19 Nov 2026 16:06:36 +0100 (CET) From: Louis-Alexis Eyraud Date: Thu, 19 Nov 2026 16:06:06 +0100 Subject: [PATCH v15 18/23] scsi: ufs: mediatek: Rework ufs_mtk_wait_idle_state X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261119-mt8196-ufs-v15-18-aae1eb0382e8@collabora.com> References: <20261119-mt8196-ufs-v15-0-aae1eb0382e8@collabora.com> In-Reply-To: <20261119-mt8196-ufs-v15-0-aae1eb0382e8@collabora.com> To: Chunfeng Yun , Vinod Koul , Neil Armstrong , Manivannan Sadhasivam , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matthias Brugger , AngeloGioacchino Del Regno , Peter Wang , Chaotian Jing , Alim Akhtar , Bart Van Assche , Bean Huo , Stanley Jhu , Stanley Jhu , "James E.J. Bottomley" , "Martin K. Petersen" , Philipp Zabel , Liam Girdwood , Mark Brown Cc: kernel@collabora.com, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-phy@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, Nicolas Frattaroli , Louis-Alexis Eyraud X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795100772; l=4343; i=louisalexis.eyraud@collabora.com; s=20250113; h=from:subject:message-id; bh=kQI0auZm6JgyXreFJ/0qKZ5CeomBYQnwhQE3Nlo0hEw=; b=C/raM31UkeDe6UGpIXtsWR6aAdezZGY2AlTi1Sb6vBgrvfyKVie9revQtP/8cuS6fyvMduajK Buwbv8tUQ8HBKpieP7OMBxR3Z1bwr/XiAg93JjbmQTEuq1R6+JljoVw X-Developer-Key: i=louisalexis.eyraud@collabora.com; a=ed25519; pk=CHFBDB2Kqh4EHc6JIqFn69GhxJJAzc0Zr4e8QxtumuM= From: Nicolas Frattaroli While ufs_mtk_wait_idle state has some code smells for me (the VS_HCE_BASE early exit seems racey at best), it can still benefit from some general cleanup to make the code flow less convoluted. Use the iopoll helpers, for one, and specifically the one that sleeps and does not busy delay, as it's being done for up to 5ms. The register read is split out to a helper function that branches between new and old style flow. Every called uses the same 5ms timeout value, so there is no point in making this a parameter. Just assume a 5ms timeout in the function. Reviewed-by: Peter Wang Reviewed-by: AngeloGioacchino Del Regno Signed-off-by: Nicolas Frattaroli Signed-off-by: Louis-Alexis Eyraud --- drivers/ufs/host/ufs-mediatek.c | 71 +++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c index cbb0e716ce28..4cc0c5af681f 100644 --- a/drivers/ufs/host/ufs-mediatek.c +++ b/drivers/ufs/host/ufs-mediatek.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -384,51 +385,39 @@ static void ufs_mtk_dbg_sel(struct ufs_hba *hba) } } -static int ufs_mtk_wait_idle_state(struct ufs_hba *hba, - unsigned long retry_ms) +static u32 ufs_mtk_read_state(struct ufs_hba *hba, bool old_style) { - u64 timeout, time_checked; - u32 val, sm; - bool wait_idle; - struct ufs_mtk_host *host = ufshcd_get_variant(hba); - - /* cannot use plain ktime_get() in suspend */ - timeout = ktime_get_mono_fast_ns() + retry_ms * 1000000UL; - - /* wait a specific time after check base */ - udelay(10); - wait_idle = false; + u32 val; - do { - time_checked = ktime_get_mono_fast_ns(); - if (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899) { - ufs_mtk_dbg_sel(hba); - val = ufshcd_readl(hba, REG_UFS_PROBE); - } else { - val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL); - val = val >> 16; - } + if (old_style) { + ufs_mtk_dbg_sel(hba); + val = ufshcd_readl(hba, REG_UFS_PROBE); + } else { + val = ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL) >> 16; + } - sm = val & 0x1f; + return FIELD_GET(0x1f, val); +} - /* - * if state is in H8 enter and H8 enter confirm - * wait until return to idle state. - */ - if ((sm >= VS_HIB_ENTER) && (sm <= VS_HIB_EXIT)) { - wait_idle = true; - udelay(50); - continue; - } else if (!wait_idle) - break; +static int ufs_mtk_wait_idle_state(struct ufs_hba *hba) +{ + struct ufs_mtk_host *host = ufshcd_get_variant(hba); + bool old_style = (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899); + u32 val; + int ret; - if (wait_idle && (sm == VS_HCE_BASE)) - break; - } while (time_checked < timeout); + /* If the device isn't in a hibernate state after 10us, don't wait. */ + udelay(10); + val = ufs_mtk_read_state(hba, old_style); + if (val < VS_HIB_ENTER || val > VS_HIB_EXIT) + return 0; - if (wait_idle && sm != VS_HCE_BASE) { - dev_info(hba->dev, "wait idle tmo: 0x%x\n", val); - return -ETIMEDOUT; + /* Poll to wait for idle */ + ret = read_poll_timeout(ufs_mtk_read_state, val, (val == VS_HCE_BASE), + 50, 5 * USEC_PER_MSEC, false, hba, old_style); + if (ret) { + dev_err(hba->dev, "Timed out waiting for idle state, val = 0x%x\n", val); + return ret; } return 0; @@ -1459,7 +1448,7 @@ static int ufs_mtk_auto_hibern8_disable(struct ufs_hba *hba) ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER); /* wait host return to idle state when auto-hibern8 off */ - ret = ufs_mtk_wait_idle_state(hba, 5); + ret = ufs_mtk_wait_idle_state(hba); if (ret) goto out; @@ -1669,7 +1658,7 @@ static int ufs_mtk_link_set_hpm(struct ufs_hba *hba) return err; /* Check link state to make sure exit h8 success */ - err = ufs_mtk_wait_idle_state(hba, 5); + err = ufs_mtk_wait_idle_state(hba); if (err) { dev_err(hba->dev, "Failed to wait for idle: %pe\n", ERR_PTR(err)); return err; -- 2.56.1