mmc: block: add card device reference counting for CQ mode

Since error handling could race with runtime suspend, increase usage_count
for the card device will prevent this race.

Change-Id: Ie95a3c631f519c7993b0032f0b674871b64e4cb6
Signed-off-by: Konstantin Dorfman <kdorfman@codeaurora.org>
[xiaonian@codeaurora.org: fixed trivial merge conflicts]
Signed-off-by: Xiaonian Wang <xiaonian@codeaurora.org>
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index 47fe11a..3931f91 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -1870,7 +1870,7 @@
 
 	if (test_and_clear_bit(0, &ctx_info->req_starved))
 		blk_run_queue(mq->queue);
-	mmc_release_host(host);
+	mmc_put_card(card);
 	return err ? 1 : 0;
 }
 
@@ -1986,7 +1986,7 @@
 
 	if (test_and_clear_bit(0, &ctx_info->req_starved))
 		blk_run_queue(mq->queue);
-	mmc_release_host(host);
+	mmc_put_card(card);
 	return err ? 1 : 0;
 }
 
@@ -3232,7 +3232,7 @@
 		return;
 	}
 
-	mmc_claim_host(card->host);
+	mmc_get_card(card);
 	/* disable CQ mode in card */
 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
 			 EXT_CSD_CMDQ, 0,
@@ -3245,7 +3245,7 @@
 		host->card->cmdq_init = false;
 	}
 out:
-	mmc_release_host(card->host);
+	mmc_put_card(card);
 }
 
 static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
@@ -3280,6 +3280,7 @@
 	struct mmc_card *card = mq->card;
 	struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
 
+	pm_runtime_get_sync(&card->dev);
 	err = mmc_cmdq_halt(host, true);
 	if (err) {
 		pr_err("halt: failed: %d\n", err);
@@ -3341,6 +3342,9 @@
 	mmc_cmdq_halt(host, false);
 
 out:
+	pm_runtime_mark_last_busy(&card->dev);
+	pm_runtime_put_autosuspend(&card->dev);
+
 	if (test_and_clear_bit(0, &ctx_info->req_starved))
 		blk_run_queue(mrq->req->q);
 }
@@ -3392,8 +3396,8 @@
 	if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state) &&
 			test_and_clear_bit(0, &ctx_info->req_starved))
 		blk_run_queue(mq->queue);
-	mmc_release_host(host);
 
+	mmc_put_card(host->card);
 	if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
 		complete(&mq->cmdq_shutdown_complete);
 	return;
@@ -3658,14 +3662,14 @@
 	struct mmc_blk_data *md = mq->data;
 	struct mmc_card *card = md->queue.card;
 
-	mmc_claim_host(card->host);
+	mmc_get_card(card);
 	ret = mmc_blk_cmdq_part_switch(card, md);
 	if (ret) {
 		pr_err("%s: %s: partition switch failed %d\n",
 				md->disk->disk_name, __func__, ret);
 		if (req)
 			blk_end_request_all(req, ret);
-		mmc_release_host(card->host);
+		mmc_put_card(card);
 		goto switch_failure;
 	}