Skip to contentSkip to conversation

mmc: Assorted bug fixes across MMC host drivers

PATCH In review Lee Jones 3 participants 16 messages Nov 23 – Nov 24

Subsystems (14)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover mmc: Assorted bug fixes across MMC host drivers

These issues were reported by Sashiko.

None of these changes have been tested on physical hardware.

Cc "Michał Mirosław" <[email protected]>
Cc "Niklas Söderlund" <[email protected]>
Cc "Uwe Kleine-König (The Capable Hub)" <[email protected]>
Cc Aaro Koskinen <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Aidan MacDonald <[email protected]>
Cc Al Cooper <[email protected]>
Cc Alex Dubov <[email protected]>
Cc Allen Pais <[email protected]>
Cc Apelete Seketeli <[email protected]>
Cc Aubin Constans <[email protected]>
Cc Binbin Zhou <[email protected]>
Cc Broadcom internal kernel review list <[email protected]>
Cc Cavium Inc <[email protected]>
Cc Christian Loehle <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Georgi Djakov <[email protected]>
Cc Guangshuo Li <[email protected]>
Cc Heiner Kallweit <[email protected]>
Cc Hiroshi DOYU <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Jaehoon Chung <[email protected]>
Cc Jan Glauber <[email protected]>
Cc Jarkko Lavinen <[email protected]>
Cc Jerome Brunet <[email protected]>
Cc Josua Mayer <[email protected]>
Cc Juha Yrjola <[email protected]>
Cc Kamal Dasu <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc Krzysztof Kozlowski <[email protected]>
Cc Lad Prabhakar <[email protected]>
Cc Lars-Peter Clausen <[email protected]>
Cc Martin Blumenstingl <[email protected]>
Cc Neil Armstrong <[email protected]>
Cc Paul Cercueil <[email protected]>
Cc Phil Blundell <[email protected]>
Cc Pierre Ossman <[email protected]>
Cc Pramod Gurav <[email protected]>
Cc Ritesh Harjani <[email protected]>
Cc Robert Richter <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc Shawn Lin <[email protected]>
Cc Tony Lindgren <[email protected]>
Cc Ulf Hansson <[email protected]>
Cc Wolfram Sang <[email protected]>
Cc Yoshihiro Shimoda <[email protected]>

Lee Jones (13):
mmc: cavium: Remove MMC slots before destroying slot platform devices
mmc: cavium: Prevent slot_pdev[] out-of-bounds write during probe
mmc: sdhci-msm: Resume runtime PM before accessing registers on remove
mmc: meson-gx: Fail request when descriptor chain DMA mapping fails
mmc: jz4740: Skip DMA cleanup in PIO mode during post_req
mmc: renesas_sdhi: Guard host->cmd access in internal DMAC issue work
mmc: tifm_sd: Synchronise timeout handler with request completion
mmc: omap: Shut down cover timer before disabling cover BH work
mmc: renesas_sdhi: Disable internal DMAC BH work items on release
mmc: jz4740: Apply DMA max_seg_size limit to unified tx-rx channel
mmc: dw_mmc: Disarm central watchdog when ending a request
mmc: renesas_sdhi: Fix SDIO manual tap correction in SDR104 mode
mmc: sdhci-brcmstb: Handle absent optional sdio_freq clock in probe

 drivers/mmc/host/cavium-octeon.c              | 17 +++++++++++++----
 drivers/mmc/host/cavium-thunderx.c            | 15 +++++++++++++--
 drivers/mmc/host/dw_mmc.c                     | 14 +++++++++++---
 drivers/mmc/host/jz4740_mmc.c                 |  6 +++++-
 drivers/mmc/host/meson-gx-mmc.c               |  7 +++++++
 drivers/mmc/host/omap.c                       |  8 ++++----
 drivers/mmc/host/renesas_sdhi_core.c          |  9 ++++-----
 drivers/mmc/host/renesas_sdhi_internal_dmac.c | 13 +++++++++++++
 drivers/mmc/host/sdhci-brcmstb.c              |  6 ++++--
 drivers/mmc/host/sdhci-msm.c                  |  8 ++++----
 drivers/mmc/host/tifm_sd.c                    | 16 ++++++++++++++--
 11 files changed, 92 insertions(+), 27 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#1 lore raw

PATCH 1/13 mmc: cavium: Remove MMC slots before destroying slot platform devices

cvm_mmc_of_slot_probe() allocates each slot and its struct mmc_host via devm_mmc_alloc_host() bound to the dummy slot platform device created in octeon_mmc_probe() and thunder_mmc_probe(), and indexes host->slot[] by the Device Tree reg property while host->slot_pdev[] is indexed by the child iteration counter.

When a slot reg index is greater than its child iteration index (for example when only slot 1 is populated or enabled), the single teardown loop in probe error handling and driver removal calls of_platform_device_destroy() on the slot platform device before calling cvm_mmc_of_slot_remove() on its corresponding host->slot[] entry. Unregistering the unbound platform device immediately releases its managed resources, freeing slot regulators and dropping the initial struct mmc_host reference via mmc_free_host() while the MMC host is still registered. The subsequent cvm_mmc_of_slot_remove() call then accesses freed regulator state and frees struct mmc_host and private slot memory mid-way through mmc_remove_host(), resulting in a use-after-free.

Remove all populated MMC slots in a first pass before destroying the child slot platform devices in a second pass across both probe error and driver removal paths.

Fixes f32b90e6887f ("mmc: cavium: Use devm_mmc_alloc_host() helper")
Fixes 0f4d4424fc1d ("mmc: cavium-octeon: destroy slot platform devices on remove")
Fixes 29ee8cf18a21 ("mmc: cavium-thunderx: destroy slot platform devices on remove")
---
 drivers/mmc/host/cavium-octeon.c   | 4 ++++
 drivers/mmc/host/cavium-thunderx.c | 4 ++++
 2 files changed, 8 insertions(+)
drivers/mmc/host/cavium-octeon.c +4 -0
index dd62b96712c8..7b75b3dfd3e6 100644
@@ -283,6 +283,8 @@ static int octeon_mmc_probe(struct platform_device *pdev)
 	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot[i])
 			cvm_mmc_of_slot_remove(host->slot[i]);
+	}
+	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot_pdev[i])
 			of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
 	}
@@ -298,6 +300,8 @@ static void octeon_mmc_remove(struct platform_device *pdev)
 	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot[i])
 			cvm_mmc_of_slot_remove(host->slot[i]);
+	}
+	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot_pdev[i])
 			of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
 	}
drivers/mmc/host/cavium-thunderx.c +4 -0
index 526ae9019b2f..b8296b2de127 100644
@@ -157,6 +157,8 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
 	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot[i])
 			cvm_mmc_of_slot_remove(host->slot[i]);
+	}
+	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot_pdev[i])
 			of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
 	}
@@ -173,6 +175,8 @@ static void thunder_mmc_remove(struct pci_dev *pdev)
 	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot[i])
 			cvm_mmc_of_slot_remove(host->slot[i]);
+	}
+	for (i = 0; i < CAVIUM_MAX_MMC; i++) {
 		if (host->slot_pdev[i])
 			of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
 	}
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#2 lore raw

PATCH 2/13 mmc: cavium: Prevent slot_pdev[] out-of-bounds write during probe

In reply to Lee Jones

octeon_mmc_probe() and thunder_mmc_probe() iterate over Device Tree child nodes and store each created slot platform device in host->slot_pdev[i], which is sized to CAVIUM_MAX_MMC (4). Neither function checks that i remains below CAVIUM_MAX_MMC before writing to host->slot_pdev[i], and both increment i even for disabled or non-mmc-slot child nodes. When a controller node has more than four child nodes (or four valid mmc-slot nodes alongside another child node on ThunderX), host->slot_pdev[i] writes past the end of the array and corrupts the adjacent set_shared_power and acquire_bus callback pointers in struct cvm_mmc_host.

Iterate only over available child nodes, advance the slot_pdev[] index only when a slot platform device is created, and fail the probe with -EINVAL if more than CAVIUM_MAX_MMC slots are present.

Fixes 8fb83b142823 ("mmc: cavium: Fix probing race with regulator")
Fixes 166bac38c3c5 ("mmc: cavium: Add MMC PCI driver for ThunderX SOCs")
---
 drivers/mmc/host/cavium-octeon.c   | 13 +++++++++----
 drivers/mmc/host/cavium-thunderx.c | 11 +++++++++--
 2 files changed, 18 insertions(+), 6 deletions(-)
drivers/mmc/host/cavium-octeon.c +9 -4
index 7b75b3dfd3e6..375d8f23b26e 100644
@@ -262,13 +262,18 @@ static int octeon_mmc_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, host);
 
 	i = 0;
-	for_each_child_of_node_scoped(node, cn) {
+	for_each_available_child_of_node_scoped(node, cn) {
+		if (i >= CAVIUM_MAX_MMC) {
+			dev_err(&pdev->dev, "Too many slots\n");
+			ret = -EINVAL;
+			octeon_mmc_set_shared_power(host, 0);
+			goto error;
+		}
 		host->slot_pdev[i] =
 			of_platform_device_create(cn, NULL, &pdev->dev);
-		if (!host->slot_pdev[i]) {
-			i++;
+		if (!host->slot_pdev[i])
 			continue;
-		}
+
 		ret = cvm_mmc_of_slot_probe(&host->slot_pdev[i]->dev, host);
 		if (ret) {
 			dev_err(&pdev->dev, "Error populating slots\n");
drivers/mmc/host/cavium-thunderx.c +9 -2
index b8296b2de127..a5346fb96f9b 100644
@@ -129,7 +129,7 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
 	if (ret)
 		goto error;
 
-	for_each_child_of_node(node, child_node) {
+	for_each_available_child_of_node(node, child_node) {
 		/*
 		 * mmc_of_parse and devm* require one device per slot.
 		 * Create a dummy device per slot and set the node pointer to
@@ -137,6 +137,13 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
 		 * of_platform_device_create.
 		 */
 		if (of_device_is_compatible(child_node, "mmc-slot")) {
+			if (i >= CAVIUM_MAX_MMC) {
+				dev_err(dev, "Too many slots\n");
+				of_node_put(child_node);
+				ret = -EINVAL;
+				goto error;
+			}
+
 			host->slot_pdev[i] = of_platform_device_create(child_node, NULL,
 								       &pdev->dev);
 			if (!host->slot_pdev[i])
@@ -147,8 +154,8 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
 				of_node_put(child_node);
 				goto error;
 			}
+			i++;
 		}
-		i++;
 	}
 	dev_info(dev, "probed\n");
 	return 0;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#3 lore raw

PATCH 3/13 mmc: sdhci-msm: Resume runtime PM before accessing registers on remove

In reply to Lee Jones

During driver unbind, __device_release_driver() drops its temporary runtime PM reference via pm_runtime_put_sync() immediately before invoking sdhci_msm_remove(). If the 50 ms autosuspend delay has already elapsed, sdhci_msm_runtime_suspend() gates msm_host->bulk_clks before sdhci_msm_remove() is entered.

sdhci_msm_remove() currently reads SDHCI_INT_STATUS and calls sdhci_remove_host() before waking the device via pm_runtime_get_sync(), performing MMIO accesses while the controller interface and core clocks are disabled and triggering a synchronous external abort on Qualcomm platforms.

Wake the device with pm_runtime_get_sync() and disable runtime PM prior to reading SDHCI_INT_STATUS and calling sdhci_remove_host(), matching other SDHCI platform drivers.

Fixes 67e6db113c90 ("mmc: sdhci-msm: Add pm_runtime and system PM support")
---
 drivers/mmc/host/sdhci-msm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/mmc/host/sdhci-msm.c +4 -4
index aecd425bf095..f62e942bf9f0 100644
@@ -2925,15 +2925,15 @@ static void sdhci_msm_remove(struct platform_device *pdev)
 	struct sdhci_host *host = platform_get_drvdata(pdev);
 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
 	struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
-	int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
-		    0xffffffff);
-
-	sdhci_remove_host(host, dead);
+	int dead;
 
 	pm_runtime_get_sync(&pdev->dev);
 	pm_runtime_disable(&pdev->dev);
 	pm_runtime_put_noidle(&pdev->dev);
 
+	dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
+	sdhci_remove_host(host, dead);
+
 	clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
 				   msm_host->bulk_clks);
 	if (!IS_ERR(msm_host->bus_clk))
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#4 lore raw

PATCH 4/13 mmc: meson-gx: Fail request when descriptor chain DMA mapping fails

In reply to Lee Jones

meson_mmc_pre_req() flags eligible transfers with SD_EMMC_DESC_CHAIN_MODE before mapping the scatterlist with dma_map_sg(). If dma_map_sg() fails and returns 0, meson_mmc_pre_req() logs an error but leaves SD_EMMC_DESC_CHAIN_MODE set with data->sg_count equal to 0. When meson_mmc_request() subsequently dispatches the transfer to meson_mmc_desc_chain_transfer(), the for_each_sg() loop executes zero iterations and desc[data->sg_count - 1].cmd_cfg writes to desc[-1], corrupting memory immediately preceding the coherent descriptor buffer before starting the DMA engine.

Check that data->sg_count is non-zero before starting a descriptor chain request in meson_mmc_request() and complete the request with -EINVAL if mapping failed.

Fixes 79ed05e329c3 ("mmc: meson-gx: add support for descriptor chain mode")
---
 drivers/mmc/host/meson-gx-mmc.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/mmc/host/meson-gx-mmc.c +7 -0
index d2b9b5df9596..f859d9ac0a0a 100644
@@ -914,6 +914,13 @@ static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
 	if (host->needs_pre_post_req)
 		meson_mmc_pre_req(mmc, mrq);
 
+	if (mrq->data && meson_mmc_desc_chain_mode(mrq->data) &&
+	    !mrq->data->sg_count) {
+		mrq->cmd->error = -EINVAL;
+		mmc_request_done(mmc, mrq);
+		return;
+	}
+
 	/* Stop execution */
 	writel(0, host->regs + SD_EMMC_START);
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#5 lore raw

PATCH 5/13 mmc: jz4740: Skip DMA cleanup in PIO mode during post_req

In reply to Lee Jones

When DMA channel acquisition fails during probe, the JZ4740 MMC driver falls back to PIO mode with host->use_dma set to false and host->dma_tx or host->dma_rx left holding an ERR_PTR() or released channel pointer. Unlike jz4740_mmc_pre_request(), jz4740_mmc_post_request() does not check host->use_dma before calling dmaengine_terminate_all() on error, resulting in an invalid or dangling DMA channel pointer dereference whenever a PIO data transfer encounters an error.

Return early from jz4740_mmc_post_request() when host->use_dma is false, matching jz4740_mmc_pre_request().

Fixes bb2f45927f8e ("mmc: jz4740: prepare next dma transfer in parallel with current transfer")
---
 drivers/mmc/host/jz4740_mmc.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/mmc/host/jz4740_mmc.c +3 -0
index 923cf1450de5..e398a575f2dc 100644
@@ -392,6 +392,9 @@ static void jz4740_mmc_post_request(struct mmc_host *mmc,
 	struct jz4740_mmc_host *host = mmc_priv(mmc);
 	struct mmc_data *data = mrq->data;
 
+	if (!host->use_dma)
+		return;
+
 	if (data && data->host_cookie != COOKIE_UNMAPPED)
 		jz4740_mmc_dma_unmap(host, data);
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#6 lore raw

PATCH 6/13 mmc: renesas_sdhi: Guard host->cmd access in internal DMAC issue work

In reply to Lee Jones

renesas_sdhi_internal_dmac_issue_work_fn() unmasks TMIO_STAT_DATAEND and dereferences host->cmd without holding host->lock or checking that host->cmd is still non-NULL. If tmio_mmc_reset_work() times out and clears host->cmd under host->lock after tmio_mmc_cmd_irq() queues host->dma_issue, or if unmasking TMIO_STAT_DATAEND allows a concurrent data-end error completion to clear host->cmd via tmio_mmc_finish_request(), evaluating host->cmd->error triggers a NULL pointer dereference. Unmasking TMIO_STAT_DATAEND outside host->lock also races with concurrent updates to host->sdcard_irq_mask.

Acquire host->lock across renesas_sdhi_internal_dmac_issue_work_fn() and bail out if host->cmd has already been cleared, matching the sibling system DMAC implementation.

Fixes c330601c9c93 ("mmc: renesas_sdhi: take DMA end interrupts into account")
---
 drivers/mmc/host/renesas_sdhi_internal_dmac.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/mmc/host/renesas_sdhi_internal_dmac.c +8 -0
index 0c3967f758c2..6831fc34cd22 100644
@@ -450,6 +450,13 @@ static void renesas_sdhi_internal_dmac_issue_work_fn(struct work_struct *work)
 {
 	struct tmio_mmc_host *host = from_work(host, work, dma_issue);
 	struct renesas_sdhi *priv = host_to_priv(host);
+	unsigned long flags;
+
+	spin_lock_irqsave(&host->lock, flags);
+	if (!host->cmd) {
+		spin_unlock_irqrestore(&host->lock, flags);
+		return;
+	}
 
 	tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
 
@@ -462,6 +469,7 @@ static void renesas_sdhi_internal_dmac_issue_work_fn(struct work_struct *work)
 		if (test_bit(SDHI_DMA_END_FLAG_ACCESS, &priv->dma_priv.end_flags))
 			queue_work(system_bh_wq, &priv->dma_priv.dma_complete);
 	}
+	spin_unlock_irqrestore(&host->lock, flags);
 }
 
 static bool renesas_sdhi_internal_dmac_complete(struct tmio_mmc_host *host)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#7 lore raw

PATCH 7/13 mmc: tifm_sd: Synchronise timeout handler with request completion

In reply to Lee Jones

tifm_sd_end_cmd() deletes host->timer with timer_delete() under sock->lock, clears host->req to NULL, and completes the request via mmc_request_done(). Because timer_delete() does not wait for an already executing timer callback and tifm_sd_abort() reads the active command opcode from host->req without holding sock->lock, a concurrent timeout expiry can dereference a NULL or freed struct mmc_request and spuriously eject the card after the request has already completed.

Serialise tifm_sd_abort() on sock->lock, return early if host->req has already been cleared or the socket is being ejected, and snapshot the command opcode and flags before releasing sock->lock prior to calling tifm_eject().

Fixes 91f8d0118a0e ("tifm: layout fixes, small changes to comments and printfs")
---
 drivers/mmc/host/tifm_sd.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/mmc/host/tifm_sd.c +14 -2
index 28ab2526dab1..30935214d17c 100644
@@ -776,12 +776,24 @@ static void tifm_sd_end_cmd(struct work_struct *t)
 static void tifm_sd_abort(struct timer_list *t)
 {
 	struct tifm_sd *host = timer_container_of(host, t, timer);
+	struct tifm_dev *sock = host->dev;
+	unsigned int opcode, cmd_flags;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sock->lock, flags);
+	if (!host->req || host->eject) {
+		spin_unlock_irqrestore(&sock->lock, flags);
+		return;
+	}
+	opcode = host->req->cmd->opcode;
+	cmd_flags = host->cmd_flags;
+	spin_unlock_irqrestore(&sock->lock, flags);
 
 	pr_err("%s : card failed to respond for a long period of time "
 	       "(%x, %x)\n",
-	       dev_name(&host->dev->dev), host->req->cmd->opcode, host->cmd_flags);
+	       dev_name(&sock->dev), opcode, cmd_flags);
 
-	tifm_eject(host->dev);
+	tifm_eject(sock);
 }
 
 static void tifm_sd_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#8 lore raw

PATCH 8/13 mmc: omap: Shut down cover timer before disabling cover BH work

In reply to Lee Jones

In mmc_omap_remove_slot(), cancel_work_sync(&slot->cover_bh_work) is called before timer_delete_sync(&slot->cover_timer). Because mmc_omap_cover_timer() queues slot->cover_bh_work and mmc_omap_cover_bh_handler() rearms slot->cover_timer, a timer expiry between the two calls (or a concurrent omap_mmc_notify_cover_event() before platform cleanup) can re-queue cover_bh_work so that it runs after the slot and its struct mmc_host have been freed.

When cover state polling is configured, shut down slot->cover_timer first with timer_shutdown_sync() to prevent re-arming, and then stop slot->cover_bh_work with disable_work_sync() so no further work can be queued during teardown.

Fixes 7584d276d47a ("MMC: OMAP: Use tasklet instead of workqueue for cover switch notification")
Fixes 921c87ba3893 ("mmc: Convert from tasklet to BH workqueue")
---
 drivers/mmc/host/omap.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/mmc/host/omap.c +4 -4
index 36df578298c3..55efc4ec9d03 100644
@@ -1356,11 +1356,11 @@ static void mmc_omap_remove_slot(struct mmc_omap_slot *slot)
 
 	if (slot->pdata->name != NULL)
 		device_remove_file(&mmc->class_dev, &dev_attr_slot_name);
-	if (slot->pdata->get_cover_state != NULL)
+	if (slot->pdata->get_cover_state) {
 		device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
-
-	cancel_work_sync(&slot->cover_bh_work);
-	timer_delete_sync(&slot->cover_timer);
+		timer_shutdown_sync(&slot->cover_timer);
+		disable_work_sync(&slot->cover_bh_work);
+	}
 	flush_workqueue(slot->host->mmc_omap_wq);
 
 	mmc_remove_host(mmc);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#9 lore raw

PATCH 9/13 mmc: renesas_sdhi: Disable internal DMAC BH work items on release

In reply to Lee Jones

renesas_sdhi_internal_dmac_request_dma() initialises both host->dma_issue and priv->dma_priv.dma_complete, which are queued onto system_bh_wq during DMA transfers and error handling. However, renesas_sdhi_internal_dmac_release_dma() only clears host->chan_rx and host->chan_tx without disabling or draining either work item, allowing a pending execution of renesas_sdhi_internal_dmac_complete_work_fn() or renesas_sdhi_internal_dmac_issue_work_fn() to run after controller clocks are disabled and the host structures are freed.

Disable and drain both work items with disable_work_sync() in renesas_sdhi_internal_dmac_release_dma().

Fixes 85683fb39d9b ("mmc: sdhi: Convert from tasklet to BH workqueue")
---
 drivers/mmc/host/renesas_sdhi_internal_dmac.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/mmc/host/renesas_sdhi_internal_dmac.c +5 -0
index 6831fc34cd22..a88946bdb567 100644
@@ -573,6 +573,11 @@ renesas_sdhi_internal_dmac_request_dma(struct tmio_mmc_host *host,
 static void
 renesas_sdhi_internal_dmac_release_dma(struct tmio_mmc_host *host)
 {
+	struct renesas_sdhi *priv = host_to_priv(host);
+
+	disable_work_sync(&host->dma_issue);
+	disable_work_sync(&priv->dma_priv.dma_complete);
+
 	/* Each value is set to zero to assume "disabling" each DMA */
 	host->chan_rx = host->chan_tx = NULL;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#10 lore raw

PATCH 10/13 mmc: jz4740: Apply DMA max_seg_size limit to unified tx-rx channel

In reply to Lee Jones

Commit afadb04f1d6e ("mmc: jz4740: Apply DMA engine limits to maximum segment size") added max_seg_size clamping at the end of jz4740_mmc_acquire_dma_channels(), below the early return for the unified "tx-rx" DMA channel added earlier by commit a474e52c3109 ("mmc:
jz4740: Support using a bi-directional DMA channel"). When "tx-rx" is used, host->mmc->max_seg_size is never clamped to dma_get_max_seg_size() and the MMC core can submit scatterlist segments exceeding the DMA controller hardware limit.

Jump to the max_seg_size clamping block when acquiring the "tx-rx" channel instead of returning early.

Fixes afadb04f1d6e ("mmc: jz4740: Apply DMA engine limits to maximum segment size")
---
 drivers/mmc/host/jz4740_mmc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mmc/host/jz4740_mmc.c +2 -1
index e398a575f2dc..71aa7930a030 100644
@@ -231,7 +231,7 @@ static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host *host)
 
 	host->dma_tx = dma_request_chan(dev, "tx-rx");
 	if (!IS_ERR(host->dma_tx))
-		return 0;
+		goto out;
 
 	if (PTR_ERR(host->dma_tx) != -ENODEV) {
 		dev_err(dev, "Failed to get dma tx-rx channel\n");
@@ -251,6 +251,7 @@ static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host *host)
 		return PTR_ERR(host->dma_rx);
 	}
 
+out:
 	/*
 	 * Limit the maximum segment size in any SG entry according to
 	 * the parameters of the DMA engine device.
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#11 lore raw

PATCH 11/13 mmc: dw_mmc: Disarm central watchdog when ending a request

In reply to Lee Jones

When EVENT_DATA_ERROR (such as SDMMC_INT_DCRC or SDMMC_INT_SBE) is handled in STATE_SENDING_DATA, send_stop_abort() starts a stop command and arms host->wd_timer for DW_MCI_WD_CMD_EVENTS. In STATE_DATA_BUSY, dw_mci_data_complete() resets the controller on data error, aborting the in-flight stop command, and dw_mci_work_func() ends the request via dw_mci_request_end() if EVENT_CMD_COMPLETE is not already pending.

Because dw_mci_request_end() does not disarm host->wd_timer, the stale command watch remains armed after mmc_request_done() and later injects a spurious SDMMC_INT_RTO and EVENT_CMD_COMPLETE while idle or during a subsequent recovery command (for example MMC_GO_IDLE_STATE, which does not re-arm the command watchdog).

Disarm the central watchdog under host->irq_lock when transitioning to STATE_IDLE in dw_mci_request_end(), and clear wd_states and wd_deadline_ns alongside wd_events when disarming in dw_mci_wd_deliver().

Fixes ce0043b9a302 ("mmc: dw_mmc: add central watchdog and convert CTO onto it")
---
 drivers/mmc/host/dw_mmc.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/mmc/host/dw_mmc.c +11 -3
index ff0b25d287a9..6865cffad51a 100644
@@ -79,8 +79,11 @@
 static void dw_mci_wd_deliver(struct dw_mci *host, unsigned long events)
 {
 	host->wd_events &= ~events;
-	if (!host->wd_events)
+	if (!host->wd_events) {
+		host->wd_states = 0;
+		host->wd_deadline_ns = 0;
 		hrtimer_try_to_cancel(&host->wd_timer);
+	}
 }
 
 /*
@@ -1887,15 +1890,20 @@ static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
 	__acquires(&host->lock)
 {
 	struct mmc_host	*prev_mmc = host->mmc;
+	unsigned long irqflags;
 
 	WARN_ON(host->cmd || host->data);
 
 	host->mrq = NULL;
 
-	if (host->state == STATE_SENDING_CMD11)
+	if (host->state == STATE_SENDING_CMD11) {
 		host->state = STATE_WAITING_CMD11_DONE;
-	else
+	} else {
+		spin_lock_irqsave(&host->irq_lock, irqflags);
+		dw_mci_wd_deliver(host, ~0UL);
+		spin_unlock_irqrestore(&host->irq_lock, irqflags);
 		host->state = STATE_IDLE;
+	}
 
 	spin_unlock(&host->lock);
 	mmc_request_done(prev_mmc, mrq);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer Reviewed-by on v1 11/13
#12 lore raw

Re: [PATCH 11/13] mmc: dw_mmc: Disarm central watchdog when ending a request

Hi

Quoted 7 lines · Lee Jones

When EVENT_DATA_ERROR (such as SDMMC_INT_DCRC or SDMMC_INT_SBE) is handled in STATE_SENDING_DATA, send_stop_abort() starts a stop command and arms host->wd_timer for DW_MCI_WD_CMD_EVENTS. In STATE_DATA_BUSY, dw_mci_data_complete() resets the controller on data error, aborting the in-flight stop command, and dw_mci_work_func() ends the request via dw_mci_request_end() if EVENT_CMD_COMPLETE is not already pending.

Sure, it's a bug for send_stop_abort() case. The dw_mmc state machine is so complicated and prone to error... This fix looks clean and minimal,

Quoted 58 lines

Because dw_mci_request_end() does not disarm host->wd_timer, the stale command watch remains armed after mmc_request_done() and later injects a spurious SDMMC_INT_RTO and EVENT_CMD_COMPLETE while idle or during a subsequent recovery command (for example MMC_GO_IDLE_STATE, which does not re-arm the command watchdog).

Disarm the central watchdog under host->irq_lock when transitioning to STATE_IDLE in dw_mci_request_end(), and clear wd_states and wd_deadline_ns alongside wd_events when disarming in dw_mci_wd_deliver().

Fixes ce0043b9a302 ("mmc: dw_mmc: add central watchdog and convert CTO onto it")
---
  drivers/mmc/host/dw_mmc.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
drivers/mmc/host/dw_mmc.c +11 -3
index ff0b25d287a9..6865cffad51a 100644
@@ -79,8 +79,11 @@
  static void dw_mci_wd_deliver(struct dw_mci *host, unsigned long events)
  {
  	host->wd_events &= ~events;
-	if (!host->wd_events)
+	if (!host->wd_events) {
+		host->wd_states = 0;
+		host->wd_deadline_ns = 0;
  		hrtimer_try_to_cancel(&host->wd_timer);
+	}
  }
 
  /*
@@ -1887,15 +1890,20 @@ static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  	__acquires(&host->lock)
  {
  	struct mmc_host	*prev_mmc = host->mmc;
+	unsigned long irqflags;
 
  	WARN_ON(host->cmd || host->data);
 
  	host->mrq = NULL;
 
-	if (host->state == STATE_SENDING_CMD11)
+	if (host->state == STATE_SENDING_CMD11) {
  		host->state = STATE_WAITING_CMD11_DONE;
-	else
+	} else {
+		spin_lock_irqsave(&host->irq_lock, irqflags);
+		dw_mci_wd_deliver(host, ~0UL);
+		spin_unlock_irqrestore(&host->irq_lock, irqflags);
  		host->state = STATE_IDLE;
+	}
 
  	spin_unlock(&host->lock);
  	mmc_request_done(prev_mmc, mrq);
author
#13 lore raw

PATCH 12/13 mmc: renesas_sdhi: Fix SDIO manual tap correction in SDR104 mode

In reply to Lee Jones

Commit 30fa7d53afd4 ("mmc: host: renesas_sdhi: Fix incorrect auto retuning for an SDIO card") switched SDIO cards from automatic to manual SCC tap correction so that DAT1 SDIO interrupts would not spuriously shift the sampling tap, but exposed two logic errors in the manual correction path:

1. renesas_sdhi_manual_correction() checks !priv->card_is_sdio instead of priv->card_is_sdio before ignoring DAT1 mismatches, so the SDIO DAT1 check never executes for SDIO cards.

2. renesas_sdhi_check_scc_error() already returns early when running in 4-tap HS400 mode, yet still passes the hs400_4taps quirk flag as use_4tap to renesas_sdhi_manual_correction(), halving the 8-tap SDR104 and HS200 tap index when writing SH_MOBILE_SDHI_SCC_TAPSET on SoCs with hs400_4taps.

Check priv->card_is_sdio without negation and drop the unused 4-tap division from renesas_sdhi_manual_correction().

Fixes 30fa7d53afd4 ("mmc: host: renesas_sdhi: Fix incorrect auto retuning for an SDIO card")
---
 drivers/mmc/host/renesas_sdhi_core.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/mmc/host/renesas_sdhi_core.c +4 -5
index cb35360239bf..497c7c75f966 100644
@@ -741,7 +741,7 @@ static int renesas_sdhi_execute_tuning(struct mmc_host *mmc, u32 opcode)
 	return ret;
 }
 
-static bool renesas_sdhi_manual_correction(struct tmio_mmc_host *host, bool use_4tap)
+static bool renesas_sdhi_manual_correction(struct tmio_mmc_host *host)
 {
 	struct renesas_sdhi *priv = host_to_priv(host);
 	unsigned int new_tap = priv->tap_set, error_tap = priv->tap_set;
@@ -783,7 +783,7 @@ static bool renesas_sdhi_manual_correction(struct tmio_mmc_host *host, bool use_
 		if (bad_taps & BIT(new_tap % priv->tap_num))
 			return test_bit(error_tap % priv->tap_num, priv->smpcmp);
 	} else {
-		if (!priv->card_is_sdio &&
+		if (priv->card_is_sdio &&
 		    !(val & SH_MOBILE_SDHI_SCC_RVSREQ_RVSERR)) {
 			u32 smpcmp = sd_scc_read32(host, priv, SH_MOBILE_SDHI_SCC_SMPCMP);
 
@@ -802,8 +802,7 @@ static bool renesas_sdhi_manual_correction(struct tmio_mmc_host *host, bool use_
 	}
 
 	priv->tap_set = (new_tap % priv->tap_num);
-	sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET,
-		       priv->tap_set / (use_4tap ? 2 : 1));
+	sd_scc_write32(host, priv, SH_MOBILE_SDHI_SCC_TAPSET, priv->tap_set);
 
 	return false;
 }
@@ -854,7 +853,7 @@ static bool renesas_sdhi_check_scc_error(struct tmio_mmc_host *host,
 	    SH_MOBILE_SDHI_SCC_RVSCNTL_RVSEN)
 		ret |= renesas_sdhi_auto_correction(host);
 	else
-		ret |= renesas_sdhi_manual_correction(host, use_4tap);
+		ret |= renesas_sdhi_manual_correction(host);
 
 	return ret;
 }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
author
#14 lore raw

PATCH 13/13 mmc: sdhci-brcmstb: Handle absent optional sdio_freq clock in probe

In reply to Lee Jones

Standard MMC Device Tree nodes may specify "clock-frequency" on SoCs that do not provide the optional 72116B0 "sdio_freq" clock, in which case devm_clk_get_optional() legitimately returns NULL. However, sdhci_brcmstb_probe() only checks IS_ERR(base_clk): when base_clk is NULL, clk_get_rate(base_clk) evaluates to 0 and overwrites SDHCI_CLOCK_V3_BASE_MASK in host->caps with 0 MHz while leaving priv->base_freq_hz non-zero for system resume.

Check IS_ERR_OR_NULL(base_clk) and clear priv->base_freq_hz on the fallback path before jumping to add_host.

Fixes 97904a59855c ("mmc: sdhci-brcmstb: Add ability to increase max clock rate for 72116b0")
---
 drivers/mmc/host/sdhci-brcmstb.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/mmc/host/sdhci-brcmstb.c +4 -2
index db857f2273e2..976cddb2ca9e 100644
@@ -585,8 +585,10 @@ static int sdhci_brcmstb_probe(struct platform_device *pdev)
 		goto add_host;
 
 	base_clk = devm_clk_get_optional(&pdev->dev, "sdio_freq");
-	if (IS_ERR(base_clk)) {
-		dev_warn(&pdev->dev, "Clock for \"sdio_freq\" not found\n");
+	if (IS_ERR_OR_NULL(base_clk)) {
+		if (IS_ERR(base_clk))
+			dev_warn(&pdev->dev, "Clock for \"sdio_freq\" not found\n");
+		priv->base_freq_hz = 0;
 		goto add_host;
 	}
 
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer Reviewed-by on v1 13/13
#15 lore raw

Re: [PATCH 13/13] mmc: sdhci-brcmstb: Handle absent optional sdio_freq clock in probe

Quoted 8 lines · Lee Jones

Standard MMC Device Tree nodes may specify "clock-frequency" on SoCs that do not provide the optional 72116B0 "sdio_freq" clock, in which case devm_clk_get_optional() legitimately returns NULL. However, sdhci_brcmstb_probe() only checks IS_ERR(base_clk): when base_clk is NULL, clk_get_rate(base_clk) evaluates to 0 and overwrites SDHCI_CLOCK_V3_BASE_MASK in host->caps with 0 MHz while leaving priv->base_freq_hz non-zero for system resume.

Quoted 7 lines

Check IS_ERR_OR_NULL(base_clk) and clear priv->base_freq_hz on the fallback path before jumping to add_host.

Fixes 97904a59855c ("mmc: sdhci-brcmstb: Add ability to increase max clock rate for 72116b0")

Not sure this deserves a Fixes: tag.

Signature · 1 line
Florian