2 * drivers/pci/pcie/aer/aerdrv.c
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * This file implements the AER root port service driver. The driver will
9 * register an irq handler. When root port triggers an AER interrupt, the irq
10 * handler will collect root port status and schedule a work.
12 * Copyright (C) 2006 Intel Corp.
13 * Tom Long Nguyen (tom.l.nguyen@intel.com)
14 * Zhang Yanmin (yanmin.zhang@intel.com)
18 #include <linux/pci.h>
19 #include <linux/pci-acpi.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/pcieport_if.h>
28 #include <linux/slab.h>
31 #include "../../pci.h"
36 #define DRIVER_VERSION "v1.0"
37 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
38 #define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
40 static int aer_probe(struct pcie_device *dev);
41 static void aer_remove(struct pcie_device *dev);
42 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
43 enum pci_channel_state error);
44 static void aer_error_resume(struct pci_dev *dev);
45 static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
47 static const struct pci_error_handlers aer_error_handlers = {
48 .error_detected = aer_error_detected,
49 .resume = aer_error_resume,
52 static struct pcie_port_service_driver aerdriver = {
54 .port_type = PCI_EXP_TYPE_ROOT_PORT,
55 .service = PCIE_PORT_SERVICE_AER,
60 .err_handler = &aer_error_handlers,
62 .reset_link = aer_root_reset,
65 static int pcie_aer_disable;
69 pcie_aer_disable = 1; /* has priority over 'forceload' */
72 bool pci_aer_available(void)
74 return !pcie_aer_disable && pci_msi_enabled();
77 static int set_device_error_reporting(struct pci_dev *dev, void *data)
79 bool enable = *((bool *)data);
80 int type = pci_pcie_type(dev);
82 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
83 (type == PCI_EXP_TYPE_UPSTREAM) ||
84 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
86 pci_enable_pcie_error_reporting(dev);
88 pci_disable_pcie_error_reporting(dev);
92 pcie_set_ecrc_checking(dev);
98 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
99 * @dev: pointer to root port's pci_dev data structure
100 * @enable: true = enable error reporting, false = disable error reporting.
102 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
105 set_device_error_reporting(dev, &enable);
107 if (!dev->subordinate)
109 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
113 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
114 * @rpc: pointer to a Root Port data structure
116 * Invoked when PCIe bus loads AER service driver.
118 static void aer_enable_rootport(struct aer_rpc *rpc)
120 struct pci_dev *pdev = rpc->rpd->port;
125 /* Clear PCIe Capability's Device Status */
126 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, ®16);
127 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
129 /* Disable system error generation in response to error messages */
130 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
131 SYSTEM_ERROR_INTR_ON_MESG_MASK);
133 aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
134 /* Clear error status */
135 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, ®32);
136 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
137 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, ®32);
138 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
139 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, ®32);
140 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
143 * Enable error reporting for the root port device and downstream port
146 set_downstream_devices_error_reporting(pdev, true);
148 /* Enable Root Port's interrupt in response to error messages */
149 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, ®32);
150 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
151 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
155 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
156 * @rpc: pointer to a Root Port data structure
158 * Invoked when PCIe bus unloads AER service driver.
160 static void aer_disable_rootport(struct aer_rpc *rpc)
162 struct pci_dev *pdev = rpc->rpd->port;
167 * Disable error reporting for the root port device and downstream port
170 set_downstream_devices_error_reporting(pdev, false);
172 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
173 /* Disable Root's interrupt in response to error messages */
174 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, ®32);
175 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
176 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
178 /* Clear Root's error status reg */
179 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32);
180 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
184 * aer_irq - Root Port's ISR
185 * @irq: IRQ assigned to Root Port
186 * @context: pointer to Root Port data structure
188 * Invoked when Root Port detects AER messages.
190 irqreturn_t aer_irq(int irq, void *context)
192 unsigned int status, id;
193 struct pcie_device *pdev = (struct pcie_device *)context;
194 struct aer_rpc *rpc = get_service_data(pdev);
199 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
201 * Must lock access to Root Error Status Reg, Root Error ID Reg,
202 * and Root error producer/consumer index
204 spin_lock_irqsave(&rpc->e_lock, flags);
206 /* Read error status */
207 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
208 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
209 spin_unlock_irqrestore(&rpc->e_lock, flags);
213 /* Read error source and clear error status */
214 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
215 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
217 /* Store error source for later DPC handler */
218 next_prod_idx = rpc->prod_idx + 1;
219 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
221 if (next_prod_idx == rpc->cons_idx) {
223 * Error Storm Condition - possibly the same error occurred.
226 spin_unlock_irqrestore(&rpc->e_lock, flags);
229 rpc->e_sources[rpc->prod_idx].status = status;
230 rpc->e_sources[rpc->prod_idx].id = id;
231 rpc->prod_idx = next_prod_idx;
232 spin_unlock_irqrestore(&rpc->e_lock, flags);
234 /* Invoke DPC handler */
235 schedule_work(&rpc->dpc_handler);
239 EXPORT_SYMBOL_GPL(aer_irq);
242 * aer_alloc_rpc - allocate Root Port data structure
243 * @dev: pointer to the pcie_dev data structure
245 * Invoked when Root Port's AER service is loaded.
247 static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
251 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
255 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
256 spin_lock_init(&rpc->e_lock);
259 INIT_WORK(&rpc->dpc_handler, aer_isr);
260 mutex_init(&rpc->rpc_mutex);
262 /* Use PCIe bus function to store rpc into PCIe device */
263 set_service_data(dev, rpc);
269 * aer_remove - clean up resources
270 * @dev: pointer to the pcie_dev data structure
272 * Invoked when PCI Express bus unloads or AER probe fails.
274 static void aer_remove(struct pcie_device *dev)
276 struct aer_rpc *rpc = get_service_data(dev);
279 /* If register interrupt service, it must be free. */
281 free_irq(dev->irq, dev);
283 flush_work(&rpc->dpc_handler);
284 aer_disable_rootport(rpc);
286 set_service_data(dev, NULL);
291 * aer_probe - initialize resources
292 * @dev: pointer to the pcie_dev data structure
293 * @id: pointer to the service id data structure
295 * Invoked when PCI Express bus loads AER service driver.
297 static int aer_probe(struct pcie_device *dev)
301 struct device *device = &dev->device;
304 status = aer_init(dev);
308 /* Alloc rpc data structure */
309 rpc = aer_alloc_rpc(dev);
311 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
316 /* Request IRQ ISR */
317 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
319 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
326 aer_enable_rootport(rpc);
332 * aer_root_reset - reset link on Root Port
333 * @dev: pointer to Root Port's pci_dev data structure
335 * Invoked by Port Bus driver when performing link reset at Root Port.
337 static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
342 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
344 /* Disable Root's interrupt in response to error messages */
345 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, ®32);
346 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
347 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
349 pci_reset_bridge_secondary_bus(dev);
350 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
352 /* Clear Root Error Status */
353 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32);
354 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
356 /* Enable Root Port's interrupt in response to error messages */
357 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, ®32);
358 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
359 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
361 return PCI_ERS_RESULT_RECOVERED;
365 * aer_error_detected - update severity status
366 * @dev: pointer to Root Port's pci_dev data structure
367 * @error: error severity being notified by port bus
369 * Invoked by Port Bus driver during error recovery.
371 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
372 enum pci_channel_state error)
374 /* Root Port has no impact. Always recovers. */
375 return PCI_ERS_RESULT_CAN_RECOVER;
379 * aer_error_resume - clean up corresponding error status bits
380 * @dev: pointer to Root Port's pci_dev data structure
382 * Invoked by Port Bus driver during nonfatal recovery.
384 static void aer_error_resume(struct pci_dev *dev)
390 /* Clean up Root device status */
391 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, ®16);
392 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
394 /* Clean AER Root Error Status */
395 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
396 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
397 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
398 if (dev->error_state == pci_channel_io_normal)
399 status &= ~mask; /* Clear corresponding nonfatal bits */
401 status &= mask; /* Clear corresponding fatal bits */
402 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
406 * aer_service_init - register AER root service driver
408 * Invoked when AER root service driver is loaded.
410 static int __init aer_service_init(void)
412 if (!pci_aer_available() || aer_acpi_firmware_first())
414 return pcie_port_service_register(&aerdriver);
416 device_initcall(aer_service_init);