[PATCH v2 2/2] scsi: ufs: core: Add a sysfs entry for ufshcd_state
From: Can Guo
Date: Tue Feb 24 2026 - 21:31:12 EST
Add a sysfs entry for ufshcd_state, such that userspace can check and
track the state transitions of hba.
Signed-off-by: Can Guo <can.guo@xxxxxxxxxxxxxxxx>
---
Documentation/ABI/testing/sysfs-driver-ufs | 9 +++++++++
drivers/ufs/core/ufs-sysfs.c | 18 ++++++++++++++++++
drivers/ufs/core/ufshcd.c | 2 ++
3 files changed, 29 insertions(+)
diff --git a/Documentation/ABI/testing/sysfs-driver-ufs b/Documentation/ABI/testing/sysfs-driver-ufs
index 665819308b40..339bb1befc9c 100644
--- a/Documentation/ABI/testing/sysfs-driver-ufs
+++ b/Documentation/ABI/testing/sysfs-driver-ufs
@@ -1778,3 +1778,12 @@ Description:
notification from UFSHCI UECDME.
The attribute is read/write.
+
+What: /sys/bus/platform/drivers/ufshcd/*/ufshcd_state
+What: /sys/bus/platform/devices/*.ufs/ufshcd_state
+Date: February 2026
+Contact: Can Guo <can.guo@xxxxxxxxxxxxxxxx>
+Description:
+ This attribute shows the state of ufshcd.
+
+ The attribute is read only.
diff --git a/drivers/ufs/core/ufs-sysfs.c b/drivers/ufs/core/ufs-sysfs.c
index 99af3c73f1af..10804ec6e252 100644
--- a/drivers/ufs/core/ufs-sysfs.c
+++ b/drivers/ufs/core/ufs-sysfs.c
@@ -97,6 +97,14 @@ static const char * const ufs_hid_states[] = {
[DEFRAG_NOT_REQUIRED] = "defrag_not_required",
};
+static const char * const ufshcd_states[] = {
+ [UFSHCD_STATE_RESET] = "reset",
+ [UFSHCD_STATE_OPERATIONAL] = "operational",
+ [UFSHCD_STATE_EH_SCHEDULED_NON_FATAL] = "eh_scheduled_non_fatal",
+ [UFSHCD_STATE_EH_SCHEDULED_FATAL] = "eh_scheduled_fatal",
+ [UFSHCD_STATE_ERROR] = "error",
+};
+
static const char *ufs_hid_state_to_string(enum ufs_hid_state state)
{
if (state < NUM_UFS_HID_STATES)
@@ -633,6 +641,14 @@ static ssize_t dme_qos_notification_store(struct device *dev,
return count;
}
+static ssize_t ufshcd_state_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct ufs_hba *hba = dev_get_drvdata(dev);
+
+ return sysfs_emit(buf, "%s\n", ufshcd_states[hba->ufshcd_state]);
+}
+
static DEVICE_ATTR_RW(rpm_lvl);
static DEVICE_ATTR_RO(rpm_target_dev_state);
static DEVICE_ATTR_RO(rpm_target_link_state);
@@ -650,6 +666,7 @@ static DEVICE_ATTR_RO(critical_health);
static DEVICE_ATTR_RW(device_lvl_exception_count);
static DEVICE_ATTR_RO(device_lvl_exception_id);
static DEVICE_ATTR_RW(dme_qos_notification);
+static DEVICE_ATTR_RO(ufshcd_state);
static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
&dev_attr_rpm_lvl.attr,
@@ -669,6 +686,7 @@ static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
&dev_attr_device_lvl_exception_count.attr,
&dev_attr_device_lvl_exception_id.attr,
&dev_attr_dme_qos_notification.attr,
+ &dev_attr_ufshcd_state.attr,
NULL
};
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index c6c7de7a0603..32a508e1582e 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -7917,6 +7917,8 @@ static void ufshcd_process_probe_result(struct ufs_hba *hba,
hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
spin_unlock_irqrestore(hba->host->host_lock, flags);
+ sysfs_notify(&hba->dev->kobj, NULL, "ufshcd_state");
+
trace_ufshcd_init(hba, ret,
ktime_to_us(ktime_sub(ktime_get(), probe_start)),
hba->curr_dev_pwr_mode, hba->uic_link_state);
--
2.34.1