Add OP-TEE based RPMB driver for UFS devices
Subsystems (3)
v7 cover Add OP-TEE based RPMB driver for UFS devices
his patch series introduces OP-TEE based RPMB (Replay Protected Memory Block) support for UFS devices, extending the kernel-level secure storage capabilities that are currently available for eMMC devices.
Previously, OP-TEE required a userspace supplicant to access RPMB partitions, which created complex dependencies and reliability issues, especially during early boot scenarios. Recent work by Linaro has moved core supplicant functionality directly into the Linux kernel for eMMC devices, eliminating userspace dependencies and enabling immediate secure storage access. This series extends the same approach to UFS devices, which are used in enterprise and mobile applications that require secure storage capabilities.
Benefits:
- Eliminates dependency on userspace supplicant for UFS RPMB access
- Enables early boot secure storage access (e.g., fTPM, secure UEFI variables)
- Provides kernel-level RPMB access as soon as UFS driver is initialized
- Removes complex initramfs dependencies and boot ordering requirements
- Ensures reliable and deterministic secure storage operations
- Supports both built-in and modular fTPM configurations.
Prerequisites:
--------------
This patch series depends on commit 7e8242405b94 ("rpmb: move struct rpmb_frame to common header") which has been merged into mainline v6.18-rc2:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7e8242405b94ceac6db820de7d4fd9318cbc1219
v6 --v7:
1. Rebased patch.
v5 -- v6:
1. Added a comment in ufshcd_create_device_id() to warn against modifying the device ID format without understanding its impact.
v4 -- v5:
1. Added helper function ufshcd_create_device_id() to generate unique device identifier by combining manufacturer ID, specification version, model name, serial number (as hex), device version, and manufacture date.
2. Added device_id field to struct ufs_dev_info for storing allocated unique device identifier string.
3. Modified UFS RPMB driver to use device_id instead of just serial_number for creating unique RPMB device identifiers
v3 -- v4:
1. Replaced patch "scsi: ufs: core: Remove duplicate macro definitions" with "scsi: ufs: core: Convert string descriptor format macros to enum" based on feedback from Bart Van Assche
2. Converted SD_ASCII_STD and SD_RAW from boolean macros to enum type for improved code readability
3. Moved ufshcd_read_string_desc() declaration from include/ufs/ufshcd.h to drivers/ufs/core/ufshcd-priv.h since it's not exported
v2 -- v3:
1. Removed patch "rpmb: move rpmb_frame struct and constants to common header". since it has been queued in mmc tree, and added a new patch:
"scsi: ufs: core: Remove duplicate macro definitions"
2. Incorporated suggestions from Jens
3. Added check if Advanced RPMB is enabled, if enabled we will not register UFS OP-TEE RPMB.
v1 -- v2:
1. Added fix tag for patch [2/3]
2. Incorporated feedback and suggestions from Bart
RFC v1 -- v1:
1. Added support for all UFS RPMB regions based on https://github.com/OP-TEE/optee_os/issues/7532
2. Incorporated feedback and suggestions from Bart
Bean Huo (3):
scsi: ufs: core: Convert string descriptor format macros to enum
scsi: ufs: core: fix incorrect buffer duplication in
ufshcd_read_string_desc()
scsi: ufs: core: Add OP-TEE based RPMB driver for UFS devices
drivers/misc/Kconfig | 2 +- drivers/ufs/core/Makefile | 1 + drivers/ufs/core/ufs-rpmb.c | 254 +++++++++++++++++++++++++++++++++ drivers/ufs/core/ufshcd-priv.h | 27 +++- drivers/ufs/core/ufshcd.c | 96 +++++++++++-- include/ufs/ufs.h | 5 + include/ufs/ufshcd.h | 12 +- 7 files changed, 376 insertions(+), 21 deletions(-) create mode 100644 drivers/ufs/core/ufs-rpmb.c
Signature · 1 line
2.34.1
PATCH v7 1/3 scsi: ufs: core: Convert string descriptor format macros to enum
From: Bean Huo <[email protected]>
Convert SD_ASCII_STD and SD_RAW from boolean macros to enum values for improved code readability. This makes ufshcd_read_string_desc() calls self-documenting by using explicit enum values instead of true/false.
Move the ufshcd_read_string_desc() declaration from include/ufs/ufshcd.h to drivers/ufs/core/ufshcd-priv.h since this function is not exported.
--- drivers/ufs/core/ufshcd-priv.h | 14 ++++++++++---- drivers/ufs/core/ufshcd.c | 8 +++----- include/ufs/ufshcd.h | 4 ---- 3 files changed, 13 insertions(+), 13 deletions(-)
drivers/ufs/core/ufshcd-priv.h +10 -4
@@ -80,11 +80,17 @@ int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag);void ufshcd_release_scsi_cmd (struct ufs_hba *hba,struct ufshcd_lrb *lrbp); -#define SD_ASCII_STD true -#define SD_RAW false -int ufshcd_read_string_desc (struct ufs_hba *hba,u8 desc_index, -u8 **buf,bool ascii); +/** +* enum ufs_descr_fmt - UFS string descriptor format +* @SD_RAW: Raw UTF-16 format +* @SD_ASCII_STD: Convert to null-terminated ASCII string +*/ +enum ufs_descr_fmt { +SD_RAW =0 , +SD_ASCII_STD =1 , +}; +int ufshcd_read_string_desc (struct ufs_hba *hba,u8 desc_index,u8 **buf,enum ufs_descr_fmt fmt);int ufshcd_send_uic_cmd (struct ufs_hba *hba,struct uic_command *uic_cmd);int ufshcd_send_bsg_uic_cmd (struct ufs_hba *hba,struct uic_command *uic_cmd);
drivers/ufs/core/ufshcd.c +3 -5
@@ -3774,16 +3774,14 @@ static inline char ufshcd_remove_non_printable(u8 ch)* @desc_index: descriptor index * @buf: pointer to buffer where descriptor would be read, * the caller should free the memory. -* @ascii: if true convert from unicode to ascii characters -* null terminated string. +* @fmt: if %SD_ASCII_STD, convert from UTF-16 to ASCII * * Return: * * string size on success. * * -ENOMEM: on allocation failure * * -EINVAL: on a wrong parameter */ -int ufshcd_read_string_desc (struct ufs_hba *hba,u8 desc_index, -u8 **buf,bool ascii) +int ufshcd_read_string_desc (struct ufs_hba *hba,u8 desc_index,u8 **buf,enum ufs_descr_fmt fmt) {struct uc_string_id *uc_str;u8 *str;
@@ -3812,7 +3810,7 @@ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, goto out; } -if (ascii) { +if (fmt ==SD_ASCII_STD ) {ssize_t ascii_len;int i;/* remove header and divide by 2 to move from UTF16 to UTF8 */
include/ufs/ufshcd.h +0 -4
@@ -1432,10 +1432,6 @@ static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)void ufshcd_auto_hibern8_update (struct ufs_hba *hba,u32 ahit);void ufshcd_fixup_dev_quirks (struct ufs_hba *hba,const struct ufs_dev_quirk *fixups); -#define SD_ASCII_STD true -#define SD_RAW false -int ufshcd_read_string_desc (struct ufs_hba *hba,u8 desc_index, -u8 **buf,bool ascii);void ufshcd_hold (struct ufs_hba *hba);void ufshcd_release (struct ufs_hba *hba);
Signature · 1 line
2.34.1
PATCH v7 2/3 scsi: ufs: core: fix incorrect buffer duplication in ufshcd_read_string_desc()
In reply to Bean HuoFrom: Bean Huo <[email protected]>
The function ufshcd_read_string_desc() was duplicating memory starting from the beginning of struct uc_string_id, which included the length and type fields. As a result, the allocated buffer contained unwanted metadata in addition to the string itself.
The correct behavior is to duplicate only the Unicode character array in the structure. Update the code so that only the actual string content is copied into the new buffer.
--- drivers/ufs/core/ufshcd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +1 -1
@@ -3836,7 +3836,7 @@ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, u8 **buf, enum u str[ret++] ='\0' ; }else { - str =kmemdup (uc_str, uc_str->len,GFP_KERNEL ); + str =kmemdup (uc_str->uc, uc_str->len,GFP_KERNEL );if (!str) { ret = -ENOMEM ; goto out;
Signature · 1 line
2.34.1
PATCH v7 3/3 scsi: ufs: core: Add OP-TEE based RPMB driver for UFS devices
In reply to Bean HuoFrom: Bean Huo <[email protected]>
This patch adds OP-TEE based RPMB support for UFS devices. This enables secure RPMB operations on UFS devices through OP-TEE, providing the same functionality available for eMMC devices and extending kernel-based secure storage support to UFS-based systems.
Benefits of OP-TEE based RPMB implementation:
- Eliminates dependency on userspace supplicant for RPMB access
- Enables early boot secure storage access (e.g., fTPM, secure UEFI variables)
- Provides kernel-level RPMB access as soon as UFS driver is initialized
- Removes complex initramfs dependencies and boot ordering requirements
- Ensures reliable and deterministic secure storage operations
- Supports both built-in and modular fTPM configurations
--- drivers/misc/Kconfig | 2 +- drivers/ufs/core/Makefile | 1 + drivers/ufs/core/ufs-rpmb.c | 254 +++++++++++++++++++++++++++++++++ drivers/ufs/core/ufshcd-priv.h | 13 ++ drivers/ufs/core/ufshcd.c | 86 ++++++++++- include/ufs/ufs.h | 5 + include/ufs/ufshcd.h | 8 +- 7 files changed, 362 insertions(+), 7 deletions(-) create mode 100644 drivers/ufs/core/ufs-rpmb.c
drivers/misc/Kconfig +1 -1
@@ -106,7 +106,7 @@ config PHANTOM config RPMB tristate "RPMB partition interface" - depends on MMC + depends on MMC || SCSI_UFSHCD help Unified RPMB unit interface for RPMB capable devices such as eMMC and UFS. Provides interface for in-kernel security controllers to access
drivers/ufs/core/Makefile +1 -0
@@ -2,6 +2,7 @@ obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o ufshcd-core-y += ufshcd.o ufs-sysfs.o ufs-mcq.o +ufshcd-core-$(CONFIG_RPMB) += ufs-rpmb.o ufshcd-core-$(CONFIG_DEBUG_FS) += ufs-debugfs.o ufshcd-core-$(CONFIG_SCSI_UFS_BSG) += ufs_bsg.o ufshcd-core-$(CONFIG_SCSI_UFS_CRYPTO) += ufshcd-crypto.o
drivers/ufs/core/ufs-rpmb.c new file +254 -0
254 lines not on this page · raw message
drivers/ufs/core/ufshcd-priv.h +13 -0
@@ -419,4 +419,17 @@ static inline u32 ufshcd_mcq_get_sq_head_slot(struct ufs_hw_queue *q) return val / sizeof(struct utp_transfer_req_desc); } +#ifdef CONFIG_RPMB +int ufs_rpmb_probe (struct ufs_hba *hba); +void ufs_rpmb_remove (struct ufs_hba *hba); +#else +static inline int ufs_rpmb_probe (struct ufs_hba *hba) +{ +return 0 ; +} +static inline void ufs_rpmb_remove (struct ufs_hba *hba) +{ +} +#endif +#endif /* _UFSHCD_PRIV_H_ */
drivers/ufs/core/ufshcd.c +82 -4
136 lines not on this page · raw message
include/ufs/ufs.h +5 -0
@@ -651,6 +651,11 @@ struct ufs_dev_info {u8 rtt_cap;/* bDeviceRTTCap */ bool hid_sup; + +/* Unique device ID string (manufacturer+model+serial+version+date) */ +char *device_id; +u8 rpmb_io_size; + u8 rpmb_region_size[4]; };#endif /* End of Header */
include/ufs/ufshcd.h +6 -2
@@ -826,6 +826,7 @@ enum ufshcd_mcq_opr {* @host: Scsi_Host instance of the driver * @dev: device handle * @ufs_device_wlun: WLUN that controls the entire UFS device. +* @ufs_rpmb_wlun: RPMB WLUN SCSI device * @hwmon_device: device instance registered with the hwmon core. * @curr_dev_pwr_mode: active UFS device power mode. * @uic_link_state: active state of the link to the UFS device.
@@ -941,8 +942,8 @@ enum ufshcd_mcq_opr {* @pm_qos_mutex: synchronizes PM QoS request and status updates * @critical_health_count: count of critical health exceptions * @dev_lvl_exception_count: count of device level exceptions since last reset -* @dev_lvl_exception_id: vendor specific information about the -* device level exception event. +* @dev_lvl_exception_id: vendor specific information about the device level exception event. +* @rpmbs: list of OP-TEE RPMB devices (one per RPMB region) */ struct ufs_hba {void __iomem *mmio_base;
@@ -960,6 +961,7 @@ struct ufs_hba {struct Scsi_Host *host;struct device *dev;struct scsi_device *ufs_device_wlun; +struct scsi_device *ufs_rpmb_wlun;#ifdef CONFIG_SCSI_UFS_HWMON struct device *hwmon_device;
@@ -1119,6 +1121,8 @@ struct ufs_hba {u64 dev_lvl_exception_id;u32 vcc_off_delay_us; + +struct list_head rpmbs; };/**
Signature · 1 line
2.34.1
Martin K. Petersen
AppliedBean,
Quoted 3 lines
his patch series introduces OP-TEE based RPMB (Replay Protected Memory Block) support for UFS devices, extending the kernel-level secure storage capabilities that are currently available for eMMC devices.
Applied to 6.19/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen