1e02602bdSBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
2e02602bdSBjorn Helgaas /*
3e02602bdSBjorn Helgaas * PCI Express Downstream Port Containment services driver
4e02602bdSBjorn Helgaas * Author: Keith Busch <keith.busch@intel.com>
5e02602bdSBjorn Helgaas *
6e02602bdSBjorn Helgaas * Copyright (C) 2016 Intel Corp.
7e02602bdSBjorn Helgaas */
8e02602bdSBjorn Helgaas
910a9990cSFrederick Lawler #define dev_fmt(fmt) "DPC: " fmt
1010a9990cSFrederick Lawler
118aefa9b0SKeith Busch #include <linux/aer.h>
12*d9a28916SBjorn Helgaas #include <linux/bitfield.h>
13e02602bdSBjorn Helgaas #include <linux/delay.h>
14e02602bdSBjorn Helgaas #include <linux/interrupt.h>
15e02602bdSBjorn Helgaas #include <linux/init.h>
16e02602bdSBjorn Helgaas #include <linux/pci.h>
17e02602bdSBjorn Helgaas
18e02602bdSBjorn Helgaas #include "portdrv.h"
19e02602bdSBjorn Helgaas #include "../pci.h"
20e02602bdSBjorn Helgaas
21e02602bdSBjorn Helgaas static const char * const rp_pio_error_string[] = {
22e02602bdSBjorn Helgaas "Configuration Request received UR Completion", /* Bit Position 0 */
23e02602bdSBjorn Helgaas "Configuration Request received CA Completion", /* Bit Position 1 */
24e02602bdSBjorn Helgaas "Configuration Request Completion Timeout", /* Bit Position 2 */
25e02602bdSBjorn Helgaas NULL,
26e02602bdSBjorn Helgaas NULL,
27e02602bdSBjorn Helgaas NULL,
28e02602bdSBjorn Helgaas NULL,
29e02602bdSBjorn Helgaas NULL,
30e02602bdSBjorn Helgaas "I/O Request received UR Completion", /* Bit Position 8 */
31e02602bdSBjorn Helgaas "I/O Request received CA Completion", /* Bit Position 9 */
32e02602bdSBjorn Helgaas "I/O Request Completion Timeout", /* Bit Position 10 */
33e02602bdSBjorn Helgaas NULL,
34e02602bdSBjorn Helgaas NULL,
35e02602bdSBjorn Helgaas NULL,
36e02602bdSBjorn Helgaas NULL,
37e02602bdSBjorn Helgaas NULL,
38e02602bdSBjorn Helgaas "Memory Request received UR Completion", /* Bit Position 16 */
39e02602bdSBjorn Helgaas "Memory Request received CA Completion", /* Bit Position 17 */
40e02602bdSBjorn Helgaas "Memory Request Completion Timeout", /* Bit Position 18 */
41e02602bdSBjorn Helgaas };
42e02602bdSBjorn Helgaas
pci_save_dpc_state(struct pci_dev * dev)434f802170SKeith Busch void pci_save_dpc_state(struct pci_dev *dev)
444f802170SKeith Busch {
454f802170SKeith Busch struct pci_cap_saved_state *save_state;
464f802170SKeith Busch u16 *cap;
474f802170SKeith Busch
484f802170SKeith Busch if (!pci_is_pcie(dev))
494f802170SKeith Busch return;
504f802170SKeith Busch
514f802170SKeith Busch save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_DPC);
524f802170SKeith Busch if (!save_state)
534f802170SKeith Busch return;
544f802170SKeith Busch
554f802170SKeith Busch cap = (u16 *)&save_state->cap.data[0];
56be06c1b4SBjorn Helgaas pci_read_config_word(dev, dev->dpc_cap + PCI_EXP_DPC_CTL, cap);
574f802170SKeith Busch }
584f802170SKeith Busch
pci_restore_dpc_state(struct pci_dev * dev)594f802170SKeith Busch void pci_restore_dpc_state(struct pci_dev *dev)
604f802170SKeith Busch {
614f802170SKeith Busch struct pci_cap_saved_state *save_state;
624f802170SKeith Busch u16 *cap;
634f802170SKeith Busch
644f802170SKeith Busch if (!pci_is_pcie(dev))
654f802170SKeith Busch return;
664f802170SKeith Busch
674f802170SKeith Busch save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_DPC);
684f802170SKeith Busch if (!save_state)
694f802170SKeith Busch return;
704f802170SKeith Busch
714f802170SKeith Busch cap = (u16 *)&save_state->cap.data[0];
72be06c1b4SBjorn Helgaas pci_write_config_word(dev, dev->dpc_cap + PCI_EXP_DPC_CTL, *cap);
734f802170SKeith Busch }
744f802170SKeith Busch
75a97396c6SLukas Wunner static DECLARE_WAIT_QUEUE_HEAD(dpc_completed_waitqueue);
76a97396c6SLukas Wunner
77a97396c6SLukas Wunner #ifdef CONFIG_HOTPLUG_PCI_PCIE
dpc_completed(struct pci_dev * pdev)78a97396c6SLukas Wunner static bool dpc_completed(struct pci_dev *pdev)
79a97396c6SLukas Wunner {
80a97396c6SLukas Wunner u16 status;
81a97396c6SLukas Wunner
82a97396c6SLukas Wunner pci_read_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_STATUS, &status);
830242132dSNaveen Naidu if ((!PCI_POSSIBLE_ERROR(status)) && (status & PCI_EXP_DPC_STATUS_TRIGGER))
84a97396c6SLukas Wunner return false;
85a97396c6SLukas Wunner
86a97396c6SLukas Wunner if (test_bit(PCI_DPC_RECOVERING, &pdev->priv_flags))
87a97396c6SLukas Wunner return false;
88a97396c6SLukas Wunner
89a97396c6SLukas Wunner return true;
90a97396c6SLukas Wunner }
91a97396c6SLukas Wunner
92a97396c6SLukas Wunner /**
93a97396c6SLukas Wunner * pci_dpc_recovered - whether DPC triggered and has recovered successfully
94a97396c6SLukas Wunner * @pdev: PCI device
95a97396c6SLukas Wunner *
96a97396c6SLukas Wunner * Return true if DPC was triggered for @pdev and has recovered successfully.
97a97396c6SLukas Wunner * Wait for recovery if it hasn't completed yet. Called from the PCIe hotplug
98a97396c6SLukas Wunner * driver to recognize and ignore Link Down/Up events caused by DPC.
99a97396c6SLukas Wunner */
pci_dpc_recovered(struct pci_dev * pdev)100a97396c6SLukas Wunner bool pci_dpc_recovered(struct pci_dev *pdev)
101a97396c6SLukas Wunner {
102a97396c6SLukas Wunner struct pci_host_bridge *host;
103a97396c6SLukas Wunner
104a97396c6SLukas Wunner if (!pdev->dpc_cap)
105a97396c6SLukas Wunner return false;
106a97396c6SLukas Wunner
107a97396c6SLukas Wunner /*
108a97396c6SLukas Wunner * Synchronization between hotplug and DPC is not supported
109a97396c6SLukas Wunner * if DPC is owned by firmware and EDR is not enabled.
110a97396c6SLukas Wunner */
111a97396c6SLukas Wunner host = pci_find_host_bridge(pdev->bus);
112a97396c6SLukas Wunner if (!host->native_dpc && !IS_ENABLED(CONFIG_PCIE_EDR))
113a97396c6SLukas Wunner return false;
114a97396c6SLukas Wunner
115a97396c6SLukas Wunner /*
116a97396c6SLukas Wunner * Need a timeout in case DPC never completes due to failure of
117a97396c6SLukas Wunner * dpc_wait_rp_inactive(). The spec doesn't mandate a time limit,
118a97396c6SLukas Wunner * but reports indicate that DPC completes within 4 seconds.
119a97396c6SLukas Wunner */
120a97396c6SLukas Wunner wait_event_timeout(dpc_completed_waitqueue, dpc_completed(pdev),
121a97396c6SLukas Wunner msecs_to_jiffies(4000));
122a97396c6SLukas Wunner
123a97396c6SLukas Wunner return test_and_clear_bit(PCI_DPC_RECOVERED, &pdev->priv_flags);
124a97396c6SLukas Wunner }
125a97396c6SLukas Wunner #endif /* CONFIG_HOTPLUG_PCI_PCIE */
126a97396c6SLukas Wunner
dpc_wait_rp_inactive(struct pci_dev * pdev)127be06c1b4SBjorn Helgaas static int dpc_wait_rp_inactive(struct pci_dev *pdev)
128e02602bdSBjorn Helgaas {
129e02602bdSBjorn Helgaas unsigned long timeout = jiffies + HZ;
130be06c1b4SBjorn Helgaas u16 cap = pdev->dpc_cap, status;
131e02602bdSBjorn Helgaas
132e02602bdSBjorn Helgaas pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
133e02602bdSBjorn Helgaas while (status & PCI_EXP_DPC_RP_BUSY &&
134e02602bdSBjorn Helgaas !time_after(jiffies, timeout)) {
135e02602bdSBjorn Helgaas msleep(10);
136e02602bdSBjorn Helgaas pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
137e02602bdSBjorn Helgaas }
138e02602bdSBjorn Helgaas if (status & PCI_EXP_DPC_RP_BUSY) {
13910a9990cSFrederick Lawler pci_warn(pdev, "root port still busy\n");
140e02602bdSBjorn Helgaas return -EBUSY;
141e02602bdSBjorn Helgaas }
142e02602bdSBjorn Helgaas return 0;
143e02602bdSBjorn Helgaas }
144e02602bdSBjorn Helgaas
dpc_reset_link(struct pci_dev * pdev)145aea47413SKuppuswamy Sathyanarayanan pci_ers_result_t dpc_reset_link(struct pci_dev *pdev)
146e02602bdSBjorn Helgaas {
147a97396c6SLukas Wunner pci_ers_result_t ret;
148f8d46c89SKeith Busch u16 cap;
149e02602bdSBjorn Helgaas
150a97396c6SLukas Wunner set_bit(PCI_DPC_RECOVERING, &pdev->priv_flags);
151a97396c6SLukas Wunner
152b09803b5SOza Pawandeep /*
153b09803b5SOza Pawandeep * DPC disables the Link automatically in hardware, so it has
154b09803b5SOza Pawandeep * already been reset by the time we get here.
155b09803b5SOza Pawandeep */
156be06c1b4SBjorn Helgaas cap = pdev->dpc_cap;
157e02602bdSBjorn Helgaas
158b09803b5SOza Pawandeep /*
159b09803b5SOza Pawandeep * Wait until the Link is inactive, then clear DPC Trigger Status
160b09803b5SOza Pawandeep * to allow the Port to leave DPC.
161b09803b5SOza Pawandeep */
1628a614499SLukas Wunner if (!pcie_wait_for_link(pdev, false))
1638a614499SLukas Wunner pci_info(pdev, "Data Link Layer Link Active not cleared in 1000 msec\n");
164b09803b5SOza Pawandeep
165a97396c6SLukas Wunner if (pdev->dpc_rp_extensions && dpc_wait_rp_inactive(pdev)) {
166a97396c6SLukas Wunner clear_bit(PCI_DPC_RECOVERED, &pdev->priv_flags);
167a97396c6SLukas Wunner ret = PCI_ERS_RESULT_DISCONNECT;
168a97396c6SLukas Wunner goto out;
169a97396c6SLukas Wunner }
170e02602bdSBjorn Helgaas
171e02602bdSBjorn Helgaas pci_write_config_word(pdev, cap + PCI_EXP_DPC_STATUS,
17256abbf8aSOza Pawandeep PCI_EXP_DPC_STATUS_TRIGGER);
173e02602bdSBjorn Helgaas
174e74b2b58SMika Westerberg if (pci_bridge_wait_for_secondary_bus(pdev, "DPC")) {
175a97396c6SLukas Wunner clear_bit(PCI_DPC_RECOVERED, &pdev->priv_flags);
176a97396c6SLukas Wunner ret = PCI_ERS_RESULT_DISCONNECT;
177a97396c6SLukas Wunner } else {
178a97396c6SLukas Wunner set_bit(PCI_DPC_RECOVERED, &pdev->priv_flags);
179a97396c6SLukas Wunner ret = PCI_ERS_RESULT_RECOVERED;
1808a614499SLukas Wunner }
181a97396c6SLukas Wunner out:
182a97396c6SLukas Wunner clear_bit(PCI_DPC_RECOVERING, &pdev->priv_flags);
183a97396c6SLukas Wunner wake_up_all(&dpc_completed_waitqueue);
184a97396c6SLukas Wunner return ret;
185b09803b5SOza Pawandeep }
186b09803b5SOza Pawandeep
dpc_process_rp_pio_error(struct pci_dev * pdev)187be06c1b4SBjorn Helgaas static void dpc_process_rp_pio_error(struct pci_dev *pdev)
188e02602bdSBjorn Helgaas {
189be06c1b4SBjorn Helgaas u16 cap = pdev->dpc_cap, dpc_status, first_error;
190e02602bdSBjorn Helgaas u32 status, mask, sev, syserr, exc, dw0, dw1, dw2, dw3, log, prefix;
191e02602bdSBjorn Helgaas int i;
192e02602bdSBjorn Helgaas
193e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_STATUS, &status);
194e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_MASK, &mask);
19510a9990cSFrederick Lawler pci_err(pdev, "rp_pio_status: %#010x, rp_pio_mask: %#010x\n",
196e02602bdSBjorn Helgaas status, mask);
197e02602bdSBjorn Helgaas
198e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_SEVERITY, &sev);
199e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_SYSERROR, &syserr);
200e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_EXCEPTION, &exc);
20110a9990cSFrederick Lawler pci_err(pdev, "RP PIO severity=%#010x, syserror=%#010x, exception=%#010x\n",
202e02602bdSBjorn Helgaas sev, syserr, exc);
203e02602bdSBjorn Helgaas
204e02602bdSBjorn Helgaas /* Get First Error Pointer */
205e02602bdSBjorn Helgaas pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &dpc_status);
206*d9a28916SBjorn Helgaas first_error = FIELD_GET(PCI_EXP_DPC_RP_PIO_FEP, dpc_status);
207e02602bdSBjorn Helgaas
208e02602bdSBjorn Helgaas for (i = 0; i < ARRAY_SIZE(rp_pio_error_string); i++) {
209f1d16b17SKeith Busch if ((status & ~mask) & (1 << i))
21010a9990cSFrederick Lawler pci_err(pdev, "[%2d] %s%s\n", i, rp_pio_error_string[i],
211e02602bdSBjorn Helgaas first_error == i ? " (First)" : "");
212e02602bdSBjorn Helgaas }
213e02602bdSBjorn Helgaas
214be06c1b4SBjorn Helgaas if (pdev->dpc_rp_log_size < 4)
215f1d16b17SKeith Busch goto clear_status;
216e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG,
217e02602bdSBjorn Helgaas &dw0);
218e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 4,
219e02602bdSBjorn Helgaas &dw1);
220e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 8,
221e02602bdSBjorn Helgaas &dw2);
222e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_HEADER_LOG + 12,
223e02602bdSBjorn Helgaas &dw3);
22410a9990cSFrederick Lawler pci_err(pdev, "TLP Header: %#010x %#010x %#010x %#010x\n",
225e02602bdSBjorn Helgaas dw0, dw1, dw2, dw3);
226e02602bdSBjorn Helgaas
227be06c1b4SBjorn Helgaas if (pdev->dpc_rp_log_size < 5)
228f1d16b17SKeith Busch goto clear_status;
229e02602bdSBjorn Helgaas pci_read_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_IMPSPEC_LOG, &log);
23010a9990cSFrederick Lawler pci_err(pdev, "RP PIO ImpSpec Log %#010x\n", log);
231e02602bdSBjorn Helgaas
232be06c1b4SBjorn Helgaas for (i = 0; i < pdev->dpc_rp_log_size - 5; i++) {
233e02602bdSBjorn Helgaas pci_read_config_dword(pdev,
234ef8a156cSIlpo Järvinen cap + PCI_EXP_DPC_RP_PIO_TLPPREFIX_LOG + i * 4, &prefix);
23510a9990cSFrederick Lawler pci_err(pdev, "TLP Prefix Header: dw%d, %#010x\n", i, prefix);
236e02602bdSBjorn Helgaas }
237f1d16b17SKeith Busch clear_status:
238f1d16b17SKeith Busch pci_write_config_dword(pdev, cap + PCI_EXP_DPC_RP_PIO_STATUS, status);
239e02602bdSBjorn Helgaas }
240e02602bdSBjorn Helgaas
dpc_get_aer_uncorrect_severity(struct pci_dev * dev,struct aer_err_info * info)2419f08a5d8SDongdong Liu static int dpc_get_aer_uncorrect_severity(struct pci_dev *dev,
2429f08a5d8SDongdong Liu struct aer_err_info *info)
2439f08a5d8SDongdong Liu {
2449f08a5d8SDongdong Liu int pos = dev->aer_cap;
2459f08a5d8SDongdong Liu u32 status, mask, sev;
2469f08a5d8SDongdong Liu
2479f08a5d8SDongdong Liu pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
2489f08a5d8SDongdong Liu pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
2499f08a5d8SDongdong Liu status &= ~mask;
2509f08a5d8SDongdong Liu if (!status)
2519f08a5d8SDongdong Liu return 0;
2529f08a5d8SDongdong Liu
2539f08a5d8SDongdong Liu pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &sev);
2549f08a5d8SDongdong Liu status &= sev;
2559f08a5d8SDongdong Liu if (status)
2569f08a5d8SDongdong Liu info->severity = AER_FATAL;
2579f08a5d8SDongdong Liu else
2589f08a5d8SDongdong Liu info->severity = AER_NONFATAL;
2599f08a5d8SDongdong Liu
2609f08a5d8SDongdong Liu return 1;
2619f08a5d8SDongdong Liu }
2629f08a5d8SDongdong Liu
dpc_process_error(struct pci_dev * pdev)263aea47413SKuppuswamy Sathyanarayanan void dpc_process_error(struct pci_dev *pdev)
264e02602bdSBjorn Helgaas {
265be06c1b4SBjorn Helgaas u16 cap = pdev->dpc_cap, status, source, reason, ext_reason;
2668aefa9b0SKeith Busch struct aer_err_info info;
267e02602bdSBjorn Helgaas
268e02602bdSBjorn Helgaas pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
2690c27e28fSKeith Busch pci_read_config_word(pdev, cap + PCI_EXP_DPC_SOURCE_ID, &source);
270e02602bdSBjorn Helgaas
27110a9990cSFrederick Lawler pci_info(pdev, "containment event, status:%#06x source:%#06x\n",
272e02602bdSBjorn Helgaas status, source);
273e02602bdSBjorn Helgaas
274e02602bdSBjorn Helgaas reason = (status & PCI_EXP_DPC_STATUS_TRIGGER_RSN) >> 1;
275e02602bdSBjorn Helgaas ext_reason = (status & PCI_EXP_DPC_STATUS_TRIGGER_RSN_EXT) >> 5;
27610a9990cSFrederick Lawler pci_warn(pdev, "%s detected\n",
277e02602bdSBjorn Helgaas (reason == 0) ? "unmasked uncorrectable error" :
278e02602bdSBjorn Helgaas (reason == 1) ? "ERR_NONFATAL" :
279e02602bdSBjorn Helgaas (reason == 2) ? "ERR_FATAL" :
280e02602bdSBjorn Helgaas (ext_reason == 0) ? "RP PIO error" :
281e02602bdSBjorn Helgaas (ext_reason == 1) ? "software trigger" :
282e02602bdSBjorn Helgaas "reserved error");
2830c27e28fSKeith Busch
284e02602bdSBjorn Helgaas /* show RP PIO error detail information */
285be06c1b4SBjorn Helgaas if (pdev->dpc_rp_extensions && reason == 3 && ext_reason == 0)
286be06c1b4SBjorn Helgaas dpc_process_rp_pio_error(pdev);
2879f08a5d8SDongdong Liu else if (reason == 0 &&
2889f08a5d8SDongdong Liu dpc_get_aer_uncorrect_severity(pdev, &info) &&
2899f08a5d8SDongdong Liu aer_get_device_error_info(pdev, &info)) {
2908aefa9b0SKeith Busch aer_print_error(pdev, &info);
291894020fdSKuppuswamy Sathyanarayanan pci_aer_clear_nonfatal_status(pdev);
2929f08a5d8SDongdong Liu pci_aer_clear_fatal_status(pdev);
2938aefa9b0SKeith Busch }
294aea47413SKuppuswamy Sathyanarayanan }
295aea47413SKuppuswamy Sathyanarayanan
dpc_handler(int irq,void * context)296aea47413SKuppuswamy Sathyanarayanan static irqreturn_t dpc_handler(int irq, void *context)
297aea47413SKuppuswamy Sathyanarayanan {
298aea47413SKuppuswamy Sathyanarayanan struct pci_dev *pdev = context;
299aea47413SKuppuswamy Sathyanarayanan
300aea47413SKuppuswamy Sathyanarayanan dpc_process_error(pdev);
301e02602bdSBjorn Helgaas
3020c27e28fSKeith Busch /* We configure DPC so it only triggers on ERR_FATAL */
303b6cf1a42SKuppuswamy Sathyanarayanan pcie_do_recovery(pdev, pci_channel_io_frozen, dpc_reset_link);
304738c4e41SKeith Busch
305738c4e41SKeith Busch return IRQ_HANDLED;
3060c27e28fSKeith Busch }
3070c27e28fSKeith Busch
dpc_irq(int irq,void * context)3080c27e28fSKeith Busch static irqreturn_t dpc_irq(int irq, void *context)
3090c27e28fSKeith Busch {
310be06c1b4SBjorn Helgaas struct pci_dev *pdev = context;
311be06c1b4SBjorn Helgaas u16 cap = pdev->dpc_cap, status;
3120c27e28fSKeith Busch
3130c27e28fSKeith Busch pci_read_config_word(pdev, cap + PCI_EXP_DPC_STATUS, &status);
3140c27e28fSKeith Busch
3150242132dSNaveen Naidu if (!(status & PCI_EXP_DPC_STATUS_INTERRUPT) || PCI_POSSIBLE_ERROR(status))
3160c27e28fSKeith Busch return IRQ_NONE;
3170c27e28fSKeith Busch
31856abbf8aSOza Pawandeep pci_write_config_word(pdev, cap + PCI_EXP_DPC_STATUS,
31956abbf8aSOza Pawandeep PCI_EXP_DPC_STATUS_INTERRUPT);
320f8d46c89SKeith Busch if (status & PCI_EXP_DPC_STATUS_TRIGGER)
321738c4e41SKeith Busch return IRQ_WAKE_THREAD;
322e02602bdSBjorn Helgaas return IRQ_HANDLED;
323e02602bdSBjorn Helgaas }
324e02602bdSBjorn Helgaas
pci_dpc_init(struct pci_dev * pdev)32527005618SKuppuswamy Sathyanarayanan void pci_dpc_init(struct pci_dev *pdev)
32627005618SKuppuswamy Sathyanarayanan {
32727005618SKuppuswamy Sathyanarayanan u16 cap;
32827005618SKuppuswamy Sathyanarayanan
32927005618SKuppuswamy Sathyanarayanan pdev->dpc_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DPC);
33027005618SKuppuswamy Sathyanarayanan if (!pdev->dpc_cap)
33127005618SKuppuswamy Sathyanarayanan return;
33227005618SKuppuswamy Sathyanarayanan
33327005618SKuppuswamy Sathyanarayanan pci_read_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_CAP, &cap);
33427005618SKuppuswamy Sathyanarayanan if (!(cap & PCI_EXP_DPC_CAP_RP_EXT))
33527005618SKuppuswamy Sathyanarayanan return;
33627005618SKuppuswamy Sathyanarayanan
33727005618SKuppuswamy Sathyanarayanan pdev->dpc_rp_extensions = true;
3385459c0b7SMika Westerberg
3395459c0b7SMika Westerberg /* Quirks may set dpc_rp_log_size if device or firmware is buggy */
3405459c0b7SMika Westerberg if (!pdev->dpc_rp_log_size) {
3415459c0b7SMika Westerberg pdev->dpc_rp_log_size =
342*d9a28916SBjorn Helgaas FIELD_GET(PCI_EXP_DPC_RP_PIO_LOG_SIZE, cap);
34327005618SKuppuswamy Sathyanarayanan if (pdev->dpc_rp_log_size < 4 || pdev->dpc_rp_log_size > 9) {
34427005618SKuppuswamy Sathyanarayanan pci_err(pdev, "RP PIO log size %u is invalid\n",
34527005618SKuppuswamy Sathyanarayanan pdev->dpc_rp_log_size);
34627005618SKuppuswamy Sathyanarayanan pdev->dpc_rp_log_size = 0;
34727005618SKuppuswamy Sathyanarayanan }
34827005618SKuppuswamy Sathyanarayanan }
3495459c0b7SMika Westerberg }
35027005618SKuppuswamy Sathyanarayanan
351e02602bdSBjorn Helgaas #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
dpc_probe(struct pcie_device * dev)352e02602bdSBjorn Helgaas static int dpc_probe(struct pcie_device *dev)
353e02602bdSBjorn Helgaas {
354e02602bdSBjorn Helgaas struct pci_dev *pdev = dev->port;
355e02602bdSBjorn Helgaas struct device *device = &dev->device;
356e02602bdSBjorn Helgaas int status;
357e02602bdSBjorn Helgaas u16 ctl, cap;
358e02602bdSBjorn Helgaas
359708b2000SKuppuswamy Sathyanarayanan if (!pcie_aer_is_native(pdev) && !pcie_ports_dpc_native)
360e02602bdSBjorn Helgaas return -ENOTSUPP;
361e02602bdSBjorn Helgaas
362738c4e41SKeith Busch status = devm_request_threaded_irq(device, dev->irq, dpc_irq,
363738c4e41SKeith Busch dpc_handler, IRQF_SHARED,
364be06c1b4SBjorn Helgaas "pcie-dpc", pdev);
365e02602bdSBjorn Helgaas if (status) {
36610a9990cSFrederick Lawler pci_warn(pdev, "request IRQ%d failed: %d\n", dev->irq,
367e02602bdSBjorn Helgaas status);
368e02602bdSBjorn Helgaas return status;
369e02602bdSBjorn Helgaas }
370e02602bdSBjorn Helgaas
371be06c1b4SBjorn Helgaas pci_read_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_CAP, &cap);
372be06c1b4SBjorn Helgaas pci_read_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_CTL, &ctl);
373e02602bdSBjorn Helgaas
3746927868eSOza Pawandeep ctl = (ctl & 0xfff4) | PCI_EXP_DPC_CTL_EN_FATAL | PCI_EXP_DPC_CTL_INT_EN;
375be06c1b4SBjorn Helgaas pci_write_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_CTL, ctl);
3769103aaf9SYicong Yang pci_info(pdev, "enabled with IRQ %d\n", dev->irq);
377e02602bdSBjorn Helgaas
37810a9990cSFrederick Lawler pci_info(pdev, "error containment capabilities: Int Msg #%d, RPExt%c PoisonedTLP%c SwTrigger%c RP PIO Log %d, DL_ActiveErr%c\n",
379e02602bdSBjorn Helgaas cap & PCI_EXP_DPC_IRQ, FLAG(cap, PCI_EXP_DPC_CAP_RP_EXT),
380e02602bdSBjorn Helgaas FLAG(cap, PCI_EXP_DPC_CAP_POISONED_TLP),
381be06c1b4SBjorn Helgaas FLAG(cap, PCI_EXP_DPC_CAP_SW_TRIGGER), pdev->dpc_rp_log_size,
382e02602bdSBjorn Helgaas FLAG(cap, PCI_EXP_DPC_CAP_DL_ACTIVE));
3834f802170SKeith Busch
3844f802170SKeith Busch pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_DPC, sizeof(u16));
385e02602bdSBjorn Helgaas return status;
386e02602bdSBjorn Helgaas }
387e02602bdSBjorn Helgaas
dpc_remove(struct pcie_device * dev)388e02602bdSBjorn Helgaas static void dpc_remove(struct pcie_device *dev)
389e02602bdSBjorn Helgaas {
390e02602bdSBjorn Helgaas struct pci_dev *pdev = dev->port;
391e02602bdSBjorn Helgaas u16 ctl;
392e02602bdSBjorn Helgaas
393be06c1b4SBjorn Helgaas pci_read_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_CTL, &ctl);
3946927868eSOza Pawandeep ctl &= ~(PCI_EXP_DPC_CTL_EN_FATAL | PCI_EXP_DPC_CTL_INT_EN);
395be06c1b4SBjorn Helgaas pci_write_config_word(pdev, pdev->dpc_cap + PCI_EXP_DPC_CTL, ctl);
396e02602bdSBjorn Helgaas }
397e02602bdSBjorn Helgaas
398e02602bdSBjorn Helgaas static struct pcie_port_service_driver dpcdriver = {
399e02602bdSBjorn Helgaas .name = "dpc",
400e02602bdSBjorn Helgaas .port_type = PCIE_ANY_PORT,
401e02602bdSBjorn Helgaas .service = PCIE_PORT_SERVICE_DPC,
402e02602bdSBjorn Helgaas .probe = dpc_probe,
403e02602bdSBjorn Helgaas .remove = dpc_remove,
404e02602bdSBjorn Helgaas };
405e02602bdSBjorn Helgaas
pcie_dpc_init(void)406c29de841SKeith Busch int __init pcie_dpc_init(void)
407e02602bdSBjorn Helgaas {
408e02602bdSBjorn Helgaas return pcie_port_service_register(&dpcdriver);
409e02602bdSBjorn Helgaas }
410