From patchwork Thu Jan 26 09:24:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 647081 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp143710pvb; Thu, 26 Jan 2023 01:24:51 -0800 (PST) X-Google-Smtp-Source: AK7set9O3usI4CbKq/ovuS08pIh6R4pI/RWD5PryVqxiWbbufeKwaC3ZWc5fy6O32jJG5XMY3K/b X-Received: by 2002:a05:6870:582a:b0:163:3814:5b73 with SMTP id r42-20020a056870582a00b0016338145b73mr866166oap.38.1674725090917; Thu, 26 Jan 2023 01:24:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674725090; cv=none; d=google.com; s=arc-20160816; b=ARlneJ72ITVvyR0f8nPGQGkLfz+w6ymbDQ+0lwLIbeImcB8dZdoSJu1V6fKGy5SAGc y4MCWbxW/xRVrFAKY+5mPOx48yNRiPoVMSiFv02ROXjj4kOY3iOSw+YXb4pb7vrWxrqL uskyrsr021DnUQYD+GjaLTvkpRDWkzIDSbIYTW0ROH15fiX0K51qhWbxmFspPoaQUixT ubg2b4rl2bLU+4GJ1b3EclvkCR9NyWrewh4s/KQa7k58UOsYKrt/XDCGUXXp6BZjrKD7 h4vg7cV7px58XU87SblmMVS1OEZHDTcexECOECJdmLAoQmM4aqcjsBj27prN/JG0nZrY 7orA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+LnBWj5DLWyNHt/X++e5tEl0oxwm7DTmEQw+hc+sHX4=; b=A64w9U5cGPQhCftK+sUYwiakJ8tEFzz7Svarht96cGzUA2MRmG4mQ9jDObDHJwFoyG Xwp6s7pW6BzdarGYAtlDmw/cqzRa8vAgi880NVZy2OVNxc44npDElyU8fwwzvDuHWq+8 D1sRxym+MPnr6RmQXOELVRZLrupsjJhLZB1oMI05uwODmFkNBcdCQJfE/EvcGwA8aaXH tKFjS59HoyWaZOJ3MItzOEUDCP12eKNC5S7UWBH2Rfg13l7E8j8rQxM0Lxs+zrz+Eie1 h079dfYPHQCiLEQc2i5lRPEuufpb0O71HzZBudFC83YP6KVgKk6vJTRlASZmSiOLOlrS t0aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cEUNwRCb; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id pn9-20020a0568704d0900b0014c64b2654fsi900058oab.301.2023.01.26.01.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 01:24:50 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cEUNwRCb; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 92DCA855B4; Thu, 26 Jan 2023 10:24:39 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="cEUNwRCb"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0656284954; Thu, 26 Jan 2023 10:24:35 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 0568084E8B for ; Thu, 26 Jan 2023 10:24:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=loic.poulain@linaro.org Received: by mail-wm1-x335.google.com with SMTP id f19-20020a1c6a13000000b003db0ef4dedcso2846828wmc.4 for ; Thu, 26 Jan 2023 01:24:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+LnBWj5DLWyNHt/X++e5tEl0oxwm7DTmEQw+hc+sHX4=; b=cEUNwRCbqy+kUQRtktvELcpH6csdI52CNptWm5JLkdKk3vsWLTLpkOvxtFwAcLsfXp LXE8ch2+PqRV9KE11OpAaJ8r4mxNzNxr3qy2vNZ3w3p3FXkVcKg2l0Sh1q2uUIpF69hC U4GBoqdmhD77PND85DysZOvBLqLAOMVDlWONsFz4PijAKawlmVLKecy6/febsoqW5dSr LXrQuf1z19vBfVvHOvLf3PgslBym0T6WFDKh0O71tgDhMeCcGzVft2F7S2HVrPui1jHw yd3yH9LZJSN41Do1WFUUUNIVtbMZEwV1zSmUfS7TFnkR4xsJNlrXBmceqt68kYKcxvyd px7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+LnBWj5DLWyNHt/X++e5tEl0oxwm7DTmEQw+hc+sHX4=; b=BBsC+k9gh+bVrAvI867Fg9L08LE2qSsYVYOeYNBee4HF1JFAIupsBG9LdWL+Hftdx8 Xevamqw4Eo1ctLCoR+NQey0jiDmmlYCP05IDnG7pVna5xZfM24b62sL4syMo1k/YMK/C 2JaujEHUY+nrKVnviHMESBGZWlwHglQa629LCNgNRsrvwEKoJ32IGu6tDnG88mVL7Tcw ZisOh7OjMM7quWpaKMJinIhOIJVOkfQ+jXWqhZNpTxS66OMxel87Tewm37j++5PBDksW XNtOuk8ZFQPa/7ch8jIXzoA0e+zwvupmgyF5wP46dSEClnTGHTVnawkplYmOPZywu2Qw 3csw== X-Gm-Message-State: AFqh2krI8FA8FtS9251YtIEjWOr+Glj6bbM3BqGyodSSCQs2C046+S0T 1XIMsD3HQoSA+T4sDLDYVfyY0w== X-Received: by 2002:a05:600c:4f4a:b0:3db:5f1:53a5 with SMTP id m10-20020a05600c4f4a00b003db05f153a5mr36004775wmq.20.1674725068489; Thu, 26 Jan 2023 01:24:28 -0800 (PST) Received: from loic-ThinkPad-T470p.. ([2a01:e0a:82c:5f0:352b:2cb1:8983:9f55]) by smtp.gmail.com with ESMTPSA id y34-20020a05600c342200b003dc16dee9b1sm920026wmp.15.2023.01.26.01.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 01:24:27 -0800 (PST) From: Loic Poulain To: sjg@chromium.org, peng.fan@nxp.com, jh80.chung@samsung.com Cc: u-boot@lists.denx.de, Loic Poulain Subject: [PATCH v2 2/3] mmc: erase: Use TRIM erase when available Date: Thu, 26 Jan 2023 10:24:18 +0100 Message-Id: <20230126092419.534514-2-loic.poulain@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230126092419.534514-1-loic.poulain@linaro.org> References: <20230126092419.534514-1-loic.poulain@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean The default erase command applies on erase group unit, and simply round down to erase group size. When the start block is not aligned to erase group size (e.g. erasing partition) it causes unwanted erasing of the previous blocks, part of the same erase group (e.g. owned by other logical partition, or by the partition table itself). To prevent this issue, a simple solution is to use TRIM as argument of the Erase command, which is usually supported with eMMC > 4.0, and allow to apply erase operation to write blocks instead of erase group Signed-off-by: Loic Poulain Reviewed-by: Simon Glass --- v2: Add mmc unit test change to the series drivers/mmc/mmc_write.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/drivers/mmc/mmc_write.c b/drivers/mmc/mmc_write.c index 5b7aeeb012..a6f93380dd 100644 --- a/drivers/mmc/mmc_write.c +++ b/drivers/mmc/mmc_write.c @@ -15,7 +15,7 @@ #include #include "mmc_private.h" -static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) +static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt, u32 args) { struct mmc_cmd cmd; ulong end; @@ -52,7 +52,7 @@ static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) goto err_out; cmd.cmdidx = MMC_CMD_ERASE; - cmd.cmdarg = MMC_ERASE_ARG; + cmd.cmdarg = args ? args : MMC_ERASE_ARG; cmd.resp_type = MMC_RSP_R1b; err = mmc_send_cmd(mmc, &cmd, NULL); @@ -77,7 +77,7 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) #endif int dev_num = block_dev->devnum; int err = 0; - u32 start_rem, blkcnt_rem; + u32 start_rem, blkcnt_rem, erase_args = 0; struct mmc *mmc = find_mmc_device(dev_num); lbaint_t blk = 0, blk_r = 0; int timeout_ms = 1000; @@ -97,13 +97,25 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) */ err = div_u64_rem(start, mmc->erase_grp_size, &start_rem); err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem); - if (start_rem || blkcnt_rem) - printf("\n\nCaution! Your devices Erase group is 0x%x\n" - "The erase range would be change to " - "0x" LBAF "~0x" LBAF "\n\n", - mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), - ((start + blkcnt + mmc->erase_grp_size - 1) - & ~(mmc->erase_grp_size - 1)) - 1); + if (start_rem || blkcnt_rem) { + if (mmc->can_trim) { + /* Trim function applies the erase operation to write + * blocks instead of erase groups. + */ + erase_args = MMC_TRIM_ARG; + } else { + /* The card ignores all LSB's below the erase group + * size, rounding down the addess to a erase group + * boundary. + */ + printf("\n\nCaution! Your devices Erase group is 0x%x\n" + "The erase range would be change to " + "0x" LBAF "~0x" LBAF "\n\n", + mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), + ((start + blkcnt + mmc->erase_grp_size - 1) + & ~(mmc->erase_grp_size - 1)) - 1); + } + } while (blk < blkcnt) { if (IS_SD(mmc) && mmc->ssr.au) { @@ -113,7 +125,7 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? mmc->erase_grp_size : (blkcnt - blk); } - err = mmc_erase_t(mmc, start + blk, blk_r); + err = mmc_erase_t(mmc, start + blk, blk_r, erase_args); if (err) break;