From patchwork Tue Sep 6 02:55:11 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: 9315427 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 EB910600CA for ; Tue, 6 Sep 2016 02:59:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E100E28B16 for ; Tue, 6 Sep 2016 02:59:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D53B228B18; Tue, 6 Sep 2016 02:59:37 +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=unavailable 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 D89EF28B16 for ; Tue, 6 Sep 2016 02:59:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755300AbcIFC5S (ORCPT ); Mon, 5 Sep 2016 22:57:18 -0400 Received: from mail-pa0-f46.google.com ([209.85.220.46]:36838 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755203AbcIFC4D (ORCPT ); Mon, 5 Sep 2016 22:56:03 -0400 Received: by mail-pa0-f46.google.com with SMTP id id6so7850441pad.3 for ; Mon, 05 Sep 2016 19:56:02 -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 :in-reply-to:references; bh=QCW15AtVOs7YySFch0xOsGnDJj2UumOY1tteuOEX2Ks=; b=T3V2E3vX/kBk9uOPDxZYPSKE7Pm9Mu/MaLbWf3+Sh/tJ7LEQTOif99avZk8tq4xw8o FzLUApATIalEIwK8ywV33GSNh6/AZ/FWGgzwKNhzBY5OTpIXtzyxlS8RRIJ6LXaOl2vg 9db3VQSPVKEpAoYhXoDNudVH7x3aQVRxig0+s= 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:in-reply-to:references; bh=QCW15AtVOs7YySFch0xOsGnDJj2UumOY1tteuOEX2Ks=; b=aA1QZ40/UHiCkFElDo6wJnAvI/ZJ8AC6va5CmucVSV76fF1VYJJlQ3uXMgRdKWjngi /4CK0Facs82YO3Wyk8LfdiM9uvottO5hVdXy6x2MiEwkokyxFVlZABLBZIeJ45PJY+Pj tZhb86oxLF/HPZWx4gXR659tzchmYgVWhCiZUlrqBgqafloySSw+ROU8I9TACzlfpSZx WJ5QPBjiBWINGCJHtoVo10jtpuT81866KaPu9Co2r0+rg2imbdvYoPdZF9cthxsGsHTy TGIHook8ZmMvE/GKMK9Btyea4t9+d1hCKhx5n6K72qcMSZkePRzVK+GwuxcUn+Yy97rx sTRA== X-Gm-Message-State: AE9vXwOjihwf2ZgGCr7pm39wY9zPkNndWGNvOLfgCcjVts0L7BQSHxyIWQon8AGPXsl27zId X-Received: by 10.66.13.200 with SMTP id j8mr67940769pac.150.1473130562261; Mon, 05 Sep 2016 19:56:02 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id g15sm139102pfa.48.2016.09.05.19.55.58 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Sep 2016 19:56:01 -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 v4 2/3] mmc: core: Factor out the alignment of erase size Date: Tue, 6 Sep 2016 10:55:11 +0800 Message-Id: <48c332c9c688796b7ea157046b8feb68a11e60d4.1473130123.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <2b6b5b930d0f37a219d5786a6b56b17516a90abd.1473130123.git.baolin.wang@linaro.org> References: <2b6b5b930d0f37a219d5786a6b56b17516a90abd.1473130123.git.baolin.wang@linaro.org> In-Reply-To: <2b6b5b930d0f37a219d5786a6b56b17516a90abd.1473130123.git.baolin.wang@linaro.org> References: <2b6b5b930d0f37a219d5786a6b56b17516a90abd.1473130123.git.baolin.wang@linaro.org> 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 --- drivers/mmc/core/core.c | 60 +++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 7d7209d..5f93eef 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -2202,6 +2202,37 @@ 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; + + /* '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 @@ -2234,31 +2265,14 @@ 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; + nr = mmc_align_erase_size(card, &from, &to, nr); + if (nr == 0) + return 0; + } else { + /* 'from' and 'to' are inclusive */ + to -= 1; } - 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