mmc: core: claim host before halt in pm runtime idle

There can be a race condition between runtime idle and incoming
requests. In such a race condition, we can send requests
while the queue is halted.
Claiming the host at the beginning of runtime idle will prevent
such a case.

Change-Id: I8e55f0798543b2de44b37da13f7770889e6fbe5f
Signed-off-by: Maya Erez <merez@codeaurora.org>
[subhashj@codeaurora.org: fixed merge conflicts]
Signed-off-by: Subhash Jadavani <subhashj@codeaurora.org>
[xiaonian@codeaurora.org: fixed trivial merge conflicts]
Signed-off-by: Xiaonian Wang <xiaonian@codeaurora.org>
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index bc7f5c1..1ac8c6e 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -1306,7 +1306,8 @@
  *	mmc_start_manual_bkops - start BKOPS for supported cards
  *	@card: MMC card to start BKOPS
  *
- *	Send START_BKOPS to the card.
+ *      Send START_BKOPS to the card.
+ *      The function should be called with claimed host.
  */
 void mmc_start_manual_bkops(struct mmc_card *card)
 {
@@ -1320,7 +1321,6 @@
 	if (mmc_card_doing_bkops(card))
 		return;
 
-	mmc_claim_host(card->host);
 	mmc_retune_hold(card->host);
 
 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_START,
@@ -1335,7 +1335,6 @@
 	}
 
 	mmc_retune_release(card->host);
-	mmc_release_host(card->host);
 }
 EXPORT_SYMBOL(mmc_start_manual_bkops);
 
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index e047288..2697665 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -2698,6 +2698,9 @@
 	bool halt_cmdq;
 
 	BUG_ON(!host->card);
+
+	mmc_claim_host(host);
+
 	halt_cmdq = mmc_card_cmdq(host->card) &&
 			(host->card->bkops.needs_check ||
 			 host->card->bkops.needs_manual);
@@ -2722,10 +2725,9 @@
 		host->card->bkops.needs_check = false;
 
 	if (host->card->bkops.needs_check) {
-		mmc_claim_host(host);
 		mmc_check_bkops(host->card);
 		host->card->bkops.needs_check = false;
-		mmc_release_host(host);
+
 	}
 
 	if (host->card->bkops.needs_manual)
@@ -2737,6 +2739,7 @@
 			pr_err("%s: %s failed to unhalt cmdq (%d)\n",
 					mmc_hostname(host), __func__, err);
 	}
+
 out:
 	/*
 	 * TODO: consider prolonging suspend when bkops
@@ -2745,6 +2748,7 @@
 	 */
 	pm_schedule_suspend(&host->card->dev, MMC_AUTOSUSPEND_DELAY_MS);
 no_suspend:
+	mmc_release_host(host);
 	pm_runtime_mark_last_busy(&host->card->dev);
 	/* return negative value in order to avoid autosuspend */
 	return (err) ? err : NO_AUTOSUSPEND;