]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
PCI/LINK: Report degraded links via link bandwidth notification
authorAlexandru Gagniuc <mr.nuke.me@gmail.com>
Wed, 27 Feb 2019 20:58:17 +0000 (14:58 -0600)
committerBjorn Helgaas <bhelgaas@google.com>
Tue, 5 Mar 2019 21:04:13 +0000 (15:04 -0600)
A warning is generated when a PCIe device is probed with a degraded link,
but there was no similar mechanism to warn when the link becomes degraded
after probing.  The Link Bandwidth Notification provides this mechanism.

Use the Link Bandwidth Management Interrupt to detect bandwidth changes,
and rescan the bandwidth, looking for the weakest point.  This is the same
logic used in probe().

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Lukas Wunner <lukas@wunner.de>
drivers/pci/pcie/Makefile
drivers/pci/pcie/bw_notification.c [new file with mode: 0644]
drivers/pci/pcie/portdrv.h
drivers/pci/pcie/portdrv_core.c
drivers/pci/pcie/portdrv_pci.c

index ab514083d5d4246ec3b56de9d578e7c2716359c1..f1d7bc1e5efae2561fecba886b15a17a17ee5c4b 100644 (file)
@@ -3,6 +3,7 @@
 # Makefile for PCI Express features and port driver
 
 pcieportdrv-y                  := portdrv_core.o portdrv_pci.o err.o
+pcieportdrv-y                  += bw_notification.o
 
 obj-$(CONFIG_PCIEPORTBUS)      += pcieportdrv.o
 
diff --git a/drivers/pci/pcie/bw_notification.c b/drivers/pci/pcie/bw_notification.c
new file mode 100644 (file)
index 0000000..d2eae3b
--- /dev/null
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PCI Express Link Bandwidth Notification services driver
+ * Author: Alexandru Gagniuc <mr.nuke.me@gmail.com>
+ *
+ * Copyright (C) 2019, Dell Inc
+ *
+ * The PCIe Link Bandwidth Notification provides a way to notify the
+ * operating system when the link width or data rate changes.  This
+ * capability is required for all root ports and downstream ports
+ * supporting links wider than x1 and/or multiple link speeds.
+ *
+ * This service port driver hooks into the bandwidth notification interrupt
+ * and warns when links become degraded in operation.
+ */
+
+#include "../pci.h"
+#include "portdrv.h"
+
+static bool pcie_link_bandwidth_notification_supported(struct pci_dev *dev)
+{
+       int ret;
+       u32 lnk_cap;
+
+       ret = pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnk_cap);
+       return (ret == PCIBIOS_SUCCESSFUL) && (lnk_cap & PCI_EXP_LNKCAP_LBNC);
+}
+
+static void pcie_enable_link_bandwidth_notification(struct pci_dev *dev)
+{
+       u16 lnk_ctl;
+
+       pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &lnk_ctl);
+       lnk_ctl |= PCI_EXP_LNKCTL_LBMIE;
+       pcie_capability_write_word(dev, PCI_EXP_LNKCTL, lnk_ctl);
+}
+
+static void pcie_disable_link_bandwidth_notification(struct pci_dev *dev)
+{
+       u16 lnk_ctl;
+
+       pcie_capability_read_word(dev, PCI_EXP_LNKCTL, &lnk_ctl);
+       lnk_ctl &= ~PCI_EXP_LNKCTL_LBMIE;
+       pcie_capability_write_word(dev, PCI_EXP_LNKCTL, lnk_ctl);
+}
+
+static irqreturn_t pcie_bw_notification_handler(int irq, void *context)
+{
+       struct pcie_device *srv = context;
+       struct pci_dev *port = srv->port;
+       struct pci_dev *dev;
+       u16 link_status, events;
+       int ret;
+
+       ret = pcie_capability_read_word(port, PCI_EXP_LNKSTA, &link_status);
+       events = link_status & PCI_EXP_LNKSTA_LBMS;
+
+       if (ret != PCIBIOS_SUCCESSFUL || !events)
+               return IRQ_NONE;
+
+       /*
+        * Print status from downstream devices, not this root port or
+        * downstream switch port.
+        */
+       down_read(&pci_bus_sem);
+       list_for_each_entry(dev, &port->subordinate->devices, bus_list)
+               __pcie_print_link_status(dev, false);
+       up_read(&pci_bus_sem);
+
+       pcie_update_link_speed(port->subordinate, link_status);
+       pcie_capability_write_word(port, PCI_EXP_LNKSTA, events);
+       return IRQ_HANDLED;
+}
+
+static int pcie_bandwidth_notification_probe(struct pcie_device *srv)
+{
+       int ret;
+
+       /* Single-width or single-speed ports do not have to support this. */
+       if (!pcie_link_bandwidth_notification_supported(srv->port))
+               return -ENODEV;
+
+       ret = request_threaded_irq(srv->irq, NULL, pcie_bw_notification_handler,
+                                  IRQF_SHARED, "PCIe BW notif", srv);
+       if (ret)
+               return ret;
+
+       pcie_enable_link_bandwidth_notification(srv->port);
+
+       return 0;
+}
+
+static void pcie_bandwidth_notification_remove(struct pcie_device *srv)
+{
+       pcie_disable_link_bandwidth_notification(srv->port);
+       free_irq(srv->irq, srv);
+}
+
+static struct pcie_port_service_driver pcie_bandwidth_notification_driver = {
+       .name           = "pcie_bw_notification",
+       .port_type      = PCIE_ANY_PORT,
+       .service        = PCIE_PORT_SERVICE_BWNOTIF,
+       .probe          = pcie_bandwidth_notification_probe,
+       .remove         = pcie_bandwidth_notification_remove,
+};
+
+int __init pcie_bandwidth_notification_init(void)
+{
+       return pcie_port_service_register(&pcie_bandwidth_notification_driver);
+}
index fbbf00b0992e50e946f7a5ebd465324bf8c8f2de..1d50dc58ac400ae1a325f788ee33352aebf58c09 100644 (file)
 #define PCIE_PORT_SERVICE_HP           (1 << PCIE_PORT_SERVICE_HP_SHIFT)
 #define PCIE_PORT_SERVICE_DPC_SHIFT    3       /* Downstream Port Containment */
 #define PCIE_PORT_SERVICE_DPC          (1 << PCIE_PORT_SERVICE_DPC_SHIFT)
+#define PCIE_PORT_SERVICE_BWNOTIF_SHIFT        4       /* Bandwidth notification */
+#define PCIE_PORT_SERVICE_BWNOTIF      (1 << PCIE_PORT_SERVICE_BWNOTIF_SHIFT)
 
-#define PCIE_PORT_DEVICE_MAXSERVICES   4
+#define PCIE_PORT_DEVICE_MAXSERVICES   5
 
 #ifdef CONFIG_PCIEAER
 int pcie_aer_init(void);
@@ -47,6 +49,8 @@ int pcie_dpc_init(void);
 static inline int pcie_dpc_init(void) { return 0; }
 #endif
 
+int pcie_bandwidth_notification_init(void);
+
 /* Port Type */
 #define PCIE_ANY_PORT                  (~0)
 
index f458ac9cb70c397868a17fa48027ccb21f80e762..7d04f9d087a62a94cf4edd5fdab4749d752f2c4e 100644 (file)
@@ -99,7 +99,7 @@ static int pcie_message_numbers(struct pci_dev *dev, int mask,
  */
 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
 {
-       int nr_entries, nvec;
+       int nr_entries, nvec, pcie_irq;
        u32 pme = 0, aer = 0, dpc = 0;
 
        /* Allocate the maximum possible number of MSI/MSI-X vectors */
@@ -135,10 +135,13 @@ static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
                        return nr_entries;
        }
 
-       /* PME and hotplug share an MSI/MSI-X vector */
-       if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
-               irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, pme);
-               irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, pme);
+       /* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
+       if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
+                   PCIE_PORT_SERVICE_BWNOTIF)) {
+               pcie_irq = pci_irq_vector(dev, pme);
+               irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pcie_irq;
+               irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pcie_irq;
+               irqs[PCIE_PORT_SERVICE_BWNOTIF_SHIFT] = pcie_irq;
        }
 
        if (mask & PCIE_PORT_SERVICE_AER)
@@ -250,6 +253,10 @@ static int get_port_device_capability(struct pci_dev *dev)
            pci_aer_available() && services & PCIE_PORT_SERVICE_AER)
                services |= PCIE_PORT_SERVICE_DPC;
 
+       if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
+           pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
+               services |= PCIE_PORT_SERVICE_BWNOTIF;
+
        return services;
 }
 
index 99d2abe88d0b53b4b25bfc1ce35126689ad3dbb2..0a87091a0800e8316a9d05a9bea7268528ff687c 100644 (file)
@@ -240,6 +240,7 @@ static void __init pcie_init_services(void)
        pcie_pme_init();
        pcie_dpc_init();
        pcie_hp_init();
+       pcie_bandwidth_notification_init();
 }
 
 static int __init pcie_portdrv_init(void)