1 // SPDX-License-Identifier: GPL-2.0
3 * Implement the AER root port service driver. The driver registers an IRQ
4 * handler. When a root port triggers an AER interrupt, the IRQ handler
5 * collects root port status and schedules work.
7 * Copyright (C) 2006 Intel Corp.
8 * Tom Long Nguyen (tom.l.nguyen@intel.com)
9 * Zhang Yanmin (yanmin.zhang@intel.com)
12 #include <linux/pci.h>
13 #include <linux/pci-acpi.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
24 #include "../../pci.h"
26 static int aer_probe(struct pcie_device *dev);
27 static void aer_remove(struct pcie_device *dev);
28 static void aer_error_resume(struct pci_dev *dev);
29 static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
31 static struct pcie_port_service_driver aerdriver = {
33 .port_type = PCI_EXP_TYPE_ROOT_PORT,
34 .service = PCIE_PORT_SERVICE_AER,
38 .error_resume = aer_error_resume,
39 .reset_link = aer_root_reset,
42 static int pcie_aer_disable;
49 bool pci_aer_available(void)
51 return !pcie_aer_disable && pci_msi_enabled();
54 static int set_device_error_reporting(struct pci_dev *dev, void *data)
56 bool enable = *((bool *)data);
57 int type = pci_pcie_type(dev);
59 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
60 (type == PCI_EXP_TYPE_UPSTREAM) ||
61 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
63 pci_enable_pcie_error_reporting(dev);
65 pci_disable_pcie_error_reporting(dev);
69 pcie_set_ecrc_checking(dev);
75 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
76 * @dev: pointer to root port's pci_dev data structure
77 * @enable: true = enable error reporting, false = disable error reporting.
79 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
82 set_device_error_reporting(dev, &enable);
84 if (!dev->subordinate)
86 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
90 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
91 * @rpc: pointer to a Root Port data structure
93 * Invoked when PCIe bus loads AER service driver.
95 static void aer_enable_rootport(struct aer_rpc *rpc)
97 struct pci_dev *pdev = rpc->rpd;
102 /* Clear PCIe Capability's Device Status */
103 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, ®16);
104 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
106 /* Disable system error generation in response to error messages */
107 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
108 SYSTEM_ERROR_INTR_ON_MESG_MASK);
110 aer_pos = pdev->aer_cap;
111 /* Clear error status */
112 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, ®32);
113 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
114 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, ®32);
115 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
116 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, ®32);
117 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
120 * Enable error reporting for the root port device and downstream port
123 set_downstream_devices_error_reporting(pdev, true);
125 /* Enable Root Port's interrupt in response to error messages */
126 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, ®32);
127 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
128 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
132 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
133 * @rpc: pointer to a Root Port data structure
135 * Invoked when PCIe bus unloads AER service driver.
137 static void aer_disable_rootport(struct aer_rpc *rpc)
139 struct pci_dev *pdev = rpc->rpd;
144 * Disable error reporting for the root port device and downstream port
147 set_downstream_devices_error_reporting(pdev, false);
150 /* Disable Root's interrupt in response to error messages */
151 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, ®32);
152 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
153 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
155 /* Clear Root's error status reg */
156 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32);
157 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
161 * aer_irq - Root Port's ISR
162 * @irq: IRQ assigned to Root Port
163 * @context: pointer to Root Port data structure
165 * Invoked when Root Port detects AER messages.
167 irqreturn_t aer_irq(int irq, void *context)
169 unsigned int status, id;
170 struct pcie_device *pdev = (struct pcie_device *)context;
171 struct aer_rpc *rpc = get_service_data(pdev);
176 pos = pdev->port->aer_cap;
178 * Must lock access to Root Error Status Reg, Root Error ID Reg,
179 * and Root error producer/consumer index
181 spin_lock_irqsave(&rpc->e_lock, flags);
183 /* Read error status */
184 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
185 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
186 spin_unlock_irqrestore(&rpc->e_lock, flags);
190 /* Read error source and clear error status */
191 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
192 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
194 /* Store error source for later DPC handler */
195 next_prod_idx = rpc->prod_idx + 1;
196 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
198 if (next_prod_idx == rpc->cons_idx) {
200 * Error Storm Condition - possibly the same error occurred.
203 spin_unlock_irqrestore(&rpc->e_lock, flags);
206 rpc->e_sources[rpc->prod_idx].status = status;
207 rpc->e_sources[rpc->prod_idx].id = id;
208 rpc->prod_idx = next_prod_idx;
209 spin_unlock_irqrestore(&rpc->e_lock, flags);
211 /* Invoke DPC handler */
212 schedule_work(&rpc->dpc_handler);
216 EXPORT_SYMBOL_GPL(aer_irq);
219 * aer_alloc_rpc - allocate Root Port data structure
220 * @dev: pointer to the pcie_dev data structure
222 * Invoked when Root Port's AER service is loaded.
224 static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
228 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
232 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
233 spin_lock_init(&rpc->e_lock);
235 rpc->rpd = dev->port;
236 INIT_WORK(&rpc->dpc_handler, aer_isr);
237 mutex_init(&rpc->rpc_mutex);
239 /* Use PCIe bus function to store rpc into PCIe device */
240 set_service_data(dev, rpc);
246 * aer_remove - clean up resources
247 * @dev: pointer to the pcie_dev data structure
249 * Invoked when PCI Express bus unloads or AER probe fails.
251 static void aer_remove(struct pcie_device *dev)
253 struct aer_rpc *rpc = get_service_data(dev);
256 /* If register interrupt service, it must be free. */
258 free_irq(dev->irq, dev);
260 flush_work(&rpc->dpc_handler);
261 aer_disable_rootport(rpc);
263 set_service_data(dev, NULL);
268 * aer_probe - initialize resources
269 * @dev: pointer to the pcie_dev data structure
271 * Invoked when PCI Express bus loads AER service driver.
273 static int aer_probe(struct pcie_device *dev)
277 struct device *device = &dev->port->dev;
279 /* Alloc rpc data structure */
280 rpc = aer_alloc_rpc(dev);
282 dev_printk(KERN_DEBUG, device, "alloc AER rpc failed\n");
287 /* Request IRQ ISR */
288 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
290 dev_printk(KERN_DEBUG, device, "request AER IRQ %d failed\n",
298 aer_enable_rootport(rpc);
299 dev_info(device, "AER enabled with IRQ %d\n", dev->irq);
304 * aer_root_reset - reset link on Root Port
305 * @dev: pointer to Root Port's pci_dev data structure
307 * Invoked by Port Bus driver when performing link reset at Root Port.
309 static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
316 /* Disable Root's interrupt in response to error messages */
317 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, ®32);
318 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
319 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
321 pci_reset_bridge_secondary_bus(dev);
322 pci_printk(KERN_DEBUG, dev, "Root Port link has been reset\n");
324 /* Clear Root Error Status */
325 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32);
326 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
328 /* Enable Root Port's interrupt in response to error messages */
329 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, ®32);
330 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
331 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
333 return PCI_ERS_RESULT_RECOVERED;
337 * aer_error_resume - clean up corresponding error status bits
338 * @dev: pointer to Root Port's pci_dev data structure
340 * Invoked by Port Bus driver during nonfatal recovery.
342 static void aer_error_resume(struct pci_dev *dev)
348 /* Clean up Root device status */
349 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, ®16);
350 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
352 /* Clean AER Root Error Status */
354 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
355 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
356 status &= ~mask; /* Clear corresponding nonfatal bits */
357 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
361 * aer_service_init - register AER root service driver
363 * Invoked when AER root service driver is loaded.
365 static int __init aer_service_init(void)
367 if (!pci_aer_available() || aer_acpi_firmware_first())
369 return pcie_port_service_register(&aerdriver);
371 device_initcall(aer_service_init);