Skip to contentSkip to conversation

mmc: core: RPMB code improvements

PATCH Superseded Bean Huo 1 participant 4 messages Sep 11, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bean Huo

author
#0 lore raw

v2 cover mmc: core: RPMB code improvements

This patch series improves the RPMB frame handling code in the MMC block driver with several code quality enhancements.

v1--v2:
1. Add fix tag
2. Oncorporate Avri's suggestions

Bean Huo (2):
mmc: core: Fix variable shadowing in mmc_route_rpmb_frames()
mmc: core: Improve RPMB frame handling code

 drivers/mmc/core/block.c    |  34 +++---
 drivers/ufs/core/ufs-rpmb.c | 225 ++++++++++++++++++++++++++++++++++++
 drivers/ufs/core/ufs-rpmb.h |  26 +++++
 3 files changed, 267 insertions(+), 18 deletions(-)
 create mode 100644 drivers/ufs/core/ufs-rpmb.c
 create mode 100644 drivers/ufs/core/ufs-rpmb.h
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 mmc: core: Fix variable shadowing in mmc_route_rpmb_frames()

From: Bean Huo <[email protected]>

Rename the inner 'frm' variable to 'resp_frm' in the write path of mmc_route_rpmb_frames() to avoid shadowing the outer 'frm' variable.

The function declares 'frm' at function scope pointing to the request frame, but then redeclares another 'frm' variable inside the write block pointing to the response frame. This shadowing makes the code confusing and error-prone.

Using 'resp_frm' for the response frame makes the distinction clear and improves code readability.

Fixes 7852028a35f0 ("mmc: block: register RPMB partition with the RPMB subsystem")
---
 drivers/mmc/core/block.c    |   6 +-
 drivers/ufs/core/ufs-rpmb.c | 225 ++++++++++++++++++++++++++++++++++++
 drivers/ufs/core/ufs-rpmb.h |  26 +++++
 3 files changed, 254 insertions(+), 3 deletions(-)
 create mode 100644 drivers/ufs/core/ufs-rpmb.c
 create mode 100644 drivers/ufs/core/ufs-rpmb.h
drivers/mmc/core/block.c +3 -3
index 9cc47bf94804..dd6cffc0df72 100644
@@ -2936,15 +2936,15 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 		return -ENOMEM;
 
 	if (write) {
-		struct rpmb_frame *frm = (struct rpmb_frame *)resp;
+		struct rpmb_frame *resp_frm = (struct rpmb_frame *)resp;
 
 		/* Send write request frame(s) */
 		set_idata(idata[0], MMC_WRITE_MULTIPLE_BLOCK,
 			  1 | MMC_CMD23_ARG_REL_WR, req, req_len);
 
 		/* Send result request frame */
-		memset(frm, 0, sizeof(*frm));
-		frm->req_resp = cpu_to_be16(RPMB_RESULT_READ);
+		memset(resp_frm, 0, sizeof(*resp_frm));
+		resp_frm->req_resp = cpu_to_be16(RPMB_RESULT_READ);
 		set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp,
 			  resp_len);
 
drivers/ufs/core/ufs-rpmb.c new file +225 -0

225 lines not on this page · raw message

drivers/ufs/core/ufs-rpmb.h new file +26 -0
new file mode 100644
index 000000000000..368b80893189
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2025 Micron Technology, Inc.
+ * Copyright (C) 2025 Qualcomm Innovation Center.
+ *
+ * Authors:
+ *	Bean Huo <[email protected]>
+ *	Can Guo <[email protected]>
+ */
+#ifndef UFS_RPMP_H
+#define UFS_RPMB_H
+
+struct ufs_hba;
+
+#ifdef CONFIG_RPMB
+int ufs_rpmb_probe(struct ufs_hba *hba);
+void ufs_rpmb_remove(struct ufs_hba *hba);
+#else
+int ufs_rpmb_probe(struct ufs_hba *hba)
+{
+	return 0;
+}
+void ufs_rpmb_remove(struct ufs_hba *hba) { }
+#endif
+
+#endif /* UFS_RPMB_H */
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/2 mmc: core: Improve RPMB frame handling code

In reply to Bean Huo

From: Bean Huo <[email protected]>

Introduce RPMB_FRAME_SIZE, CHECK_SIZE_NEQ(), and CHECK_SIZE_ALIGNED() macros to replace repetitive sizeof(struct rpmb_frame) checks in mmc_route_rpmb_frames().

---
 drivers/mmc/core/block.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
drivers/mmc/core/block.c +14 -16
index dd6cffc0df72..b32eefcca4b7 100644
@@ -121,6 +121,10 @@ struct rpmb_frame {
 #define RPMB_READ_DATA         0x4    /* Read data from RPMB partition */
 #define RPMB_RESULT_READ       0x5    /* Read result request  (Internal) */
 
+#define RPMB_FRAME_SIZE        sizeof(struct rpmb_frame)
+#define CHECK_SIZE_NEQ(val) ((val) != sizeof(struct rpmb_frame))
+#define CHECK_SIZE_ALIGNED(val) IS_ALIGNED((val), sizeof(struct rpmb_frame))
+
 static DEFINE_MUTEX(block_mutex);
 
 /*
@@ -2864,12 +2868,12 @@ static void set_idata(struct mmc_blk_ioc_data *idata, u32 opcode,
 	 * The size of an RPMB frame must match what's expected by the
 	 * hardware.
 	 */
-	BUILD_BUG_ON(sizeof(struct rpmb_frame) != 512);
+	static_assert(!CHECK_SIZE_NEQ(512), "RPMB frame size must be 512 bytes");
 
 	idata->ic.opcode = opcode;
 	idata->ic.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
 	idata->ic.write_flag = write_flag;
-	idata->ic.blksz = sizeof(struct rpmb_frame);
+	idata->ic.blksz = RPMB_FRAME_SIZE;
 	idata->ic.blocks = buf_bytes /  idata->ic.blksz;
 	idata->buf = buf;
 	idata->buf_bytes = buf_bytes;
@@ -2893,32 +2897,28 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 	if (IS_ERR(md->queue.card))
 		return PTR_ERR(md->queue.card);
 
-	if (req_len < sizeof(*frm))
+	if (req_len < RPMB_FRAME_SIZE)
 		return -EINVAL;
 
 	req_type = be16_to_cpu(frm->req_resp);
 	switch (req_type) {
 	case RPMB_PROGRAM_KEY:
-		if (req_len != sizeof(struct rpmb_frame) ||
-		    resp_len != sizeof(struct rpmb_frame))
+		if (CHECK_SIZE_NEQ(req_len) || CHECK_SIZE_NEQ(resp_len))
 			return -EINVAL;
 		write = true;
 		break;
 	case RPMB_GET_WRITE_COUNTER:
-		if (req_len != sizeof(struct rpmb_frame) ||
-		    resp_len != sizeof(struct rpmb_frame))
+		if (CHECK_SIZE_NEQ(req_len) || CHECK_SIZE_NEQ(resp_len))
 			return -EINVAL;
 		write = false;
 		break;
 	case RPMB_WRITE_DATA:
-		if (req_len % sizeof(struct rpmb_frame) ||
-		    resp_len != sizeof(struct rpmb_frame))
+		if (!CHECK_SIZE_ALIGNED(req_len) || CHECK_SIZE_NEQ(resp_len))
 			return -EINVAL;
 		write = true;
 		break;
 	case RPMB_READ_DATA:
-		if (req_len != sizeof(struct rpmb_frame) ||
-		    resp_len % sizeof(struct rpmb_frame))
+		if (CHECK_SIZE_NEQ(req_len) || !CHECK_SIZE_ALIGNED(resp_len))
 			return -EINVAL;
 		write = false;
 		break;
@@ -2926,10 +2926,8 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 		return -EINVAL;
 	}
 
-	if (write)
-		cmd_count = 3;
-	else
-		cmd_count = 2;
+	/* Write operations require 3 commands, read operations require 2 */
+	cmd_count = write ? 3 : 2;
 
 	idata = alloc_idata(rpmb, cmd_count);
 	if (!idata)
@@ -2943,7 +2941,7 @@ static int mmc_route_rpmb_frames(struct device *dev, u8 *req,
 			  1 | MMC_CMD23_ARG_REL_WR, req, req_len);
 
 		/* Send result request frame */
-		memset(resp_frm, 0, sizeof(*resp_frm));
+		memset(resp_frm, 0, RPMB_FRAME_SIZE);
 		resp_frm->req_resp = cpu_to_be16(RPMB_RESULT_READ);
 		set_idata(idata[1], MMC_WRITE_MULTIPLE_BLOCK, 1, resp,
 			  resp_len);
Signature · 1 line
2.34.1
author
#3 lore raw
In reply to Bean Huo

ignore this serials patch, since it mixed with our other patches, will send v3.

Quoted 18 lines · Bean Huo

This patch series improves the RPMB frame handling code in the MMC block driver with several code quality enhancements.

v1--v2:
    1. Add fix tag
    2. Oncorporate Avri's suggestions

Bean Huo (2):
  mmc: core: Fix variable shadowing in mmc_route_rpmb_frames()
  mmc: core: Improve RPMB frame handling code

 drivers/mmc/core/block.c    |  34 +++--- drivers/ufs/core/ufs-rpmb.c | 225 ++++++++++++++++++++++++++++++++++++ drivers/ufs/core/ufs-rpmb.h |  26 +++++

 3 files changed, 267 insertions(+), 18 deletions(-)

 create mode 100644 drivers/ufs/core/ufs-rpmb.c
 create mode 100644 drivers/ufs/core/ufs-rpmb.