Skip to contentSkip to conversation

Add sysfs entries to facilitate UFS UniPro QoS monitoring

PATCH Superseded Can Guo 2 participants 4 messages Mar 1 – Mar 4

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Can Guo

author
#0 lore raw

v3 cover Add sysfs entries to facilitate UFS UniPro QoS monitoring

While userspace can currently configure UniPro Quality of Service (QoS) monitoring via UFS BSG, tracking events requires constant polling of UniPro attributes. Additionally, UFS host reset caused by err handling or resuming (from LPM) can reset UniPro QoS monitoring attributes.

This series introduces a sysfs attribute to the UFS core to improve the observability of DME QoS monitoring. Userspace can configure and enable UniPro QoS monitor via UniPro QoS Attributes (using UFS BSG) and get notified by the proposed dme_qos_notification attribute without polling UniPro QoS Status attribute. The dme_qos_notification attribute is a bitfield with the following bit assignments:

Bit Description
=== ======================================
0 DME QoS Monitor has been reset by host
1 QoS from TX is detected
2 QoS from RX is detected
3 QoS from PA_INIT is detected

v2 -> v3:
1. Updated texts in cover letter.
2. Removed the second patch in v2.
3. Incorporated comments from Bart.
2. Improved the Documentation writeup in the first patch.
3. Added sysfs_put() in ufshcd_hba_exit().
4. Used Bit[0] in dme_qos_notification attribute to indicate DME QoS has been reset by host.

v1 -> v2:
1. Removed a blank line in the first patch in v1.
2. Updated texts in cover letter.

Can Guo (1):
scsi: ufs: core: Add support to notify userspace of UniPro QoS events

 Documentation/ABI/testing/sysfs-driver-ufs | 23 +++++++++++++++++
 drivers/ufs/core/ufs-sysfs.c               | 30 ++++++++++++++++++++++
 drivers/ufs/core/ufshcd.c                  | 24 ++++++++++++++---
 include/ufs/ufshcd.h                       |  9 +++++++
 include/ufs/ufshci.h                       |  1 +
 5 files changed, 84 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/1 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. The dme_qos_notification attribute is a bitfield with the following bit assignments:

Bit Description
=== ======================================
0 DME QoS Monitor has been reset by host
1 QoS from TX is detected
2 QoS from RX is detected
3 QoS from PA_INIT is detected

---
 Documentation/ABI/testing/sysfs-driver-ufs | 23 +++++++++++++++++
 drivers/ufs/core/ufs-sysfs.c               | 30 ++++++++++++++++++++++
 drivers/ufs/core/ufshcd.c                  | 24 ++++++++++++++---
 include/ufs/ufshcd.h                       |  9 +++++++
 include/ufs/ufshci.h                       |  1 +
 5 files changed, 84 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-driver-ufs +23 -0
index a90612ab5780..3362525adc93 100644
@@ -1768,3 +1768,26 @@ 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 reports and clears pending DME (Device Management
+		Entity) Quality of Service (QoS) notifications. This attribute
+		is a bitfield with the following bit assignments:
+
+		Bit	Description
+		===	======================================
+		0	DME QoS Monitor has been reset by host
+		1	QoS from TX is detected
+		2	QoS from RX is detected
+		3	QoS from PA_INIT is detected
+
+		Reading this attribute returns the pending DME QoS notification
+		bits. Writing '0' to this attribute clears pending DME QoS
+		notification bits. Writing any non-zero value is invalid and
+		will be rejected.
+
+		The attribute is read/write.
drivers/ufs/core/ufs-sysfs.c +30 -0
index 384d958615d7..99af3c73f1af 100644
@@ -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.c +21 -3
index 8349fe2090db..ccd9cf228210 100644
@@ -6962,10 +6962,19 @@ 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);
+			if (hba->dme_qos_sysfs_handle)
+				sysfs_notify_dirent(hba->dme_qos_sysfs_handle);
+		}
+
 		retval |= IRQ_HANDLED;
 	}
 
@@ -9097,6 +9106,12 @@ static int ufshcd_post_device_init(struct ufs_hba *hba)
 
 	/* UFS device is also active now */
 	ufshcd_set_ufs_dev_active(hba);
+
+	/* Indicate that DME QoS Monitor has been reset */
+	atomic_set(&hba->dme_qos_notification, 0x1);
+	if (hba->dme_qos_sysfs_handle)
+		sysfs_notify_dirent(hba->dme_qos_sysfs_handle);
+
 	ufshcd_force_reset_auto_bkops(hba);
 
 	ufshcd_set_timestamp_attr(hba);
@@ -9729,6 +9744,7 @@ static void ufshcd_hba_exit(struct ufs_hba *hba)
 		hba->is_powered = false;
 		ufs_put_device_desc(hba);
 	}
+	sysfs_put(hba->dme_qos_sysfs_handle);
 }
 
 static int ufshcd_execute_start_stop(struct scsi_device *sdev,
@@ -11044,6 +11060,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 +9 -0
index 8563b6648976..182f301c11e7 100644
@@ -943,6 +943,11 @@ 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: Bitfield of pending DME Quality of Service (QoS)
+ *	events. Bits[3:1] reflect the corresponding bits of UIC DME Error Code
+ *	field within the Host Controller's UECDME register. Bit[0] is a flag
+ *	indicating that the DME QoS Monitor has been reset by the host.
+ * @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 +1121,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
index 806fdaf52bd9..49a3a279e448 100644
@@ -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
#2 lore raw

Re: [PATCH v3 1/1] scsi: ufs: core: Add support to notify userspace of UniPro QoS events

Documentation/ABI/testing/sysfs-driver-ufs Description: · line 1774

Quoted 9 lines · Can Guo
(quoted diff) +0 -0
@@ -1768,3 +1768,26 @@ Description:

                ====================   ===========================

                The attribute is read only.

(quoted diff) +2 -0
+
+What:         

/sys/bus/platform/drivers/ufshcd/*/dme_qos_notification

Documentation/ABI/testing/sysfs-driver-ufs +2 -0
+What:          /sys/bus/platform/devices/*.ufs/dme_qos_notification
+Date:          February 2026

March 2026

drivers/ufs/core/ufshcd.c static int ufshcd_post_device_init(struct · line 9113

Quoted 9 lines
drivers/ufs/core/ufshcd.c +3 -0
@@ -9097,6 +9106,12 @@ static int ufshcd_post_device_init(struct
 ufs_hba *hba)
 
         /* UFS device is also active now */
         ufshcd_set_ufs_dev_active(hba);
+
+       /* Indicate that DME QoS Monitor has been reset */
+       atomic_set(&hba->dme_qos_notification, 0x1);
 

Reset value should be 0?

include/ufs/ufshcd.h struct ufs_hba { · line 1126

Quoted 7 lines
include/ufs/ufshcd.h +3 -0
@@ -1116,6 +1121,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;

Why is dme_qos_sysfs_handle necessary?
Wouldn't it be sufficient to use sysfs_notify(&hba->dev->kobj, NULL, "dme_qos_notification"); without checking whether dme_qos_sysfs_handle is NULL?

Thanks
Peter

author
#3 lore raw

Re: [PATCH v3 1/1] scsi: ufs: core: Add support to notify userspace of UniPro QoS events

Quoted 5 lines · Peter Wang (王信友)
Quoted 3 lines · Can Guo
(quoted diff) +0 -0
@@ -1768,3 +1768,26 @@ Description:

                ====================   ===========================

Quoted 8 lines

Documentation/ABI/testing/sysfs-driver-ufs Description: · line 1774

Quoted 6 lines

                The attribute is read only.

(quoted diff) +2 -0
+
+What:         

/sys/bus/platform/drivers/ufshcd/*/dme_qos_notification

Documentation/ABI/testing/sysfs-driver-ufs +2 -0
+What:          /sys/bus/platform/devices/*.ufs/dme_qos_notification
+Date:          February 2026

March 2026

Good catch

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -9097,6 +9106,12 @@ static int ufshcd_post_device_init(struct
 ufs_hba *hba)
 
Quoted 8 lines

drivers/ufs/core/ufshcd.c static int ufshcd_post_device_init(struct ufs_hba *hba) · line 9116

Quoted 6 lines
drivers/ufs/core/ufshcd.c +3 -0
         /* UFS device is also active now */
         ufshcd_set_ufs_dev_active(hba);
+
+       /* Indicate that DME QoS Monitor has been reset */
+       atomic_set(&hba->dme_qos_notification, 0x1);
 

Reset value should be 0?

Bit[0] is used to communicate to userspace that DME QOS has been reset, see also the bit assignments explained in the patch.

Quoted 13 lines

include/ufs/ufshcd.h struct ufs_hba { · line 1126

Quoted 8 lines
include/ufs/ufshcd.h +3 -0
@@ -1116,6 +1121,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;

Why is dme_qos_sysfs_handle necessary?
Wouldn't it be sufficient to use sysfs_notify(&hba->dev->kobj, NULL, "dme_qos_notification"); without checking whether dme_qos_sysfs_handle is NULL?

sysfs_notify() cannot be used under atomic context, i.e., IRQ context.

Thanks,
Can Guo.

Quoted 18 lines

Thanks
Peter

************* MEDIATEK Confidentiality Notice
********************
The information contained in this e-mail message (including any attachments) may be confidential, proprietary, privileged, or otherwise exempt from disclosure under applicable laws. It is intended to be conveyed only to the designated recipient(s). Any use, dissemination, distribution, printing, retaining or copying of this e-mail (including its attachments) by unintended recipient(s) is strictly prohibited and may be unlawful. If you are not an intended recipient of this e-mail, or believe

that you have received this e-mail in error, please notify the sender immediately (by replying to this e-mail), delete any and all copies of this e-mail (including any attachments) from your system, and do not disclose the content of this e-mail to any other person. Thank you!