mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters
@ 2026-11-25 11:33 Po-Wen Kao
  2026-11-25 11:33 ` [PATCH v3 1/3] scsi: ufs: core: Remove debugfs before removing SCSI host Po-Wen Kao
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Po-Wen Kao @ 2026-11-25 11:33 UTC (permalink / raw)
  To: linux-scsi
  Cc: Konstantin Vyshetsky, Randall Huang, Alim Akhtar,
	Bart Van Assche, Bean Huo, Stanley Jhu, James E . J . Bottomley,
	Martin K . Petersen, Peter Wang, Can Guo, linux-kernel,
	Brian Kao

From: Brian Kao <powenkao@google.com>

This series exposes the MIPI UniPro v3.0 PA layer TFS/FEC error
counters (0x15C3-0x15C7) under debugfs:

  /sys/kernel/debug/ufshcd/<dev>/pa_err_stats/{host,device}/

Patch 1 fixes an existing teardown race by removing debugfs and
disabling debugfs_ee_work before scsi_remove_host() removes the WLUN.
Patch 2 defers debugfs creation until after device init while keeping
debugfs_ee_work initialization before IRQs are enabled. Patch 3 adds
the PA_TFS_* and UniPro 2.0/3.0 definitions and creates
pa_err_stats/{host,device}/ based on PA_LOCALVERINFO / PA_REMOTEVERINFO.

Changes in v3 (suggested by Bart Van Assche):
- Split the ufshcd_remove() teardown fix into patch 1 with Fixes and
  stable tags.
- Patch 2: Initialize debugfs_ee_work and debugfs_ee_rate_limit_ms
  before IRQs are enabled via ufs_debugfs_ee_init(), and document the
  later fault-injection attribute initialization.
- Patch 3: Gate host/ and device/ on UniPro version (PA_LOCALVERINFO /
  PA_REMOTEVERINFO >= UFS_UNIPRO_VER_3_0), merge the duplicated host
  and device attribute arrays into one via debugfs_create_file_aux_num(),
  update the hba_from_file() comment, and clarify the 64-bit read/reset
  comments.

Changes in v2:
- Defer ufs_debugfs_hba_init() until after device init (suggested by
  Peter Wang).
- Create pa_err_stats/host/ and device/ only when supported instead of
  returning -EOPNOTSUPP on read/write.

Link: https://lore.kernel.org/linux-scsi/20261124012951.3143833-1-powenkao@google.com/
Link: https://lore.kernel.org/linux-scsi/20261123025745.1743640-1-powenkao@google.com/

Brian Kao (3):
  scsi: ufs: core: Remove debugfs before removing SCSI host
  scsi: ufs: core: Defer debugfs init until after device init
  scsi: ufs: core: Add debugfs for PA TFS/FEC error counters

 drivers/ufs/core/ufs-debugfs.c | 244 ++++++++++++++++++++++++++++++++-
 drivers/ufs/core/ufs-debugfs.h |   2 +
 drivers/ufs/core/ufshcd.c      |   8 +-
 include/ufs/unipro.h           |   9 +-
 4 files changed, 252 insertions(+), 11 deletions(-)


base-commit: 08fec21faae44fe002467976befd68e6e62969b5
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/3] scsi: ufs: core: Remove debugfs before removing SCSI host
  2026-11-25 11:33 [PATCH v3 0/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao
@ 2026-11-25 11:33 ` Po-Wen Kao
  2026-11-25 11:34 ` [PATCH v3 2/3] scsi: ufs: core: Defer debugfs init until after device init Po-Wen Kao
  2026-11-25 11:34 ` [PATCH v3 3/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao
  2 siblings, 0 replies; 4+ messages in thread
From: Po-Wen Kao @ 2026-11-25 11:33 UTC (permalink / raw)
  To: linux-scsi
  Cc: Konstantin Vyshetsky, Randall Huang, Alim Akhtar,
	Bart Van Assche, Bean Huo, Stanley Jhu, James E . J . Bottomley,
	Martin K . Petersen, Peter Wang, Can Guo, linux-kernel,
	Brian Kao, stable, John Garry, Li Qiang, HOYOUNG SEO,
	Hyeoncheol Jeong, Adrian Hunter, Asutosh Das

From: Brian Kao <powenkao@google.com>

ufs_debugfs_hba_exit() is currently called only from ufshcd_hba_exit(),
after scsi_remove_host() has removed hba->ufs_device_wlun. Active
debugfs write handlers (e.g. exception_event_mask) and
hba->debugfs_ee_work call ufs_debugfs_get_user_access() ->
ufshcd_rpm_get_sync(), which can dereference a NULL or freed
hba->ufs_device_wlun during unbind.

Call ufs_debugfs_hba_exit() from ufshcd_remove() alongside
ufs_sysfs_remove_nodes(), before scsi_remove_host(), so in-flight
debugfs file operations are drained while the WLUN is still present.
Switch cancel_delayed_work_sync() to disable_delayed_work_sync()
(introduced in v6.10) so an in-flight exception event cannot re-queue
debugfs_ee_work during teardown, and clear hba->debugfs_root so the
existing ufs_debugfs_hba_exit() call in ufshcd_hba_exit() remains safe
on both normal remove and probe failure paths.

Fixes: b294ff3e3449 ("scsi: ufs: core: Enable power management for wlun")
Cc: stable@vger.kernel.org # v6.10+
Signed-off-by: Brian Kao <powenkao@google.com>
---
 drivers/ufs/core/ufs-debugfs.c | 3 ++-
 drivers/ufs/core/ufshcd.c      | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/ufs/core/ufs-debugfs.c b/drivers/ufs/core/ufs-debugfs.c
index 9f83628f3e2c..89753c40b27b 100644
--- a/drivers/ufs/core/ufs-debugfs.c
+++ b/drivers/ufs/core/ufs-debugfs.c
@@ -529,5 +529,6 @@ void ufs_debugfs_hba_init(struct ufs_hba *hba)
 void ufs_debugfs_hba_exit(struct ufs_hba *hba)
 {
 	debugfs_remove_recursive(hba->debugfs_root);
-	cancel_delayed_work_sync(&hba->debugfs_ee_work);
+	hba->debugfs_root = NULL;
+	disable_delayed_work_sync(&hba->debugfs_ee_work);
 }
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 56a77f4a2a6e..646359d446c3 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -11027,6 +11027,7 @@ void ufshcd_remove(struct ufs_hba *hba)
 	ufs_bsg_remove(hba);
 	ufs_rpmb_remove(hba);
 	ufs_sysfs_remove_nodes(hba->dev);
+	ufs_debugfs_hba_exit(hba);
 	cancel_delayed_work_sync(&hba->ufs_rtc_update_work);
 	blk_mq_destroy_queue(hba->tmf_queue);
 	blk_put_queue(hba->tmf_queue);
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 2/3] scsi: ufs: core: Defer debugfs init until after device init
  2026-11-25 11:33 [PATCH v3 0/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao
  2026-11-25 11:33 ` [PATCH v3 1/3] scsi: ufs: core: Remove debugfs before removing SCSI host Po-Wen Kao
@ 2026-11-25 11:34 ` Po-Wen Kao
  2026-11-25 11:34 ` [PATCH v3 3/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao
  2 siblings, 0 replies; 4+ messages in thread
From: Po-Wen Kao @ 2026-11-25 11:34 UTC (permalink / raw)
  To: linux-scsi
  Cc: Konstantin Vyshetsky, Randall Huang, Alim Akhtar,
	Bart Van Assche, Bean Huo, Stanley Jhu, James E . J . Bottomley,
	Martin K . Petersen, Peter Wang, Can Guo, linux-kernel,
	Brian Kao, John Garry, Li Qiang, HOYOUNG SEO, Hyeoncheol Jeong

From: Brian Kao <powenkao@google.com>

ufshcd_hba_init() currently creates the debugfs hierarchy and fault
injection attributes before the controller version (hba->ufs_version),
locks (hba->host_sem, hba->ee_ctrl_mutex), UniPro link, and device
descriptor are initialized.

Move ufs_debugfs_hba_init() and ufs_fault_inject_hba_init() into
ufshcd_init() alongside ufs_sysfs_add_nodes(), and remove the now
redundant ufs_debugfs_hba_exit() call from ufshcd_hba_exit() since
probe failures occur before ufs_debugfs_hba_init(). Split out
ufs_debugfs_ee_init() to initialize hba->debugfs_ee_work and
hba->debugfs_ee_rate_limit_ms in ufshcd_init() next to hba->eeh_work,
before interrupts are enabled.

Note that hba->trigger_eh_attr and hba->timeout_attr are now
initialized after device init. Until then the zero-initialized
fault_attr has probability 0, so should_fail() returns false and
module-parameter fault injection takes effect after device init rather
than during early probe device-management commands.

Suggested-by: Peter Wang <peter.wang@mediatek.com>
Signed-off-by: Brian Kao <powenkao@google.com>
---
 drivers/ufs/core/ufs-debugfs.c | 11 +++++++----
 drivers/ufs/core/ufs-debugfs.h |  2 ++
 drivers/ufs/core/ufshcd.c      |  7 +++----
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/ufs/core/ufs-debugfs.c b/drivers/ufs/core/ufs-debugfs.c
index 89753c40b27b..a3731dea11c7 100644
--- a/drivers/ufs/core/ufs-debugfs.c
+++ b/drivers/ufs/core/ufs-debugfs.c
@@ -472,14 +472,17 @@ static const struct ufs_debugfs_attr ufs_tx_eqtr_attrs[] = {
 	{ }
 };
 
-void ufs_debugfs_hba_init(struct ufs_hba *hba)
+void ufs_debugfs_ee_init(struct ufs_hba *hba)
 {
-	const struct ufs_debugfs_attr *attr;
-	struct dentry *root;
-
 	/* Set default exception event rate limit period to 20ms */
 	hba->debugfs_ee_rate_limit_ms = 20;
 	INIT_DELAYED_WORK(&hba->debugfs_ee_work, ufs_debugfs_restart_ee);
+}
+
+void ufs_debugfs_hba_init(struct ufs_hba *hba)
+{
+	const struct ufs_debugfs_attr *attr;
+	struct dentry *root;
 
 	root = debugfs_create_dir(dev_name(hba->dev), ufs_debugfs_root);
 	if (IS_ERR_OR_NULL(root))
diff --git a/drivers/ufs/core/ufs-debugfs.h b/drivers/ufs/core/ufs-debugfs.h
index e5bba9671862..d0acfd045a8b 100644
--- a/drivers/ufs/core/ufs-debugfs.h
+++ b/drivers/ufs/core/ufs-debugfs.h
@@ -13,12 +13,14 @@ struct ufs_hba;
 #ifdef CONFIG_DEBUG_FS
 void __init ufs_debugfs_init(void);
 void ufs_debugfs_exit(void);
+void ufs_debugfs_ee_init(struct ufs_hba *hba);
 void ufs_debugfs_hba_init(struct ufs_hba *hba);
 void ufs_debugfs_hba_exit(struct ufs_hba *hba);
 void ufs_debugfs_exception_event(struct ufs_hba *hba, u16 status);
 #else
 static inline void ufs_debugfs_init(void) {}
 static inline void ufs_debugfs_exit(void) {}
+static inline void ufs_debugfs_ee_init(struct ufs_hba *hba) {}
 static inline void ufs_debugfs_hba_init(struct ufs_hba *hba) {}
 static inline void ufs_debugfs_hba_exit(struct ufs_hba *hba) {}
 static inline void ufs_debugfs_exception_event(struct ufs_hba *hba, u16 status) {}
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 646359d446c3..443dcd7ed12c 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10115,9 +10115,6 @@ static int ufshcd_hba_init(struct ufs_hba *hba)
 	if (err)
 		goto out_disable_vreg;
 
-	ufs_debugfs_hba_init(hba);
-	ufs_fault_inject_hba_init(hba);
-
 	hba->is_powered = true;
 	goto out;
 
@@ -10139,7 +10136,6 @@ static void ufshcd_hba_exit(struct ufs_hba *hba)
 		ufshcd_exit_clk_gating(hba);
 		if (hba->eh_wq)
 			destroy_workqueue(hba->eh_wq);
-		ufs_debugfs_hba_exit(hba);
 		ufshcd_vops_exit(hba);
 		ufshcd_setup_vreg(hba, false);
 		ufshcd_setup_clocks(hba, false);
@@ -11375,6 +11371,7 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	}
 	INIT_WORK(&hba->eh_work, ufshcd_err_handler);
 	INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
+	ufs_debugfs_ee_init(hba);
 
 	sema_init(&hba->host_sem, 1);
 
@@ -11506,6 +11503,8 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	ufs_sysfs_add_nodes(hba->dev);
 	hba->dme_qos_sysfs_handle = sysfs_get_dirent(hba->dev->kobj.sd,
 						     "dme_qos_notification");
+	ufs_debugfs_hba_init(hba);
+	ufs_fault_inject_hba_init(hba);
 	async_schedule(ufshcd_async_scan, hba);
 
 	device_enable_async_suspend(dev);
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 3/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters
  2026-11-25 11:33 [PATCH v3 0/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao
  2026-11-25 11:33 ` [PATCH v3 1/3] scsi: ufs: core: Remove debugfs before removing SCSI host Po-Wen Kao
  2026-11-25 11:34 ` [PATCH v3 2/3] scsi: ufs: core: Defer debugfs init until after device init Po-Wen Kao
@ 2026-11-25 11:34 ` Po-Wen Kao
  2 siblings, 0 replies; 4+ messages in thread
From: Po-Wen Kao @ 2026-11-25 11:34 UTC (permalink / raw)
  To: linux-scsi
  Cc: Konstantin Vyshetsky, Randall Huang, Alim Akhtar,
	Bart Van Assche, Bean Huo, Stanley Jhu, James E . J . Bottomley,
	Martin K . Petersen, Peter Wang, Can Guo, linux-kernel,
	Brian Kao, Li Qiang, John Garry

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 and UFS_UNIPRO_VER_{2_0,3_0} in
include/ufs/unipro.h and expose the counters under debugfs:

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

Skip creation entirely on hosts setting UFSHCD_QUIRK_BROKEN_UIC_CMD.
Otherwise gate each subdirectory independently by UniPro version:
host/ is created when PA_LOCALVERINFO reports UniPro >= 3.0, and
device/ is created when PA_REMOTEVERINFO reports UniPro >= 3.0 (the
pa_err_stats parent directory is omitted if neither is supported).

Each file reads the local (host) or peer (device) PA attribute and
resets it to 0 when '0' is written. Because MIPI UniPro v3.0 does not
define hardware latching between PA_TFS_RecErrorCountL and
PA_TFS_RecErrorCountH, reads sample the high/low words with a bounded
hi-lo-hi retry loop to avoid 32-bit rollover tearing, while writing 0
to PA_TFS_RecErrorCountL clears the full 64-bit counter per Section
5.6.7. Positive UIC command result codes are translated to standard
negative errno values.

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

diff --git a/drivers/ufs/core/ufs-debugfs.c b/drivers/ufs/core/ufs-debugfs.c
index a3731dea11c7..4c8b6274434b 100644
--- a/drivers/ufs/core/ufs-debugfs.c
+++ b/drivers/ufs/core/ufs-debugfs.c
@@ -15,7 +15,10 @@ struct ufs_debugfs_attr {
 	const struct file_operations	*fops;
 };
 
-/* @file corresponds to a debugfs attribute in directory hba->debugfs_root. */
+/*
+ * @file corresponds to a debugfs attribute whose parent directory stores
+ * hba in i_private.
+ */
 static inline struct ufs_hba *hba_from_file(const struct file *file)
 {
 	return d_inode(file->f_path.dentry->d_parent)->i_private;
@@ -206,6 +209,229 @@ 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;
+};
+
+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_read_rec_cnt(struct ufs_hba *hba, u8 peer, u64 *val)
+{
+	u32 hi_before, lo, hi_after;
+	int err, retry;
+
+	/*
+	 * MIPI UniPro v3.0 Section 5.6.7 defines PA_TFS_RecErrorCountH and
+	 * PA_TFS_RecErrorCountL as separate 32-bit MIB attributes without
+	 * latching the upper word on a low-word read. Sample hi-lo-hi to
+	 * avoid 32-bit rollover tearing.
+	 */
+	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);
+	u8 peer = debugfs_get_aux_num(s->file);
+	int err;
+
+	if (!hba->ufs_device_wlun)
+		return -ENODEV;
+
+	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, 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, 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);
+	u8 peer = debugfs_get_aux_num(file);
+	unsigned int val;
+	int err;
+
+	if (!hba->ufs_device_wlun)
+		return -ENODEV;
+
+	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;
+	}
+
+	/*
+	 * Writing 0 resets the selected counter. For recovered_fec_symbols
+	 * (PA_TFS_RECERRORCOUNTL), setting the low word clears the entire
+	 * 64-bit PA_TFS_RecErrorCount counter per MIPI UniPro v3.0
+	 * Section 5.6.7.
+	 */
+	err = ufshcd_dme_set_attr(hba, UIC_ARG_MIB(attr->mib_attr),
+				  ATTR_SET_NOR, 0, 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_pa_err_attrs[] = {
+	{ "fec_err_cnt", PA_TFS_FECERRORCOUNT },
+	{ "crc_err_cnt", PA_TFS_CRCERRORCOUNT },
+	{ "format_err_cnt", PA_TFS_FORMATERRORCOUNT },
+	{ "recovered_fec_symbols", PA_TFS_RECERRORCOUNTL },
+	{ }
+};
+
+static enum ufs_unipro_ver ufs_get_unipro_ver(struct ufs_hba *hba, u8 peer)
+{
+	u32 attr = peer == DME_PEER ? PA_REMOTEVERINFO : PA_LOCALVERINFO;
+	u32 val;
+
+	/* PA_REMOTEVERINFO is a local MIB attribute cached at link startup. */
+	if (ufshcd_dme_get(hba, UIC_ARG_MIB(attr), &val))
+		return UFS_UNIPRO_VER_RESERVED;
+
+	return val & UFS_UNIPRO_VER_MASK;
+}
+
+static void ufs_debugfs_pa_err_create_dir(struct ufs_hba *hba,
+					  struct dentry *parent,
+					  const char *name, u8 peer)
+{
+	const struct ufs_pa_err_attr *pa_attr;
+	struct dentry *sub_dir;
+
+	sub_dir = debugfs_create_dir(name, parent);
+	if (IS_ERR_OR_NULL(sub_dir))
+		return;
+
+	d_inode(sub_dir)->i_private = hba;
+	for (pa_attr = ufs_pa_err_attrs; pa_attr->name; pa_attr++)
+		debugfs_create_file_aux_num(pa_attr->name, 0600, sub_dir,
+					    (void *)pa_attr, peer,
+					    &ufs_pa_err_cnt_fops);
+}
+
+static void ufs_debugfs_pa_err_init(struct ufs_hba *hba)
+{
+	bool host_sup, dev_sup;
+	struct dentry *pa_dir;
+
+	if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD)
+		return;
+
+	host_sup = ufs_get_unipro_ver(hba, DME_LOCAL) >= UFS_UNIPRO_VER_3_0;
+	dev_sup = ufs_get_unipro_ver(hba, DME_PEER) >= UFS_UNIPRO_VER_3_0;
+	if (!host_sup && !dev_sup)
+		return;
+
+	pa_dir = debugfs_create_dir("pa_err_stats", hba->debugfs_root);
+	if (IS_ERR_OR_NULL(pa_dir))
+		return;
+
+	if (host_sup)
+		ufs_debugfs_pa_err_create_dir(hba, pa_dir, "host", DME_LOCAL);
+	if (dev_sup)
+		ufs_debugfs_pa_err_create_dir(hba, pa_dir, "device", DME_PEER);
+}
+
 static const struct ufs_debugfs_attr ufs_attrs[] = {
 	{ "stats", 0400, &ufs_debugfs_stats_fops },
 	{ "saved_err", 0600, &ufs_saved_err_fops },
@@ -497,6 +723,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..27f15ac7c0ef 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
@@ -391,7 +396,9 @@ enum ufs_unipro_ver {
 	UFS_UNIPRO_VER_1_6  = 3, /* UniPro version 1.6 */
 	UFS_UNIPRO_VER_1_61 = 4, /* UniPro version 1.61 */
 	UFS_UNIPRO_VER_1_8  = 5, /* UniPro version 1.8 */
-	UFS_UNIPRO_VER_MAX  = 6, /* UniPro unsupported version */
+	UFS_UNIPRO_VER_2_0  = 6, /* UniPro version 2.0 */
+	UFS_UNIPRO_VER_3_0  = 7, /* UniPro version 3.0 */
+	UFS_UNIPRO_VER_MAX  = 8, /* UniPro unsupported version */
 	/* UniPro version field mask in PA_LOCALVERINFO */
 	UFS_UNIPRO_VER_MASK = 0xF,
 };
-- 
2.56.1.839.g22a8290db9-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25 11:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 11:33 [PATCH v3 0/3] scsi: ufs: core: Add debugfs for PA TFS/FEC error counters Po-Wen Kao
2026-11-25 11:33 ` [PATCH v3 1/3] scsi: ufs: core: Remove debugfs before removing SCSI host Po-Wen Kao
2026-11-25 11:34 ` [PATCH v3 2/3] scsi: ufs: core: Defer debugfs init until after device init Po-Wen Kao
2026-11-25 11:34 ` [PATCH v3 3/3] 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®