2 * PCI Express Downstream Port Containment services driver
3 * Copyright (C) 2016 Intel Corp.
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/delay.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/pcieport_if.h>
17 struct pcie_device
*dev
;
18 struct work_struct work
;
22 static void dpc_wait_link_inactive(struct pci_dev
*pdev
)
24 unsigned long timeout
= jiffies
+ HZ
;
27 pcie_capability_read_word(pdev
, PCI_EXP_LNKSTA
, &lnk_status
);
28 while (lnk_status
& PCI_EXP_LNKSTA_DLLLA
&&
29 !time_after(jiffies
, timeout
)) {
31 pcie_capability_read_word(pdev
, PCI_EXP_LNKSTA
, &lnk_status
);
33 if (lnk_status
& PCI_EXP_LNKSTA_DLLLA
)
34 dev_warn(&pdev
->dev
, "Link state not disabled for DPC event");
37 static void interrupt_event_handler(struct work_struct
*work
)
39 struct dpc_dev
*dpc
= container_of(work
, struct dpc_dev
, work
);
40 struct pci_dev
*dev
, *temp
, *pdev
= dpc
->dev
->port
;
41 struct pci_bus
*parent
= pdev
->subordinate
;
43 pci_lock_rescan_remove();
44 list_for_each_entry_safe_reverse(dev
, temp
, &parent
->devices
,
47 pci_stop_and_remove_bus_device(dev
);
50 pci_unlock_rescan_remove();
52 dpc_wait_link_inactive(pdev
);
53 pci_write_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_STATUS
,
54 PCI_EXP_DPC_STATUS_TRIGGER
| PCI_EXP_DPC_STATUS_INTERRUPT
);
57 static irqreturn_t
dpc_irq(int irq
, void *context
)
59 struct dpc_dev
*dpc
= (struct dpc_dev
*)context
;
60 struct pci_dev
*pdev
= dpc
->dev
->port
;
63 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_STATUS
, &status
);
64 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_SOURCE_ID
,
69 dev_info(&dpc
->dev
->device
, "DPC containment event, status:%#06x source:%#06x\n",
72 if (status
& PCI_EXP_DPC_STATUS_TRIGGER
) {
73 u16 reason
= (status
>> 1) & 0x3;
75 dev_warn(&dpc
->dev
->device
, "DPC %s triggered, remove downstream devices\n",
76 (reason
== 0) ? "unmasked uncorrectable error" :
77 (reason
== 1) ? "ERR_NONFATAL" :
78 (reason
== 2) ? "ERR_FATAL" : "extended error");
79 schedule_work(&dpc
->work
);
84 #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
85 static int dpc_probe(struct pcie_device
*dev
)
88 struct pci_dev
*pdev
= dev
->port
;
92 dpc
= devm_kzalloc(&dev
->device
, sizeof(*dpc
), GFP_KERNEL
);
96 dpc
->cap_pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_DPC
);
98 INIT_WORK(&dpc
->work
, interrupt_event_handler
);
99 set_service_data(dev
, dpc
);
101 status
= devm_request_irq(&dev
->device
, dev
->irq
, dpc_irq
, IRQF_SHARED
,
104 dev_warn(&dev
->device
, "request IRQ%d failed: %d\n", dev
->irq
,
109 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CAP
, &cap
);
110 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, &ctl
);
112 ctl
|= PCI_EXP_DPC_CTL_EN_NONFATAL
| PCI_EXP_DPC_CTL_INT_EN
;
113 pci_write_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, ctl
);
115 dev_info(&dev
->device
, "DPC error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
116 cap
& 0xf, FLAG(cap
, PCI_EXP_DPC_CAP_RP_EXT
),
117 FLAG(cap
, PCI_EXP_DPC_CAP_POISONED_TLP
),
118 FLAG(cap
, PCI_EXP_DPC_CAP_SW_TRIGGER
), (cap
>> 8) & 0xf,
119 FLAG(cap
, PCI_EXP_DPC_CAP_DL_ACTIVE
));
123 static void dpc_remove(struct pcie_device
*dev
)
125 struct dpc_dev
*dpc
= get_service_data(dev
);
126 struct pci_dev
*pdev
= dev
->port
;
129 pci_read_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, &ctl
);
130 ctl
&= ~(PCI_EXP_DPC_CTL_EN_NONFATAL
| PCI_EXP_DPC_CTL_INT_EN
);
131 pci_write_config_word(pdev
, dpc
->cap_pos
+ PCI_EXP_DPC_CTL
, ctl
);
134 static struct pcie_port_service_driver dpcdriver
= {
136 .port_type
= PCIE_ANY_PORT
,
137 .service
= PCIE_PORT_SERVICE_DPC
,
139 .remove
= dpc_remove
,
142 static int __init
dpc_service_init(void)
144 return pcie_port_service_register(&dpcdriver
);
147 static void __exit
dpc_service_exit(void)
149 pcie_port_service_unregister(&dpcdriver
);
152 MODULE_DESCRIPTION("PCI Express Downstream Port Containment driver");
153 MODULE_AUTHOR("Keith Busch <keith.busch@intel.com>");
154 MODULE_LICENSE("GPL");
155 MODULE_VERSION("0.1");
157 module_init(dpc_service_init
);
158 module_exit(dpc_service_exit
);