From nobody Sat Feb 7 06:35:27 2026 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4FCE2DECC5; Sat, 24 Jan 2026 12:03:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769256218; cv=pass; b=o2uQ/f6AvrGWbatIuXJybKEJOCg6sNiWAwjJGhILVStyKMTiYZ5pkVdVR6akToriWTkJaq0etPnntZYyP2jC025Md9tUFdXV9HDDgymzYL6b/he2aSgh1dhDbTyzA1+QiZbtfdLHoTR+If1PX15OX/KdSdep0GM3BjkNPPIRkZM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769256218; c=relaxed/simple; bh=bKULkiSdnrA8nwen42fkkXB8yJm6JRElMRZ+goyqIQw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bRvI3isP5xr2TCjGrvPXC7hJElEphdbEhMx91ebUIfScy9SBUxwBFOShPi4cJtFaTaINURMZLrpKHZxAwwEjpq6rHXRHg6aPVWijhgXBs42K4qMxx3GSkNoJER6kxxP3oBCQg2VZ9OAKFtPLg0vcyvaTHt0KaINKEH+wqK9lUjA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b=NHMrDp75; arc=pass smtp.client-ip=136.143.188.112 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 (1024-bit key) header.d=collabora.com header.i=nicolas.frattaroli@collabora.com header.b="NHMrDp75" ARC-Seal: i=1; a=rsa-sha256; t=1769256189; cv=none; d=zohomail.com; s=zohoarc; b=YbikC+ALrBSQocWHTuLrH23sKxTB+si5p4/aJtDHa77DggWIr9rsweZO3g9l8KG3U7OCZ7Cugrp2tqCKcRW6XS/QHA5hL58GQPVFdvnZGVx3SporTEkrO1HkDS0+esxIybHD88noWj6xgIYR9xpDfhQQsxuWH5Gu0HrqatM5iRQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1769256189; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=gjSLvwTgrrP3mUBEhIBlXKbE0FaKi8bZDGULITLn1ns=; b=l/FsYEE+HjHR7XD4QA1Hqmg1Jxk6KdS8Y5U4ISPP0J8n2D4kgfAfK5c4jZkZaGs/6BqTaavnzx20DS7SoZk4CyZrD5s4zWFP807Qz3u96VQeqrCyKwx6qmjYZEGp6vO5uyhfXeX0eUYhQx1TwmSw+IHEmuZQ6GGmuNa3nj6KdVo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=nicolas.frattaroli@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1769256189; s=zohomail; d=collabora.com; i=nicolas.frattaroli@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:References:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=gjSLvwTgrrP3mUBEhIBlXKbE0FaKi8bZDGULITLn1ns=; b=NHMrDp75Hd1Q6Crs52h7Cm902/BigAaK05tO1883aFNM/l6Gt8iXO5zaNYkZy8Bf wzvsyBO51fEIQVqQKkdVA0csvgxdrl4wyU36LjVNkPo3+/2PSg5f5fQbL/xJ+zCxKA8 YKSTQeY8wGkGJQ6ixwYX8YMiKaM3qhtFREn6suKc= Received: by mx.zohomail.com with SMTPS id 1769256187122724.0072043219542; Sat, 24 Jan 2026 04:03:07 -0800 (PST) From: Nicolas Frattaroli Date: Sat, 24 Jan 2026 13:01:04 +0100 Subject: [PATCH v6 18/24] scsi: ufs: mediatek: Rework ufs_mtk_wait_idle_state Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260124-mt8196-ufs-v6-18-e7c005b60028@collabora.com> References: <20260124-mt8196-ufs-v6-0-e7c005b60028@collabora.com> In-Reply-To: <20260124-mt8196-ufs-v6-0-e7c005b60028@collabora.com> To: Alim Akhtar , Avri Altman , Bart Van Assche , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matthias Brugger , AngeloGioacchino Del Regno , Chunfeng Yun , Vinod Koul , Kishon Vijay Abraham I , Peter Wang , Stanley Jhu , "James E.J. Bottomley" , "Martin K. Petersen" , Philipp Zabel , Liam Girdwood , Mark Brown , Chaotian Jing , Neil Armstrong Cc: Louis-Alexis Eyraud , kernel@collabora.com, linux-scsi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-phy@lists.infradead.org, Nicolas Frattaroli X-Mailer: b4 0.14.3 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: AngeloGioacchino Del Regno Signed-off-by: Nicolas Frattaroli --- 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-mediate= k.c index 2b1f26b55782..b5c75d228c85 100644 --- a/drivers/ufs/host/ufs-mediatek.c +++ b/drivers/ufs/host/ufs-mediatek.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -380,51 +381,39 @@ static void ufs_mtk_dbg_sel(struct ufs_hba *hba) } } =20 -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 =3D ufshcd_get_variant(hba); - - /* cannot use plain ktime_get() in suspend */ - timeout =3D ktime_get_mono_fast_ns() + retry_ms * 1000000UL; - - /* wait a specific time after check base */ - udelay(10); - wait_idle =3D false; + u32 val; =20 - do { - time_checked =3D ktime_get_mono_fast_ns(); - if (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899) { - ufs_mtk_dbg_sel(hba); - val =3D ufshcd_readl(hba, REG_UFS_PROBE); - } else { - val =3D ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL); - val =3D val >> 16; - } + if (old_style) { + ufs_mtk_dbg_sel(hba); + val =3D ufshcd_readl(hba, REG_UFS_PROBE); + } else { + val =3D ufshcd_readl(hba, REG_UFS_UFS_MMIO_OTSD_CTRL) >> 16; + } =20 - sm =3D val & 0x1f; + return FIELD_GET(0x1f, val); +} =20 - /* - * if state is in H8 enter and H8 enter confirm - * wait until return to idle state. - */ - if ((sm >=3D VS_HIB_ENTER) && (sm <=3D VS_HIB_EXIT)) { - wait_idle =3D 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 =3D ufshcd_get_variant(hba); + bool old_style =3D (host->legacy_ip_ver || host->ip_ver < IP_VER_MT6899); + u32 val; + int ret; =20 - if (wait_idle && (sm =3D=3D VS_HCE_BASE)) - break; - } while (time_checked < timeout); + /* If the device is already in the base state after 10us, don't wait. */ + udelay(10); + if (ufs_mtk_read_state(hba, old_style) =3D=3D VS_HCE_BASE) + return 0; =20 - if (wait_idle && sm !=3D VS_HCE_BASE) { - dev_info(hba->dev, "wait idle tmo: 0x%x\n", val); - return -ETIMEDOUT; + /* Poll to wait for idle */ + ret =3D read_poll_timeout(ufs_mtk_read_state, val, + (val < VS_HIB_ENTER || val > VS_HIB_EXIT), 50, + 5 * USEC_PER_MSEC, false, hba, old_style); + if (ret) { + dev_err(hba->dev, "Timed out waiting for idle state, val =3D 0x%x\n", va= l); + return ret; } =20 return 0; @@ -1389,7 +1378,7 @@ static int ufs_mtk_auto_hibern8_disable(struct ufs_hb= a *hba) ufshcd_writel(hba, 0, REG_AUTO_HIBERNATE_IDLE_TIMER); =20 /* wait host return to idle state when auto-hibern8 off */ - ret =3D ufs_mtk_wait_idle_state(hba, 5); + ret =3D ufs_mtk_wait_idle_state(hba); if (ret) goto out; =20 @@ -1593,7 +1582,7 @@ static int ufs_mtk_link_set_hpm(struct ufs_hba *hba) return err; =20 /* Check link state to make sure exit h8 success */ - err =3D ufs_mtk_wait_idle_state(hba, 5); + err =3D ufs_mtk_wait_idle_state(hba); if (err) { dev_err(hba->dev, "Failed to wait for idle: %pe\n", ERR_PTR(err)); return err; --=20 2.52.0