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' */
75 static int set_device_error_reporting(struct pci_dev
*dev
, void *data
)
77 bool enable
= *((bool *)data
);
79 if ((dev
->pcie_type
== PCI_EXP_TYPE_ROOT_PORT
) ||
80 (dev
->pcie_type
== PCI_EXP_TYPE_UPSTREAM
) ||
81 (dev
->pcie_type
== PCI_EXP_TYPE_DOWNSTREAM
)) {
83 pci_enable_pcie_error_reporting(dev
);
85 pci_disable_pcie_error_reporting(dev
);
89 pcie_set_ecrc_checking(dev
);
95 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
96 * @dev: pointer to root port's pci_dev data structure
97 * @enable: true = enable error reporting, false = disable error reporting.
99 static void set_downstream_devices_error_reporting(struct pci_dev
*dev
,
102 set_device_error_reporting(dev
, &enable
);
104 if (!dev
->subordinate
)
106 pci_walk_bus(dev
->subordinate
, set_device_error_reporting
, &enable
);
110 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
111 * @rpc: pointer to a Root Port data structure
113 * Invoked when PCIe bus loads AER service driver.
115 static void aer_enable_rootport(struct aer_rpc
*rpc
)
117 struct pci_dev
*pdev
= rpc
->rpd
->port
;
122 pos
= pci_pcie_cap(pdev
);
123 /* Clear PCIe Capability's Device Status */
124 pci_read_config_word(pdev
, pos
+PCI_EXP_DEVSTA
, ®16
);
125 pci_write_config_word(pdev
, pos
+PCI_EXP_DEVSTA
, reg16
);
127 /* Disable system error generation in response to error messages */
128 pci_read_config_word(pdev
, pos
+ PCI_EXP_RTCTL
, ®16
);
129 reg16
&= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK
);
130 pci_write_config_word(pdev
, pos
+ PCI_EXP_RTCTL
, reg16
);
132 aer_pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_ERR
);
133 /* Clear error status */
134 pci_read_config_dword(pdev
, aer_pos
+ PCI_ERR_ROOT_STATUS
, ®32
);
135 pci_write_config_dword(pdev
, aer_pos
+ PCI_ERR_ROOT_STATUS
, reg32
);
136 pci_read_config_dword(pdev
, aer_pos
+ PCI_ERR_COR_STATUS
, ®32
);
137 pci_write_config_dword(pdev
, aer_pos
+ PCI_ERR_COR_STATUS
, reg32
);
138 pci_read_config_dword(pdev
, aer_pos
+ PCI_ERR_UNCOR_STATUS
, ®32
);
139 pci_write_config_dword(pdev
, aer_pos
+ PCI_ERR_UNCOR_STATUS
, reg32
);
142 * Enable error reporting for the root port device and downstream port
145 set_downstream_devices_error_reporting(pdev
, true);
147 /* Enable Root Port's interrupt in response to error messages */
148 pci_read_config_dword(pdev
, aer_pos
+ PCI_ERR_ROOT_COMMAND
, ®32
);
149 reg32
|= ROOT_PORT_INTR_ON_MESG_MASK
;
150 pci_write_config_dword(pdev
, aer_pos
+ PCI_ERR_ROOT_COMMAND
, reg32
);
154 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
155 * @rpc: pointer to a Root Port data structure
157 * Invoked when PCIe bus unloads AER service driver.
159 static void aer_disable_rootport(struct aer_rpc
*rpc
)
161 struct pci_dev
*pdev
= rpc
->rpd
->port
;
166 * Disable error reporting for the root port device and downstream port
169 set_downstream_devices_error_reporting(pdev
, false);
171 pos
= pci_find_ext_capability(pdev
, PCI_EXT_CAP_ID_ERR
);
172 /* Disable Root's interrupt in response to error messages */
173 pci_read_config_dword(pdev
, pos
+ PCI_ERR_ROOT_COMMAND
, ®32
);
174 reg32
&= ~ROOT_PORT_INTR_ON_MESG_MASK
;
175 pci_write_config_dword(pdev
, pos
+ PCI_ERR_ROOT_COMMAND
, reg32
);
177 /* Clear Root's error status reg */
178 pci_read_config_dword(pdev
, pos
+ PCI_ERR_ROOT_STATUS
, ®32
);
179 pci_write_config_dword(pdev
, pos
+ PCI_ERR_ROOT_STATUS
, reg32
);
183 * aer_irq - Root Port's ISR
184 * @irq: IRQ assigned to Root Port
185 * @context: pointer to Root Port data structure
187 * Invoked when Root Port detects AER messages.
189 irqreturn_t
aer_irq(int irq
, void *context
)
191 unsigned int status
, id
;
192 struct pcie_device
*pdev
= (struct pcie_device
*)context
;
193 struct aer_rpc
*rpc
= get_service_data(pdev
);
198 pos
= pci_find_ext_capability(pdev
->port
, PCI_EXT_CAP_ID_ERR
);
200 * Must lock access to Root Error Status Reg, Root Error ID Reg,
201 * and Root error producer/consumer index
203 spin_lock_irqsave(&rpc
->e_lock
, flags
);
205 /* Read error status */
206 pci_read_config_dword(pdev
->port
, pos
+ PCI_ERR_ROOT_STATUS
, &status
);
207 if (!(status
& (PCI_ERR_ROOT_UNCOR_RCV
|PCI_ERR_ROOT_COR_RCV
))) {
208 spin_unlock_irqrestore(&rpc
->e_lock
, flags
);
212 /* Read error source and clear error status */
213 pci_read_config_dword(pdev
->port
, pos
+ PCI_ERR_ROOT_ERR_SRC
, &id
);
214 pci_write_config_dword(pdev
->port
, pos
+ PCI_ERR_ROOT_STATUS
, status
);
216 /* Store error source for later DPC handler */
217 next_prod_idx
= rpc
->prod_idx
+ 1;
218 if (next_prod_idx
== AER_ERROR_SOURCES_MAX
)
220 if (next_prod_idx
== rpc
->cons_idx
) {
222 * Error Storm Condition - possibly the same error occurred.
225 spin_unlock_irqrestore(&rpc
->e_lock
, flags
);
228 rpc
->e_sources
[rpc
->prod_idx
].status
= status
;
229 rpc
->e_sources
[rpc
->prod_idx
].id
= id
;
230 rpc
->prod_idx
= next_prod_idx
;
231 spin_unlock_irqrestore(&rpc
->e_lock
, flags
);
233 /* Invoke DPC handler */
234 schedule_work(&rpc
->dpc_handler
);
238 EXPORT_SYMBOL_GPL(aer_irq
);
241 * aer_alloc_rpc - allocate Root Port data structure
242 * @dev: pointer to the pcie_dev data structure
244 * Invoked when Root Port's AER service is loaded.
246 static struct aer_rpc
*aer_alloc_rpc(struct pcie_device
*dev
)
250 rpc
= kzalloc(sizeof(struct aer_rpc
), GFP_KERNEL
);
254 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
255 spin_lock_init(&rpc
->e_lock
);
258 INIT_WORK(&rpc
->dpc_handler
, aer_isr
);
259 mutex_init(&rpc
->rpc_mutex
);
260 init_waitqueue_head(&rpc
->wait_release
);
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 wait_event(rpc
->wait_release
, rpc
->prod_idx
== rpc
->cons_idx
);
285 aer_disable_rootport(rpc
);
287 set_service_data(dev
, NULL
);
292 * aer_probe - initialize resources
293 * @dev: pointer to the pcie_dev data structure
294 * @id: pointer to the service id data structure
296 * Invoked when PCI Express bus loads AER service driver.
298 static int __devinit
aer_probe(struct pcie_device
*dev
)
302 struct device
*device
= &dev
->device
;
305 status
= aer_init(dev
);
309 /* Alloc rpc data structure */
310 rpc
= aer_alloc_rpc(dev
);
312 dev_printk(KERN_DEBUG
, device
, "alloc rpc failed\n");
317 /* Request IRQ ISR */
318 status
= request_irq(dev
->irq
, aer_irq
, IRQF_SHARED
, "aerdrv", dev
);
320 dev_printk(KERN_DEBUG
, device
, "request IRQ failed\n");
327 aer_enable_rootport(rpc
);
333 * aer_root_reset - reset link on Root Port
334 * @dev: pointer to Root Port's pci_dev data structure
336 * Invoked by Port Bus driver when performing link reset at Root Port.
338 static pci_ers_result_t
aer_root_reset(struct pci_dev
*dev
)
343 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
);
345 /* Disable Root's interrupt in response to error messages */
346 pci_read_config_dword(dev
, pos
+ PCI_ERR_ROOT_COMMAND
, ®32
);
347 reg32
&= ~ROOT_PORT_INTR_ON_MESG_MASK
;
348 pci_write_config_dword(dev
, pos
+ PCI_ERR_ROOT_COMMAND
, reg32
);
350 aer_do_secondary_bus_reset(dev
);
351 dev_printk(KERN_DEBUG
, &dev
->dev
, "Root Port link has been reset\n");
353 /* Clear Root Error Status */
354 pci_read_config_dword(dev
, pos
+ PCI_ERR_ROOT_STATUS
, ®32
);
355 pci_write_config_dword(dev
, pos
+ PCI_ERR_ROOT_STATUS
, reg32
);
357 /* Enable Root Port's interrupt in response to error messages */
358 pci_read_config_dword(dev
, pos
+ PCI_ERR_ROOT_COMMAND
, ®32
);
359 reg32
|= ROOT_PORT_INTR_ON_MESG_MASK
;
360 pci_write_config_dword(dev
, pos
+ PCI_ERR_ROOT_COMMAND
, reg32
);
362 return PCI_ERS_RESULT_RECOVERED
;
366 * aer_error_detected - update severity status
367 * @dev: pointer to Root Port's pci_dev data structure
368 * @error: error severity being notified by port bus
370 * Invoked by Port Bus driver during error recovery.
372 static pci_ers_result_t
aer_error_detected(struct pci_dev
*dev
,
373 enum pci_channel_state error
)
375 /* Root Port has no impact. Always recovers. */
376 return PCI_ERS_RESULT_CAN_RECOVER
;
380 * aer_error_resume - clean up corresponding error status bits
381 * @dev: pointer to Root Port's pci_dev data structure
383 * Invoked by Port Bus driver during nonfatal recovery.
385 static void aer_error_resume(struct pci_dev
*dev
)
391 /* Clean up Root device status */
392 pos
= pci_pcie_cap(dev
);
393 pci_read_config_word(dev
, pos
+ PCI_EXP_DEVSTA
, ®16
);
394 pci_write_config_word(dev
, pos
+ PCI_EXP_DEVSTA
, reg16
);
396 /* Clean AER Root Error Status */
397 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_ERR
);
398 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_STATUS
, &status
);
399 pci_read_config_dword(dev
, pos
+ PCI_ERR_UNCOR_SEVER
, &mask
);
400 if (dev
->error_state
== pci_channel_io_normal
)
401 status
&= ~mask
; /* Clear corresponding nonfatal bits */
403 status
&= mask
; /* Clear corresponding fatal bits */
404 pci_write_config_dword(dev
, pos
+ PCI_ERR_UNCOR_STATUS
, status
);
408 * aer_service_init - register AER root service driver
410 * Invoked when AER root service driver is loaded.
412 static int __init
aer_service_init(void)
414 if (pcie_aer_disable
)
416 if (!pci_msi_enabled())
418 return pcie_port_service_register(&aerdriver
);
422 * aer_service_exit - unregister AER root service driver
424 * Invoked when AER root service driver is unloaded.
426 static void __exit
aer_service_exit(void)
428 pcie_port_service_unregister(&aerdriver
);
431 module_init(aer_service_init
);
432 module_exit(aer_service_exit
);