From patchwork Wed Jul 27 07:17:27 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: 9249343 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 BB96D607F0 for ; Wed, 27 Jul 2016 07:17:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A52F327AC2 for ; Wed, 27 Jul 2016 07:17:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 99BB327BF4; Wed, 27 Jul 2016 07:17:50 +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 2F34727AC2 for ; Wed, 27 Jul 2016 07:17:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752022AbcG0HRs (ORCPT ); Wed, 27 Jul 2016 03:17:48 -0400 Received: from mail-pf0-f173.google.com ([209.85.192.173]:36137 "EHLO mail-pf0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751681AbcG0HRs (ORCPT ); Wed, 27 Jul 2016 03:17:48 -0400 Received: by mail-pf0-f173.google.com with SMTP id h186so8046598pfg.3 for ; Wed, 27 Jul 2016 00:17:47 -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=o2gcb/VycrCVuh4OZf0cxittni7TA80ChSOcH/gseus=; b=cqKWhhH+AWnCfz4wLxe/PlQtoj4uvjd7KoMjZOEzXAU8Kz9hszcrrWmHrwmmi6hRlO SPJqDXv6WslzhLqt4Cu/7X0A1TVelaTXVu+NoxNMqPRwO0mnLGLyVcYXKrR5bMsUAsZ9 V3lxFPdWeJn4v14abbbJCIvJIK4hS4Er2+axA= 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=o2gcb/VycrCVuh4OZf0cxittni7TA80ChSOcH/gseus=; b=ReSXQwSXeJdHIORnv/sh3BUCLVwqDsCU5A/W95YlpAO1AGKvuFoIMwXzkct+8crULw RZsUiIgh8SO0PVc6Xtog8Vc0w7M5Q2u07a42dO2nmbqyxjOE+4arcZCPK6RzUYTT1eDQ Zah6FvGWP4XIkEdDqbfF/HM1OURe3yrNlVM9YzhLHAsWMaaAmljapD+eE9dqyMTxTeDY 4YVfhm8XA2xkkmsJmQWZ+UakNCzm3CKlDli8v968gSSONvwB1IUNTzhM3FLlbdWFIFhz uPulMkhcf/NIv09Gbw/XnHj2u0DYJtC1SFtghaTHDE76EfDQ9qYYLlR485GcyP8sAa87 Dd+A== X-Gm-Message-State: AEkooutmghWYgzcIBoax7zSXeXwzJVOuHZZ70HkxM7bAIWQByw/+nQ2jsymaiNaw2oaCnQ9Y X-Received: by 10.98.86.154 with SMTP id h26mr46763635pfj.22.1469603867301; Wed, 27 Jul 2016 00:17:47 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id x184sm6258131pfd.70.2016.07.27.00.17.43 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 27 Jul 2016 00:17:46 -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 v2] mmc: core: Optimize the mmc erase size alignment Date: Wed, 27 Jul 2016 15:17:27 +0800 Message-Id: <5117da51a7f11a16a8d10af060e2123f0d0a3ae8.1469603653.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 --- Changes since v1: - Add the alignment if card->erase_size is not power of 2. --- drivers/mmc/core/core.c | 78 +++++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index b4c08d1a..303a917 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -2195,6 +2195,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 @@ -2210,13 +2255,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; @@ -2234,31 +2272,11 @@ int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, } 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; + rem = mmc_align_erase_size(card, &from, &to, nr); + if (rem == 0) + return 0; } - if (nr == 0) - return 0; - - to = from + nr; - - if (to <= from) - return -EINVAL; - - /* 'from' and 'to' are inclusive */ - to -= 1; - /* * Special case where only one erase-group fits in the timeout budget: * If the region crosses an erase-group boundary on this particular