Skip to contentSkip to conversation

mmc-utils: add ability to flush optional eMMC cache

PATCH Superseded Michele Dionisio 2 participants 8 messages Jul 8, 2025 – Jul 10, 2025 MMC, SD and SDIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover mmc-utils: add ability to flush optional eMMC cache

eMMC 5.0 introduce command to flush cache. Linux Kernel support it, so it can be nice to have this feature also in mmc-utils.

I would appreciate any comments on the usefulness of the change and a review.

Regards
Michele

Michele Dionisio (1):
mmc-utils: add ability to flush optional eMMC cache

 man/mmc.1  |  4 ++++
 mmc.c      |  5 +++++
 mmc.h      |  1 +
 mmc_cmds.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 5 files changed, 61 insertions(+)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 mmc-utils: add ability to flush optional eMMC cache

From: Michele Dionisio <[email protected]>

The MMC 5.0 instroduce command to flush cache

---
 man/mmc.1  |  4 ++++
 mmc.c      |  5 +++++
 mmc.h      |  1 +
 mmc_cmds.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 5 files changed, 61 insertions(+)
man/mmc.1 +4 -0
index bccabf3..665fe6d 100644
@@ -109,6 +109,10 @@ NOTE! The cache is an optional feature on devices >= eMMC4.5.
 Disable the eMMC cache feature on <device>.
 NOTE! The cache is an optional feature on devices >= eMMC4.5.
 .TP
+.BR "cache flush <device>"
+Flush the eMMC cache on <device>.
+NOTE! The cache is an optional feature on devices >= eMMC5.0.
+.TP
 .BR "<cmd> --help"
 Show detailed help for a command or subset of commands.
 
mmc.c +5 -0
index 6770a45..3c82504 100644
@@ -231,6 +231,11 @@ static struct Command commands[] = {
 		"Disable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
 	},
+	{ do_cache_flush, -1,
+	  "cache flush", "<device>\n"
+		"flush the eMMC cache <device>.\n"
+		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
+	},
 	{ do_read_csd, -1,
 	  "csd read", "<device path>\n"
 		  "Print CSD data from <device path>.\n"
mmc.h +1 -0
index 9fc22ec..2bb346b 100644
@@ -152,6 +152,7 @@
 #define EXT_CSD_DATA_SECTOR_SIZE	61 /* R */
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1	53
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0	52
+#define EXT_CSD_FLUSH_CACHE     32
 #define EXT_CSD_CACHE_CTRL		33
 #define EXT_CSD_MODE_CONFIG		30
 #define EXT_CSD_MODE_OPERATION_CODES	29	/* W */
mmc_cmds.c +50 -0

56 lines not on this page · raw message

mmc_cmds.h +1 -0
index ce35d3e..f767deb 100644
@@ -50,6 +50,7 @@ int do_rpmb_sec_wp_mode_clear(int nargs, char **argv);
 int do_rpmb_sec_wp_en_read(int nargs, char **argv);
 int do_cache_en(int nargs, char **argv);
 int do_cache_dis(int nargs, char **argv);
+int do_cache_flush(int nargs, char **argv);
 int do_ffu(int nargs, char **argv);
 int do_opt_ffu1(int nargs, char **argv);
 int do_opt_ffu2(int nargs, char **argv);
Signature · 1 line
2.43.0
#2 lore raw
Quoted 3 lines

From: Michele Dionisio <[email protected]>

The MMC 5.0 instroduce command to flush cache

Please elaborate.
E.g. A use case in which you find this functionality useful.

Quoted 22 lines
---
 man/mmc.1  |  4 ++++
 mmc.c      |  5 +++++
 mmc.h      |  1 +
 mmc_cmds.c | 50
++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 5 files changed, 61 insertions(+)
man/mmc.1 +0 -0
index bccabf3..665fe6d 100644
@@ -109,6 +109,10 @@ NOTE! The cache is an optional feature on devices >=

eMMC4.5.
Disable the eMMC cache feature on <device>.
NOTE! The cache is an optional feature on devices >= eMMC4.5. .TP
+.BR "cache flush <device>"

man/mmc.1 NOTE! The cache is an optional feature on devices >= eMMC4.5. · line 117

Quoted 5 lines
man/mmc.1 +3 -0
+Flush the eMMC cache on <device>.
+NOTE! The cache is an optional feature on devices >= eMMC5.0.
+.TP
 .BR "<cmd> --help"
 Show detailed help for a command or subset of commands.

Need to update docs/HOWTO.rst as well.

mmc.c static struct Command commands[] = { · line 234

Quoted 10 lines
mmc.c +1 -0
index 6770a45..3c82504 100644
@@ -231,6 +231,11 @@ static struct Command commands[] = {
                "Disable the eMMC cache feature on <device>.\n"
                "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
        },
+       { do_cache_flush, -1,

Better reduce its scope and set it to 1 as you don't have any further arguments

Quoted 32 lines
mmc.c +4 -0
+         "cache flush", "<device>\n"
+               "flush the eMMC cache <device>.\n"
+               "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
+       },
        { do_read_csd, -1,
          "csd read", "<device path>\n"
                  "Print CSD data from <device path>.\n"
mmc.h +1 -0
index 9fc22ec..2bb346b 100644
@@ -152,6 +152,7 @@
 #define EXT_CSD_DATA_SECTOR_SIZE       61 /* R */
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1     53
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0     52
+#define EXT_CSD_FLUSH_CACHE     32
 #define EXT_CSD_CACHE_CTRL             33
 #define EXT_CSD_MODE_CONFIG            30
 #define EXT_CSD_MODE_OPERATION_CODES   29      /* W */
mmc_cmds.c +6 -0
index b16ac69..1b26f31 100644
@@ -2870,6 +2870,56 @@ int do_cache_dis(int nargs, char **argv)
        return do_cache_ctrl(0, nargs, argv);  }
 
+int do_cache_flush(int nargs, char **argv) {
+       __u8 ext_csd[512];
+       int fd, ret;
+       char *device;
+
+       device = argv[1];

mmc_cmds.c int do_cache_dis(int nargs, char **argv) · line 2887

Quoted 10 lines
mmc_cmds.c +10 -0
+
+       fd = open(device, O_RDWR);
+       if (fd < 0) {
+               perror("open");
+               exit(1);
+       }
+
+       ret = read_extcsd(fd, ext_csd);
+       if (ret) {
+               fprintf(stderr, "Could not read EXT_CSD from %s\n", device);

How about reporting what kind of error you got?

Quoted 10 lines
mmc_cmds.c +10 -0
+               exit(1);
+       }
+
+       if (ext_csd[EXT_CSD_REV] < EXT_CSD_REV_V5_0) {
+               fprintf(stderr,
+                       "The CACHE FLUSH option is only availabe on devices >= "
+                       "MMC 5.0 %s\n", device);
+               exit(1);
+       }
+

mmc_cmds.c int do_cache_dis(int nargs, char **argv) · line 2909

Quoted 10 lines
mmc_cmds.c +10 -0
+       /* If the cache size is zero, this device does not have a cache */
+       if (!(ext_csd[EXT_CSD_CACHE_SIZE_3] ||
+                       ext_csd[EXT_CSD_CACHE_SIZE_2] ||
+                       ext_csd[EXT_CSD_CACHE_SIZE_1] ||
+                       ext_csd[EXT_CSD_CACHE_SIZE_0])) {
+               fprintf(stderr,
+                       "The CACHE option is not available on %s\n",
+                       device);
+               exit(1);
+       }

I think you need to check CACHE_EN bit in CACHE_CTRL [33] instead?

mmc_cmds.c int do_cache_dis(int nargs, char **argv) · line 2915

Quoted 6 lines
mmc_cmds.c +6 -0
+
+       ret = write_extcsd_value(fd, EXT_CSD_FLUSH_CACHE, 1, 0);
+       if (ret) {
+               fprintf(stderr,
+                       "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
+                       EXT_CSD_FLUSH_CACHE, EXT_CSD_FLUSH_CACHE, device);

Here also, otherwise do you really need ret?

mmc_cmds.c int do_cache_dis(int nargs, char **argv) · line 2920

Quoted 5 lines
mmc_cmds.c +5 -0
+               exit(1);
+       }
+
+       close(fd);
+       return ret;

ret is 0 here?

Thanks,
Avri

Quoted 17 lines
mmc_cmds.c +2 -0
+}
+
 static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)  {
        int ret = 0;
mmc_cmds.h +1 -1
index ce35d3e..f767deb 100644
@@ -50,6 +50,7 @@ int do_rpmb_sec_wp_mode_clear(int nargs, char
 **argv);  int do_rpmb_sec_wp_en_read(int nargs, char **argv);  int
 do_cache_en(int nargs, char **argv);  int do_cache_dis(int nargs, char **argv);
+int do_cache_flush(int nargs, char **argv);
 int do_ffu(int nargs, char **argv);
 int do_opt_ffu1(int nargs, char **argv);  int do_opt_ffu2(int nargs, char
 **argv);
--

2.43.0

#3 lore raw

I update docs/HOWTO.rst, add check to not flush if cache is disabled, and other minor change that you suggest, but I not update log message because detail error message is printed inside read/write_extcsd.

I split my patch in 3 because the first 2 does not add nothing but only do the same change that you suggest for the other command that involve 'cache'

#4 lore raw

PATCH 1/3 mmc-utils: Doc: add missing documentation for cache commands

---
 docs/HOWTO.rst | 10 ++++++++++
 1 file changed, 10 insertions(+)
docs/HOWTO.rst +10 -0
index f2f2f35..9b1f1a8 100644
@@ -107,3 +107,13 @@ Running mmc-utils
     ``mmc rpmb secure-wp-enable <rpmb device> <key file>``
         Disable updating WP related EXT_CSD and CSD fields.
         Applicable only if secure write protect mode is enabled.
+
+
+
+    ``mmc cache enable <device>``
+        Enable the eMMC cache feature on <device>.
+        Applicable only if device version >= eMMC4.5.
+
+    ``mmc cache disable <device>``
+        Disable the eMMC cache feature on <device>.
+        Applicable only if device version >= eMMC4.5.
Signature · 1 line
2.43.0
#5 lore raw

PATCH 2/3 mmc-utils: 1 parameter is the required number not the minimum

In reply to Michele Dionisio
---
 mmc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mmc.c +2 -2
index 6770a45..7433755 100644
@@ -221,12 +221,12 @@ static struct Command commands[] = {
 		  "or read without verification\n"
 		  "  $ mmc rpmb secure-wp-en-read /dev/mmcblk0 /dev/mmcblk0rpmb",
 	},
-	{ do_cache_en, -1,
+	{ do_cache_en, 1,
 	  "cache enable", "<device>\n"
 		"Enable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
 	},
-	{ do_cache_dis, -1,
+	{ do_cache_dis, 1,
 	  "cache disable", "<device>\n"
 		"Disable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
Signature · 1 line
2.43.0
#6 lore raw

PATCH 3/3 mmc-utils: add ability to flush optional eMMC cache

In reply to Michele Dionisio

From: Michele Dionisio <[email protected]>

The MMC 5.0 instroduce command to flush cache. This feature can be use on embedded device there power is not stable.

---
 docs/HOWTO.rst |  4 ++++
 man/mmc.1      |  4 ++++
 mmc.c          |  5 ++++
 mmc.h          |  1 +
 mmc_cmds.c     | 64 +++++++++++++++++++++++++++++++++++++++++++++-----
 mmc_cmds.h     |  1 +
 6 files changed, 73 insertions(+), 6 deletions(-)
docs/HOWTO.rst +4 -0
index 9b1f1a8..ab2e255 100644
@@ -117,3 +117,7 @@ Running mmc-utils
     ``mmc cache disable <device>``
         Disable the eMMC cache feature on <device>.
         Applicable only if device version >= eMMC4.5.
+
+    ``mmc cache flush <device>``
+        Flush the eMMC cache for <device>.
+        Applicable only if device version >= eMMC5.0.
man/mmc.1 +4 -0
index bccabf3..665fe6d 100644
@@ -109,6 +109,10 @@ NOTE! The cache is an optional feature on devices >= eMMC4.5.
 Disable the eMMC cache feature on <device>.
 NOTE! The cache is an optional feature on devices >= eMMC4.5.
 .TP
+.BR "cache flush <device>"
+Flush the eMMC cache on <device>.
+NOTE! The cache is an optional feature on devices >= eMMC5.0.
+.TP
 .BR "<cmd> --help"
 Show detailed help for a command or subset of commands.
 
mmc.c +5 -0
index 7433755..bd9d498 100644
@@ -231,6 +231,11 @@ static struct Command commands[] = {
 		"Disable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
 	},
+	{ do_cache_flush, 1,
+	  "cache flush", "<device>\n"
+		"flush the eMMC cache <device>.\n"
+		"NOTE! The cache is an optional feature on devices >= eMMC5.0.",
+	},
 	{ do_read_csd, -1,
 	  "csd read", "<device path>\n"
 		  "Print CSD data from <device path>.\n"
mmc.h +1 -0
index 9fc22ec..2bb346b 100644
@@ -152,6 +152,7 @@
 #define EXT_CSD_DATA_SECTOR_SIZE	61 /* R */
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1	53
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0	52
+#define EXT_CSD_FLUSH_CACHE     32
 #define EXT_CSD_CACHE_CTRL		33
 #define EXT_CSD_MODE_CONFIG		30
 #define EXT_CSD_MODE_OPERATION_CODES	29	/* W */
mmc_cmds.c +58 -6

76 lines not on this page · raw message

mmc_cmds.h +1 -0
index ce35d3e..f767deb 100644
@@ -50,6 +50,7 @@ int do_rpmb_sec_wp_mode_clear(int nargs, char **argv);
 int do_rpmb_sec_wp_en_read(int nargs, char **argv);
 int do_cache_en(int nargs, char **argv);
 int do_cache_dis(int nargs, char **argv);
+int do_cache_flush(int nargs, char **argv);
 int do_ffu(int nargs, char **argv);
 int do_opt_ffu1(int nargs, char **argv);
 int do_opt_ffu2(int nargs, char **argv);
Signature · 1 line
2.43.0
#7 lore raw
In reply to Michele Dionisio
Quoted 2 lines

eMMC 5.0 introduce command to flush cache. Linux Kernel support it, so it can be nice to have this feature also in mmc-utils.

Maybe you can drop the cover letter and elaborate the commit log of patch 1/1 with these lines.
Also maybe you can give some stringer line of reasoning beyond "nice" as the cash is constantly flushed on every runtime suspend.

Thanks,
Avri

Quoted 20 lines

I would appreciate any comments on the usefulness of the change and a review.

Regards
Michele

Michele Dionisio (1):
mmc-utils: add ability to flush optional eMMC cache

 man/mmc.1  |  4 ++++
 mmc.c      |  5 +++++
 mmc.h      |  1 +
 mmc_cmds.c | 50
++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 5 files changed, 61 insertions(+)

--
2.43.0