mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters
@ 2026-11-23  2:57 Po-Wen Kao
  0 siblings, 0 replies; only message in thread
From: Po-Wen Kao @ 2026-11-23  2:57 UTC (permalink / raw)
  To: linux-scsi
  Cc: Konstantin Vyshetsky, Brian Kao, Alim Akhtar, Bart Van Assche,
	Bean Huo, Stanley Jhu, James E.J. Bottomley, Martin K. Petersen,
	Peter Wang, Can Guo, John Garry, Li Qiang, open list

From: Brian Kao <powenkao@google.com>

MIPI UniPro v3.0 introduces the Transport Framing Structure (TFS) with
Reed-Solomon Forward Error Correction (FEC) in the Physical Adapter
(PA) layer, along with five PA MIB attributes (0x15C3-0x15C7) that
count TFS frames with uncorrectable FEC errors, TFS CRC errors, TFS
format errors, and 64-bit corrected FEC symbols. Exposing these
counters allows observing pre-FEC symbol correction and framing errors
for HS-G6 (1b1b) link-margin diagnostics without dropping the link.

Define the PA_TFS_* attributes in include/ufs/unipro.h and expose them
under debugfs:

  /sys/kernel/debug/ufshcd/<dev>/pa_err_stats/{host,device}/
    - fec_err_cnt
    - crc_err_cnt
    - format_err_cnt
    - recovered_fec_symbols

Each file reads the corresponding local (host) or peer (device) PA
attribute and resets it to 0 when '0' is written. Per MIPI UniPro v3.0
Section 5.6.7, writing PA_TFS_RecErrorCountL clears the full 64-bit
PA_TFS_RecErrorCount counter, while reads sample the high/low words
using a bounded hi-lo-hi retry loop to avoid 32-bit rollover tearing.
These hardware counters increment only while the TFS/FEC path (1b1b
line coding) is active and may be reset across link startup, link-off
suspend, or host reset.

Because ufs_debugfs_hba_init() runs before hba->ufs_version and the
device descriptor are probed, support is checked at access time
(attached device W-LUN, UFSHCI >= 5.0, UFS device >= 5.0 for peer
entries, and UFSHCD_STATE_OPERATIONAL under host_sem). Positive UniPro
UIC command result codes from DME helpers are translated to standard
negative errno values before returning to userspace because seq_file
treats a positive ->show() return as SEQ_SKIP and a positive ->write()
return is interpreted as bytes written.

Signed-off-by: Brian Kao <powenkao@google.com>
---
 drivers/ufs/core/ufs-debugfs.c | 215 +++++++++++++++++++++++++++++++++
 include/ufs/unipro.h           |   5 +
 2 files changed, 220 insertions(+)

diff --git a/drivers/ufs/core/ufs-debugfs.c b/drivers/ufs/core/ufs-debugfs.c
index 9f83628f3e2c..004d3c2ae11f 100644
--- a/drivers/ufs/core/ufs-debugfs.c
+++ b/drivers/ufs/core/ufs-debugfs.c
@@ -206,6 +206,219 @@ static const struct file_operations ufs_saved_err_fops = {
 	.release	= single_release,
 };
 
+#define UFS_PA_ERR_REC_READ_RETRIES	3
+
+struct ufs_pa_err_attr {
+	const char *name;
+	u32 mib_attr;
+	u8 peer;
+};
+
+static int ufs_dme_err_to_errno(int err)
+{
+	if (err <= 0)
+		return err;
+
+	switch (err) {
+	case UIC_CMD_RESULT_INVALID_ATTR:
+		return -EOPNOTSUPP;
+	case UIC_CMD_RESULT_INVALID_ATTR_VALUE:
+		return -EINVAL;
+	case UIC_CMD_RESULT_READ_ONLY_ATTR:
+		return -EPERM;
+	case UIC_CMD_RESULT_BUSY:
+		return -EBUSY;
+	default:
+		return -EIO;
+	}
+}
+
+static int ufs_pa_err_check_supported(struct ufs_hba *hba, u8 peer)
+{
+	if (!hba->ufs_device_wlun)
+		return -ENODEV;
+
+	if (hba->ufs_version < ufshci_version(5, 0) ||
+	    (peer == DME_PEER && hba->dev_info.wspecversion < 0x500))
+		return -EOPNOTSUPP;
+
+	return 0;
+}
+
+static int ufs_pa_err_read_rec_cnt(struct ufs_hba *hba, u8 peer, u64 *val)
+{
+	u32 hi_before, lo, hi_after;
+	int err, retry;
+
+	for (retry = 0; retry < UFS_PA_ERR_REC_READ_RETRIES; retry++) {
+		err = ufshcd_dme_get_attr(hba,
+					  UIC_ARG_MIB(PA_TFS_RECERRORCOUNTH),
+					  &hi_before, peer);
+		if (err)
+			return ufs_dme_err_to_errno(err);
+
+		err = ufshcd_dme_get_attr(hba,
+					  UIC_ARG_MIB(PA_TFS_RECERRORCOUNTL),
+					  &lo, peer);
+		if (err)
+			return ufs_dme_err_to_errno(err);
+
+		err = ufshcd_dme_get_attr(hba,
+					  UIC_ARG_MIB(PA_TFS_RECERRORCOUNTH),
+					  &hi_after, peer);
+		if (err)
+			return ufs_dme_err_to_errno(err);
+
+		if (hi_before == hi_after) {
+			*val = ((u64)hi_after << 32) | lo;
+			return 0;
+		}
+	}
+
+	return -EAGAIN;
+}
+
+static int ufs_pa_err_cnt_show(struct seq_file *s, void *data)
+{
+	const struct ufs_pa_err_attr *attr = s->private;
+	struct ufs_hba *hba = hba_from_file(s->file);
+	int err;
+
+	err = ufs_pa_err_check_supported(hba, attr->peer);
+	if (err)
+		return err;
+
+	err = ufs_debugfs_get_user_access(hba);
+	if (err)
+		return err;
+
+	if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
+		err = -EBUSY;
+		goto out;
+	}
+
+	if (attr->mib_attr == PA_TFS_RECERRORCOUNTL) {
+		u64 val;
+
+		err = ufs_pa_err_read_rec_cnt(hba, attr->peer, &val);
+		if (!err)
+			seq_printf(s, "%llu\n", val);
+	} else {
+		u32 val;
+
+		err = ufshcd_dme_get_attr(hba, UIC_ARG_MIB(attr->mib_attr),
+					  &val, attr->peer);
+		err = ufs_dme_err_to_errno(err);
+		if (!err)
+			seq_printf(s, "%u\n", val);
+	}
+
+out:
+	ufs_debugfs_put_user_access(hba);
+	return err;
+}
+
+static ssize_t ufs_pa_err_cnt_write(struct file *file, const char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	const struct ufs_pa_err_attr *attr = file->f_inode->i_private;
+	struct ufs_hba *hba = hba_from_file(file);
+	unsigned int val;
+	int err;
+
+	err = ufs_pa_err_check_supported(hba, attr->peer);
+	if (err)
+		return err;
+
+	err = kstrtouint_from_user(buf, count, 0, &val);
+	if (err)
+		return err;
+
+	if (val != 0)
+		return -EINVAL;
+
+	err = ufs_debugfs_get_user_access(hba);
+	if (err)
+		return err;
+
+	if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
+		err = -EBUSY;
+		goto out;
+	}
+
+	/*
+	 * Per MIPI UniPro v3.0 Section 5.6.7, setting PA_TFS_RecErrorCountL
+	 * clears the entire 64-bit PA_TFS_RecErrorCount counter to 0.
+	 */
+	err = ufshcd_dme_set_attr(hba, UIC_ARG_MIB(attr->mib_attr),
+				  ATTR_SET_NOR, 0, attr->peer);
+	err = ufs_dme_err_to_errno(err);
+
+out:
+	ufs_debugfs_put_user_access(hba);
+	return err ? err : count;
+}
+
+static int ufs_pa_err_cnt_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, ufs_pa_err_cnt_show, inode->i_private);
+}
+
+static const struct file_operations ufs_pa_err_cnt_fops = {
+	.owner		= THIS_MODULE,
+	.open		= ufs_pa_err_cnt_open,
+	.read		= seq_read,
+	.write		= ufs_pa_err_cnt_write,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+
+static const struct ufs_pa_err_attr ufs_host_pa_err_attrs[] = {
+	{ "fec_err_cnt", PA_TFS_FECERRORCOUNT, DME_LOCAL },
+	{ "crc_err_cnt", PA_TFS_CRCERRORCOUNT, DME_LOCAL },
+	{ "format_err_cnt", PA_TFS_FORMATERRORCOUNT, DME_LOCAL },
+	{ "recovered_fec_symbols", PA_TFS_RECERRORCOUNTL, DME_LOCAL },
+	{ }
+};
+
+static const struct ufs_pa_err_attr ufs_dev_pa_err_attrs[] = {
+	{ "fec_err_cnt", PA_TFS_FECERRORCOUNT, DME_PEER },
+	{ "crc_err_cnt", PA_TFS_CRCERRORCOUNT, DME_PEER },
+	{ "format_err_cnt", PA_TFS_FORMATERRORCOUNT, DME_PEER },
+	{ "recovered_fec_symbols", PA_TFS_RECERRORCOUNTL, DME_PEER },
+	{ }
+};
+
+static void ufs_debugfs_pa_err_init(struct ufs_hba *hba)
+{
+	static const struct {
+		const char *name;
+		const struct ufs_pa_err_attr *attrs;
+	} dirs[] = {
+		{ "host", ufs_host_pa_err_attrs },
+		{ "device", ufs_dev_pa_err_attrs },
+	};
+	const struct ufs_pa_err_attr *pa_attr;
+	struct dentry *pa_dir, *sub_dir;
+	unsigned int i;
+
+	pa_dir = debugfs_create_dir("pa_err_stats", hba->debugfs_root);
+	if (IS_ERR_OR_NULL(pa_dir))
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(dirs); i++) {
+		sub_dir = debugfs_create_dir(dirs[i].name, pa_dir);
+		if (IS_ERR_OR_NULL(sub_dir))
+			continue;
+
+		d_inode(sub_dir)->i_private = hba;
+		for (pa_attr = dirs[i].attrs; pa_attr->name; pa_attr++)
+			debugfs_create_file(pa_attr->name, 0600, sub_dir,
+					    (void *)pa_attr,
+					    &ufs_pa_err_cnt_fops);
+	}
+}
+
 static const struct ufs_debugfs_attr ufs_attrs[] = {
 	{ "stats", 0400, &ufs_debugfs_stats_fops },
 	{ "saved_err", 0600, &ufs_saved_err_fops },
@@ -494,6 +707,8 @@ void ufs_debugfs_hba_init(struct ufs_hba *hba)
 	debugfs_create_u32("exception_event_rate_limit_ms", 0600, hba->debugfs_root,
 			   &hba->debugfs_ee_rate_limit_ms);
 
+	ufs_debugfs_pa_err_init(hba);
+
 	if (!(hba->caps & UFSHCD_CAP_TX_EQUALIZATION))
 		return;
 
diff --git a/include/ufs/unipro.h b/include/ufs/unipro.h
index 9c168703b104..b82f9a9e838f 100644
--- a/include/ufs/unipro.h
+++ b/include/ufs/unipro.h
@@ -200,6 +200,11 @@
 #define PA_PACPFRAMECOUNT	0x15C0
 #define PA_PACPERRORCOUNT	0x15C1
 #define PA_PHYTESTCONTROL	0x15C2
+#define PA_TFS_FECERRORCOUNT	0x15C3
+#define PA_TFS_CRCERRORCOUNT	0x15C4
+#define PA_TFS_FORMATERRORCOUNT	0x15C5
+#define PA_TFS_RECERRORCOUNTL	0x15C6
+#define PA_TFS_RECERRORCOUNTH	0x15C7
 #define PA_TXHSG4SYNCLENGTH	0x15D0
 #define PA_PEERRXHSG4ADAPTINITIAL		0x15D3
 #define PA_TXHSADAPTTYPE	0x15D4

base-commit: 08fec21faae44fe002467976befd68e6e62969b5
-- 
2.56.1.750.gf70dc682cf-goog


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-23  2:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  2:57 [PATCH] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®