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/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"
39 MODULE_AUTHOR(DRIVER_AUTHOR
);
40 MODULE_DESCRIPTION(DRIVER_DESC
);
41 MODULE_LICENSE("GPL");
43 static int __devinit
aer_probe(struct pcie_device
*dev
);
44 static void aer_remove(struct pcie_device
*dev
);
45 static pci_ers_result_t
aer_error_detected(struct pci_dev
*dev
,
46 enum pci_channel_state error
);
47 static void aer_error_resume(struct pci_dev
*dev
);
48 static pci_ers_result_t
aer_root_reset(struct pci_dev
*dev
);
50 static struct pci_error_handlers aer_error_handlers
= {
51 .error_detected
= aer_error_detected
,
52 .resume
= aer_error_resume
,
55 static struct pcie_port_service_driver aerdriver
= {
57 .port_type
= PCI_EXP_TYPE_ROOT_PORT
,
58 .service
= PCIE_PORT_SERVICE_AER
,
63 .err_handler
= &aer_error_handlers
,
65 .reset_link
= aer_root_reset
,
68 static int pcie_aer_disable
;
72 pcie_aer_disable
= 1; /* has priority over 'forceload' */
76 * aer_irq - Root Port's ISR
77 * @irq: IRQ assigned to Root Port
78 * @context: pointer to Root Port data structure
80 * Invoked when Root Port detects AER messages.
82 irqreturn_t
aer_irq(int irq
, void *context
)
84 unsigned int status
, id
;
85 struct pcie_device
*pdev
= (struct pcie_device
*)context
;
86 struct aer_rpc
*rpc
= get_service_data(pdev
);
91 pos
= pci_find_ext_capability(pdev
->port
, PCI_EXT_CAP_ID_ERR
);
93 * Must lock access to Root Error Status Reg, Root Error ID Reg,
94 * and Root error producer/consumer index
96 spin_lock_irqsave(&rpc
->e_lock
, flags
);
98 /* Read error status */
99 pci_read_config_dword(pdev
->port
, pos
+ PCI_ERR_ROOT_STATUS
, &status
);
100 if (!(status
& ROOT_ERR_STATUS_MASKS
)) {
101 spin_unlock_irqrestore(&rpc
->e_lock
, flags
);
105 /* Read error source and clear error status */
106 pci_read_config_dword(pdev
->port
, pos
+ PCI_ERR_ROOT_COR_SRC
, &id
);
107 pci_write_config_dword(pdev
->port
, pos
+ PCI_ERR_ROOT_STATUS
, status
);
109 /* Store error source for later DPC handler */
110 next_prod_idx
= rpc
->prod_idx
+ 1;
111 if (next_prod_idx
== AER_ERROR_SOURCES_MAX
)
113 if (next_prod_idx
== rpc
->cons_idx
) {
115 * Error Storm Condition - possibly the same error occurred.
118 spin_unlock_irqrestore(&rpc
->e_lock
, flags
);
121 rpc
->e_sources
[rpc
->prod_idx
].status
= status
;
122 rpc
->e_sources
[rpc
->prod_idx
].id
= id
;
123 rpc
->prod_idx
= next_prod_idx
;
124 spin_unlock_irqrestore(&rpc
->e_lock
, flags
);
126 /* Invoke DPC handler */
127 schedule_work(&rpc
->dpc_handler
);
131 EXPORT_SYMBOL_GPL(aer_irq
);
134 * aer_alloc_rpc - allocate Root Port data structure
135 * @dev: pointer to the pcie_dev data structure
137 * Invoked when Root Port's AER service is loaded.
139 static struct aer_rpc
*aer_alloc_rpc(struct pcie_device
*dev
)
143 rpc
= kzalloc(sizeof(struct aer_rpc
), GFP_KERNEL
);
148 * Initialize Root lock access, e_lock, to Root Error Status Reg,
149 * Root Error ID Reg, and Root error producer/consumer index.
151 spin_lock_init(&rpc
->e_lock
);
154 INIT_WORK(&rpc
->dpc_handler
, aer_isr
);
155 rpc
->prod_idx
= rpc
->cons_idx
= 0;
156 mutex_init(&rpc
->rpc_mutex
);
157 init_waitqueue_head(&rpc
->wait_release
);
159 /* Use PCIe bus function to store rpc into PCIe device */
160 set_service_data(dev
, rpc
);
166 * aer_remove - clean up resources
167 * @dev: pointer to the pcie_dev data structure
169 * Invoked when PCI Express bus unloads or AER probe fails.
171 static void aer_remove(struct pcie_device
*dev
)
173 struct aer_rpc
*rpc
= get_service_data(dev
);
176 /* If register interrupt service, it must be free. */
178 free_irq(dev
->irq
, dev
);
180 wait_event(rpc
->wait_release
, rpc
->prod_idx
== rpc
->cons_idx
);
182 aer_delete_rootport(rpc
);
183 set_service_data(dev
, NULL
);
188 * aer_probe - initialize resources
189 * @dev: pointer to the pcie_dev data structure
190 * @id: pointer to the service id data structure
192 * Invoked when PCI Express bus loads AER service driver.
194 static int __devinit
aer_probe(struct pcie_device
*dev
)
198 struct device
*device
= &dev
->device
;
201 status
= aer_init(dev
);
205 /* Alloc rpc data structure */
206 rpc
= aer_alloc_rpc(dev
);
208 dev_printk(KERN_DEBUG
, device
, "alloc rpc failed\n");
213 /* Request IRQ ISR */
214 status
= request_irq(dev
->irq
, aer_irq
, IRQF_SHARED
, "aerdrv", dev
);
216 dev_printk(KERN_DEBUG
, device
, "request IRQ failed\n");
223 aer_enable_rootport(rpc
);
229 * aer_root_reset - reset link on Root Port
230 * @dev: pointer to Root Port's pci_dev data structure
232 * Invoked by Port Bus driver when performing link reset at Root Port.
234 static pci_ers_result_t
aer_root_reset(struct pci_dev
*dev
)
240 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
);
242 /* Disable Root's interrupt in response to error messages */
243 pci_write_config_dword(dev
, pos
+ PCI_ERR_ROOT_COMMAND
, 0);
245 /* Assert Secondary Bus Reset */
246 pci_read_config_word(dev
, PCI_BRIDGE_CONTROL
, &p2p_ctrl
);
247 p2p_ctrl
|= PCI_BRIDGE_CTL_BUS_RESET
;
248 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, p2p_ctrl
);
251 * we should send hot reset message for 2ms to allow it time to
252 * propogate to all downstream ports
256 /* De-assert Secondary Bus Reset */
257 p2p_ctrl
&= ~PCI_BRIDGE_CTL_BUS_RESET
;
258 pci_write_config_word(dev
, PCI_BRIDGE_CONTROL
, p2p_ctrl
);
261 * System software must wait for at least 100ms from the end
262 * of a reset of one or more device before it is permitted
263 * to issue Configuration Requests to those devices.
266 dev_printk(KERN_DEBUG
, &dev
->dev
, "Root Port link has been reset\n");
268 /* Enable Root Port's interrupt in response to error messages */
269 pci_read_config_dword(dev
, pos
+ PCI_ERR_ROOT_STATUS
, &status
);
270 pci_write_config_dword(dev
, pos
+ PCI_ERR_ROOT_STATUS
, status
);
271 pci_write_config_dword(dev
,
272 pos
+ PCI_ERR_ROOT_COMMAND
,
273 ROOT_PORT_INTR_ON_MESG_MASK
);
275 return PCI_ERS_RESULT_RECOVERED
;
279 * aer_error_detected - update severity status
280 * @dev: pointer to Root Port's pci_dev data structure
281 * @error: error severity being notified by port bus
283 * Invoked by Port Bus driver during error recovery.
285 static pci_ers_result_t
aer_error_detected(struct pci_dev
*dev
,
286 enum pci_channel_state error
)
288 /* Root Port has no impact. Always recovers. */
289 return PCI_ERS_RESULT_CAN_RECOVER
;
293 * aer_error_resume - clean up corresponding error status bits
294 * @dev: pointer to Root Port's pci_dev data structure
296 * Invoked by Port Bus driver during nonfatal recovery.
298 static void aer_error_resume(struct pci_dev
*dev
)
304 /* Clean up Root device status */
305 pos
= pci_pcie_cap(dev
);
306 pci_read_config_word(dev
, pos
+ PCI_EXP_DEVSTA
, ®16
);
307 pci_write_config_word(dev
, pos
+ PCI_EXP_DEVSTA
, reg16
);
309 /* Clean AER Root Error Status */
310 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
);
311 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_STATUS
, &status
);
312 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_SEVER
, &mask
);
313 if (dev
->error_state
== pci_channel_io_normal
)
314 status
&= ~mask
; /* Clear corresponding nonfatal bits */
316 status
&= mask
; /* Clear corresponding fatal bits */
317 pci_write_config_dword(dev
, pos
+ PCI_ERR_UNCOR_STATUS
, status
);
321 * aer_service_init - register AER root service driver
323 * Invoked when AER root service driver is loaded.
325 static int __init
aer_service_init(void)
327 if (pcie_aer_disable
)
329 if (!pci_msi_enabled())
331 return pcie_port_service_register(&aerdriver
);
335 * aer_service_exit - unregister AER root service driver
337 * Invoked when AER root service driver is unloaded.
339 static void __exit
aer_service_exit(void)
341 pcie_port_service_unregister(&aerdriver
);
344 module_init(aer_service_init
);
345 module_exit(aer_service_exit
);