Add sysfs entries to facilitate UFS UniPro QoS monitoring
Subsystems (2)
v2 cover Add sysfs entries to facilitate UFS UniPro QoS monitoring
This series introduces two sysfs entries to the UFS core to improve the observability of UFS Link and HBA states from userspace.
While userspace can currently configure UniPro QoS monitoring via UFS BSG, tracking events requires constant polling of UniPro attributes. This series enables event-driven monitoring by using sysfs_notify_dirent(), allowing the driver to proactively signal userspace via poll() whenever a DME QoS interrupt occurs.
Additionally, UFS host or device resets triggered by error handling can reset UniPro QoS monitoring attributes. The second patch introduces a ufshcd_state sysfs entry, allowing userspace to monitor the HBA state and determine when a re-configuration of QoS attributes is necessary following a reset.
Together, these changes provide an efficient, low-overhead mechanism for userspace to perform real-time link quality monitoring and reporting.
v1 -> v2:
1. Removed a blank line in the first patch in v1.
2. Updated texts in cover letter.
Can Guo (2):
scsi: ufs: core: Add support to notify userspace of UniPro QoS events
scsi: ufs: core: Add a sysfs entry for ufshcd_state
Documentation/ABI/testing/sysfs-driver-ufs | 19 +++++++++ drivers/ufs/core/ufs-sysfs.c | 48 ++++++++++++++++++++++ drivers/ufs/core/ufshcd-priv.h | 6 +++ drivers/ufs/core/ufshcd.c | 17 ++++++-- include/ufs/ufshcd.h | 6 +++ include/ufs/ufshci.h | 1 + 6 files changed, 94 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 scsi: ufs: core: Add support to notify userspace of UniPro QoS events
The UniPro stack manages to repair many potential Link problems without the need to notify the Application Layer. Repair mechanisms of the stack include L2 re-transmission and successful handling of PA_INIT.req. Nevertheless, any successful repair sequence requires Link bandwidth that is no longer vailable for the Application. Therefore, it may be useful for an Application to understand how often such repair attempts are made.
The DME implements Quality of Service monitoring using a simple counting scheme, counting error events and comparing them against the number of correctly received or transmitted bytes. When the error counter exceeds a programmed threshold before the byte counter overflows, a DME_QoS.ind is issued to the Application and both counters are reset. When the byte counter overflows before the error counter has reached the programmed threshold, both counters are reset without triggering a DME_QoS.ind.
The DME provides Link quality monitoring for the following purposes:
1. Detection of re-occurring repaired fatal error conditions on the Link (PA_INIT loop). This kind of detection is useful if capabilities exchanged between local and peer permit a potential operation at a higher M-PHY Gear, but the physical interconnect between local and peer Device does not, or, after Line quality degradation, no longer satisfies channel characteristics.
2. Detection of degraded inbound or outbound Link quality, to allow an Application to issue an ADAPT sequence for a Link running in HS-G4 or higher HS Gears. This kind of detection is used to monitor a slowly degrading Link quality, e.g., one being affected by temperature and voltage variations, against the expected M-PHY bit error rate.
Userspace can configure and enable UniPro QoS via UniPro QoS Attributes (via UFS BSG) and get notified by dme_qos_notification without polling UniPro QoS Status attribute.
--- Documentation/ABI/testing/sysfs-driver-ufs | 10 ++++++++ drivers/ufs/core/ufs-sysfs.c | 30 ++++++++++++++++++++++ drivers/ufs/core/ufshcd-priv.h | 6 +++++ drivers/ufs/core/ufshcd.c | 15 ++++++++--- include/ufs/ufshcd.h | 6 +++++ include/ufs/ufshci.h | 1 + 6 files changed, 65 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-driver-ufs +10 -0
@@ -1768,3 +1768,13 @@ Description: ==================== =========================== The attribute is read only. + +What: /sys/bus/platform/drivers/ufshcd/*/dme_qos_notification +What: /sys/bus/platform/devices/*.ufs/dme_qos_notification +Date: February 2026 +Contact: Can Guo <[email protected]> +Description: + This attribute shows and clears the DME Quality of Service + notification from UFSHCI UECDME. + + The attribute is read/write.
drivers/ufs/core/ufs-sysfs.c +30 -0
@@ -605,6 +605,34 @@ static ssize_t device_lvl_exception_id_show(struct device *dev,return sysfs_emit (buf,"%llu\n" , exception_id); } +static ssize_t dme_qos_notification_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +struct ufs_hba *hba =dev_get_drvdata (dev); + +return sysfs_emit (buf,"0x%x\n" ,atomic_read (&hba->dme_qos_notification)); +} + +static ssize_t dme_qos_notification_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct ufs_hba *hba =dev_get_drvdata (dev); +unsigned int value; + +if (kstrtouint (buf,0 , &value)) +return -EINVAL ; + +/* the only supported usecase is to reset the dme_qos_notification */ +if (value) +return -EINVAL ; + +atomic_set (&hba->dme_qos_notification,0 ); + +return count; +} +static DEVICE_ATTR_RW (rpm_lvl);static DEVICE_ATTR_RO (rpm_target_dev_state);static DEVICE_ATTR_RO (rpm_target_link_state);
@@ -621,6 +649,7 @@ static DEVICE_ATTR_RW(pm_qos_enable);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 struct attribute *ufs_sysfs_ufshcd_attrs[] = { &dev_attr_rpm_lvl.attr,
@@ -639,6 +668,7 @@ static struct attribute *ufs_sysfs_ufshcd_attrs[] = { &dev_attr_critical_health.attr, &dev_attr_device_lvl_exception_count.attr, &dev_attr_device_lvl_exception_id.attr, + &dev_attr_dme_qos_notification.attr, NULL };
drivers/ufs/core/ufshcd-priv.h +6 -0
@@ -446,4 +446,10 @@ static inline void ufs_rpmb_remove(struct ufs_hba *hba) }#endif +static inline void sysfs_notify_dirent_safe (struct kernfs_node *sd) +{ +if (sd) +sysfs_notify_dirent (sd); +} +#endif /* _UFSHCD_PRIV_H_ */
drivers/ufs/core/ufshcd.c +12 -3
@@ -6962,10 +6962,17 @@ static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba) } reg =ufshcd_readl (hba,REG_UIC_ERROR_CODE_DME ); -if ((reg &UIC_DME_ERROR ) && - (reg &UIC_DME_ERROR_CODE_MASK )) { +if (reg &UIC_DME_ERROR ) {ufshcd_update_evt_hist (hba,UFS_EVT_DME_ERR , reg); - hba->uic_error |=UFSHCD_UIC_DME_ERROR ; + +if (reg &UIC_DME_ERROR_CODE_MASK ) + hba->uic_error |=UFSHCD_UIC_DME_ERROR ; + +if (reg &UIC_DME_QOS_MASK ) { +atomic_set (&hba->dme_qos_notification, reg &UIC_DME_QOS_MASK ); +sysfs_notify_dirent_safe (hba->dme_qos_sysfs_handle); + } + retval |=IRQ_HANDLED ; }
@@ -11044,6 +11051,8 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) goto out_disable;ufs_sysfs_add_nodes (hba->dev); + hba->dme_qos_sysfs_handle =sysfs_get_dirent (hba->dev->kobj.sd, +"dme_qos_notification" );async_schedule (ufshcd_async_scan, hba);device_enable_async_suspend (dev);
include/ufs/ufshcd.h +6 -0
@@ -943,6 +943,8 @@ enum ufshcd_mcq_opr {* @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. +* @dme_qos_notification: UFS host controller DME QoS notification +* @dme_qos_sysfs_handle: handle for 'dme_qos_notification' sysfs entry * @rpmbs: list of OP-TEE RPMB devices (one per RPMB region) */ struct ufs_hba {
@@ -1116,6 +1118,10 @@ struct ufs_hba {int critical_health_count;atomic_t dev_lvl_exception_count;u64 dev_lvl_exception_id; + +atomic_t dme_qos_notification; +struct kernfs_node *dme_qos_sysfs_handle; +u32 vcc_off_delay_us;struct list_head rpmbs; };
include/ufs/ufshci.h +1 -0
@@ -271,6 +271,7 @@ enum {/* UECDME - Host UIC Error Code DME 48h */ #define UIC_DME_ERROR 0x80000000#define UIC_DME_ERROR_CODE_MASK 0x1 +#define UIC_DME_QOS_MASK 0xE/* UTRIACR - Interrupt Aggregation control register - 0x4Ch */ #define INT_AGGR_TIMEOUT_VAL_MASK 0xFF
Signature · 1 line
2.34.1
Bart Van Assche
reviewerRe: [PATCH v2 1/2] scsi: ufs: core: Add support to notify userspace of UniPro QoS events
Documentation/ABI/testing/sysfs-driver-ufs Description: · line 1780
Quoted 9 lines · Can Guo
Documentation/ABI/testing/sysfs-driver-ufs +9
-0+What: /sys/bus/platform/drivers/ufshcd/*/dme_qos_notification +What: /sys/bus/platform/devices/*.ufs/dme_qos_notification +Date: February 2026 +Contact: Can Guo <[email protected]> +Description: + This attribute shows and clears the DME Quality of Service + notification from UFSHCI UECDME. + + The attribute is read/write.
The above text is incomplete. It should explain that dme_qos_notification is a bitfield, what the meaning of the bits in this bitfield are, when this bitfield is updated, that the only value that can be written into this bitfield is 0 and also what the effect of writing 0 into this bitfield is.
drivers/ufs/core/ufshcd-priv.h static inline void ufs_rpmb_remove(struct ufs_hba *hba) · line 453
Quoted 13 lines
drivers/ufs/core/ufshcd-priv.h +5
-0@@ -446,4 +446,10 @@ static inline void ufs_rpmb_remove(struct ufs_hba *hba) }#endif +static inline void sysfs_notify_dirent_safe (struct kernfs_node *sd) +{ +if (sd) +sysfs_notify_dirent (sd); +}
This function is very short and is not used outsidedrivers/ufs/core/ufshcd.c. Is it really needed to introduce this function? If this function is preserved, please consider moving it into drivers/ufs/core/ufshcd.c.
drivers/ufs/core/ufshcd-priv.h int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) · line 11058
Quoted 9 lines
drivers/ufs/core/ufshcd-priv.h +2
-0@@ -11044,6 +11051,8 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) goto out_disable;ufs_sysfs_add_nodes (hba->dev); + hba->dme_qos_sysfs_handle =sysfs_get_dirent (hba->dev->kobj.sd, +"dme_qos_notification" );async_schedule (ufshcd_async_scan, hba);device_enable_async_suspend (dev);
Where is the sysfs_put() call that corresponds to the above sysfs_get_dirent() call?
Quoted 1 line
+ * @dme_qos_notification: UFS host controller DME QoS notification
Please explain also here that this is a bitfield and where the meaning of the bits in this bitfield are defined.
Thanks,
Bart.
Re: [PATCH v2 1/2] scsi: ufs: core: Add support to notify userspace of UniPro QoS events
Hi Bart,
Quoted 17 lines · Bart Van Assche
Documentation/ABI/testing/sysfs-driver-ufs Description: · line 1780
Quoted 9 lines · Can Guo
Documentation/ABI/testing/sysfs-driver-ufs +9
-0+What: /sys/bus/platform/drivers/ufshcd/*/dme_qos_notification +What: /sys/bus/platform/devices/*.ufs/dme_qos_notification +Date: February 2026 +Contact: Can Guo <[email protected]> +Description: + This attribute shows and clears the DME Quality of Service + notification from UFSHCI UECDME. + + The attribute is read/write.The above text is incomplete. It should explain that
dme_qos_notificationis a bitfield, what the meaning of the bits in this bitfield are, when this bitfield is updated, that the only value that can be written into this bitfield is 0 and also what the effect of writing 0 into this bitfield is.
Done.
Quoted 10 lines
Quoted 10 lines
drivers/ufs/core/ufshcd-priv.h +0
-0@@ -446,4 +446,10 @@ static inline void ufs_rpmb_remove(struct ufs_hba *hba) } #endif +static inline void sysfs_notify_dirent_safe(struct kernfs_node *sd)
Quoted 8 lines
drivers/ufs/core/ufshcd-priv.h static inline void ufs_rpmb_remove(struct ufs_hba *hba) · line 453
Quoted 4 lines
drivers/ufs/core/ufshcd-priv.h +4
-0+{ + if (sd) + sysfs_notify_dirent(sd); +}This function is very short and is not used outside
drivers/ufs/core/ufshcd.c. Is it really needed to introduce this function? If this function is preserved, please consider moving it intodrivers/ufs/core/ufshcd.c.
Done.
Quoted 5 lines
drivers/ufs/core/ufshcd-priv.h int ufshcd_init(struct ufs_hba *hba, void
Quoted 2 lines
drivers/ufs/core/ufshcd-priv.h +0
-0@@ -11044,6 +11051,8 @@ int ufshcd_init(struct ufs_hba *hba, void
__iomem*mmio_base, unsigned int irq)Quoted 2 lines
drivers/ufs/core/ufshcd-priv.h +0
-0goto out_disable;
Quoted 8 lines
drivers/ufs/core/ufshcd-priv.h int ufshcd_init(struct ufs_hba *hba, void · line 11058
Quoted 6 lines
drivers/ufs/core/ufshcd-priv.h +2
-0ufs_sysfs_add_nodes(hba->dev); + hba->dme_qos_sysfs_handle = sysfs_get_dirent(hba->dev->kobj.sd, + "dme_qos_notification"); async_schedule(ufshcd_async_scan, hba); device_enable_async_suspend(dev);Where is the
sysfs_put()call that corresponds to the abovesysfs_get_dirent()call?
Good catch, I missed it.. Will add it in next version.
Quoted 5 lines
Quoted 2 lines
+ * @dme_qos_notification: UFS host controller DME QoS notification
Please explain also here that this is a bitfield and where the meaning of the bits in this bitfield are defined.
Done.
Quoted 4 lines
Thanks,
Bart.
PATCH v2 2/2 scsi: ufs: core: Add a sysfs entry for ufshcd_state
In reply to Can GuoAdd a sysfs entry for ufshcd_state, such that userspace can check and track the state transitions of hba.
--- 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(+)
Documentation/ABI/testing/sysfs-driver-ufs +9 -0
@@ -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 <[email protected]> +Description: + This attribute shows the state of ufshcd. + + The attribute is read only.
drivers/ufs/core/ufs-sysfs.c +18 -0
@@ -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 };
drivers/ufs/core/ufshcd.c +2 -0
@@ -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);
Signature · 1 line
2.34.1
Bart Van Assche
reviewerRe: [PATCH v2 2/2] scsi: ufs: core: Add a sysfs entry for ufshcd_state
Documentation/ABI/testing/sysfs-driver-ufs Description: · line 1789
Quoted 8 lines · Can Guo
Documentation/ABI/testing/sysfs-driver-ufs +8
-0+What: /sys/bus/platform/drivers/ufshcd/*/ufshcd_state +What: /sys/bus/platform/devices/*.ufs/ufshcd_state +Date: February 2026 +Contact: Can Guo <[email protected]> +Description: + This attribute shows the state of ufshcd. + + The attribute is read only.
Please expand "state of ufshcd", e.g. into "state of the UFS host controller driver".
drivers/ufs/core/ufs-sysfs.c static const char * const ufs_hid_states[] = { · line 106
Quoted 7 lines
drivers/ufs/core/ufs-sysfs.c +7
-0+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" , +};
Please follow the kernel coding style with regard to spaces around "*".
drivers/ufs/core/ufs-sysfs.c static ssize_t dme_qos_notification_store(struct device *dev, · line 650
Quoted 7 lines
drivers/ufs/core/ufs-sysfs.c +7
-0+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]); +}
In the above function, please check that hba->ufshcd_state does not exceed the bounds of the ufshcd_states[] array and also that ufshcd_states[hba->ufshcd_state] is not NULL.
drivers/ufs/core/ufshcd.c static void ufshcd_process_probe_result(struct ufs_hba *hba, · line 7924
Quoted 13 lines
drivers/ufs/core/ufshcd.c +2
-0@@ -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);
Shouldn't there be one sysfs_notify(&hba->dev->kobj, NULL, "ufshcd_state") call after every hba->ufshcd_state change?
Thanks,
Bart.
Re: [PATCH v2 2/2] scsi: ufs: core: Add a sysfs entry for ufshcd_state
Hi Bart,
Quoted 34 lines · Bart Van Assche
Documentation/ABI/testing/sysfs-driver-ufs Description: · line 1789
Quoted 8 lines · Can Guo
Documentation/ABI/testing/sysfs-driver-ufs +8
-0+What: /sys/bus/platform/drivers/ufshcd/*/ufshcd_state +What: /sys/bus/platform/devices/*.ufs/ufshcd_state +Date: February 2026 +Contact: Can Guo <[email protected]> +Description: + This attribute shows the state of ufshcd. + + The attribute is read only.Please expand "state of ufshcd", e.g. into "state of the UFS host controller driver".
Quoted 8 lines
+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",
+};Please follow the kernel coding style with regard to spaces around "*".
drivers/ufs/core/ufs-sysfs.c static ssize_t dme_qos_notification_store(struct device *dev, · line 650
Quoted 7 lines
drivers/ufs/core/ufs-sysfs.c +7
-0+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]); +}In the above function, please check that
hba->ufshcd_statedoes not exceed the bounds of the ufshcd_states[] array and also that ufshcd_states[hba->ufshcd_state] is not NULL.
Quoted 15 lines
Quoted 12 lines
drivers/ufs/core/ufshcd.c +1
-0@@ -7917,6 +7917,8 @@ static void ufshcd_process_probe_result(structufs_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)),Quoted 1 line
hba->curr_dev_pwr_mode,hba->uic_link_state);Shouldn't there be one
sysfs_notify(&hba->dev->kobj, NULL, "ufshcd_state")call after everyhba->ufshcd_statechange?
Thanks for your review. My first thinking was to indicate to userspace that DME QoS monitor has been reset by host. But on second thought, it would be much simpler if I just use Bit[0] in dme_qos_nofitication attribute to communicate that information to userspace, because DME QoS events are mapped to Bit[3:1], meaning Bit[0] is free anyways. I am dropping this change in next version.
Thanks,
Can Guo.
Quoted 4 lines
Thanks,
Bart.