mmc: core: Factor out the alignment of erase size
authorBaolin Wang <baolin.wang@linaro.org>
Wed, 7 Sep 2016 02:38:24 +0000 (10:38 +0800)
committerUlf Hansson <ulf.hansson@linaro.org>
Mon, 26 Sep 2016 19:31:25 +0000 (21:31 +0200)
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 <baolin.wang@linaro.org>
Tested-by: Shawn Lin <shawn.lin@rock-chips.com>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
drivers/mmc/core/core.c

index 59b452d5dc8196acda21b7e99c633b73893a8460..9ce6c25a8f54057e388ae996894fabd452379d5f 100644 (file)
@@ -2205,6 +2205,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
@@ -2243,26 +2273,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;