From patchwork Wed Sep 7 02:38:24 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: 9318133 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 A925360752 for ; Wed, 7 Sep 2016 02:39:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87A1928F89 for ; Wed, 7 Sep 2016 02:39:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6990628F8B; Wed, 7 Sep 2016 02:39:15 +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 C13A628F89 for ; Wed, 7 Sep 2016 02:39:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933471AbcIGCjO (ORCPT ); Tue, 6 Sep 2016 22:39:14 -0400 Received: from mail-pa0-f52.google.com ([209.85.220.52]:36288 "EHLO mail-pa0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932687AbcIGCjN (ORCPT ); Tue, 6 Sep 2016 22:39:13 -0400 Received: by mail-pa0-f52.google.com with SMTP id id6so1036698pad.3 for ; Tue, 06 Sep 2016 19:39:13 -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=kSEUDRYB1V1PkNgNfIjMkdxxAKbGWHNt8jCefwgo3+4=; b=VsMRrb3fGIRcGfLsNlWI8Kzjvjq/dK6f77U96D8cPqgWMrnI1AG6NLHbuMs83y3MMZ eROKsDuZc9N6cOvCk9TDstVq4BxW/kicUqogchfO566rpTY4/q5Q0gQoeQrYTtZBolHN 60QtQHWduAoJy+b+BWac+2XOuvjfxpRa0REvc= 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=kSEUDRYB1V1PkNgNfIjMkdxxAKbGWHNt8jCefwgo3+4=; b=FH3AvupyhKWK8B6VNV7YRkmyH9ppwvofpPbOFSD6L2z68A3GgQLAWYHujcdQEm/9Eq YIvE2U6QWkHPjlMdXZIUrPuAwf+yYPuvCc7UVVtSEexR1TioxbtCJWyCkhgWZ4Zx0V9j 3lNutmdWSkdgmvxA8SPf1Ibpbju1hr/rzi/KHPJI3nxCRl7J30TxhUVcrqrgNDoT7Cm5 nPkj0MRNhlfXw7Gd1/vQNm033jF0dDDEgYTQvpQ5AypZZSRTy4TjgAgIedujB/klsGsR 5HQKfCbLCkFDnk60m036j1NypbdyXShMkKzJ06HhSJj/RQUfiu6wn3R0EiiCwLw1z/pC pQKg== X-Gm-Message-State: AE9vXwMBRy0kUgG/u44G6Scg/HVM/J6jSOiBeQlSjxUu0F/KMbIDOGsgMwDpoobNWH5yt3O9 X-Received: by 10.66.253.7 with SMTP id zw7mr77743712pac.25.1473215952594; Tue, 06 Sep 2016 19:39:12 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id v124sm43985971pfb.14.2016.09.06.19.39.08 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Sep 2016 19:39:12 -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 v5 1/2] mmc: core: Factor out the alignment of erase size Date: Wed, 7 Sep 2016 10:38:24 +0800 Message-Id: <48ff46b96e01df36aa5d1c2daf1091d2ec2e729f.1473215578.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 In order to clean up the mmc_erase() function and do some optimization for erase size alignment, factor out the guts of erase size alignment into mmc_align_erase_size() function. Signed-off-by: Baolin Wang Tested-by: Shawn Lin --- Changes since v4: - Abandon the patch removing the checking if mmc can erase. - Add missing validations in mmc_erase(). Changes since v3: - Split into 3 separate patches. - Add test tag by Shawn. 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 | 48 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index e55cde6..11b4897 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -2202,6 +2202,36 @@ 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; + + 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; + + *to = from_new + nr_new; + *from = from_new; + + return nr_new; +} + /** * mmc_erase - erase sectors. * @card: card to erase @@ -2240,26 +2270,12 @@ 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 (arg == MMC_ERASE_ARG) + nr = mmc_align_erase_size(card, &from, &to, nr); if (nr == 0) return 0; - to = from + nr; - if (to <= from) return -EINVAL;