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: 75589 Delivered-To: patch@linaro.org Received: by 10.140.106.11 with SMTP id d11csp100599qgf; Tue, 6 Sep 2016 19:39:15 -0700 (PDT) X-Received: by 10.66.152.238 with SMTP id vb14mr12532446pab.78.1473215954945; Tue, 06 Sep 2016 19:39:14 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b126si38521887pfg.111.2016.09.06.19.39.14; Tue, 06 Sep 2016 19:39:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-mmc-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-mmc-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933471AbcIGCjO (ORCPT + 3 others); 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 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(-) -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-mmc" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html 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;