[PATCH v3 4/6] PCI/FLIT: Add support for the Extended Flit Error Counter

From: Yazen Ghannam

Date: Fri Dec 04 2026 - 13:16:32 EST


Capability Version 2h adds an optional Extended Flit Error Counter, with
a 16-bit count and trigger and programmable decrement rates.[1] Use it
in place of the Flit Error Counter where present.

[1] PCIe r7.1, sec 6.38.1 and 7.7.9.17 to 7.7.9.20

Signed-off-by: Yazen Ghannam <yazen.ghannam@xxxxxxx>
---
drivers/pci/pcie/flit.c | 106 ++++++++++++++++++++++++++++++++--
include/uapi/linux/pci_regs.h | 10 ++++
2 files changed, 110 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/pcie/flit.c b/drivers/pci/pcie/flit.c
index 00409dd7bb95..97395b694238 100644
--- a/drivers/pci/pcie/flit.c
+++ b/drivers/pci/pcie/flit.c
@@ -37,12 +37,15 @@

enum flit_save_slot {
FLIT_SAVE_CNTR_CTRL,
+ FLIT_SAVE_EXT_CNTR_CTRL1,
+ FLIT_SAVE_EXT_CNTR_CTRL2,
FLIT_SAVE_SLOTS
};

struct flit_cntr {
u16 ctrl; /* Control register offset */
u16 sta; /* Status register offset */
+ bool dword; /* Control and Status are dwords */
enum flit_save_slot slot; /* save buffer slot of Control */
u32 en; /* Counter Enable */
u32 intr_en; /* Interrupt Enable */
@@ -60,14 +63,31 @@ static const struct flit_cntr flit_base_cntr = {
.intr_gen = PCI_FLIT_ERR_CNTR_STA_INTR_GEN,
};

+static const struct flit_cntr flit_ext_cntr = {
+ .ctrl = PCI_FLIT_EXT_CNTR_CTRL1,
+ .sta = PCI_FLIT_EXT_CNTR_STA,
+ .dword = true,
+ .slot = FLIT_SAVE_EXT_CNTR_CTRL1,
+ .en = PCI_FLIT_EXT_CNTR_CTRL1_EN,
+ .intr_en = PCI_FLIT_EXT_CNTR_CTRL1_INTR_EN,
+ .trigger = PCI_FLIT_EXT_CNTR_CTRL1_TRIGGER,
+ .intr_gen = PCI_FLIT_EXT_CNTR_STA_INTR_GEN,
+};
+
struct flit_info {
spinlock_t lock; /* serializes Counter Control read-modify-write */
const struct flit_cntr *cntr; /* the counter the driver runs */
+ u32 cntr_cap; /* Flit Error Counter Capability, 0 in 1h */
u32 enable; /* what flit_cntr_enable() turns on */
u16 trigger; /* Trigger Event on Error Count to program */
bool cntr_enabled; /* Counter Enable as the driver intends it */
};

+static bool flit_ext(struct flit_info *info)
+{
+ return info->cntr == &flit_ext_cntr;
+}
+
/* The port types pcie_portdrv_probe() claims */
static bool flit_is_port(struct pci_dev *pdev)
{
@@ -93,10 +113,46 @@ static bool flit_is_native(struct pci_dev *pdev)
return host->native_flit && host->native_aer;
}

+static u32 flit_read_cntr_cap(struct pci_dev *pdev)
+{
+ u32 hdr, cap;
+
+ pci_read_config_dword(pdev, pdev->flit_cap, &hdr);
+ if (PCI_EXT_CAP_VER(hdr) < 2)
+ return 0;
+
+ pci_read_config_dword(pdev, pdev->flit_cap + PCI_FLIT_ERR_CNTR_CAP, &cap);
+
+ return cap;
+}
+
+static void flit_select_cntr(struct pci_dev *pdev)
+{
+ struct flit_info *info = pdev->flit_info;
+
+ info->cntr = &flit_base_cntr;
+ if (!(info->cntr_cap & PCI_FLIT_ERR_CNTR_CAP_EXT))
+ return;
+
+ /* Running both counters, or both interrupts, is undefined */
+ pci_clear_and_set_config_word(pdev,
+ pdev->flit_cap + PCI_FLIT_ERR_CNTR_CTRL,
+ PCI_FLIT_ERR_CNTR_CTRL_EN |
+ PCI_FLIT_ERR_CNTR_CTRL_INTR_EN, 0);
+
+ info->cntr = &flit_ext_cntr;
+}
+
static u32 flit_cntr_read(struct pci_dev *pdev, u16 reg)
{
int pos = pdev->flit_cap + reg;
u16 val16;
+ u32 val;
+
+ if (pdev->flit_info->cntr->dword) {
+ pci_read_config_dword(pdev, pos, &val);
+ return val;
+ }

pci_read_config_word(pdev, pos, &val16);

@@ -106,12 +162,18 @@ static u32 flit_cntr_read(struct pci_dev *pdev, u16 reg)

static void flit_cntr_write(struct pci_dev *pdev, u16 reg, u32 val)
{
- pci_write_config_word(pdev, pdev->flit_cap + reg, val);
+ int pos = pdev->flit_cap + reg;
+
+ if (pdev->flit_info->cntr->dword)
+ pci_write_config_dword(pdev, pos, val);
+ else
+ pci_write_config_word(pdev, pos, val);
}

void pci_flit_init(struct pci_dev *pdev)
{
struct flit_info *info;
+ unsigned int slots;
u16 trigger;
u32 ctrl;

@@ -130,17 +192,24 @@ void pci_flit_init(struct pci_dev *pdev)

pdev->flit_info = info;

+ info->cntr_cap = flit_read_cntr_cap(pdev);
+
spin_lock_init(&info->lock);

- info->cntr = &flit_base_cntr;
+ flit_select_cntr(pdev);
info->enable = info->cntr->en | info->cntr->intr_en;

ctrl = flit_cntr_read(pdev, info->cntr->ctrl);
trigger = field_get(info->cntr->trigger, ctrl);
info->trigger = trigger ? trigger : FLIT_DEFAULT_TRIGGER;

+ slots = flit_ext(info) ? FLIT_SAVE_SLOTS : FLIT_SAVE_CNTR_CTRL + 1;
pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_FLIT,
- FLIT_SAVE_SLOTS * sizeof(u32));
+ slots * sizeof(u32));
+
+ pci_dbg(pdev, "Flit Logging Extended Capability present, using the %s\n",
+ flit_ext(info) ? "Extended Flit Error Counter" :
+ "Flit Error Counter");
}

void pci_flit_exit(struct pci_dev *pdev)
@@ -165,7 +234,7 @@ void pci_save_flit_state(struct pci_dev *pdev)
const struct flit_cntr *cntr;
u16 flit = pdev->flit_cap;
unsigned long flags;
- u32 *slots;
+ u32 *slots, ctrl1;
u16 ctrl;

if (!flit)
@@ -181,6 +250,17 @@ void pci_save_flit_state(struct pci_dev *pdev)
pci_read_config_word(pdev, flit + PCI_FLIT_ERR_CNTR_CTRL, &ctrl);
slots[FLIT_SAVE_CNTR_CTRL] = ctrl;

+ if (flit_ext(info)) {
+ pci_read_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL1,
+ &ctrl1);
+ pci_read_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL2,
+ &slots[FLIT_SAVE_EXT_CNTR_CTRL2]);
+
+ /* Reads as zero, but restoring a one would clear the counter */
+ ctrl1 &= ~PCI_FLIT_EXT_CNTR_CTRL1_CLEAR;
+ slots[FLIT_SAVE_EXT_CNTR_CTRL1] = ctrl1;
+ }
+
/* Counter Enable is clear in the register while an event is serviced */
if (info->cntr_enabled)
slots[cntr->slot] |= cntr->en;
@@ -203,10 +283,21 @@ void pci_restore_flit_state(struct pci_dev *pdev)
if (!slots)
return;

- /* Event servicing can run during a runtime resume or slot reset */
+ /*
+ * Event servicing can run during a runtime resume or slot reset. Write
+ * Flit Error Counter Control first so the two counters never overlap,
+ * and the decrement rates before the counter they apply to.
+ */
spin_lock_irqsave(&info->lock, flags);
pci_write_config_word(pdev, flit + PCI_FLIT_ERR_CNTR_CTRL,
slots[FLIT_SAVE_CNTR_CTRL]);
+
+ if (flit_ext(info)) {
+ pci_write_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL2,
+ slots[FLIT_SAVE_EXT_CNTR_CTRL2]);
+ pci_write_config_dword(pdev, flit + PCI_FLIT_EXT_CNTR_CTRL1,
+ slots[FLIT_SAVE_EXT_CNTR_CTRL1]);
+ }
spin_unlock_irqrestore(&info->lock, flags);
}

@@ -218,7 +309,10 @@ static void __flit_cntr_ctrl_update(struct pci_dev *pdev, u32 clear, u32 set)

lockdep_assert_held(&pdev->flit_info->lock);

- pci_clear_and_set_config_word(pdev, pos, clear, set);
+ if (cntr->dword)
+ pci_clear_and_set_config_dword(pdev, pos, clear, set);
+ else
+ pci_clear_and_set_config_word(pdev, pos, clear, set);
}

/* pcie_do_recovery() restores the save buffer without saving it first */
diff --git a/include/uapi/linux/pci_regs.h b/include/uapi/linux/pci_regs.h
index 73482024359b..803bfb6fbf49 100644
--- a/include/uapi/linux/pci_regs.h
+++ b/include/uapi/linux/pci_regs.h
@@ -1203,6 +1203,16 @@
#define PCI_FLIT_ERR_CNTR_CTRL_TRIGGER 0x0ff0 /* Trigger Event on Error Count */
#define PCI_FLIT_ERR_CNTR_STA 0x0e /* Flit Error Counter Status Register */
#define PCI_FLIT_ERR_CNTR_STA_INTR_GEN 0x0008 /* Interrupt Generated based on Trigger */
+#define PCI_FLIT_ERR_CNTR_CAP 0x3c /* Flit Error Counter Capability Register */
+#define PCI_FLIT_ERR_CNTR_CAP_EXT 0x00000002 /* Extended Flit Error Counter Supported */
+#define PCI_FLIT_EXT_CNTR_CTRL1 0x40 /* Ext Flit Error Counter Control 1 */
+#define PCI_FLIT_EXT_CNTR_CTRL1_EN 0x00000001 /* Ext Flit Error Counter Enable */
+#define PCI_FLIT_EXT_CNTR_CTRL1_INTR_EN 0x00000002 /* Ext Flit Error Counter Intr Enable */
+#define PCI_FLIT_EXT_CNTR_CTRL1_CLEAR 0x00000010 /* Clear Ext Flit Error Counter */
+#define PCI_FLIT_EXT_CNTR_CTRL1_TRIGGER 0x001fffe0 /* Trigger Event on Ext Error Count */
+#define PCI_FLIT_EXT_CNTR_CTRL2 0x44 /* Ext Flit Error Counter Control 2 */
+#define PCI_FLIT_EXT_CNTR_STA 0x48 /* Ext Flit Error Counter Status */
+#define PCI_FLIT_EXT_CNTR_STA_INTR_GEN 0x00000080 /* Intr Generated on Ext Trigger */

/* Native PCIe Enclosure Management */
#define PCI_NPEM_CAP 0x04 /* NPEM capability register */
--
2.55.0