From patchwork Thu Oct 20 08:19:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 78441 Delivered-To: patches@linaro.org Received: by 10.140.97.247 with SMTP id m110csp657232qge; Thu, 20 Oct 2016 01:19:58 -0700 (PDT) X-Received: by 10.25.215.88 with SMTP id o85mr10906634lfg.98.1476951598073; Thu, 20 Oct 2016 01:19:58 -0700 (PDT) Return-Path: Received: from mail-lf0-x22a.google.com (mail-lf0-x22a.google.com. [2a00:1450:4010:c07::22a]) by mx.google.com with ESMTPS id y125si5628660lfd.374.2016.10.20.01.19.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Oct 2016 01:19:58 -0700 (PDT) Received-SPF: pass (google.com: domain of ulf.hansson@linaro.org designates 2a00:1450:4010:c07::22a as permitted sender) client-ip=2a00:1450:4010:c07::22a; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 2a00:1450:4010:c07::22a as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-lf0-x22a.google.com with SMTP id b81so71606876lfe.1 for ; Thu, 20 Oct 2016 01:19:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=x3dnzLol+K53rDzeXCIDbb7e8qEYgBjH6UoNvuUjQ0o=; b=KTPUtNrIt7EkIr6vfTIxH4M20ceYxpx3b1Zr5zKHw8+/x+1Tekw1y3Wz9fKCufX6Qe GV+6vaqm7GyQpfpe893ow4BUbXS1lSI/n/vghYUXUzYDbh3i6LFhyCZOIjJsrQ+ID8B4 w8FfN5OTYP+Ezejtie0h1w2RQDG2nOax+4BBY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=x3dnzLol+K53rDzeXCIDbb7e8qEYgBjH6UoNvuUjQ0o=; b=AAz5Z6npDwKs1DbcK3hoYEuj7ut8FDZZ+lKwmeV4aY7yaWuCu43hHKJK4w/jzMlUNA u9L94E07RwJX6yRW7+Uk6zBl0NO0HZ88A7zW+m45/jMiOC5bsA6P35tmP3Q7L37udiJK DD2p/B3FXq3HtNknWRHur3s8EN01oEfgst9R9YM1ANzsUassKNQjZB8Y9DtMaPdEXPqx f8EidXLZUW6bmMtP84PKmzqknj2OEzpTYcgpXrOOeZ4q3DxZ/Qu40qHEINe/7bjGFnEU rJBEAd1+6nCManxpd01ZkDv6s9BEIUDO6yb3Pq98Wh4hCs884eTNCeOqrBGMtb277V1o f3kQ== X-Gm-Message-State: AA6/9RmRRqX/RiPS8PFXMOBjUmg7A5xe1UCTne8GkbRagqcxHtWHSFtarl6sv36c5lWvAwHXlZg= X-Received: by 10.25.8.210 with SMTP id 201mr10986558lfi.156.1476951597541; Thu, 20 Oct 2016 01:19:57 -0700 (PDT) Return-Path: Received: from uffe-Latitude-E6430s.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id h7sm11672584ljh.21.2016.10.20.01.19.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 20 Oct 2016 01:19:56 -0700 (PDT) From: Ulf Hansson To: linux-mmc@vger.kernel.org, Ulf Hansson Cc: Jaehoon Chung , Adrian Hunter , Linus Walleij , Chaotian Jing Subject: [PATCH 3/4] mmc: core: Factor out code related to polling in __mmc_switch() Date: Thu, 20 Oct 2016 10:19:38 +0200 Message-Id: <1476951579-26125-4-git-send-email-ulf.hansson@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1476951579-26125-1-git-send-email-ulf.hansson@linaro.org> References: <1476951579-26125-1-git-send-email-ulf.hansson@linaro.org> In yet another step of cleaning up __mmc_switch(), let's factor out the code that deals with card busy polling. Signed-off-by: Ulf Hansson --- drivers/mmc/core/mmc_ops.c | 108 +++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 49 deletions(-) -- 1.9.1 diff --git a/drivers/mmc/core/mmc_ops.c b/drivers/mmc/core/mmc_ops.c index 5a77af7..a84a880 100644 --- a/drivers/mmc/core/mmc_ops.c +++ b/drivers/mmc/core/mmc_ops.c @@ -468,6 +468,63 @@ int mmc_switch_status(struct mmc_card *card) return mmc_switch_status_error(card->host, status); } +static int mmc_poll_for_busy(struct mmc_card *card, unsigned int timeout_ms, + bool send_status, bool ignore_crc) +{ + struct mmc_host *host = card->host; + int err; + unsigned long timeout; + u32 status = 0; + bool expired = false; + bool busy = false; + + /* We have an unspecified cmd timeout, use the fallback value. */ + if (!timeout_ms) + timeout_ms = MMC_OPS_TIMEOUT_MS; + + /* + * In cases when not allowed to poll by using CMD13 or because we aren't + * capable of polling by using ->card_busy(), then rely on waiting the + * stated timeout to be sufficient. + */ + if (!send_status && !host->ops->card_busy) { + mmc_delay(timeout_ms); + return 0; + } + + timeout = jiffies + msecs_to_jiffies(timeout_ms) + 1; + do { + /* + * Due to the possibility of being preempted after + * sending the status command, check the expiration + * time first. + */ + expired = time_after(jiffies, timeout); + if (send_status) { + err = __mmc_send_status(card, &status, ignore_crc); + if (err) + return err; + } + if (host->ops->card_busy) { + if (!host->ops->card_busy(host)) + break; + busy = true; + } + + /* Timeout if the device never leaves the program state. */ + if (expired && + (R1_CURRENT_STATE(status) == R1_STATE_PRG || busy)) { + pr_err("%s: Card stuck in programming state! %s\n", + mmc_hostname(host), __func__); + return -ETIMEDOUT; + } + } while (R1_CURRENT_STATE(status) == R1_STATE_PRG || busy); + + err = mmc_switch_status_error(host, status); + + return err; +} + /** * __mmc_switch - modify EXT_CSD register * @card: the MMC card associated with the data transfer @@ -489,11 +546,7 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value, struct mmc_host *host = card->host; int err; struct mmc_command cmd = {0}; - unsigned long timeout; - u32 status = 0; bool use_r1b_resp = use_busy_signal; - bool expired = false; - bool busy = false; mmc_retune_hold(host); @@ -543,51 +596,8 @@ int __mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value, goto out; } - /* We have an unspecified cmd timeout, use the fallback value. */ - if (!timeout_ms) - timeout_ms = MMC_OPS_TIMEOUT_MS; - - /* - * In cases when not allowed to poll by using CMD13 or because we aren't - * capable of polling by using ->card_busy(), then rely on waiting the - * stated timeout to be sufficient. - */ - if (!send_status && !host->ops->card_busy) { - mmc_delay(timeout_ms); - goto out; - } - - /* Let's poll to find out when the command is completed. */ - timeout = jiffies + msecs_to_jiffies(timeout_ms) + 1; - do { - /* - * Due to the possibility of being preempted after - * sending the status command, check the expiration - * time first. - */ - expired = time_after(jiffies, timeout); - if (send_status) { - err = __mmc_send_status(card, &status, ignore_crc); - if (err) - goto out; - } - if (host->ops->card_busy) { - if (!host->ops->card_busy(host)) - break; - busy = true; - } - - /* Timeout if the device never leaves the program state. */ - if (expired && - (R1_CURRENT_STATE(status) == R1_STATE_PRG || busy)) { - pr_err("%s: Card stuck in programming state! %s\n", - mmc_hostname(host), __func__); - err = -ETIMEDOUT; - goto out; - } - } while (R1_CURRENT_STATE(status) == R1_STATE_PRG || busy); - - err = mmc_switch_status_error(host, status); + /* Let's try to poll to find out when the command is completed. */ + err = mmc_poll_for_busy(card, timeout_ms, send_status, ignore_crc); out: mmc_retune_release(host);