blob: e11c03194063d1f088e3bfc44a563516b1743c50 [file] [log] [blame]
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001/*
2 * drivers/pci/pcie/aer/aerdrv.c
3 *
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
6 * for more details.
7 *
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.
11 *
12 * Copyright (C) 2006 Intel Corp.
13 * Tom Long Nguyen (tom.l.nguyen@intel.com)
14 * Zhang Yanmin (yanmin.zhang@intel.com)
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/pci.h>
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/pm.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/pcieport_if.h>
27
28#include "aerdrv.h"
Alex Chiang5d9526d2008-06-04 11:39:07 -060029#include "../../pci.h"
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080030
31/*
32 * Version Information
33 */
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"
37MODULE_AUTHOR(DRIVER_AUTHOR);
38MODULE_DESCRIPTION(DRIVER_DESC);
39MODULE_LICENSE("GPL");
40
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +010041static int __devinit aer_probe (struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080042static void aer_remove(struct pcie_device *dev);
43static int aer_suspend(struct pcie_device *dev, pm_message_t state)
44{return 0;}
45static int aer_resume(struct pcie_device *dev) {return 0;}
46static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
47 enum pci_channel_state error);
48static void aer_error_resume(struct pci_dev *dev);
49static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
50
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080051static struct pci_error_handlers aer_error_handlers = {
52 .error_detected = aer_error_detected,
53 .resume = aer_error_resume,
54};
55
Sam Ravnborgc1996c22007-02-27 10:22:00 +010056static struct pcie_port_service_driver aerdriver = {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080057 .name = "aer",
Rafael J. Wysocki22106362009-01-13 14:46:46 +010058 .port_type = PCIE_ANY_PORT,
59 .service = PCIE_PORT_SERVICE_AER,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080060
61 .probe = aer_probe,
62 .remove = aer_remove,
63
64 .suspend = aer_suspend,
65 .resume = aer_resume,
66
67 .err_handler = &aer_error_handlers,
68
69 .reset_link = aer_root_reset,
70};
71
Randy Dunlap7f785762007-10-05 13:17:58 -070072static int pcie_aer_disable;
73
74void pci_no_aer(void)
75{
76 pcie_aer_disable = 1; /* has priority over 'forceload' */
77}
78
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080079/**
80 * aer_irq - Root Port's ISR
81 * @irq: IRQ assigned to Root Port
82 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080083 *
84 * Invoked when Root Port detects AER messages.
85 **/
David Howells7d12e782006-10-05 14:55:46 +010086static irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080087{
88 unsigned int status, id;
89 struct pcie_device *pdev = (struct pcie_device *)context;
90 struct aer_rpc *rpc = get_service_data(pdev);
91 int next_prod_idx;
92 unsigned long flags;
93 int pos;
94
Jesse Barnes09276782008-10-18 17:33:19 -070095 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080096 /*
97 * Must lock access to Root Error Status Reg, Root Error ID Reg,
98 * and Root error producer/consumer index
99 */
100 spin_lock_irqsave(&rpc->e_lock, flags);
101
102 /* Read error status */
103 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
104 if (!(status & ROOT_ERR_STATUS_MASKS)) {
105 spin_unlock_irqrestore(&rpc->e_lock, flags);
106 return IRQ_NONE;
107 }
108
109 /* Read error source and clear error status */
110 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id);
111 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
112
113 /* Store error source for later DPC handler */
114 next_prod_idx = rpc->prod_idx + 1;
115 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
116 next_prod_idx = 0;
117 if (next_prod_idx == rpc->cons_idx) {
118 /*
119 * Error Storm Condition - possibly the same error occurred.
120 * Drop the error.
121 */
122 spin_unlock_irqrestore(&rpc->e_lock, flags);
123 return IRQ_HANDLED;
124 }
125 rpc->e_sources[rpc->prod_idx].status = status;
126 rpc->e_sources[rpc->prod_idx].id = id;
127 rpc->prod_idx = next_prod_idx;
128 spin_unlock_irqrestore(&rpc->e_lock, flags);
129
130 /* Invoke DPC handler */
131 schedule_work(&rpc->dpc_handler);
132
133 return IRQ_HANDLED;
134}
135
136/**
137 * aer_alloc_rpc - allocate Root Port data structure
138 * @dev: pointer to the pcie_dev data structure
139 *
140 * Invoked when Root Port's AER service is loaded.
141 **/
142static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
143{
144 struct aer_rpc *rpc;
145
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700146 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800147 GFP_KERNEL)))
148 return NULL;
149
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800150 /*
151 * Initialize Root lock access, e_lock, to Root Error Status Reg,
152 * Root Error ID Reg, and Root error producer/consumer index.
153 */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700154 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800155
156 rpc->rpd = dev;
David Howells65f27f32006-11-22 14:55:48 +0000157 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800158 rpc->prod_idx = rpc->cons_idx = 0;
159 mutex_init(&rpc->rpc_mutex);
160 init_waitqueue_head(&rpc->wait_release);
161
162 /* Use PCIE bus function to store rpc into PCIE device */
163 set_service_data(dev, rpc);
164
165 return rpc;
166}
167
168/**
169 * aer_remove - clean up resources
170 * @dev: pointer to the pcie_dev data structure
171 *
172 * Invoked when PCI Express bus unloads or AER probe fails.
173 **/
174static void aer_remove(struct pcie_device *dev)
175{
176 struct aer_rpc *rpc = get_service_data(dev);
177
178 if (rpc) {
179 /* If register interrupt service, it must be free. */
180 if (rpc->isr)
181 free_irq(dev->irq, dev);
182
183 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
184
185 aer_delete_rootport(rpc);
186 set_service_data(dev, NULL);
187 }
188}
189
190/**
191 * aer_probe - initialize resources
192 * @dev: pointer to the pcie_dev data structure
193 * @id: pointer to the service id data structure
194 *
195 * Invoked when PCI Express bus loads AER service driver.
196 **/
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +0100197static int __devinit aer_probe (struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800198{
199 int status;
200 struct aer_rpc *rpc;
201 struct device *device = &dev->device;
202
203 /* Init */
204 if ((status = aer_init(dev)))
205 return status;
206
207 /* Alloc rpc data structure */
208 if (!(rpc = aer_alloc_rpc(dev))) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600209 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800210 aer_remove(dev);
211 return -ENOMEM;
212 }
213
214 /* Request IRQ ISR */
Thomas Gleixner38515e92007-02-14 00:33:16 -0800215 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800216 dev))) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600217 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800218 aer_remove(dev);
219 return status;
220 }
221
222 rpc->isr = 1;
223
224 aer_enable_rootport(rpc);
225
226 return status;
227}
228
229/**
230 * aer_root_reset - reset link on Root Port
231 * @dev: pointer to Root Port's pci_dev data structure
232 *
233 * Invoked by Port Bus driver when performing link reset at Root Port.
234 **/
235static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
236{
237 u16 p2p_ctrl;
238 u32 status;
239 int pos;
240
Jesse Barnes09276782008-10-18 17:33:19 -0700241 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800242
243 /* Disable Root's interrupt in response to error messages */
244 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
245
246 /* Assert Secondary Bus Reset */
247 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
248 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
249 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
250
251 /* De-assert Secondary Bus Reset */
252 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
253 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
254
255 /*
256 * System software must wait for at least 100ms from the end
257 * of a reset of one or more device before it is permitted
258 * to issue Configuration Requests to those devices.
259 */
260 msleep(200);
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600261 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800262
263 /* Enable Root Port's interrupt in response to error messages */
264 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
265 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
266 pci_write_config_dword(dev,
267 pos + PCI_ERR_ROOT_COMMAND,
268 ROOT_PORT_INTR_ON_MESG_MASK);
269
270 return PCI_ERS_RESULT_RECOVERED;
271}
272
273/**
274 * aer_error_detected - update severity status
275 * @dev: pointer to Root Port's pci_dev data structure
276 * @error: error severity being notified by port bus
277 *
278 * Invoked by Port Bus driver during error recovery.
279 **/
280static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
281 enum pci_channel_state error)
282{
283 /* Root Port has no impact. Always recovers. */
284 return PCI_ERS_RESULT_CAN_RECOVER;
285}
286
287/**
288 * aer_error_resume - clean up corresponding error status bits
289 * @dev: pointer to Root Port's pci_dev data structure
290 *
291 * Invoked by Port Bus driver during nonfatal recovery.
292 **/
293static void aer_error_resume(struct pci_dev *dev)
294{
295 int pos;
296 u32 status, mask;
297 u16 reg16;
298
299 /* Clean up Root device status */
300 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
301 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
302 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
303
304 /* Clean AER Root Error Status */
Jesse Barnes09276782008-10-18 17:33:19 -0700305 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800306 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
307 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
308 if (dev->error_state == pci_channel_io_normal)
309 status &= ~mask; /* Clear corresponding nonfatal bits */
310 else
311 status &= mask; /* Clear corresponding fatal bits */
312 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
313}
314
315/**
316 * aer_service_init - register AER root service driver
317 *
318 * Invoked when AER root service driver is loaded.
319 **/
320static int __init aer_service_init(void)
321{
Randy Dunlap7f785762007-10-05 13:17:58 -0700322 if (pcie_aer_disable)
323 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100324 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800325}
326
327/**
328 * aer_service_exit - unregister AER root service driver
329 *
330 * Invoked when AER root service driver is unloaded.
331 **/
332static void __exit aer_service_exit(void)
333{
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100334 pcie_port_service_unregister(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800335}
336
337module_init(aer_service_init);
338module_exit(aer_service_exit);