From 19682239a60c1b53cad8319eaeb58e71d4213cee Mon Sep 17 00:00:00 2001
From: Jonathan Bell <jonathan@raspberrypi.com>
Date: Mon, 15 Jul 2024 13:38:38 +0100
Subject: [PATCH 1168/1215] fixup: mmc: restrict posted write counts for SD
 cards in CQ mode

Leaving card->max_posted_writes unintialised was a bad thing to do.

Also, cqe_enable is 1 if hsq is enabled as hsq substitutes the cqhci
implementation with its own.

Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
---
 drivers/mmc/core/mmc.c   |  1 +
 drivers/mmc/core/queue.c |  2 +-
 drivers/mmc/core/sd.c    | 14 ++++++++------
 3 files changed, 10 insertions(+), 7 deletions(-)

--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -1663,6 +1663,7 @@ static int mmc_init_card(struct mmc_host
 		card->ocr = ocr;
 		card->type = MMC_TYPE_MMC;
 		card->rca = 1;
+		card->max_posted_writes = 1;
 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
 	}
 
--- a/drivers/mmc/core/queue.c
+++ b/drivers/mmc/core/queue.c
@@ -268,7 +268,7 @@ static blk_status_t mmc_mq_queue_rq(stru
 			spin_unlock_irq(&mq->lock);
 			return BLK_STS_RESOURCE;
 		}
-		if (host->cqe_enabled && req_op(req) == REQ_OP_WRITE &&
+		if (!host->hsq_enabled && host->cqe_enabled && req_op(req) == REQ_OP_WRITE &&
 		    mq->pending_writes >= card->max_posted_writes) {
 			spin_unlock_irq(&mq->lock);
 			return BLK_STS_RESOURCE;
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -1105,13 +1105,14 @@ static int sd_parse_ext_reg_perf(struct
 			 mmc_hostname(card->host),
 			 card->ext_csd.cmdq_depth);
 		/*
-		 * If CQ is enabled, there is a contract between host and card such that VDD will
-		 * be maintained and removed only if a power off notification is provided.
-		 * An SD card in an accessible slot means surprise removal is a possibility.
-		 * As a middle ground, limit max posted writes to 1 unless the card is "hardwired".
+		 * If CQ is enabled, there is a contract between host and card such that
+		 * VDD will be maintained and removed only if a power off notification
+		 * is provided. An SD card in an accessible slot means surprise removal
+		 * is a possibility. As a middle ground, keep the default maximum of 1
+		 * posted write unless the card is "hardwired".
 		 */
-		if (mmc_card_is_removable(card->host))
-			card->max_posted_writes = 1;
+		if (!mmc_card_is_removable(card->host))
+			card->max_posted_writes = card->ext_csd.cmdq_depth;
 	}
 
 	card->ext_perf.fno = fno;
@@ -1383,6 +1384,7 @@ retry:
 
 		card->ocr = ocr;
 		card->type = MMC_TYPE_SD;
+		card->max_posted_writes = 1;
 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
 	}
 
