From patchwork Wed Aug 31 09:32:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 9306743 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5B82260487 for ; Wed, 31 Aug 2016 09:33:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4C77028E8A for ; Wed, 31 Aug 2016 09:33:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4079528E8C; Wed, 31 Aug 2016 09:33:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C425A28E8A for ; Wed, 31 Aug 2016 09:33:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759545AbcHaJdK (ORCPT ); Wed, 31 Aug 2016 05:33:10 -0400 Received: from mail-pf0-f178.google.com ([209.85.192.178]:33081 "EHLO mail-pf0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759373AbcHaJdJ (ORCPT ); Wed, 31 Aug 2016 05:33:09 -0400 Received: by mail-pf0-f178.google.com with SMTP id y134so17798904pfg.0 for ; Wed, 31 Aug 2016 02:33:09 -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; bh=mOz7Srk3zDUy/618252N2RuGra28NWPRPh7eSlYlxo0=; b=HA47nTbkNorauY/mCxgaPisvcLS3tnKrHsggNZ3Oq2wM7yKWb7TUmyBygxZUa5nJCt nHkgILqD3V8DisIJLkzJJoETNAAUGQpmGBZHOijlMfKt8I4EZev6Hei9eLMYE1VkT+KQ 6NXu/Stw6Hmi9wJuYVoODy0Jq990aNh85FXUs= 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; bh=mOz7Srk3zDUy/618252N2RuGra28NWPRPh7eSlYlxo0=; b=Z58dmS9XLjFs65OkqayPrNMu9wJXB20s4JGMHFINK0+CuZwzeVqIhSlAGAK6NnfOBg CVIQzZnqNa4eODa8LIWn87MLS9rB3qlTDfEyeAk7lMrIxzheidogjGa5aDyf4ldIifhD AJwEpuuXKPPD4HQu7tb8SjMzv1umZHN3RY6VA8znAcyt8vMLLma0+fIk54BlVMWXaaNA l9lzNH8Egz/I3oi7+bnnNppzwYPSeVN5AM89Iqh5kkaOp4QHTowoGpdAqeGtAePwMrRJ lS8iACtFjOzlyHIitJ+pNMBxvJzi65MAaPmPoII3lDUyAgJ8Wh6nNOw2HxO0/edjjh1k z1OA== X-Gm-Message-State: AE9vXwN7CFtHG3wR03QMLWUGFJ5ZxdzzaSYT5htSCGC1a7nttlUCIu3l566D3E+VeqN8TJOj X-Received: by 10.98.202.202 with SMTP id y71mr15231348pfk.10.1472635988574; Wed, 31 Aug 2016 02:33:08 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id c125sm63517053pfc.40.2016.08.31.02.33.04 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 31 Aug 2016 02:33:08 -0700 (PDT) From: Baolin Wang To: ulf.hansson@linaro.org Cc: adrian.hunter@intel.com, rmk+kernel@arm.linux.org.uk, shawn.lin@rock-chips.com, dianders@chromium.org, heiko@sntech.de, david@protonic.nl, hdegoede@redhat.com, linux-mmc@vger.kernel.org, linux-kernel@vger.kernel.org, broonie@kernel.org, linus.walleij@linaro.org, baolin.wang@linaro.org Subject: [PATCH v3] mmc: core: Optimize the mmc erase size alignment Date: Wed, 31 Aug 2016 17:32:31 +0800 Message-Id: <9de99671750430d7d80a8f351c0ec7a7088c9b00.1472635587.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-mmc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-mmc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Before issuing mmc_erase() function, users always have checked if it can erase with mmc_can_erase/trim/discard() function, thus remove the redundant erase checking in mmc_erase() function. This patch also optimizes the erase start/end sector alignment with round_up()/round_down() function, when erase command is MMC_ERASE_ARG. Signed-off-by: Baolin Wang Tested-by: Shawn Lin --- Changes since v2: - Add nr checking and other optimization in mmc_erase() function. Changes since v1: - Add the alignment if card->erase_size is not power of 2. --- drivers/mmc/core/core.c | 82 ++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index e55cde6..52156d4 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -2202,6 +2202,51 @@ out: return err; } +static unsigned int mmc_align_erase_size(struct mmc_card *card, + unsigned int *from, + unsigned int *to, + unsigned int nr) +{ + unsigned int from_new = *from, nr_new = nr, rem; + + if (is_power_of_2(card->erase_size)) { + unsigned int temp = from_new; + + from_new = round_up(temp, card->erase_size); + rem = from_new - temp; + + if (nr_new > rem) + nr_new -= rem; + else + return 0; + + nr_new = round_down(nr_new, card->erase_size); + } else { + rem = from_new % card->erase_size; + if (rem) { + rem = card->erase_size - rem; + from_new += rem; + if (nr_new > rem) + nr_new -= rem; + else + return 0; + } + + rem = nr_new % card->erase_size; + if (rem) + nr_new -= rem; + } + + if (nr_new == 0) + return 0; + + /* 'from' and 'to' are inclusive */ + *to = from_new + nr_new - 1; + *from = from_new; + + return nr_new; +} + /** * mmc_erase - erase sectors. * @card: card to erase @@ -2217,13 +2262,6 @@ int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, unsigned int rem, to = from + nr; int err; - if (!(card->host->caps & MMC_CAP_ERASE) || - !(card->csd.cmdclass & CCC_ERASE)) - return -EOPNOTSUPP; - - if (!card->erase_size) - return -EOPNOTSUPP; - if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) return -EOPNOTSUPP; @@ -2240,31 +2278,17 @@ int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, return -EINVAL; } - if (arg == MMC_ERASE_ARG) { - rem = from % card->erase_size; - if (rem) { - rem = card->erase_size - rem; - from += rem; - if (nr > rem) - nr -= rem; - else - return 0; - } - rem = nr % card->erase_size; - if (rem) - nr -= rem; - } - if (nr == 0) return 0; - to = from + nr; - - if (to <= from) - return -EINVAL; - - /* 'from' and 'to' are inclusive */ - to -= 1; + if (arg == MMC_ERASE_ARG) { + nr = mmc_align_erase_size(card, &from, &to, nr); + if (nr == 0) + return 0; + } else { + /* 'from' and 'to' are inclusive */ + to -= 1; + } /* * Special case where only one erase-group fits in the timeout budget: