Skip to contentSkip to conversation

mmc: Add Command Queue support

PATCH Superseded Adrian Hunter 3 participants 12 messages Oct 24, 2017 – Oct 27, 2017

Subsystems (4)

v12 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Adrian Hunter

author maintainer
#0 lore raw

v12 cover mmc: Add Command Queue support

Hi

Here is V12 of the hardware command queue patches without the software command queue patches, now using blk-mq and now with blk-mq support for non-CQE I/O.

HW CMDQ offers 25% - 50% better random multi-threaded I/O. I see a slight 2% drop in sequential read speed but no change to sequential write.

Non-CQE blk-mq showed a 3% decrease in sequential read performance. This seemed to be coming from the inferior latency of running work items compared with a dedicated thread. Hacking blk-mq workqueue to be unbound reduced the performance degradation from 3% to 1%.

While we should look at changing blk-mq to give better workqueue performance, a bigger gain is likely to be made by adding a new host API to enable the next already-prepared request to be issued directly from within ->done() callback of the current request.

Changes since V11:
Split "mmc: block: Add CQE and blk-mq support" into 2 patches

Changes since V10:
mmc: core: Remove unnecessary host claim
mmc: core: Introduce host claiming by context
mmc: core: Add support for handling CQE requests
mmc: mmc: Enable Command Queuing
mmc: mmc: Enable CQE's
mmc: block: Use local variables in mmc_blk_data_prep()
mmc: block: Prepare CQE data
mmc: block: Factor out mmc_setup_queue()
mmc: core: Add parameter use_blk_mq
mmc: core: Export mmc_start_bkops()
mmc: core: Export mmc_start_request()
mmc: core: Export mmc_retune_hold_now() and mmc_retune_release()
Dropped because they have been applied
mmc: block: Add CQE and blk-mq support
Extend blk-mq support for asynchronous read / writes to all host controllers including those that require polling. The direct completion path is still available but depends on a new capability flag.
Drop blk-mq support for synchronous read / writes.

Venkat Gopalakrishnan (1):
mmc: cqhci: support for command queue enabled host

Changes since V9:
mmc: block: Add CQE and blk-mq support
- reinstate mq support for REQ_OP_DRV_IN/OUT that was removed because it was incorrectly assumed to be handled by the rpmb character device
- don't check for rpmb block device anymore
mmc: cqhci: support for command queue enabled host
Fix cqhci_set_irqs() as per Haibo Chen

Changes since V8:
Re-based
mmc: core: Introduce host claiming by context
Slightly simplified as per Ulf
mmc: core: Export mmc_retune_hold_now() and mmc_retune_release()
New patch.
mmc: block: Add CQE and blk-mq support
Fix missing ->post_req() on the error path

Changes since V7:
Re-based
mmc: core: Introduce host claiming by context
Slightly simplified
mmc: core: Add parameter use_blk_mq
New patch.
mmc: core: Remove unnecessary host claim
New patch.
mmc: core: Export mmc_start_bkops()
New patch.
mmc: core: Export mmc_start_request()
New patch.
mmc: block: Add CQE and blk-mq support
Add blk-mq support for non_CQE requests

Changes since V6:
mmc: core: Introduce host claiming by context
New patch.
mmc: core: Move mmc_start_areq() declaration
Dropped because it has been applied
mmc: block: Fix block status codes
Dropped because it has been applied
mmc: host: Add CQE interface
Dropped because it has been applied
mmc: core: Turn off CQE before sending commands
Dropped because it has been applied
mmc: block: Factor out mmc_setup_queue()
New patch.
mmc: block: Add CQE support
Drop legacy support and add blk-mq support

Changes since V5:
Re-based
mmc: core: Add mmc_retune_hold_now()
Dropped because it has been applied
mmc: core: Add members to mmc_request and mmc_data for CQE's
Dropped because it has been applied
mmc: core: Move mmc_start_areq() declaration
New patch at Ulf's request
mmc: block: Fix block status codes
Another un-related patch
mmc: host: Add CQE interface
Move recovery_notifier() callback to struct mmc_request
mmc: core: Add support for handling CQE requests
Roll __mmc_cqe_request_done() into mmc_cqe_request_done() Move function declarations requested by Ulf
mmc: core: Remove unused MMC_CAP2_PACKED_CMD
Dropped because it has been applied
mmc: block: Add CQE support
Add explanation to commit message
Adjustment for changed recovery_notifier() callback
mmc: cqhci: support for command queue enabled host
Adjustment for changed recovery_notifier() callback
mmc: sdhci-pci: Add CQHCI support for Intel GLK
Add DCMD capability for Intel controllers except GLK

Changes since V4:
mmc: core: Add mmc_retune_hold_now()
Add explanation to commit message.
mmc: host: Add CQE interface
Add comments to callback declarations.
mmc: core: Turn off CQE before sending commands
Add explanation to commit message.
mmc: core: Add support for handling CQE requests
Add comments as requested by Ulf.
mmc: core: Remove unused MMC_CAP2_PACKED_CMD
New patch.
mmc: mmc: Enable Command Queuing
Adjust for removal of MMC_CAP2_PACKED_CMD.
Add a comment about Packed Commands.
mmc: mmc: Enable CQE's
Remove un-necessary check for MMC_CAP2_CQE
mmc: block: Use local variables in mmc_blk_data_prep()
New patch.
mmc: block: Prepare CQE data
Adjust due to "mmc: block: Use local variables in mmc_blk_data_prep()" Remove priority setting.
Add explanation to commit message.
mmc: cqhci: support for command queue enabled host
Fix transfer descriptor setting in cqhci_set_tran_desc() for 32-bit DMA

Changes since V3:
Adjusted ...blk_end_request...() for new block status codes Fixed CQHCI transaction descriptor for "no DCMD" case

Changes since V2:
Dropped patches that have been applied. Re-based
Added "mmc: sdhci-pci: Add CQHCI support for Intel GLK"

Changes since V1:

"Share mmc request array between partitions" is dependent on changes in "Introduce queue semantics", so added that and block fixes:

Added "Fix is_waiting_last_req set incorrectly" Added "Fix cmd error reset failure path" Added "Use local var for mqrq_cur" Added "Introduce queue semantics"

Changes since RFC:

Re-based on next.
Added comment about command queue priority. Added some acks and reviews.

Adrian Hunter (4):
mmc: core: Add parameter use_blk_mq
mmc: block: Add blk-mq support
mmc: block: Add CQE support
mmc: sdhci-pci: Add CQHCI support for Intel GLK

Venkat Gopalakrishnan (1):
mmc: cqhci: support for command queue enabled host

 drivers/mmc/Kconfig               |   11 +
 drivers/mmc/core/block.c          |  801 +++++++++++++++++++++++++-
 drivers/mmc/core/block.h          |   12 +
 drivers/mmc/core/core.c           |    7 +
 drivers/mmc/core/core.h           |    2 +
 drivers/mmc/core/host.c           |    2 +
 drivers/mmc/core/host.h           |    4 +
 drivers/mmc/core/queue.c          |  426 +++++++++++++-
 drivers/mmc/core/queue.h          |   56 ++
 drivers/mmc/host/Kconfig          |   14 +
 drivers/mmc/host/Makefile         |    1 +
 drivers/mmc/host/cqhci.c          | 1150 +++++++++++++++++++++++++++++++++++++
 drivers/mmc/host/cqhci.h          |  240 ++++++++
 drivers/mmc/host/sdhci-pci-core.c |  155 ++++-
 include/linux/mmc/host.h          |    2 +
 15 files changed, 2852 insertions(+), 31 deletions(-)
 create mode 100644 drivers/mmc/host/cqhci.c
 create mode 100644 drivers/mmc/host/cqhci.h

Regards
Adrian

author maintainer
#2 lore raw

PATCH v12 2/5 mmc: block: Add blk-mq support

Define and use a blk-mq queue. Discards and flushes are processed synchronously, but reads and writes asynchronously. In order to support slow DMA unmapping, DMA unmapping is not done until after the next request is started. That means the request is not completed until then. If there is no next request then the completion is done by queued work.

---
 drivers/mmc/core/block.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/mmc/core/block.h |  10 +
 drivers/mmc/core/queue.c | 302 ++++++++++++++++++++--
 drivers/mmc/core/queue.h |  41 +++
 include/linux/mmc/host.h |   1 +
 5 files changed, 979 insertions(+), 30 deletions(-)
drivers/mmc/core/block.c +648 -7
index ea80ff4cd7f9..002446e8dc5d 100644
@@ -1264,7 +1264,10 @@ static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
 		break;
 	}
 	mq_rq->drv_op_result = ret;
-	blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+	if (req->mq_ctx)
+		blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+	else
+		blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
 }
 
 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
@@ -1307,7 +1310,10 @@ static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
 	else
 		mmc_blk_reset_success(md, type);
 fail:
-	blk_end_request(req, status, blk_rq_bytes(req));
+	if (req->mq_ctx)
+		blk_mq_end_request(req, status);
+	else
+		blk_end_request(req, status, blk_rq_bytes(req));
 }
 
 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
@@ -1377,7 +1383,10 @@ static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
 	if (!err)
 		mmc_blk_reset_success(md, type);
 out:
-	blk_end_request(req, status, blk_rq_bytes(req));
+	if (req->mq_ctx)
+		blk_mq_end_request(req, status);
+	else
+		blk_end_request(req, status, blk_rq_bytes(req));
 }
 
 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
@@ -1387,7 +1396,10 @@ static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
 	int ret = 0;
 
 	ret = mmc_flush_cache(card);
-	blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+	if (req->mq_ctx)
+		blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+	else
+		blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
 }
 
 /*
@@ -1413,15 +1425,18 @@ static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
 	}
 }
 
-#define CMD_ERRORS							\
-	(R1_OUT_OF_RANGE |	/* Command argument out of range */	\
-	 R1_ADDRESS_ERROR |	/* Misaligned address */		\
+#define CMD_ERRORS_EXCL_OOR						\
+	(R1_ADDRESS_ERROR |	/* Misaligned address */		\
 	 R1_BLOCK_LEN_ERROR |	/* Transferred block length incorrect */\
 	 R1_WP_VIOLATION |	/* Tried to write to protected block */	\
 	 R1_CARD_ECC_FAILED |	/* Card ECC failed */			\
 	 R1_CC_ERROR |		/* Card controller error */		\
 	 R1_ERROR)		/* General/unknown error */
 
+#define CMD_ERRORS							\
+	(CMD_ERRORS_EXCL_OOR |						\
+	 R1_OUT_OF_RANGE)	/* Command argument out of range */	\
+
 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
 {
 	u32 val;
@@ -1766,6 +1781,632 @@ static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
 	mqrq->areq.err_check = mmc_blk_err_check;
 }
 
+#define MMC_MAX_RETRIES		5
+#define MMC_DATA_RETRIES	2
+#define MMC_NO_RETRIES		(MMC_MAX_RETRIES + 1)
+
+/* Single sector read during recovery */
+static void mmc_blk_ss_read(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	blk_status_t status;
+
+	while (1) {
+		mmc_blk_rw_rq_prep(mqrq, mq->card, 1, mq);
+
+		mmc_wait_for_req(mq->card->host, &mqrq->brq.mrq);
+
+		/*
+		 * Not expecting command errors, so just give up in that case.
+		 * If there are retries remaining, the request will get
+		 * requeued.
+		 */
+		if (mqrq->brq.cmd.error)
+			return;
+
+		if (blk_rq_bytes(req) <= 512)
+			break;
+
+		status = mqrq->brq.data.error ? BLK_STS_IOERR : BLK_STS_OK;
+
+		blk_update_request(req, status, 512);
+	}
+
+	mqrq->retries = MMC_NO_RETRIES;
+}
+
+static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
+{
+	return !!brq->mrq.sbc;
+}
+
+static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
+{
+	return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
+}
+
+static inline bool mmc_blk_in_tran_state(u32 status)
+{
+	/*
+	 * Some cards mishandle the status bits, so make sure to check both the
+	 * busy indication and the card state.
+	 */
+	return status & R1_READY_FOR_DATA &&
+	       (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
+}
+
+/*
+ * Check for errors the host controller driver might not have seen such as
+ * response mode errors or invalid card state.
+ */
+static bool mmc_blk_status_error(struct request *req, u32 status)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_blk_request *brq = &mqrq->brq;
+	u32 stop_err_bits = mmc_blk_stop_err_bits(brq);
+
+	return brq->cmd.resp[0]  & CMD_ERRORS    ||
+	       brq->stop.resp[0] & stop_err_bits ||
+	       status            & stop_err_bits ||
+	       (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
+}
+
+static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
+{
+	return !brq->sbc.error && !brq->cmd.error &&
+	       !(brq->cmd.resp[0] & CMD_ERRORS);
+}
+
+static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
+{
+	if (host->actual_clock)
+		return host->actual_clock / 1000;
+
+	/* Clock may be subject to a divisor, fudge it by a factor of 2. */
+	if (host->ios.clock)
+		return host->ios.clock / 2000;
+
+	/* How can there be no clock */
+	WARN_ON_ONCE(1);
+	return 100; /* 100 kHz is minimum possible value */
+}
+
+static unsigned long mmc_blk_data_timeout_jiffies(struct mmc_host *host,
+						  struct mmc_data *data)
+{
+	unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
+	unsigned int khz;
+
+	if (data->timeout_clks) {
+		khz = mmc_blk_clock_khz(host);
+		ms += DIV_ROUND_UP(data->timeout_clks, khz);
+	}
+
+	return msecs_to_jiffies(ms);
+}
+
+static int mmc_blk_card_stuck(struct mmc_card *card, struct request *req,
+			      u32 *resp_errs)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_data *data = &mqrq->brq.data;
+	unsigned long timeout;
+	u32 status;
+	int err;
+
+	timeout = jiffies + mmc_blk_data_timeout_jiffies(card->host, data);
+
+	while (1) {
+		bool done = time_after(jiffies, timeout);
+
+		err = __mmc_send_status(card, &status, 5);
+		if (err) {
+			pr_err("%s: error %d requesting status\n",
+			       req->rq_disk->disk_name, err);
+			break;
+		}
+
+		/* Accumulate any response error bits seen */
+		if (resp_errs)
+			*resp_errs |= status;
+
+		if (mmc_blk_in_tran_state(status))
+			break;
+
+		/* Timeout if the device never becomes ready */
+		if (done) {
+			pr_err("%s: Card stuck in wrong state! %s %s\n",
+				mmc_hostname(card->host),
+				req->rq_disk->disk_name, __func__);
+			err = -ETIMEDOUT;
+			break;
+		}
+	}
+
+	return err;
+}
+
+static int mmc_blk_send_stop(struct mmc_card *card)
+{
+	struct mmc_command cmd = {
+		.opcode = MMC_STOP_TRANSMISSION,
+		.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
+	};
+
+	return mmc_wait_for_cmd(card->host, &cmd, 5);
+}
+
+static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
+{
+	int err;
+
+	mmc_retune_hold_now(card->host);
+
+	mmc_blk_send_stop(card);
+
+	err = mmc_blk_card_stuck(card, req, NULL);
+
+	mmc_retune_release(card->host);
+
+	return err;
+}
+
+static void mmc_blk_rw_recovery(struct mmc_queue *mq, struct request *req)
+{
+	int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_blk_request *brq = &mqrq->brq;
+	struct mmc_blk_data *md = mq->blkdata;
+	struct mmc_card *card = mq->card;
+	u32 status;
+	u32 blocks;
+	int err;
+
+	/*
+	 * Status error bits might get lost during re-tuning so don't allow
+	 * re-tuning yet.
+	 */
+	mmc_retune_hold_now(card->host);
+
+	/*
+	 * Some errors the host driver might not have seen. Set the number of
+	 * bytes transferred to zero in that case.
+	 */
+	err = __mmc_send_status(card, &status, 0);
+	if (err || mmc_blk_status_error(req, status))
+		brq->data.bytes_xfered = 0;
+
+	mmc_retune_release(card->host);
+
+	/*
+	 * Try again to get the status. This also provides an opportunity for
+	 * re-tuning.
+	 */
+	if (err)
+		err = __mmc_send_status(card, &status, 0);
+
+	/*
+	 * Nothing more to do after the number of bytes transferred has been
+	 * updated and there is no card.
+	 */
+	if (err && mmc_detect_card_removed(card->host))
+		return;
+
+	/* Try to get back to "tran" state */
+	if (err || !mmc_blk_in_tran_state(status))
+		err = mmc_blk_fix_state(mq->card, req);
+
+	/*
+	 * Special case for SD cards where the card might record the number of
+	 * blocks written.
+	 */
+	if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
+	    rq_data_dir(req) == WRITE && !mmc_sd_num_wr_blocks(card, &blocks))
+		brq->data.bytes_xfered = blocks << 9;
+
+	/* Reset if the card is in a bad state */
+	if (err && mmc_blk_reset(md, card->host, type)) {
+		pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
+		mqrq->retries = MMC_NO_RETRIES;
+		return;
+	}
+
+	/*
+	 * If anything was done, just return and if there is anything remaining
+	 * on the request it will get requeued.
+	 */
+	if (brq->data.bytes_xfered)
+		return;
+
+	/* Reset before last retry */
+	if (mqrq->retries + 1 == MMC_MAX_RETRIES)
+		mmc_blk_reset(md, card->host, type);
+
+	/* Command errors fail fast, so use all MMC_MAX_RETRIES */
+	if (brq->sbc.error || brq->cmd.error)
+		return;
+
+	/* Reduce the remaining retries for data errors */
+	if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
+		mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
+		return;
+	}
+
+	/* FIXME: Missing single sector read for large sector size */
+	if (rq_data_dir(req) == READ && !mmc_large_sector(card)) {
+		/* Read one sector at a time */
+		mmc_blk_ss_read(mq, req);
+		return;
+	}
+}
+
+static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
+{
+	mmc_blk_eval_resp_error(brq);
+
+	return brq->sbc.error || brq->cmd.error || brq->stop.error ||
+	       brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
+}
+
+static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	u32 status = 0;
+	int err;
+
+	if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
+		return 0;
+
+	mmc_retune_hold_now(card->host);
+
+	err = mmc_blk_card_stuck(card, req, &status);
+
+	mmc_retune_release(card->host);
+
+	/*
+	 * Do not assume data transferred correctly if there are any error bits
+	 * set.
+	 */
+	if (!err && status & mmc_blk_stop_err_bits(&mqrq->brq)) {
+		mqrq->brq.data.bytes_xfered = 0;
+		err = -EIO;
+	}
+
+	/* Copy the exception bit so it will be seen later on */
+	if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
+		mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
+
+	return err;
+}
+
+static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
+					    struct request *req)
+{
+	int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+
+	mmc_blk_reset_success(mq->blkdata, type);
+}
+
+static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
+
+	if (nr_bytes) {
+		if (blk_update_request(req, BLK_STS_OK, nr_bytes))
+			blk_mq_requeue_request(req, true);
+		else
+			__blk_mq_end_request(req, BLK_STS_OK);
+	} else if (mqrq->retries++ < MMC_MAX_RETRIES) {
+		blk_mq_requeue_request(req, true);
+	} else {
+		if (mmc_card_removed(mq->card))
+			req->rq_flags |= RQF_QUIET;
+		blk_mq_end_request(req, BLK_STS_IOERR);
+	}
+}
+
+static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
+					struct mmc_queue_req *mqrq)
+{
+	return mmc_card_mmc(mq->card) &&
+	       (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
+		mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
+}
+
+static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
+				 struct mmc_queue_req *mqrq)
+{
+	if (mmc_blk_urgent_bkops_needed(mq, mqrq))
+		mmc_start_bkops(mq->card, true);
+}
+
+void mmc_blk_mq_complete(struct request *req)
+{
+	struct mmc_queue *mq = req->q->queuedata;
+
+	mmc_blk_mq_complete_rq(mq, req);
+}
+
+static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
+				       struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_host *host = mq->card->host;
+	bool failed;
+
+	failed = mmc_blk_rq_error(&mqrq->brq) ||
+		 mmc_blk_card_busy(mq->card, req);
+
+	if (!mmc_queue_direct_complete(host))
+		mmc_retune_release(host);
+
+	if (failed)
+		mmc_blk_rw_recovery(mq, req);
+	else
+		mmc_blk_rw_reset_success(mq, req);
+
+	mmc_blk_urgent_bkops(mq, mqrq);
+}
+
+static void mmc_blk_mq_acct_req_done(struct mmc_queue *mq, struct request *req)
+{
+	struct request_queue *q = req->q;
+	unsigned long flags;
+	bool put_card;
+
+	spin_lock_irqsave(q->queue_lock, flags);
+
+	mq->in_flight[mmc_issue_type(mq, req)] -= 1;
+
+	put_card = mmc_tot_in_flight(mq) == 0;
+
+	spin_unlock_irqrestore(q->queue_lock, flags);
+
+	if (put_card)
+		mmc_put_card(mq->card, &mq->ctx);
+}
+
+static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_request *mrq = &mqrq->brq.mrq;
+	struct mmc_host *host = mq->card->host;
+
+	if (host->ops->post_req)
+		host->ops->post_req(host, mrq, 0);
+
+	/*
+	 * Block layer timeouts race with completions which means the normal
+	 * completion path cannot be used during recovery.
+	 */
+	if (mq->in_recovery)
+		mmc_blk_mq_complete_rq(mq, req);
+	else
+		blk_mq_complete_request(req);
+
+	mmc_blk_mq_acct_req_done(mq, req);
+}
+
+void mmc_blk_mq_recovery(struct mmc_queue *mq)
+{
+	struct request *req = mq->recovery_req;
+
+	mq->recovery_req = NULL;
+	mq->rw_wait = false;
+
+	mmc_blk_mq_poll_completion(mq, req);
+
+	mmc_blk_mq_post_req(mq, req);
+}
+
+static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
+					 struct request **prev_req)
+{
+	if (mmc_queue_direct_complete(mq->card->host))
+		return;
+
+	mutex_lock(&mq->complete_lock);
+
+	if (!mq->complete_req)
+		goto out_unlock;
+
+	mmc_blk_mq_poll_completion(mq, mq->complete_req);
+
+	if (prev_req)
+		*prev_req = mq->complete_req;
+	else
+		mmc_blk_mq_post_req(mq, mq->complete_req);
+
+	mq->complete_req = NULL;
+
+out_unlock:
+	mutex_unlock(&mq->complete_lock);
+}
+
+void mmc_blk_mq_complete_work(struct work_struct *work)
+{
+	struct mmc_queue *mq = container_of(work, struct mmc_queue,
+					    complete_work);
+
+	mmc_blk_mq_complete_prev_req(mq, NULL);
+}
+
+static void mmc_blk_mq_req_done(struct mmc_request *mrq)
+{
+	struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
+						  brq.mrq);
+	struct request *req = mmc_queue_req_to_req(mqrq);
+	struct request_queue *q = req->q;
+	struct mmc_queue *mq = q->queuedata;
+	struct mmc_host *host = mq->card->host;
+	unsigned long flags;
+
+	if (mmc_blk_rq_error(&mqrq->brq) ||
+	    mmc_blk_urgent_bkops_needed(mq, mqrq)) {
+		spin_lock_irqsave(q->queue_lock, flags);
+		mq->recovery_needed = true;
+		mq->recovery_req = req;
+		spin_unlock_irqrestore(q->queue_lock, flags);
+		wake_up(&mq->wait);
+		schedule_work(&mq->recovery_work);
+		return;
+	}
+
+	if (!mmc_queue_direct_complete(host)) {
+		bool waiting;
+
+		spin_lock_irqsave(q->queue_lock, flags);
+		mq->complete_req = req;
+		mq->rw_wait = false;
+		waiting = mq->waiting;
+		spin_unlock_irqrestore(q->queue_lock, flags);
+
+		if (waiting)
+			wake_up(&mq->wait);
+		else
+			kblockd_schedule_work(&mq->complete_work);
+
+		return;
+	}
+
+	mmc_blk_rw_reset_success(mq, req);
+
+	mq->rw_wait = false;
+	wake_up(&mq->wait);
+
+	mmc_blk_mq_post_req(mq, req);
+}
+
+static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
+{
+	struct request_queue *q = mq->queue;
+	unsigned long flags;
+	bool done;
+
+	spin_lock_irqsave(q->queue_lock, flags);
+	if (mq->recovery_needed) {
+		*err = -EBUSY;
+		done = true;
+	} else {
+		done = !mq->rw_wait;
+	}
+	mq->waiting = !done;
+	spin_unlock_irqrestore(q->queue_lock, flags);
+
+	return done;
+}
+
+static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
+{
+	int err = 0;
+
+	wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
+
+	mmc_blk_mq_complete_prev_req(mq, prev_req);
+
+	return err;
+}
+
+static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
+				  struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_host *host = mq->card->host;
+	struct request *prev_req = NULL;
+	int err = 0;
+
+	mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
+
+	mqrq->brq.mrq.done = mmc_blk_mq_req_done;
+
+	if (host->ops->pre_req)
+		host->ops->pre_req(host, &mqrq->brq.mrq);
+
+	err = mmc_blk_rw_wait(mq, &prev_req);
+	if (err)
+		goto out_post_req;
+
+	mq->rw_wait = true;
+
+	err = mmc_start_request(host, &mqrq->brq.mrq);
+
+	if (prev_req)
+		mmc_blk_mq_post_req(mq, prev_req);
+
+	if (err)
+		mq->rw_wait = false;
+
+	/* Release re-tuning here where there is no synchronization required */
+	if (mmc_queue_direct_complete(host))
+		mmc_retune_release(host);
+
+out_post_req:
+	if (err && host->ops->post_req)
+		host->ops->post_req(host, &mqrq->brq.mrq, err);
+
+	return err;
+}
+
+static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
+{
+	return mmc_blk_rw_wait(mq, NULL);
+}
+
+enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_blk_data *md = mq->blkdata;
+	struct mmc_card *card = md->queue.card;
+	struct mmc_host *host = card->host;
+	int ret;
+
+	ret = mmc_blk_part_switch(card, md->part_type);
+	if (ret)
+		return MMC_REQ_FAILED_TO_START;
+
+	switch (mmc_issue_type(mq, req)) {
+	case MMC_ISSUE_SYNC:
+		ret = mmc_blk_wait_for_idle(mq, host);
+		if (ret)
+			return MMC_REQ_BUSY;
+		switch (req_op(req)) {
+		case REQ_OP_DRV_IN:
+		case REQ_OP_DRV_OUT:
+			mmc_blk_issue_drv_op(mq, req);
+			break;
+		case REQ_OP_DISCARD:
+			mmc_blk_issue_discard_rq(mq, req);
+			break;
+		case REQ_OP_SECURE_ERASE:
+			mmc_blk_issue_secdiscard_rq(mq, req);
+			break;
+		case REQ_OP_FLUSH:
+			mmc_blk_issue_flush(mq, req);
+			break;
+		default:
+			WARN_ON_ONCE(1);
+			return MMC_REQ_FAILED_TO_START;
+		}
+		return MMC_REQ_FINISHED;
+	case MMC_ISSUE_ASYNC:
+		switch (req_op(req)) {
+		case REQ_OP_READ:
+		case REQ_OP_WRITE:
+			ret = mmc_blk_mq_issue_rw_rq(mq, req);
+			break;
+		default:
+			WARN_ON_ONCE(1);
+			ret = -EINVAL;
+		}
+		if (!ret)
+			return MMC_REQ_STARTED;
+		return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
+	default:
+		WARN_ON_ONCE(1);
+		return MMC_REQ_FAILED_TO_START;
+	}
+}
+
 static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
 			       struct mmc_blk_request *brq, struct request *req,
 			       bool old_req_pending)
drivers/mmc/core/block.h +10 -0
index 860ca7c8df86..b145ed81402b 100644
@@ -6,4 +6,14 @@
 
 void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req);
 
+enum mmc_issued;
+
+enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req);
+void mmc_blk_mq_complete(struct request *req);
+void mmc_blk_mq_recovery(struct mmc_queue *mq);
+
+struct work_struct;
+
+void mmc_blk_mq_complete_work(struct work_struct *work);
+
 #endif
drivers/mmc/core/queue.c +279 -23

375 lines not on this page · raw message

drivers/mmc/core/queue.h +41 -0

69 lines not on this page · raw message

include/linux/mmc/host.h +1 -0
index ce2075d6f429..4b68a95a8818 100644
@@ -324,6 +324,7 @@ struct mmc_host {
 #define MMC_CAP_DRIVER_TYPE_A	(1 << 23)	/* Host supports Driver Type A */
 #define MMC_CAP_DRIVER_TYPE_C	(1 << 24)	/* Host supports Driver Type C */
 #define MMC_CAP_DRIVER_TYPE_D	(1 << 25)	/* Host supports Driver Type D */
+#define MMC_CAP_DIRECT_COMPLETE	(1 << 27)	/* RW reqs can be completed within mmc_request_done() */
 #define MMC_CAP_CD_WAKE		(1 << 28)	/* Enable card detect wake */
 #define MMC_CAP_CMD_DURING_TFR	(1 << 29)	/* Commands during data transfer */
 #define MMC_CAP_CMD23		(1 << 30)	/* CMD23 supported. */
Signature · 1 line
1.9.1
author maintainer
#6 lore raw

PATCH v12 3/5 mmc: block: Add CQE support

In reply to Adrian Hunter

Add CQE support to the block driver, including:
- optionally using DCMD for flush requests
- "manually" issuing discard requests
- issuing read / write requests to the CQE
- supporting block-layer timeouts
- handling recovery
- supporting re-tuning

CQE offers 25% - 50% better random multi-threaded I/O. There is a slight (e.g. 2%) drop in sequential read speed but no observable change to sequential write.

CQE automatically sends the commands to complete requests. However it only supports reads / writes and so-called "direct commands" (DCMD). Furthermore DCMD is limited to one command at a time, but discards require 3 commands. That makes issuing discards through CQE very awkward, but some CQE's don't support DCMD anyway. So for discards, the existing non-CQE approach is taken, where the mmc core code issues the 3 commands one at a time i.e. mmc_erase(). Where DCMD is used, is for issuing flushes.

---
 drivers/mmc/core/block.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/mmc/core/block.h |   2 +
 drivers/mmc/core/queue.c | 134 ++++++++++++++++++++++++++++++++++++++++--
 drivers/mmc/core/queue.h |  15 +++++
 4 files changed, 294 insertions(+), 7 deletions(-)
drivers/mmc/core/block.c +148 -2
index 002446e8dc5d..5a1fa678667b 100644
@@ -112,6 +112,7 @@ struct mmc_blk_data {
 #define MMC_BLK_WRITE		BIT(1)
 #define MMC_BLK_DISCARD		BIT(2)
 #define MMC_BLK_SECDISCARD	BIT(3)
+#define MMC_BLK_CQE_RECOVERY	BIT(4)
 
 	/*
 	 * Only set in main mmc_blk_data associated
@@ -1713,6 +1714,138 @@ static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
 		*do_data_tag_p = do_data_tag;
 }
 
+#define MMC_CQE_RETRIES 2
+
+static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_request *mrq = &mqrq->brq.mrq;
+	struct request_queue *q = req->q;
+	struct mmc_host *host = mq->card->host;
+	unsigned long flags;
+	bool put_card;
+	int err;
+
+	mmc_cqe_post_req(host, mrq);
+
+	if (mrq->cmd && mrq->cmd->error)
+		err = mrq->cmd->error;
+	else if (mrq->data && mrq->data->error)
+		err = mrq->data->error;
+	else
+		err = 0;
+
+	if (err) {
+		if (mqrq->retries++ < MMC_CQE_RETRIES)
+			blk_mq_requeue_request(req, true);
+		else
+			blk_mq_end_request(req, BLK_STS_IOERR);
+	} else if (mrq->data) {
+		if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
+			blk_mq_requeue_request(req, true);
+		else
+			__blk_mq_end_request(req, BLK_STS_OK);
+	} else {
+		blk_mq_end_request(req, BLK_STS_OK);
+	}
+
+	spin_lock_irqsave(q->queue_lock, flags);
+
+	mq->in_flight[mmc_issue_type(mq, req)] -= 1;
+
+	put_card = mmc_tot_in_flight(mq) == 0;
+
+	mmc_cqe_check_busy(mq);
+
+	spin_unlock_irqrestore(q->queue_lock, flags);
+
+	if (!mq->cqe_busy)
+		blk_mq_run_hw_queues(q, true);
+
+	if (put_card)
+		mmc_put_card(mq->card, &mq->ctx);
+}
+
+void mmc_blk_cqe_recovery(struct mmc_queue *mq)
+{
+	struct mmc_card *card = mq->card;
+	struct mmc_host *host = card->host;
+	int err;
+
+	pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
+
+	err = mmc_cqe_recovery(host);
+	if (err)
+		mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
+	else
+		mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
+
+	pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
+}
+
+static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
+{
+	struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
+						  brq.mrq);
+	struct request *req = mmc_queue_req_to_req(mqrq);
+	struct request_queue *q = req->q;
+	struct mmc_queue *mq = q->queuedata;
+
+	/*
+	 * Block layer timeouts race with completions which means the normal
+	 * completion path cannot be used during recovery.
+	 */
+	if (mq->in_recovery)
+		mmc_blk_cqe_complete_rq(mq, req);
+	else
+		blk_mq_complete_request(req);
+}
+
+static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
+{
+	mrq->done		= mmc_blk_cqe_req_done;
+	mrq->recovery_notifier	= mmc_cqe_recovery_notifier;
+
+	return mmc_cqe_start_req(host, mrq);
+}
+
+static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
+						 struct request *req)
+{
+	struct mmc_blk_request *brq = &mqrq->brq;
+
+	memset(brq, 0, sizeof(*brq));
+
+	brq->mrq.cmd = &brq->cmd;
+	brq->mrq.tag = req->tag;
+
+	return &brq->mrq;
+}
+
+static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+	struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
+
+	mrq->cmd->opcode = MMC_SWITCH;
+	mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
+			(EXT_CSD_FLUSH_CACHE << 16) |
+			(1 << 8) |
+			EXT_CSD_CMD_SET_NORMAL;
+	mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
+
+	return mmc_blk_cqe_start_req(mq->card->host, mrq);
+}
+
+static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
+{
+	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+
+	mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
+
+	return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
+}
+
 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
 			       struct mmc_card *card,
 			       int disable_multi,
@@ -2125,7 +2258,10 @@ void mmc_blk_mq_complete(struct request *req)
 {
 	struct mmc_queue *mq = req->q->queuedata;
 
-	mmc_blk_mq_complete_rq(mq, req);
+	if (mq->use_cqe)
+		mmc_blk_cqe_complete_rq(mq, req);
+	else
+		mmc_blk_mq_complete_rq(mq, req);
 }
 
 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
@@ -2350,6 +2486,9 @@ static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
 
 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
 {
+	if (mq->use_cqe)
+		return host->cqe_ops->cqe_wait_for_idle(host);
+
 	return mmc_blk_rw_wait(mq, NULL);
 }
 
@@ -2388,11 +2527,18 @@ enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
 			return MMC_REQ_FAILED_TO_START;
 		}
 		return MMC_REQ_FINISHED;
+	case MMC_ISSUE_DCMD:
 	case MMC_ISSUE_ASYNC:
 		switch (req_op(req)) {
+		case REQ_OP_FLUSH:
+			ret = mmc_blk_cqe_issue_flush(mq, req);
+			break;
 		case REQ_OP_READ:
 		case REQ_OP_WRITE:
-			ret = mmc_blk_mq_issue_rw_rq(mq, req);
+			if (mq->use_cqe)
+				ret = mmc_blk_cqe_issue_rw_rq(mq, req);
+			else
+				ret = mmc_blk_mq_issue_rw_rq(mq, req);
 			break;
 		default:
 			WARN_ON_ONCE(1);
drivers/mmc/core/block.h +2 -0
index b145ed81402b..5ad22c1c0318 100644
@@ -6,6 +6,8 @@
 
 void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req);
 
+void mmc_blk_cqe_recovery(struct mmc_queue *mq);
+
 enum mmc_issued;
 
 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req);
drivers/mmc/core/queue.c +129 -5

198 lines not on this page · raw message

drivers/mmc/core/queue.h +15 -0
index 00b4ce2ce83c..9bbfbb1fad7b 100644
@@ -16,6 +16,7 @@ enum mmc_issued {
 
 enum mmc_issue_type {
 	MMC_ISSUE_SYNC,
+	MMC_ISSUE_DCMD,
 	MMC_ISSUE_ASYNC,
 	MMC_ISSUE_MAX,
 };
@@ -91,6 +92,10 @@ struct mmc_queue {
 	int			qcnt;
 
 	int			in_flight[MMC_ISSUE_MAX];
+	unsigned int		cqe_busy;
+#define MMC_CQE_DCMD_BUSY	BIT(0)
+#define MMC_CQE_QUEUE_FULL	BIT(1)
+	bool			use_cqe;
 	bool			recovery_needed;
 	bool			in_recovery;
 	bool			rw_wait;
@@ -111,11 +116,21 @@ extern int mmc_init_queue(struct mmc_queue *, struct mmc_card *, spinlock_t *,
 extern unsigned int mmc_queue_map_sg(struct mmc_queue *,
 				     struct mmc_queue_req *);
 
+void mmc_cqe_check_busy(struct mmc_queue *mq);
+void mmc_cqe_recovery_notifier(struct mmc_request *mrq);
+
 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req);
 
 static inline int mmc_tot_in_flight(struct mmc_queue *mq)
 {
 	return mq->in_flight[MMC_ISSUE_SYNC] +
+	       mq->in_flight[MMC_ISSUE_DCMD] +
+	       mq->in_flight[MMC_ISSUE_ASYNC];
+}
+
+static inline int mmc_cqe_qcnt(struct mmc_queue *mq)
+{
+	return mq->in_flight[MMC_ISSUE_DCMD] +
 	       mq->in_flight[MMC_ISSUE_ASYNC];
 }
 
Signature · 1 line
1.9.1
author maintainer
#8 lore raw

PATCH v12 5/5 mmc: sdhci-pci: Add CQHCI support for Intel GLK

In reply to Adrian Hunter

Add CQHCI initialization and implement CQHCI operations for Intel GLK.

---
 drivers/mmc/host/Kconfig          |   1 +
 drivers/mmc/host/sdhci-pci-core.c | 155 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/mmc/host/Kconfig +1 -0
index 3092b7085cb5..2b02a9788bb6 100644
@@ -81,6 +81,7 @@ config MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER
 config MMC_SDHCI_PCI
 	tristate "SDHCI support on PCI bus"
 	depends on MMC_SDHCI && PCI
+	select MMC_CQHCI
 	help
 	  This selects the PCI Secure Digital Host Controller Interface.
 	  Most controllers found today are PCI devices.
drivers/mmc/host/sdhci-pci-core.c +154 -1

203 lines not on this page · raw message

Signature · 1 line
1.9.1
author maintainer
#1 lore raw

PATCH v12 1/5 mmc: core: Add parameter use_blk_mq

In reply to Adrian Hunter

Until mmc has blk-mq support fully implemented and tested, add a parameter use_blk_mq, default to false unless config option MMC_MQ_DEFAULT is selected.

---
 drivers/mmc/Kconfig      | 11 +++++++++++
 drivers/mmc/core/core.c  |  7 +++++++
 drivers/mmc/core/core.h  |  2 ++
 drivers/mmc/core/host.c  |  2 ++
 drivers/mmc/core/host.h  |  4 ++++
 include/linux/mmc/host.h |  1 +
 6 files changed, 27 insertions(+)
drivers/mmc/Kconfig +11 -0
index ec21388311db..98202934bd29 100644
@@ -12,6 +12,17 @@ menuconfig MMC
 	  If you want MMC/SD/SDIO support, you should say Y here and
 	  also to your specific host controller driver.
 
+config MMC_MQ_DEFAULT
+	bool "MMC: use blk-mq I/O path by default"
+	depends on MMC && BLOCK
+	---help---
+	  This option enables the new blk-mq based I/O path for MMC block
+	  devices by default.  With the option the mmc_core.use_blk_mq
+	  module/boot option defaults to Y, without it to N, but it can
+	  still be overridden either way.
+
+	  If unsure say N.
+
 if MMC
 
 source "drivers/mmc/core/Kconfig"
drivers/mmc/core/core.c +7 -0
index 1f0f44f4dd5f..5df03cb73be7 100644
@@ -66,6 +66,13 @@
 bool use_spi_crc = 1;
 module_param(use_spi_crc, bool, 0);
 
+#ifdef CONFIG_MMC_MQ_DEFAULT
+bool mmc_use_blk_mq = true;
+#else
+bool mmc_use_blk_mq = false;
+#endif
+module_param_named(use_blk_mq, mmc_use_blk_mq, bool, S_IWUSR | S_IRUGO);
+
 static int mmc_schedule_delayed_work(struct delayed_work *work,
 				     unsigned long delay)
 {
drivers/mmc/core/core.h +2 -0
index 71e6c6d7ceb7..8c5dd8d31400 100644
@@ -35,6 +35,8 @@ struct mmc_bus_ops {
 	int (*reset)(struct mmc_host *);
 };
 
+extern bool mmc_use_blk_mq;
+
 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
 void mmc_detach_bus(struct mmc_host *host);
 
drivers/mmc/core/host.c +2 -0
index 35a9e4fd1a9f..62ef6cb0ece4 100644
@@ -404,6 +404,8 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 
 	host->fixed_drv_type = -EINVAL;
 
+	host->use_blk_mq = mmc_use_blk_mq;
+
 	return host;
 }
 
drivers/mmc/core/host.h +4 -0
index fb689a1065ed..6eaf558e62d6 100644
@@ -74,6 +74,10 @@ static inline bool mmc_card_hs400es(struct mmc_card *card)
 	return card->host->ios.enhanced_strobe;
 }
 
+static inline bool mmc_host_use_blk_mq(struct mmc_host *host)
+{
+	return host->use_blk_mq;
+}
 
 #endif
 
include/linux/mmc/host.h +1 -0
index e7743eca1021..ce2075d6f429 100644
@@ -380,6 +380,7 @@ struct mmc_host {
 	unsigned int		doing_retune:1;	/* re-tuning in progress */
 	unsigned int		retune_now:1;	/* do re-tuning at next req */
 	unsigned int		retune_paused:1; /* re-tuning is temporarily disabled */
+	unsigned int		use_blk_mq:1;	/* use blk-mq */
 
 	int			rescan_disable;	/* disable card detection */
 	int			rescan_entered;	/* used with nonremovable devices */
Signature · 1 line
1.9.1
author maintainer
#7 lore raw

PATCH v12 4/5 mmc: cqhci: support for command queue enabled host

In reply to Adrian Hunter

From: Venkat Gopalakrishnan <[email protected]>

This patch adds CMDQ support for command-queue compatible hosts.

Command queue is added in eMMC-5.1 specification. This enables the controller to process upto 32 requests at a time.

Adrian Hunter contributed renaming to cqhci, recovery, suspend and resume, cqhci_off, cqhci_wait_for_idle, and external timeout handling.

---
 drivers/mmc/host/Kconfig  |   13 +
 drivers/mmc/host/Makefile |    1 +
 drivers/mmc/host/cqhci.c  | 1150 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/host/cqhci.h  |  240 ++++++++++
 4 files changed, 1404 insertions(+)
 create mode 100644 drivers/mmc/host/cqhci.c
 create mode 100644 drivers/mmc/host/cqhci.h
drivers/mmc/host/Kconfig +13 -0
index 567028c9219a..3092b7085cb5 100644
@@ -857,6 +857,19 @@ config MMC_SUNXI
 	  This selects support for the SD/MMC Host Controller on
 	  Allwinner sunxi SoCs.
 
+config MMC_CQHCI
+	tristate "Command Queue Host Controller Interface support"
+	depends on HAS_DMA
+	help
+	  This selects the Command Queue Host Controller Interface (CQHCI)
+	  support present in host controllers of Qualcomm Technologies, Inc
+	  amongst others.
+	  This controller supports eMMC devices with command queue support.
+
+	  If you have a controller with this interface, say Y or M here.
+
+	  If unsure, say N.
+
 config MMC_TOSHIBA_PCI
 	tristate "Toshiba Type A SD/MMC Card Interface Driver"
 	depends on PCI
drivers/mmc/host/Makefile +1 -0
index ab61a3e39c0b..de140e3ef402 100644
@@ -91,6 +91,7 @@ obj-$(CONFIG_MMC_SDHCI_ST)		+= sdhci-st.o
 obj-$(CONFIG_MMC_SDHCI_MICROCHIP_PIC32)	+= sdhci-pic32.o
 obj-$(CONFIG_MMC_SDHCI_BRCMSTB)		+= sdhci-brcmstb.o
 obj-$(CONFIG_MMC_SDHCI_OMAP)		+= sdhci-omap.o
+obj-$(CONFIG_MMC_CQHCI)			+= cqhci.o
 
 ifeq ($(CONFIG_CB710_DEBUG),y)
 	CFLAGS-cb710-mmc	+= -DDEBUG
drivers/mmc/host/cqhci.c new file +1150 -0

1,150 lines not on this page · raw message

drivers/mmc/host/cqhci.h new file +240 -0

240 lines not on this page · raw message

Signature · 1 line
1.9.1
#9 lore raw
In reply to Adrian Hunter
Quoted 3 lines · Adrian Hunter

Here is V12 of the hardware command queue patches without the software command queue patches, now using blk-mq and now with blk-mq support for non-CQE I/O.

Since I had my test setup going I gave this a spin with the same set of tests that I used before/after my MQ patches.

It is using the same setup and same eMMC, but I hade to rebase onto Ulf's very latest next branch to apply your patches.

I default-enabled multiqueue.

Results:

sync
echo 3 > /proc/sys/vm/drop_caches
sync
time dd if=/dev/mmcblk3 of=/dev/null bs=1M count=1024 1024+0 records in
1024+0 records out
1073741824 bytes (1.0GB) copied, 24.251922 seconds, 42.2MB/s

real    0m 24.25s
user    0m 0.03s
sys     0m 3.80s

mount /dev/mmcblk3p1 /mnt/
cd /mnt/
sync
echo 3 > /proc/sys/vm/drop_caches
sync
time find . > /dev/null

real    0m 3.24s
user    0m 0.22s
sys     0m 1.23s

sync
echo 3 > /proc/sys/vm/drop_caches
sync
iozone -az -i0 -i1 -i2 -s 20m -I -f /mnt/foo.test

random random
kB reclen write rewrite read reread read write

20480       4     1615     1571     6612     6714     6494      531
20480       8     2143     2295    11559    11563    11499     1164
20480      16     3894     4202    17826    17823    17755     1369
20480      32     5816     7489    23741    23759    23709     3016
20480      64     7393     9167    27532    27526    27502     3591
20480     128     7328     8097    29184    29161    29159     5592
20480     256     7194     8752    29424    29434    29424     6700
20480     512     8984     9930    29903    29911    29909     7420
20480    1024     7072     7446    27684    27685    27681     7444
20480    2048     6840     8199    27398    27420    27418     6766
20480    4096     8137     6805    28091    28089    28093     8209
20480    8192     7255     7485    28386    28384    28383     7479
20480   16384     7078     7448    28584    28585    28585     7447

In short: no performance regressions.

Performance-wise this is on par with my own patch set for MQ.

As you know my pet peeve is "enable MQ by default" and I see no reason from a performance perspective not to enable MQ by default on this patch set or mine for that matter.

Quoted 4 lines

While we should look at changing blk-mq to give better workqueue performance, a bigger gain is likely to be made by adding a new host API to enable the next already-prepared request to be issued directly from within ->done() callback of the current request.

My patch series switches the stack around to make it possible to do this. But it doesn't go the whole way to complete the requests from interrupt context.

Since we have to send commands for retune etc request finalization cannot easily be done from interrupt context.

But I am thinking about testing to hack it
using some ugly approaches ... like assuming we don't need any retune etc and just say all is fine and optimistically complete the request directly in the interrupt handler if all was OK and wait for errors to happen before retuning.

Yours,
Linus Walleij

author maintainer
#10 lore raw
Quoted 58 lines · Linus Walleij
Quoted 3 lines · Adrian Hunter

Here is V12 of the hardware command queue patches without the software command queue patches, now using blk-mq and now with blk-mq support for non-CQE I/O.

Since I had my test setup going I gave this a spin with the same set of tests that I used before/after my MQ patches.

It is using the same setup and same eMMC, but I hade to rebase onto Ulf's very latest next branch to apply your patches.

I default-enabled multiqueue.

Results:

sync
echo 3 > /proc/sys/vm/drop_caches
sync
time dd if=/dev/mmcblk3 of=/dev/null bs=1M count=1024 1024+0 records in
1024+0 records out
1073741824 bytes (1.0GB) copied, 24.251922 seconds, 42.2MB/s

real    0m 24.25s
user    0m 0.03s
sys     0m 3.80s

mount /dev/mmcblk3p1 /mnt/
cd /mnt/
sync
echo 3 > /proc/sys/vm/drop_caches
sync
time find . > /dev/null

real    0m 3.24s
user    0m 0.22s
sys     0m 1.23s

sync
echo 3 > /proc/sys/vm/drop_caches
sync
iozone -az -i0 -i1 -i2 -s 20m -I -f /mnt/foo.test

random random
kB reclen write rewrite read reread read write

20480       4     1615     1571     6612     6714     6494      531
20480       8     2143     2295    11559    11563    11499     1164
20480      16     3894     4202    17826    17823    17755     1369
20480      32     5816     7489    23741    23759    23709     3016
20480      64     7393     9167    27532    27526    27502     3591
20480     128     7328     8097    29184    29161    29159     5592
20480     256     7194     8752    29424    29434    29424     6700
20480     512     8984     9930    29903    29911    29909     7420
20480    1024     7072     7446    27684    27685    27681     7444
20480    2048     6840     8199    27398    27420    27418     6766
20480    4096     8137     6805    28091    28089    28093     8209
20480    8192     7255     7485    28386    28384    28383     7479
20480   16384     7078     7448    28584    28585    28585     7447
Quoted 1 line

In short: no performance regressions.

You really need to test cards that are fast. A decent UHS-I SD card can do over 80 MB/s for reads and of course HS400 eMMC can do over 300 MB/s.

Quoted 6 lines

Performance-wise this is on par with my own patch set for MQ.

As you know my pet peeve is "enable MQ by default" and I see no reason from a performance perspective not to enable MQ by default on this patch set or mine for that matter.

That is a side-issue. A single small patch can change that.

Quoted 6 lines
Quoted 5 lines

While we should look at changing blk-mq to give better workqueue performance, a bigger gain is likely to be made by adding a new host API to enable the next already-prepared request to be issued directly from within ->done() callback of the current request.

Quoted 6 lines

My patch series switches the stack around to make it possible to do this. But it doesn't go the whole way to complete the requests from interrupt context.

Since we have to send commands for retune etc request finalization cannot easily be done from interrupt context.

Re-tuning and background operations are rare and slow, so there is no reason to try to start them from interrupt context.

Quoted 6 lines

But I am thinking about testing to hack it
using some ugly approaches ... like assuming we don't need any retune etc and just say all is fine and optimistically complete the request directly in the interrupt handler if all was OK and wait for errors to happen before retuning.

It already works that way. Re-tuning happens before you start a request. We prevent re-tuning in between dependent requests, like between starting a transfer and CMD13 polling for completion.

#11 lore raw
Quoted 11 lines · Adrian Hunter
Quoted 6 lines · Linus Walleij

My patch series switches the stack around to make it possible to do this. But it doesn't go the whole way to complete the requests from interrupt context.

Since we have to send commands for retune etc request finalization cannot easily be done from interrupt context.

Re-tuning and background operations are rare and slow, so there is no reason to try to start them from interrupt context.

OK I will try to get them out of the way and see what happens, hehe :)

What I mean is that we were checking - on every command - if BKOPS or retune needs to happen. And then doing it. Thus all was done in process context.

Quoted 9 lines
Quoted 5 lines

But I am thinking about testing to hack it
using some ugly approaches ... like assuming we don't need any retune etc and just say all is fine and optimistically complete the request directly in the interrupt handler if all was OK and wait for errors to happen before retuning.

It already works that way. Re-tuning happens before you start a request. We prevent re-tuning in between dependent requests, like between starting a transfer and CMD13 polling for completion.

Ah that is what these if()s do ... right. I'll see if I can get around this then.

Yours,
Linus Walleij

#3 lore raw

Re: [PATCH V12 2/5] mmc: block: Add blk-mq support

In reply to Adrian Hunter
Quoted 68 lines · Adrian Hunter

Define and use a blk-mq queue. Discards and flushes are processed synchronously, but reads and writes asynchronously. In order to support slow DMA unmapping, DMA unmapping is not done until after the next request is started. That means the request is not completed until then. If there is no next request then the completion is done by queued work.

---
 drivers/mmc/core/block.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/mmc/core/block.h |  10 +
 drivers/mmc/core/queue.c | 302 ++++++++++++++++++++--
 drivers/mmc/core/queue.h |  41 +++
 include/linux/mmc/host.h |   1 +
 5 files changed, 979 insertions(+), 30 deletions(-)
drivers/mmc/core/block.c +16 -4
index ea80ff4cd7f9..002446e8dc5d 100644
@@ -1264,7 +1264,10 @@ static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
                break;
        }
        mq_rq->drv_op_result = ret;
-       blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       if (req->mq_ctx)
+               blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       else
+               blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
 }
 
 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
@@ -1307,7 +1310,10 @@ static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
        else
                mmc_blk_reset_success(md, type);
 fail:
-       blk_end_request(req, status, blk_rq_bytes(req));
+       if (req->mq_ctx)
+               blk_mq_end_request(req, status);
+       else
+               blk_end_request(req, status, blk_rq_bytes(req));
 }
 
 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
@@ -1377,7 +1383,10 @@ static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
        if (!err)
                mmc_blk_reset_success(md, type);
 out:
-       blk_end_request(req, status, blk_rq_bytes(req));
+       if (req->mq_ctx)
+               blk_mq_end_request(req, status);
+       else
+               blk_end_request(req, status, blk_rq_bytes(req));
 }
 
 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
@@ -1387,7 +1396,10 @@ static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
        int ret = 0;
 
        ret = mmc_flush_cache(card);
-       blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       if (req->mq_ctx)
+               blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       else
+               blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
 }
 
 /*

drivers/mmc/core/block.c static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, · line 1429

Quoted 8 lines
drivers/mmc/core/block.c +2 -3
@@ -1413,15 +1425,18 @@ static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
        }
 }
 
-#define CMD_ERRORS                                                     \
-       (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
-        R1_ADDRESS_ERROR |     /* Misaligned address */                \
+#define CMD_ERRORS_EXCL_OOR                                            \
+       (R1_ADDRESS_ERROR |     /* Misaligned address */                \

This looks unrelated to blkmq support.

drivers/mmc/core/block.c static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, · line 1439

Quoted 10 lines
drivers/mmc/core/block.c +4 -0
         R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
         R1_WP_VIOLATION |      /* Tried to write to protected block */ \
         R1_CARD_ECC_FAILED |   /* Card ECC failed */                   \
         R1_CC_ERROR |          /* Card controller error */             \
         R1_ERROR)              /* General/unknown error */
 
+#define CMD_ERRORS                                                     \
+       (CMD_ERRORS_EXCL_OOR |                                          \
+        R1_OUT_OF_RANGE)       /* Command argument out of range */     \
+

Ditto.

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 1786

Quoted 10 lines
drivers/mmc/core/block.c +3 -0
 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
 {
        u32 val;
@@ -1766,6 +1781,632 @@ static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
        mqrq->areq.err_check = mmc_blk_err_check;
 }
 
+#define MMC_MAX_RETRIES                5
+#define MMC_DATA_RETRIES       2
+#define MMC_NO_RETRIES         (MMC_MAX_RETRIES + 1)

What's are these defines about? Do you intend to use different retries for the blkmq case comparing to the legacy request path? If so, why?

Quoted 30 lines
drivers/mmc/core/block.c +30 -0
+
+/* Single sector read during recovery */
+static void mmc_blk_ss_read(struct mmc_queue *mq, struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       blk_status_t status;
+
+       while (1) {
+               mmc_blk_rw_rq_prep(mqrq, mq->card, 1, mq);
+
+               mmc_wait_for_req(mq->card->host, &mqrq->brq.mrq);
+
+               /*
+                * Not expecting command errors, so just give up in that case.
+                * If there are retries remaining, the request will get
+                * requeued.
+                */
+               if (mqrq->brq.cmd.error)
+                       return;
+
+               if (blk_rq_bytes(req) <= 512)
+                       break;
+
+               status = mqrq->brq.data.error ? BLK_STS_IOERR : BLK_STS_OK;
+
+               blk_update_request(req, status, 512);
+       }
+
+       mqrq->retries = MMC_NO_RETRIES;
+}

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 1826

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+
+static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
+{
+       return !!brq->mrq.sbc;
+}
+
+static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
+{
+       return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
+}

Again, this seems like a non blkmq specific thing.

Quoted 252 lines
drivers/mmc/core/block.c +252 -0
+
+static inline bool mmc_blk_in_tran_state(u32 status)
+{
+       /*
+        * Some cards mishandle the status bits, so make sure to check both the
+        * busy indication and the card state.
+        */
+       return status & R1_READY_FOR_DATA &&
+              (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
+}
+
+/*
+ * Check for errors the host controller driver might not have seen such as
+ * response mode errors or invalid card state.
+ */
+static bool mmc_blk_status_error(struct request *req, u32 status)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_blk_request *brq = &mqrq->brq;
+       u32 stop_err_bits = mmc_blk_stop_err_bits(brq);
+
+       return brq->cmd.resp[0]  & CMD_ERRORS    ||
+              brq->stop.resp[0] & stop_err_bits ||
+              status            & stop_err_bits ||
+              (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
+}
+
+static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
+{
+       return !brq->sbc.error && !brq->cmd.error &&
+              !(brq->cmd.resp[0] & CMD_ERRORS);
+}
+
+static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
+{
+       if (host->actual_clock)
+               return host->actual_clock / 1000;
+
+       /* Clock may be subject to a divisor, fudge it by a factor of 2. */
+       if (host->ios.clock)
+               return host->ios.clock / 2000;
+
+       /* How can there be no clock */
+       WARN_ON_ONCE(1);
+       return 100; /* 100 kHz is minimum possible value */
+}
+
+static unsigned long mmc_blk_data_timeout_jiffies(struct mmc_host *host,
+                                                 struct mmc_data *data)
+{
+       unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
+       unsigned int khz;
+
+       if (data->timeout_clks) {
+               khz = mmc_blk_clock_khz(host);
+               ms += DIV_ROUND_UP(data->timeout_clks, khz);
+       }
+
+       return msecs_to_jiffies(ms);
+}
+
+static int mmc_blk_card_stuck(struct mmc_card *card, struct request *req,
+                             u32 *resp_errs)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_data *data = &mqrq->brq.data;
+       unsigned long timeout;
+       u32 status;
+       int err;
+
+       timeout = jiffies + mmc_blk_data_timeout_jiffies(card->host, data);
+
+       while (1) {
+               bool done = time_after(jiffies, timeout);
+
+               err = __mmc_send_status(card, &status, 5);
+               if (err) {
+                       pr_err("%s: error %d requesting status\n",
+                              req->rq_disk->disk_name, err);
+                       break;
+               }
+
+               /* Accumulate any response error bits seen */
+               if (resp_errs)
+                       *resp_errs |= status;
+
+               if (mmc_blk_in_tran_state(status))
+                       break;
+
+               /* Timeout if the device never becomes ready */
+               if (done) {
+                       pr_err("%s: Card stuck in wrong state! %s %s\n",
+                               mmc_hostname(card->host),
+                               req->rq_disk->disk_name, __func__);
+                       err = -ETIMEDOUT;
+                       break;
+               }
+       }
+
+       return err;
+}
+
+static int mmc_blk_send_stop(struct mmc_card *card)
+{
+       struct mmc_command cmd = {
+               .opcode = MMC_STOP_TRANSMISSION,
+               .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
+       };
+
+       return mmc_wait_for_cmd(card->host, &cmd, 5);
+}
+
+static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
+{
+       int err;
+
+       mmc_retune_hold_now(card->host);
+
+       mmc_blk_send_stop(card);
+
+       err = mmc_blk_card_stuck(card, req, NULL);
+
+       mmc_retune_release(card->host);
+
+       return err;
+}
+
+static void mmc_blk_rw_recovery(struct mmc_queue *mq, struct request *req)
+{
+       int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_blk_request *brq = &mqrq->brq;
+       struct mmc_blk_data *md = mq->blkdata;
+       struct mmc_card *card = mq->card;
+       u32 status;
+       u32 blocks;
+       int err;
+
+       /*
+        * Status error bits might get lost during re-tuning so don't allow
+        * re-tuning yet.
+        */
+       mmc_retune_hold_now(card->host);
+
+       /*
+        * Some errors the host driver might not have seen. Set the number of
+        * bytes transferred to zero in that case.
+        */
+       err = __mmc_send_status(card, &status, 0);
+       if (err || mmc_blk_status_error(req, status))
+               brq->data.bytes_xfered = 0;
+
+       mmc_retune_release(card->host);
+
+       /*
+        * Try again to get the status. This also provides an opportunity for
+        * re-tuning.
+        */
+       if (err)
+               err = __mmc_send_status(card, &status, 0);
+
+       /*
+        * Nothing more to do after the number of bytes transferred has been
+        * updated and there is no card.
+        */
+       if (err && mmc_detect_card_removed(card->host))
+               return;
+
+       /* Try to get back to "tran" state */
+       if (err || !mmc_blk_in_tran_state(status))
+               err = mmc_blk_fix_state(mq->card, req);
+
+       /*
+        * Special case for SD cards where the card might record the number of
+        * blocks written.
+        */
+       if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
+           rq_data_dir(req) == WRITE && !mmc_sd_num_wr_blocks(card, &blocks))
+               brq->data.bytes_xfered = blocks << 9;
+
+       /* Reset if the card is in a bad state */
+       if (err && mmc_blk_reset(md, card->host, type)) {
+               pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
+               mqrq->retries = MMC_NO_RETRIES;
+               return;
+       }
+
+       /*
+        * If anything was done, just return and if there is anything remaining
+        * on the request it will get requeued.
+        */
+       if (brq->data.bytes_xfered)
+               return;
+
+       /* Reset before last retry */
+       if (mqrq->retries + 1 == MMC_MAX_RETRIES)
+               mmc_blk_reset(md, card->host, type);
+
+       /* Command errors fail fast, so use all MMC_MAX_RETRIES */
+       if (brq->sbc.error || brq->cmd.error)
+               return;
+
+       /* Reduce the remaining retries for data errors */
+       if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
+               mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
+               return;
+       }
+
+       /* FIXME: Missing single sector read for large sector size */
+       if (rq_data_dir(req) == READ && !mmc_large_sector(card)) {
+               /* Read one sector at a time */
+               mmc_blk_ss_read(mq, req);
+               return;
+       }
+}
+
+static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
+{
+       mmc_blk_eval_resp_error(brq);
+
+       return brq->sbc.error || brq->cmd.error || brq->stop.error ||
+              brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
+}
+
+static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       u32 status = 0;
+       int err;
+
+       if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
+               return 0;
+
+       mmc_retune_hold_now(card->host);
+
+       err = mmc_blk_card_stuck(card, req, &status);
+
+       mmc_retune_release(card->host);
+
+       /*
+        * Do not assume data transferred correctly if there are any error bits
+        * set.
+        */
+       if (!err && status & mmc_blk_stop_err_bits(&mqrq->brq)) {
+               mqrq->brq.data.bytes_xfered = 0;
+               err = -EIO;
+       }
+
+       /* Copy the exception bit so it will be seen later on */
+       if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
+               mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
+

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2088

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+       return err;
+}
+
+static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
+                                           struct request *req)
+{
+       int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+
+       mmc_blk_reset_success(mq->blkdata, type);
+}

I understand that all the above new line and code (around 300 lines) is something you need for the blkmq support, in the rest of this patch.

However, it looks like you are adding completely new code that either already exists in the legacy request path (in some slightly different format), or could serve as clean up/re-factorization of the legacy request path.

This is not the way you should format a path for converting to blkmq. The reasons are:
*) It makes it hard to review.
**) There is no need to through away *all* old mmc blk/core code, which I assume is your plan for next step. Instead the proper way is to re-factor it, an make it possible to re-use those parts that makes sense.

Quoted 24 lines
drivers/mmc/core/block.c +24 -0
+
+static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
+
+       if (nr_bytes) {
+               if (blk_update_request(req, BLK_STS_OK, nr_bytes))
+                       blk_mq_requeue_request(req, true);
+               else
+                       __blk_mq_end_request(req, BLK_STS_OK);
+       } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
+               blk_mq_requeue_request(req, true);
+       } else {
+               if (mmc_card_removed(mq->card))
+                       req->rq_flags |= RQF_QUIET;
+               blk_mq_end_request(req, BLK_STS_IOERR);
+       }
+}
+
+static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
+                                       struct mmc_queue_req *mqrq)
+{
+       return mmc_card_mmc(mq->card) &&

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2122

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+              (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
+               mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
+}
+
+static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
+                                struct mmc_queue_req *mqrq)
+{
+       if (mmc_blk_urgent_bkops_needed(mq, mqrq))
+               mmc_start_bkops(mq->card, true);
+}

Ditto for the two above functions.

Quoted 9 lines
drivers/mmc/core/block.c +9 -0
+
+void mmc_blk_mq_complete(struct request *req)
+{
+       struct mmc_queue *mq = req->q->queuedata;
+
+       mmc_blk_mq_complete_rq(mq, req);
+}
+
+static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2141

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+                                      struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_host *host = mq->card->host;
+       bool failed;
+
+       failed = mmc_blk_rq_error(&mqrq->brq) ||
+                mmc_blk_card_busy(mq->card, req);
+
+       if (!mmc_queue_direct_complete(host))

Can you please make the changes related to completing the request in the mmc_request_done() into a separate patch.

It's better to first get the default behavior in place, then we can improve things on top. Again, that also makes it easier to review.

No I am giving up reaching this point. I didn't even get the actual blkmq converting part, which is the core part I should be spending my time to review. Sorry!

Some final comments around the system-wide PM support below.

[...]

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2151

Quoted 11 lines
drivers/mmc/core/block.c +10 -0
+static void mmc_mq_queue_suspend(struct mmc_queue *mq)
+{
+       blk_mq_quiesce_queue(mq->queue);
+
+       /*
+        * The host remains claimed while there are outstanding requests, so
+        * simply claiming and releasing here ensures there are none.
+        */
+       mmc_claim_host(mq->card->host);
+       mmc_release_host(mq->card->host);

This looks fragile.

Seems like an interface in blkmq with flushes the queue and suspend it is missing, however there are of course reasons why it doesn't exist.

I assume the blk_mq_quiesce_queue() guarantees no new requests is being pushed to us after calling it, but it still seem a bit racy to rely on the host claim/release thing.

Let's bring this up as question for the block people experts.

BTW, I was talking with Bart and Rafael about generic suspend issues for block/fs at Kernelsummit the other day. I will try to follow up on that to make sure we also do the right things in mmc.

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2153

Quoted 2 lines
drivers/mmc/core/block.c +2 -0
+}
+

[...]

Kind regards
Uffe

author maintainer
#4 lore raw

Re: [PATCH V12 2/5] mmc: block: Add blk-mq support

Quoted 72 lines · Ulf Hansson
Quoted 71 lines · Adrian Hunter

Define and use a blk-mq queue. Discards and flushes are processed synchronously, but reads and writes asynchronously. In order to support slow DMA unmapping, DMA unmapping is not done until after the next request is started. That means the request is not completed until then. If there is no next request then the completion is done by queued work.

---
 drivers/mmc/core/block.c | 655 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/mmc/core/block.h |  10 +
 drivers/mmc/core/queue.c | 302 ++++++++++++++++++++--
 drivers/mmc/core/queue.h |  41 +++
 include/linux/mmc/host.h |   1 +
 5 files changed, 979 insertions(+), 30 deletions(-)
drivers/mmc/core/block.c +16 -4
index ea80ff4cd7f9..002446e8dc5d 100644
@@ -1264,7 +1264,10 @@ static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
                break;
        }
        mq_rq->drv_op_result = ret;
-       blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       if (req->mq_ctx)
+               blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       else
+               blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
 }
 
 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
@@ -1307,7 +1310,10 @@ static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
        else
                mmc_blk_reset_success(md, type);
 fail:
-       blk_end_request(req, status, blk_rq_bytes(req));
+       if (req->mq_ctx)
+               blk_mq_end_request(req, status);
+       else
+               blk_end_request(req, status, blk_rq_bytes(req));
 }
 
 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
@@ -1377,7 +1383,10 @@ static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
        if (!err)
                mmc_blk_reset_success(md, type);
 out:
-       blk_end_request(req, status, blk_rq_bytes(req));
+       if (req->mq_ctx)
+               blk_mq_end_request(req, status);
+       else
+               blk_end_request(req, status, blk_rq_bytes(req));
 }
 
 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
@@ -1387,7 +1396,10 @@ static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
        int ret = 0;
 
        ret = mmc_flush_cache(card);
-       blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       if (req->mq_ctx)
+               blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
+       else
+               blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
 }
 
 /*
@@ -1413,15 +1425,18 @@ static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
        }
 }
 
Quoted 7 lines

drivers/mmc/core/block.c static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, · line 1429

Quoted 5 lines
drivers/mmc/core/block.c +2 -3
-#define CMD_ERRORS                                                     \
-       (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
-        R1_ADDRESS_ERROR |     /* Misaligned address */                \
+#define CMD_ERRORS_EXCL_OOR                                            \
+       (R1_ADDRESS_ERROR |     /* Misaligned address */                \

This looks unrelated to blkmq support.

No, it is preserving existing oor functionality.

Quoted 7 lines
Quoted 7 lines
drivers/mmc/core/block.c +0 -0
         R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
         R1_WP_VIOLATION |      /* Tried to write to protected block */ \
         R1_CARD_ECC_FAILED |   /* Card ECC failed */                   \
         R1_CC_ERROR |          /* Card controller error */             \
         R1_ERROR)              /* General/unknown error */
 
Quoted 6 lines

drivers/mmc/core/block.c static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, · line 1439

Quoted 4 lines
drivers/mmc/core/block.c +4 -0
+#define CMD_ERRORS                                                     \
+       (CMD_ERRORS_EXCL_OOR |                                          \
+        R1_OUT_OF_RANGE)       /* Command argument out of range */     \
+

Ditto.

And ditto.

Quoted 8 lines
Quoted 8 lines
drivers/mmc/core/block.c +0 -0
 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
 {
        u32 val;
@@ -1766,6 +1781,632 @@ static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
        mqrq->areq.err_check = mmc_blk_err_check;
 }
 
Quoted 6 lines

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 1786

Quoted 3 lines
drivers/mmc/core/block.c +3 -0
+#define MMC_MAX_RETRIES                5
+#define MMC_DATA_RETRIES       2
+#define MMC_NO_RETRIES         (MMC_MAX_RETRIES + 1)

What's are these defines about? Do you intend to use different retries for the blkmq case comparing to the legacy request path? If so, why?

Same number of retries but now actually explicit.

Quoted 35 lines
Quoted 35 lines
drivers/mmc/core/block.c +34 -0
+
+/* Single sector read during recovery */
+static void mmc_blk_ss_read(struct mmc_queue *mq, struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       blk_status_t status;
+
+       while (1) {
+               mmc_blk_rw_rq_prep(mqrq, mq->card, 1, mq);
+
+               mmc_wait_for_req(mq->card->host, &mqrq->brq.mrq);
+
+               /*
+                * Not expecting command errors, so just give up in that case.
+                * If there are retries remaining, the request will get
+                * requeued.
+                */
+               if (mqrq->brq.cmd.error)
+                       return;
+
+               if (blk_rq_bytes(req) <= 512)
+                       break;
+
+               status = mqrq->brq.data.error ? BLK_STS_IOERR : BLK_STS_OK;
+
+               blk_update_request(req, status, 512);
+       }
+
+       mqrq->retries = MMC_NO_RETRIES;
+}
+
+static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
+{
+       return !!brq->mrq.sbc;
Quoted 8 lines

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 1826

Quoted 6 lines
drivers/mmc/core/block.c +6 -0
+}
+
+static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
+{
+       return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
+}

Again, this seems like a non blkmq specific thing.

Again, preserving existing functionality.

Quoted 268 lines
Quoted 253 lines
drivers/mmc/core/block.c +252 -0
+
+static inline bool mmc_blk_in_tran_state(u32 status)
+{
+       /*
+        * Some cards mishandle the status bits, so make sure to check both the
+        * busy indication and the card state.
+        */
+       return status & R1_READY_FOR_DATA &&
+              (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
+}
+
+/*
+ * Check for errors the host controller driver might not have seen such as
+ * response mode errors or invalid card state.
+ */
+static bool mmc_blk_status_error(struct request *req, u32 status)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_blk_request *brq = &mqrq->brq;
+       u32 stop_err_bits = mmc_blk_stop_err_bits(brq);
+
+       return brq->cmd.resp[0]  & CMD_ERRORS    ||
+              brq->stop.resp[0] & stop_err_bits ||
+              status            & stop_err_bits ||
+              (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
+}
+
+static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
+{
+       return !brq->sbc.error && !brq->cmd.error &&
+              !(brq->cmd.resp[0] & CMD_ERRORS);
+}
+
+static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
+{
+       if (host->actual_clock)
+               return host->actual_clock / 1000;
+
+       /* Clock may be subject to a divisor, fudge it by a factor of 2. */
+       if (host->ios.clock)
+               return host->ios.clock / 2000;
+
+       /* How can there be no clock */
+       WARN_ON_ONCE(1);
+       return 100; /* 100 kHz is minimum possible value */
+}
+
+static unsigned long mmc_blk_data_timeout_jiffies(struct mmc_host *host,
+                                                 struct mmc_data *data)
+{
+       unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
+       unsigned int khz;
+
+       if (data->timeout_clks) {
+               khz = mmc_blk_clock_khz(host);
+               ms += DIV_ROUND_UP(data->timeout_clks, khz);
+       }
+
+       return msecs_to_jiffies(ms);
+}
+
+static int mmc_blk_card_stuck(struct mmc_card *card, struct request *req,
+                             u32 *resp_errs)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_data *data = &mqrq->brq.data;
+       unsigned long timeout;
+       u32 status;
+       int err;
+
+       timeout = jiffies + mmc_blk_data_timeout_jiffies(card->host, data);
+
+       while (1) {
+               bool done = time_after(jiffies, timeout);
+
+               err = __mmc_send_status(card, &status, 5);
+               if (err) {
+                       pr_err("%s: error %d requesting status\n",
+                              req->rq_disk->disk_name, err);
+                       break;
+               }
+
+               /* Accumulate any response error bits seen */
+               if (resp_errs)
+                       *resp_errs |= status;
+
+               if (mmc_blk_in_tran_state(status))
+                       break;
+
+               /* Timeout if the device never becomes ready */
+               if (done) {
+                       pr_err("%s: Card stuck in wrong state! %s %s\n",
+                               mmc_hostname(card->host),
+                               req->rq_disk->disk_name, __func__);
+                       err = -ETIMEDOUT;
+                       break;
+               }
+       }
+
+       return err;
+}
+
+static int mmc_blk_send_stop(struct mmc_card *card)
+{
+       struct mmc_command cmd = {
+               .opcode = MMC_STOP_TRANSMISSION,
+               .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
+       };
+
+       return mmc_wait_for_cmd(card->host, &cmd, 5);
+}
+
+static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
+{
+       int err;
+
+       mmc_retune_hold_now(card->host);
+
+       mmc_blk_send_stop(card);
+
+       err = mmc_blk_card_stuck(card, req, NULL);
+
+       mmc_retune_release(card->host);
+
+       return err;
+}
+
+static void mmc_blk_rw_recovery(struct mmc_queue *mq, struct request *req)
+{
+       int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_blk_request *brq = &mqrq->brq;
+       struct mmc_blk_data *md = mq->blkdata;
+       struct mmc_card *card = mq->card;
+       u32 status;
+       u32 blocks;
+       int err;
+
+       /*
+        * Status error bits might get lost during re-tuning so don't allow
+        * re-tuning yet.
+        */
+       mmc_retune_hold_now(card->host);
+
+       /*
+        * Some errors the host driver might not have seen. Set the number of
+        * bytes transferred to zero in that case.
+        */
+       err = __mmc_send_status(card, &status, 0);
+       if (err || mmc_blk_status_error(req, status))
+               brq->data.bytes_xfered = 0;
+
+       mmc_retune_release(card->host);
+
+       /*
+        * Try again to get the status. This also provides an opportunity for
+        * re-tuning.
+        */
+       if (err)
+               err = __mmc_send_status(card, &status, 0);
+
+       /*
+        * Nothing more to do after the number of bytes transferred has been
+        * updated and there is no card.
+        */
+       if (err && mmc_detect_card_removed(card->host))
+               return;
+
+       /* Try to get back to "tran" state */
+       if (err || !mmc_blk_in_tran_state(status))
+               err = mmc_blk_fix_state(mq->card, req);
+
+       /*
+        * Special case for SD cards where the card might record the number of
+        * blocks written.
+        */
+       if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
+           rq_data_dir(req) == WRITE && !mmc_sd_num_wr_blocks(card, &blocks))
+               brq->data.bytes_xfered = blocks << 9;
+
+       /* Reset if the card is in a bad state */
+       if (err && mmc_blk_reset(md, card->host, type)) {
+               pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
+               mqrq->retries = MMC_NO_RETRIES;
+               return;
+       }
+
+       /*
+        * If anything was done, just return and if there is anything remaining
+        * on the request it will get requeued.
+        */
+       if (brq->data.bytes_xfered)
+               return;
+
+       /* Reset before last retry */
+       if (mqrq->retries + 1 == MMC_MAX_RETRIES)
+               mmc_blk_reset(md, card->host, type);
+
+       /* Command errors fail fast, so use all MMC_MAX_RETRIES */
+       if (brq->sbc.error || brq->cmd.error)
+               return;
+
+       /* Reduce the remaining retries for data errors */
+       if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
+               mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
+               return;
+       }
+
+       /* FIXME: Missing single sector read for large sector size */
+       if (rq_data_dir(req) == READ && !mmc_large_sector(card)) {
+               /* Read one sector at a time */
+               mmc_blk_ss_read(mq, req);
+               return;
+       }
+}
+
+static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
+{
+       mmc_blk_eval_resp_error(brq);
+
+       return brq->sbc.error || brq->cmd.error || brq->stop.error ||
+              brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
+}
+
+static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       u32 status = 0;
+       int err;
+
+       if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
+               return 0;
+
+       mmc_retune_hold_now(card->host);
+
+       err = mmc_blk_card_stuck(card, req, &status);
+
+       mmc_retune_release(card->host);
+
+       /*
+        * Do not assume data transferred correctly if there are any error bits
+        * set.
+        */
+       if (!err && status & mmc_blk_stop_err_bits(&mqrq->brq)) {
+               mqrq->brq.data.bytes_xfered = 0;
+               err = -EIO;
+       }
+
+       /* Copy the exception bit so it will be seen later on */
+       if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
+               mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
+

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2088

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+       return err;
+}
+
+static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
+                                           struct request *req)
+{
+       int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+
+       mmc_blk_reset_success(mq->blkdata, type);
+}

I understand that all the above new line and code (around 300 lines) is something you need for the blkmq support, in the rest of this patch.

Quoted 4 lines

However, it looks like you are adding completely new code that either already exists in the legacy request path (in some slightly different format), or could serve as clean up/re-factorization of the legacy request path.

The legacy error handling is a non-starter. Messy and convoluted. There is no reason to keep using it. Structurally it doesn't fit with blk-mq because it is split in different places and mixes in logic that is not related with error-handling.

Quoted 4 lines

This is not the way you should format a path for converting to blkmq. The reasons are:
*) It makes it hard to review.

But the few comments in this email suggest you have spent maybe 30 minutes. It really looks like you haven't tried. Where are the questions?

Quoted 4 lines

**) There is no need to through away *all* old mmc blk/core code, which I assume is your plan for next step. Instead the proper way is to re-factor it, an make it possible to re-use those parts that makes sense.

That is just nonsense. We definitely want to throw away the messy convoluted legacy code. The new code is much simpler. Did you read it?

Quoted 29 lines
Quoted 29 lines
drivers/mmc/core/block.c +28 -0
+
+static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
+
+       if (nr_bytes) {
+               if (blk_update_request(req, BLK_STS_OK, nr_bytes))
+                       blk_mq_requeue_request(req, true);
+               else
+                       __blk_mq_end_request(req, BLK_STS_OK);
+       } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
+               blk_mq_requeue_request(req, true);
+       } else {
+               if (mmc_card_removed(mq->card))
+                       req->rq_flags |= RQF_QUIET;
+               blk_mq_end_request(req, BLK_STS_IOERR);
+       }
+}
+
+static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
+                                       struct mmc_queue_req *mqrq)
+{
+       return mmc_card_mmc(mq->card) &&
+              (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
+               mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
+}
+
Quoted 8 lines

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2122

Quoted 6 lines
drivers/mmc/core/block.c +6 -0
+static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
+                                struct mmc_queue_req *mqrq)
+{
+       if (mmc_blk_urgent_bkops_needed(mq, mqrq))
+               mmc_start_bkops(mq->card, true);
+}

Ditto for the two above functions.

Again it is the same functionality. In what way is it different?

Quoted 24 lines
Quoted 10 lines
drivers/mmc/core/block.c +9 -0
+
+void mmc_blk_mq_complete(struct request *req)
+{
+       struct mmc_queue *mq = req->q->queuedata;
+
+       mmc_blk_mq_complete_rq(mq, req);
+}
+
+static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2141

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+                                      struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_host *host = mq->card->host;
+       bool failed;
+
+       failed = mmc_blk_rq_error(&mqrq->brq) ||
+                mmc_blk_card_busy(mq->card, req);
+
+       if (!mmc_queue_direct_complete(host))

Can you please make the changes related to completing the request in the mmc_request_done() into a separate patch.

Quoted 2 lines

It's better to first get the default behavior in place, then we can improve things on top. Again, that also makes it easier to review.

I am not sure what you mean. Did you read the code? mmc_queue_direct_complete() is a capability that is not yet used, the default behavior is preserved the way you asked. You can keep asked for the same lines of code to be in different patches, but it isn't going to change the code.

Quoted 4 lines

No I am giving up reaching this point. I didn't even get the actual blkmq converting part, which is the core part I should be spending my time to review. Sorry!

It looks much more like you are not trying. Not a single technical issue raised! Not a single technical question!

Quoted 6 lines

Some final comments around the system-wide PM support below.

[...]

Quoted 1 line
Quoted 12 lines

drivers/mmc/core/block.c

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+static void mmc_mq_queue_suspend(struct mmc_queue *mq)
+{
+       blk_mq_quiesce_queue(mq->queue);
+
+       /*
+        * The host remains claimed while there are outstanding requests, so
+        * simply claiming and releasing here ensures there are none.
+        */
+       mmc_claim_host(mq->card->host);
+       mmc_release_host(mq->card->host);

This looks fragile.

It isn't. The driver would already be broken if we could have requests in flight but not have the host claimed.

Quoted 7 lines

Seems like an interface in blkmq with flushes the queue and suspend it is missing, however there are of course reasons why it doesn't exist.

I assume the blk_mq_quiesce_queue() guarantees no new requests is being pushed to us after calling it, but it still seem a bit racy to rely on the host claim/release thing.

Race with what?

Quoted 2 lines

Let's bring this up as question for the block people experts.

You have had almost a year to learn about blk-mq. What have you been doing?

And what are you going to do to get an answer from "the block people experts" - there is a good chance few of those cc'ed will read this.

This all looks like a weak excuse to do nothing.

You need to stop playing games. If you are not prepared to put the effort in, then let the CQE code go in. After all the dependency is a figment of your imagination. CQE has been ready to go for ages. Why are you making up reasons for delaying it?

Another possibility is to make me maintainer of the block driver since it seems I am the one that knows most about it.

#5 lore raw

Re: [PATCH V12 2/5] mmc: block: Add blk-mq support

Quoted 10 lines
Quoted 8 lines

drivers/mmc/core/block.c static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, · line 1429

Quoted 6 lines
drivers/mmc/core/block.c +2 -3
-#define CMD_ERRORS                                                     \
-       (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
-        R1_ADDRESS_ERROR |     /* Misaligned address */                \
+#define CMD_ERRORS_EXCL_OOR                                            \
+       (R1_ADDRESS_ERROR |     /* Misaligned address */                \

This looks unrelated to blkmq support.

No, it is preserving existing oor functionality.

So why change it in this patch?

[...]

Quoted 33 lines
Quoted 28 lines
Quoted 13 lines
drivers/mmc/core/block.c +13 -0
+       /*
+        * Do not assume data transferred correctly if there are any error bits
+        * set.
+        */
+       if (!err && status & mmc_blk_stop_err_bits(&mqrq->brq)) {
+               mqrq->brq.data.bytes_xfered = 0;
+               err = -EIO;
+       }
+
+       /* Copy the exception bit so it will be seen later on */
+       if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
+               mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
+

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2088

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+       return err;
+}
+
+static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
+                                           struct request *req)
+{
+       int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
+
+       mmc_blk_reset_success(mq->blkdata, type);
+}

I understand that all the above new line and code (around 300 lines) is something you need for the blkmq support, in the rest of this patch.

Quoted 4 lines

However, it looks like you are adding completely new code that either already exists in the legacy request path (in some slightly different format), or could serve as clean up/re-factorization of the legacy request path.

Quoted 4 lines

The legacy error handling is a non-starter. Messy and convoluted. There is no reason to keep using it. Structurally it doesn't fit with blk-mq because it is split in different places and mixes in logic that is not related with error-handling.

No matter you like it or not, the legacy request error path is what we got now.

The way forward is not to just through it away and start over, but instead to take small steps and make parts useful also for blkmq.

Yes, it's more work, but on the other hand, small changes which moves things in the right direction are also more easy to review.

So, no, sorry, I don't buy it!

Quoted 8 lines
Quoted 5 lines

This is not the way you should format a path for converting to blkmq. The reasons are:
*) It makes it hard to review.

But the few comments in this email suggest you have spent maybe 30 minutes. It really looks like you haven't tried. Where are the questions?

On this version, I spent a few hours on it, but really, it isn't easy to look at ~1000 new lines in one single patch, especially changes that re-implements the entire mmc request layer.

Quoted 8 lines
Quoted 5 lines

**) There is no need to through away *all* old mmc blk/core code, which I assume is your plan for next step. Instead the proper way is to re-factor it, an make it possible to re-use those parts that makes sense.

That is just nonsense. We definitely want to throw away the messy convoluted legacy code. The new code is much simpler. Did you read it?

Yes, the new code is simpler, but as stated, re-factoring some of the old code that can be re-used, is the way forward.

[...]

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
drivers/mmc/core/block.c +8 -0
+static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
+                                       struct mmc_queue_req *mqrq)
+{
+       return mmc_card_mmc(mq->card) &&
+              (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
+               mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
+}
+
Quoted 8 lines

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2122

Quoted 6 lines
drivers/mmc/core/block.c +6 -0
+static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
+                                struct mmc_queue_req *mqrq)
+{
+       if (mmc_blk_urgent_bkops_needed(mq, mqrq))
+               mmc_start_bkops(mq->card, true);
+}

Ditto for the two above functions.

Quoted 1 line

Again it is the same functionality. In what way is it different?

These checks are being done also in the legacy path, so in principle adding these and not using them there, means open-coding to me.

Quoted 25 lines
Quoted 25 lines
Quoted 11 lines
drivers/mmc/core/block.c +9 -0
+
+void mmc_blk_mq_complete(struct request *req)
+{
+       struct mmc_queue *mq = req->q->queuedata;
+
+       mmc_blk_mq_complete_rq(mq, req);
+}
+
+static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,

drivers/mmc/core/block.c static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, · line 2141

Quoted 10 lines
drivers/mmc/core/block.c +10 -0
+                                      struct request *req)
+{
+       struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
+       struct mmc_host *host = mq->card->host;
+       bool failed;
+
+       failed = mmc_blk_rq_error(&mqrq->brq) ||
+                mmc_blk_card_busy(mq->card, req);
+
+       if (!mmc_queue_direct_complete(host))

Can you please make the changes related to completing the request in the mmc_request_done() into a separate patch.

Quoted 8 lines
Quoted 2 lines

It's better to first get the default behavior in place, then we can improve things on top. Again, that also makes it easier to review.

I am not sure what you mean. Did you read the code? mmc_queue_direct_complete() is a capability that is not yet used, the default behavior is preserved the way you asked. You can keep asked for the same lines of code to be in different patches, but it isn't going to change the code.

Let me try to make this more clear then.

This is about keeping patches reviewable and about making one change per patch.

More precisely, move all code related to MMC_CAP_DIRECT_COMPLETE (the new cap which you introduce in this patch), into a separate patch on top of this one. That makes it both easy to review this patch, but also the one that introduces the new cap.

Quoted 28 lines
Quoted 5 lines

No I am giving up reaching this point. I didn't even get the actual blkmq converting part, which is the core part I should be spending my time to review. Sorry!

It looks much more like you are not trying. Not a single technical issue raised! Not a single technical question!

Quoted 6 lines

Some final comments around the system-wide PM support below.

[...]

Quoted 1 line
Quoted 12 lines

drivers/mmc/core/queue.c int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, · line 479

Quoted 10 lines
drivers/mmc/core/queue.c +10 -0
+static void mmc_mq_queue_suspend(struct mmc_queue *mq)
+{
+       blk_mq_quiesce_queue(mq->queue);
+
+       /*
+        * The host remains claimed while there are outstanding requests, so
+        * simply claiming and releasing here ensures there are none.
+        */
+       mmc_claim_host(mq->card->host);
+       mmc_release_host(mq->card->host);

This looks fragile.

Quoted 2 lines

It isn't. The driver would already be broken if we could have requests in flight but not have the host claimed.

Yes, but this boils done to that I am not sure what blk_mq_quiesce_queue() really means from dispatching point of view.

Would it possible that a dispatched request became preempted just before we was about to call mmc_claim_host() for it, thus the above mmc_claim_host() claims the host before?

Are you sure that can't happen?

Quoted 10 lines
Quoted 8 lines

Seems like an interface in blkmq with flushes the queue and suspend it is missing, however there are of course reasons why it doesn't exist.

I assume the blk_mq_quiesce_queue() guarantees no new requests is being pushed to us after calling it, but it still seem a bit racy to rely on the host claim/release thing.

Race with what?

See above.

Quoted 5 lines
Quoted 3 lines

Let's bring this up as question for the block people experts.

You have had almost a year to learn about blk-mq. What have you been doing?

Sorry, I haven't looked closely enough at the PM support. Obviously my bad.

Quoted 6 lines

And what are you going to do to get an answer from "the block people experts" - there is a good chance few of those cc'ed will read this.

This all looks like a weak excuse to do nothing.

Quoted 7 lines

You need to stop playing games. If you are not prepared to put the effort in, then let the CQE code go in. After all the dependency is a figment of your imagination. CQE has been ready to go for ages. Why are you making up reasons for delaying it?

Another possibility is to make me maintainer of the block driver since it seems I am the one that knows most about it.

Adrian, I have appreciated your contributions to mmc under a very long time, and I still am. I don't doubt your expertise in the area!

However, it seems like you don't want to listen to peoples opinions regarding this series - and I really don't get what's the deal. A man of your skills should easily be able to address those comments, but you have mostly been arguing and chosen to ignore them.

As long as I have something to say in this community, I will never accept poorly written patches, at least that goes for the mmc core layer.

That said, if other people think are better suited than me, please go ahead and make a formal suggestion.

Kind regards
Uffe