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/module.h>
19 #include <linux/pci.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/pcieport_if.h>
29 #include "../../pci.h"
34 #define DRIVER_VERSION "v1.0"
35 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
36 #define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
37 MODULE_AUTHOR(DRIVER_AUTHOR);
38 MODULE_DESCRIPTION(DRIVER_DESC);
39 MODULE_LICENSE("GPL");
41 static int __devinit aer_probe (struct pcie_device *dev,
42 const struct pcie_port_service_id *id );
43 static void aer_remove(struct pcie_device *dev);
44 static int aer_suspend(struct pcie_device *dev, pm_message_t state)
46 static int aer_resume(struct pcie_device *dev) {return 0;}
47 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
48 enum pci_channel_state error);
49 static void aer_error_resume(struct pci_dev *dev);
50 static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
53 * PCI Express bus's AER Root service driver data structure
55 static struct pcie_port_service_id aer_id[] = {
59 .port_type = PCIE_RC_PORT,
60 .service_type = PCIE_PORT_SERVICE_AER,
62 { /* end: all zeroes */ }
65 static struct pci_error_handlers aer_error_handlers = {
66 .error_detected = aer_error_detected,
67 .resume = aer_error_resume,
70 static struct pcie_port_service_driver aerdriver = {
72 .id_table = &aer_id[0],
77 .suspend = aer_suspend,
80 .err_handler = &aer_error_handlers,
82 .reset_link = aer_root_reset,
85 static int pcie_aer_disable;
89 pcie_aer_disable = 1; /* has priority over 'forceload' */
93 * aer_irq - Root Port's ISR
94 * @irq: IRQ assigned to Root Port
95 * @context: pointer to Root Port data structure
97 * Invoked when Root Port detects AER messages.
99 static irqreturn_t aer_irq(int irq, void *context)
101 unsigned int status, id;
102 struct pcie_device *pdev = (struct pcie_device *)context;
103 struct aer_rpc *rpc = get_service_data(pdev);
108 pos = pci_find_aer_capability(pdev->port);
110 * Must lock access to Root Error Status Reg, Root Error ID Reg,
111 * and Root error producer/consumer index
113 spin_lock_irqsave(&rpc->e_lock, flags);
115 /* Read error status */
116 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
117 if (!(status & ROOT_ERR_STATUS_MASKS)) {
118 spin_unlock_irqrestore(&rpc->e_lock, flags);
122 /* Read error source and clear error status */
123 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id);
124 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
126 /* Store error source for later DPC handler */
127 next_prod_idx = rpc->prod_idx + 1;
128 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
130 if (next_prod_idx == rpc->cons_idx) {
132 * Error Storm Condition - possibly the same error occurred.
135 spin_unlock_irqrestore(&rpc->e_lock, flags);
138 rpc->e_sources[rpc->prod_idx].status = status;
139 rpc->e_sources[rpc->prod_idx].id = id;
140 rpc->prod_idx = next_prod_idx;
141 spin_unlock_irqrestore(&rpc->e_lock, flags);
143 /* Invoke DPC handler */
144 schedule_work(&rpc->dpc_handler);
150 * aer_alloc_rpc - allocate Root Port data structure
151 * @dev: pointer to the pcie_dev data structure
153 * Invoked when Root Port's AER service is loaded.
155 static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
159 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
164 * Initialize Root lock access, e_lock, to Root Error Status Reg,
165 * Root Error ID Reg, and Root error producer/consumer index.
167 spin_lock_init(&rpc->e_lock);
170 INIT_WORK(&rpc->dpc_handler, aer_isr);
171 rpc->prod_idx = rpc->cons_idx = 0;
172 mutex_init(&rpc->rpc_mutex);
173 init_waitqueue_head(&rpc->wait_release);
175 /* Use PCIE bus function to store rpc into PCIE device */
176 set_service_data(dev, rpc);
182 * aer_remove - clean up resources
183 * @dev: pointer to the pcie_dev data structure
185 * Invoked when PCI Express bus unloads or AER probe fails.
187 static void aer_remove(struct pcie_device *dev)
189 struct aer_rpc *rpc = get_service_data(dev);
192 /* If register interrupt service, it must be free. */
194 free_irq(dev->irq, dev);
196 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
198 aer_delete_rootport(rpc);
199 set_service_data(dev, NULL);
204 * aer_probe - initialize resources
205 * @dev: pointer to the pcie_dev data structure
206 * @id: pointer to the service id data structure
208 * Invoked when PCI Express bus loads AER service driver.
210 static int __devinit aer_probe (struct pcie_device *dev,
211 const struct pcie_port_service_id *id )
215 struct device *device = &dev->device;
218 if ((status = aer_init(dev)))
221 /* Alloc rpc data structure */
222 if (!(rpc = aer_alloc_rpc(dev))) {
223 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
228 /* Request IRQ ISR */
229 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
231 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
238 aer_enable_rootport(rpc);
244 * aer_root_reset - reset link on Root Port
245 * @dev: pointer to Root Port's pci_dev data structure
247 * Invoked by Port Bus driver when performing link reset at Root Port.
249 static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
255 pos = pci_find_aer_capability(dev);
257 /* Disable Root's interrupt in response to error messages */
258 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
260 /* Assert Secondary Bus Reset */
261 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
262 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
263 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
265 /* De-assert Secondary Bus Reset */
266 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
267 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
270 * System software must wait for at least 100ms from the end
271 * of a reset of one or more device before it is permitted
272 * to issue Configuration Requests to those devices.
275 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
277 /* Enable Root Port's interrupt in response to error messages */
278 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
279 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
280 pci_write_config_dword(dev,
281 pos + PCI_ERR_ROOT_COMMAND,
282 ROOT_PORT_INTR_ON_MESG_MASK);
284 return PCI_ERS_RESULT_RECOVERED;
288 * aer_error_detected - update severity status
289 * @dev: pointer to Root Port's pci_dev data structure
290 * @error: error severity being notified by port bus
292 * Invoked by Port Bus driver during error recovery.
294 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
295 enum pci_channel_state error)
297 /* Root Port has no impact. Always recovers. */
298 return PCI_ERS_RESULT_CAN_RECOVER;
302 * aer_error_resume - clean up corresponding error status bits
303 * @dev: pointer to Root Port's pci_dev data structure
305 * Invoked by Port Bus driver during nonfatal recovery.
307 static void aer_error_resume(struct pci_dev *dev)
313 /* Clean up Root device status */
314 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
315 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, ®16);
316 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
318 /* Clean AER Root Error Status */
319 pos = pci_find_aer_capability(dev);
320 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
321 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
322 if (dev->error_state == pci_channel_io_normal)
323 status &= ~mask; /* Clear corresponding nonfatal bits */
325 status &= mask; /* Clear corresponding fatal bits */
326 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
330 * aer_service_init - register AER root service driver
332 * Invoked when AER root service driver is loaded.
334 static int __init aer_service_init(void)
336 if (pcie_aer_disable)
338 return pcie_port_service_register(&aerdriver);
342 * aer_service_exit - unregister AER root service driver
344 * Invoked when AER root service driver is unloaded.
346 static void __exit aer_service_exit(void)
348 pcie_port_service_unregister(&aerdriver);
351 module_init(aer_service_init);
352 module_exit(aer_service_exit);