public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Oltean <vladimir.oltean@nxp.com>
To: linux-phy@lists.infradead.org
Cc: Vinod Koul <vkoul@kernel.org>,
	Neil Armstrong <neil.armstrong@linaro.org>,
	dri-devel@lists.freedesktop.org, freedreno@lists.freedesktop.org,
	linux-arm-kernel@lists.infradead.org,
	linux-arm-msm@vger.kernel.org, linux-can@vger.kernel.org,
	linux-gpio@vger.kernel.org, linux-ide@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
	linux-pci@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	linux-riscv@lists.infradead.org,
	linux-rockchip@lists.infradead.org,
	linux-samsung-soc@vger.kernel.org, linux-scsi@vger.kernel.org,
	linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org,
	linux-usb@vger.kernel.org, netdev@vger.kernel.org,
	spacemit@lists.linux.dev, UNGLinuxDriver@microchip.com,
	Bart Van Assche <bvanassche@acm.org>,
	Alim Akhtar <alim.akhtar@samsung.com>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Peter Griffin <peter.griffin@linaro.org>,
	"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Chanho Park <chanho61.park@samsung.com>
Subject: Re: [PATCH v7 phy-next 09/27] scsi: ufs: exynos: stop poking into struct phy guts
Date: Mon, 4 May 2026 14:48:28 +0300	[thread overview]
Message-ID: <20260504114828.n7r7kujaegusdkbw@skbuf> (raw)
In-Reply-To: <20260430110652.558622-10-vladimir.oltean@nxp.com>

[-- Attachment #1: Type: text/plain, Size: 1891 bytes --]

Sashiko says:
> commit 76ded6c0290bdb192c2ae1af9a2e6a47fc39c9a1
> Author: Vladimir Oltean <vladimir.oltean@nxp.com>
> 
> scsi: ufs: exynos: stop poking into struct phy guts
> This commit modifies the Exynos UFS driver to stop directly accessing the
> generic PHY's power_count. Instead, it tracks the PHY power state internally
> using a new phy_powered_on flag to properly handle link retries and
> suspend/resume operations.
> 
> > diff --git a/drivers/ufs/host/ufs-exynos.c b/drivers/ufs/host/ufs-exynos.c
> > --- a/drivers/ufs/host/ufs-exynos.c
> > +++ b/drivers/ufs/host/ufs-exynos.c
> [ ... ]
> > @@ -1513,6 +1516,9 @@ static void exynos_ufs_exit(struct ufs_hba *hba)
> >  {
> >  	struct exynos_ufs *ufs = ufshcd_get_variant(hba);
> >
> > +	if (!ufs->phy_powered_on)
> > +		return;
> > +
> >  	phy_power_off(ufs->phy);
> >  	phy_exit(ufs->phy);
> >  }
> 
> Does this introduce a regression where initialization resources are leaked
> if the driver is unbound while suspended?
> Looking at exynos_ufs_suspend(), the PHY is powered off and phy_powered_on
> is set to false, but phy_exit() is intentionally not called so the PHY
> remains initialized:
> exynos_ufs_suspend()
>         if (!ufshcd_is_link_active(hba) && ufs->phy_powered_on) {
>                 phy_power_off(ufs->phy);
>                 ufs->phy_powered_on = false;
>         }
> If an unbind or fatal error occurs while in this suspended state,
> exynos_ufs_exit() will see phy_powered_on as false and return immediately.
> The initialization reference and any resources allocated by phy_init()
> would never be released since phy_exit() is completely skipped.
> [ ... ]

I think I've been looking at this issue all wrong, not considering why
the UFS HCD driver calls phy_exit() -> phy_init() at runtime and what
can be done about that.

I have replaced this patch with the one attached, which I will be sending
for v8.

[-- Attachment #2: 0001-scsi-ufs-exynos-use-dedicated-API-for-updating-PHY-b.patch --]
[-- Type: text/x-diff, Size: 9438 bytes --]

From c687a8568c6c7837bb0bd539bf14343d7d0c63a1 Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <vladimir.oltean@nxp.com>
Date: Mon, 4 May 2026 14:00:51 +0300
Subject: [PATCH] scsi: ufs: exynos: use dedicated API for updating PHY bus
 width

I am trying to get rid of code instances where PHY consumers (like the
Exynos UFS HCD) poke inside struct phy fields, in order to further turn
struct phy into an opaque data structure.

The ufs-exynos.c driver interacts with phy-samsung-ufs.c in order to
power it on and to update the lane count. For the later purpose, it
(ab)uses phy_set_bus_width().

The phy_set_bus_width() function is a PHY provider function, not a
consumer one, and I am calling its use from ufs-exynos.c an abuse
because
(1) commit 8feed347d33b ("phy: add phy_get_bus_width()/phy_set_bus_width()
    calls") clearly states so.
(2) phy_set_bus_width() only alters phy->attrs.bus_width, and does not
    call into phy_ops at all. So a consumer that makes a call to
    phy_set_bus_width() will not produce any hardware change in the
    provider at all.

This is where the Exynos UFS HCD driver decided to be creative and
hijacked phy_init() to pick up the bus_width attribute.

This requires a very careful dance where the PHY consumer needs to
simultaneously juggle two requirements:
- the UFS PHY needs to pick up the updated lane count in its
  samsung_ufs_phy_init() handler for the phy_init() call
- phy_init() calls need to be balanced with phy_exit(), otherwise
  subsequent phy_init() calls don't make it into samsung_ufs_phy_init()
  and just leave the PHY with an elevated init_count
- phy_power_on() can't be called without phy_init()

This is why the following bug fix commits exist:
3d73b200f989 ("scsi: ufs: ufs-exynos: Change ufs phy control sequence")
7f05fd9a3b6f ("scsi: ufs: exynos: Ensure consistent phy reference counts")

Currently the UFS HCD driver tries to keep the PHY init_count and
power_count in tight lockstep, but even this is error-prone. For
example, if exynos_ufs_suspend() runs and then exynos_ufs_exit(),
the PHY power_count will underflow.

If we address the root issue first (phy_init() abused to pick up new
lane count) by introducing a new PHY consumer method which actually does
call into the PHY provider driver, then we are able to absorb the entire
UFS HCD dance and update the lane count without altering the PHY
init_count or power_count.

Then we are much more free to call phy_init() from wherever we want, and
same goes for phy_power_on().

It is typical to call phy_init() right after phy_get(), and doing so
will naturally balance it with phy_exit().

We can also leave the phy_power_on() call to be on demand, placed inside
exynos_ufs_pre_link(). Because this call can be made multiple times and
is not balanced with anything else, we need a consumer-specific "bool
phy_powered_on" which ensures that we call phy_power_on() at most once,
and that exynos_ufs_exit() only calls phy_power_off() if phy_power_on()
was previously called. Using the phy->power_count for this purpose is
undesirable because
(a) it is going away
(b) the PHY API supports multiple consumers for the same provider, so it
    cannot offer an equivalent helper because it doesn't want consumers
    to interfere with each other

Inside the new samsung_ufs_phy_request_bus_width(), I've sanity checked
that the bus width is either 1 or 2 lanes. This coincides with
samsung_ufs_phy_config() which only configures LANE_0 and LANE_1.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
---
Cc: Alim Akhtar <alim.akhtar@samsung.com>
Cc: Bart Van Assche <bvanassche@acm.org>
Cc: Peter Griffin <peter.griffin@linaro.org>
Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Krzysztof Kozlowski <krzk@kernel.org>
Cc: Chanho Park <chanho61.park@samsung.com>

v7->v8:
- rewrote commit after Sashiko pointed out the new handling is still
  not correct:
  https://sashiko.dev/#/patchset/20260430110652.558622-1-vladimir.oltean@nxp.com
- removed Reviewed-by, Tested-by and Acked-by tags from Alim, Bart and
  Peter
v6->v7: collect tags from Martin and Peter
v5->v6: collect tags from Alim Akhtar
v4->v5: collect tag, add "scsi: " prefix to commit title
v3->v4: none
v2->v3:
- add Cc Chanho Park, author of commit 3d73b200f989 ("scsi: ufs:
  ufs-exynos: Change ufs phy control sequence")
v1->v2:
- add better ufs->phy_powered_on handling in exynos_ufs_exit(),
  exynos_ufs_suspend() and exynos_ufs_resume() which ensures we won't
  enter a phy->power_count underrun condition
---
 drivers/phy/phy-core.c                | 18 +++++++++++
 drivers/phy/samsung/phy-samsung-ufs.c | 30 ++++++++++++------
 drivers/ufs/host/ufs-exynos.c         | 45 ++++++++++++++++++++++-----
 drivers/ufs/host/ufs-exynos.h         |  1 +
 4 files changed, 77 insertions(+), 17 deletions(-)

diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index 21aaf2f76e53..6305efe210d6 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -606,6 +606,24 @@ int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
 }
 EXPORT_SYMBOL_GPL(phy_validate);
 
+int phy_request_bus_width(struct phy *phy, int bus_width)
+{
+	int ret;
+
+	if (!phy)
+		return -EINVAL;
+
+	if (!phy->ops->request_bus_width)
+		return -EOPNOTSUPP;
+
+	mutex_lock(&phy->mutex);
+	ret = phy->ops->request_bus_width(phy, bus_width);
+	mutex_unlock(&phy->mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(phy_request_bus_width);
+
 /**
  * _of_phy_get() - lookup and obtain a reference to a phy by phandle
  * @np: device_node for which to get the phy
diff --git a/drivers/phy/samsung/phy-samsung-ufs.c b/drivers/phy/samsung/phy-samsung-ufs.c
index 00e570d699f3..5d7b842bff1a 100644
--- a/drivers/phy/samsung/phy-samsung-ufs.c
+++ b/drivers/phy/samsung/phy-samsung-ufs.c
@@ -161,16 +161,6 @@ static int samsung_ufs_phy_clks_init(struct samsung_ufs_phy *phy)
 	return devm_clk_bulk_get(phy->dev, num_clks, phy->clks);
 }
 
-static int samsung_ufs_phy_request_bus_width(struct phy *phy, int bus_width)
-{
-	if (bus_width != 1 && bus_width != 2)
-		return -EINVAL;
-
-	ss_phy->lane_cnt = phy->attrs.bus_width;
-
-	return 0;
-}
-
 static int samsung_ufs_phy_init(struct phy *phy)
 {
 	struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
@@ -213,6 +203,26 @@ static int samsung_ufs_phy_power_off(struct phy *phy)
 	return 0;
 }
 
+static int samsung_ufs_phy_request_bus_width(struct phy *phy, int bus_width)
+{
+	struct samsung_ufs_phy *ss_phy = get_samsung_ufs_phy(phy);
+
+	if (bus_width != 1 && bus_width != 2)
+		return -EINVAL;
+
+	ss_phy->lane_cnt = phy->attrs.bus_width;
+
+	if (phy->init_count)
+		samsung_ufs_phy_init(phy);
+
+	if (phy->power_count) {
+		samsung_ufs_phy_power_off(phy);
+		return samsung_ufs_phy_power_on(phy);
+	}
+
+	return 0;
+}
+
 static int samsung_ufs_phy_set_mode(struct phy *generic_phy,
 				    enum phy_mode mode, int submode)
 {
diff --git a/drivers/ufs/host/ufs-exynos.c b/drivers/ufs/host/ufs-exynos.c
index fb616d1599eb..b90876b268db 100644
--- a/drivers/ufs/host/ufs-exynos.c
+++ b/drivers/ufs/host/ufs-exynos.c
@@ -959,6 +959,40 @@ static void exynos_ufs_phy_exit(struct exynos_ufs *ufs)
 	phy_exit(ufs->phy);
 }
 
+static int exynos_ufs_phy_power_on(struct exynos_ufs *ufs)
+{
+	int ret;
+
+	if (ufs->phy_powered_on)
+		return 0;
+
+	ret = phy_power_on(ufs->phy);
+	if (ret) {
+		dev_err(ufs->hba->dev, "Failed to power on PHY: %pe\n",
+			ERR_PTR(ret));
+		return ret;
+	}
+
+	ufs->phy_powered_on = true;
+
+	return 0;
+}
+
+static void exynos_ufs_phy_power_off(struct exynos_ufs *ufs)
+{
+	int ret;
+
+	if (!ufs->phy_powered_on)
+		return;
+
+	ret = phy_power_off(ufs->phy);
+	if (ret)
+		dev_warn(ufs->hba->dev, "Failed to power off PHY: %pe\n",
+			 ERR_PTR(ret));
+
+	ufs->phy_powered_on = false;
+}
+
 static int exynos_ufs_phy_update_bus_width(struct exynos_ufs *ufs)
 {
 	struct ufs_hba *hba = ufs->hba;
@@ -979,10 +1013,7 @@ static int exynos_ufs_phy_update_bus_width(struct exynos_ufs *ufs)
 	if (ret)
 		return ret;
 
-	if (generic_phy->power_count)
-		phy_power_off(generic_phy);
-
-	return phy_power_on(generic_phy);
+	return exynos_ufs_phy_power_on(ufs);
 }
 
 static void exynos_ufs_config_unipro(struct exynos_ufs *ufs)
@@ -1524,7 +1555,7 @@ static void exynos_ufs_exit(struct ufs_hba *hba)
 {
 	struct exynos_ufs *ufs = ufshcd_get_variant(hba);
 
-	phy_power_off(ufs->phy);
+	exynos_ufs_phy_power_off(ufs);
 	exynos_ufs_phy_exit(ufs);
 }
 
@@ -1739,7 +1770,7 @@ static int exynos_ufs_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
 		ufs->drv_data->suspend(ufs);
 
 	if (!ufshcd_is_link_active(hba))
-		phy_power_off(ufs->phy);
+		exynos_ufs_phy_power_off(ufs);
 
 	return 0;
 }
@@ -1749,7 +1780,7 @@ static int exynos_ufs_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
 	struct exynos_ufs *ufs = ufshcd_get_variant(hba);
 
 	if (!ufshcd_is_link_active(hba))
-		phy_power_on(ufs->phy);
+		exynos_ufs_phy_power_on(ufs);
 
 	exynos_ufs_config_smu(ufs);
 	exynos_ufs_fmp_resume(hba);
diff --git a/drivers/ufs/host/ufs-exynos.h b/drivers/ufs/host/ufs-exynos.h
index abe7e472759e..683b9150e2ba 100644
--- a/drivers/ufs/host/ufs-exynos.h
+++ b/drivers/ufs/host/ufs-exynos.h
@@ -227,6 +227,7 @@ struct exynos_ufs {
 	int avail_ln_rx;
 	int avail_ln_tx;
 	int rx_sel_idx;
+	bool phy_powered_on;
 	struct ufs_pa_layer_attr dev_req_params;
 	struct ufs_phy_time_cfg t_cfg;
 	ktime_t entry_hibern8_t;
-- 
2.34.1


  reply	other threads:[~2026-05-04 11:48 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30 11:06 [PATCH v7 phy-next 00/27] Split Generic PHY consumer and provider Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 01/27] ata: add <linux/pm_runtime.h> where missing Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 02/27] PCI: Add missing headers transitively included by <linux/phy/phy.h> Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 03/27] usb: add " Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 04/27] drm: add <linux/pm_runtime.h> where missing Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 05/27] phy: " Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 06/27] phy: spacemit: include missing <linux/phy/phy.h> Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 07/27] net: lan969x: include missing <linux/of.h> Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 08/27] PCI: Remove device links to PHY Vladimir Oltean
2026-05-04  7:57   ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 09/27] scsi: ufs: exynos: stop poking into struct phy guts Vladimir Oltean
2026-05-04 11:48   ` Vladimir Oltean [this message]
2026-04-30 11:06 ` [PATCH v7 phy-next 10/27] scsi: ufs: qcom: keep parallel track of PHY power state Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 11/27] scsi: ufs: qcom: include missing <linux/interrupt.h> Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 12/27] drm/rockchip: dw_hdmi: avoid direct dereference of phy->dev.of_node Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 13/27] usb: host: tegra: " Vladimir Oltean
2026-05-05  9:39   ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 14/27] usb: gadget: tegra-xudc: " Vladimir Oltean
2026-05-05  9:34   ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 15/27] phy: move provider API out of public <linux/phy/phy.h> Vladimir Oltean
2026-05-05  9:30   ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 16/27] phy: make phy_get_mode(), phy_(get|set)_bus_width() NULL tolerant Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 17/27] phy: introduce phy_get_max_link_rate() helper for consumers Vladimir Oltean
2026-04-30 11:59   ` Geert Uytterhoeven
2026-04-30 13:14     ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 18/27] drm/rockchip: dsi: include PHY provider header Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 19/27] drm: bridge: cdns-mhdp8546: use consumer API for getting PHY bus width Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 20/27] media: sunxi: a83-mips-csi2: include PHY provider header Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 21/27] net: renesas: rswitch: " Vladimir Oltean
2026-05-05  9:24   ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 22/27] pinctrl: tegra-xusb: " Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 23/27] power: supply: cpcap-charger: include missing <linux/property.h> Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 24/27] phy: include PHY provider header (1/2) Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 25/27] phy: include PHY provider header (2/2) Vladimir Oltean
2026-05-05  9:22   ` Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 26/27] phy: remove temporary provider compatibility from consumer header Vladimir Oltean
2026-04-30 11:06 ` [PATCH v7 phy-next 27/27] MAINTAINERS: add regexes for linux-phy Vladimir Oltean

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260504114828.n7r7kujaegusdkbw@skbuf \
    --to=vladimir.oltean@nxp.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=alim.akhtar@samsung.com \
    --cc=bvanassche@acm.org \
    --cc=chanho61.park@samsung.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=freedreno@lists.freedesktop.org \
    --cc=krzk@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-ide@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-phy@lists.infradead.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux-samsung-soc@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=linux-tegra@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=neil.armstrong@linaro.org \
    --cc=netdev@vger.kernel.org \
    --cc=peter.griffin@linaro.org \
    --cc=spacemit@lists.linux.dev \
    --cc=vkoul@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox