mmc: block: ensure CMDQ is empty before queuing cache flush

Some devices might stop responding to new commands if device cache flush
is queued to host controller while host controller is still processing
outstanding requests.

To workaround this issue, we are making the cmdq thread to wait for all
the oustanding requests to be finished before queuing cache flush command
on host controller.

Change-Id: I15387734f51ca4cadfc9e11270f14d8a0806a00f
Signed-off-by: Subhash Jadavani <subhashj@codeaurora.org>
[xiaonian@codeaurora.org: fixed trivial merge conflicts,
changed MMC_QUIRK_CMDQ_EMPTY_BEFORE_FLUSH definition to
bit 18]
Signed-off-by: Xiaonian Wang <xiaonian@codeaurora.org>
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 7705592..b59ec5d 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -3790,6 +3790,19 @@
 			else
 				ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
 		} else if (req_op(req) == REQ_OP_FLUSH) {
+			if (card->quirks &
+			    MMC_QUIRK_CMDQ_EMPTY_BEFORE_FLUSH) {
+				ret = wait_event_interruptible(
+					card->host->cmdq_ctx.queue_empty_wq,
+					(!card->host->cmdq_ctx.active_reqs));
+				if (ret) {
+					pr_err("%s: failed while waiting for the CMDQ to be empty %s err (%d)\n",
+						mmc_hostname(card->host),
+						__func__, ret);
+					BUG_ON(1);
+				}
+			}
+
 			ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
 		} else {
 			ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
@@ -4285,6 +4298,8 @@
 		  MMC_QUIRK_BLK_NO_CMD23),
 	MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
 		  MMC_QUIRK_BLK_NO_CMD23),
+	MMC_FIXUP(CID_NAME_ANY, CID_MANFID_TOSHIBA, CID_OEMID_ANY,
+		  add_quirk_mmc, MMC_QUIRK_CMDQ_EMPTY_BEFORE_FLUSH),
 
 	/*
 	 * Some MMC cards need longer data read timeout than indicated in CSD.