>From c687a8568c6c7837bb0bd539bf14343d7d0c63a1 Mon Sep 17 00:00:00 2001 From: Vladimir Oltean 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 --- Cc: Alim Akhtar Cc: Bart Van Assche Cc: Peter Griffin Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: Krzysztof Kozlowski Cc: Chanho Park 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