blob: 14081f807e503f6aa2e7e339fd53a559ae1a4d01 [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040020#include <linux/sched.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080021#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/pm.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/pcieport_if.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080029
30#include "aerdrv.h"
Alex Chiang5d9526d2008-06-04 11:39:07 -060031#include "../../pci.h"
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080032
33/*
34 * Version Information
35 */
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"
39MODULE_AUTHOR(DRIVER_AUTHOR);
40MODULE_DESCRIPTION(DRIVER_DESC);
41MODULE_LICENSE("GPL");
42
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090043static int __devinit aer_probe(struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080044static void aer_remove(struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080045static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
46 enum pci_channel_state error);
47static void aer_error_resume(struct pci_dev *dev);
48static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
49
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080050static struct pci_error_handlers aer_error_handlers = {
51 .error_detected = aer_error_detected,
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090052 .resume = aer_error_resume,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080053};
54
Sam Ravnborgc1996c22007-02-27 10:22:00 +010055static struct pcie_port_service_driver aerdriver = {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080056 .name = "aer",
Kenji Kaneshige694f88e2009-11-25 21:06:15 +090057 .port_type = PCI_EXP_TYPE_ROOT_PORT,
Rafael J. Wysocki22106362009-01-13 14:46:46 +010058 .service = PCIE_PORT_SERVICE_AER,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080059
60 .probe = aer_probe,
61 .remove = aer_remove,
62
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080063 .err_handler = &aer_error_handlers,
64
65 .reset_link = aer_root_reset,
66};
67
Randy Dunlap7f785762007-10-05 13:17:58 -070068static int pcie_aer_disable;
69
70void pci_no_aer(void)
71{
72 pcie_aer_disable = 1; /* has priority over 'forceload' */
73}
74
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080075/**
76 * aer_irq - Root Port's ISR
77 * @irq: IRQ assigned to Root Port
78 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080079 *
80 * Invoked when Root Port detects AER messages.
81 **/
Huang Ying634deb02009-04-24 10:45:23 +080082irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080083{
84 unsigned int status, id;
85 struct pcie_device *pdev = (struct pcie_device *)context;
86 struct aer_rpc *rpc = get_service_data(pdev);
87 int next_prod_idx;
88 unsigned long flags;
89 int pos;
90
Jesse Barnes09276782008-10-18 17:33:19 -070091 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080092 /*
93 * Must lock access to Root Error Status Reg, Root Error ID Reg,
94 * and Root error producer/consumer index
95 */
96 spin_lock_irqsave(&rpc->e_lock, flags);
97
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);
102 return IRQ_NONE;
103 }
104
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);
108
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)
112 next_prod_idx = 0;
113 if (next_prod_idx == rpc->cons_idx) {
114 /*
115 * Error Storm Condition - possibly the same error occurred.
116 * Drop the error.
117 */
118 spin_unlock_irqrestore(&rpc->e_lock, flags);
119 return IRQ_HANDLED;
120 }
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);
125
126 /* Invoke DPC handler */
127 schedule_work(&rpc->dpc_handler);
128
129 return IRQ_HANDLED;
130}
Huang Ying634deb02009-04-24 10:45:23 +0800131EXPORT_SYMBOL_GPL(aer_irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800132
133/**
134 * aer_alloc_rpc - allocate Root Port data structure
135 * @dev: pointer to the pcie_dev data structure
136 *
137 * Invoked when Root Port's AER service is loaded.
138 **/
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900139static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800140{
141 struct aer_rpc *rpc;
142
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900143 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
144 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800145 return NULL;
146
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800147 /*
148 * Initialize Root lock access, e_lock, to Root Error Status Reg,
149 * Root Error ID Reg, and Root error producer/consumer index.
150 */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700151 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800152
153 rpc->rpd = dev;
David Howells65f27f32006-11-22 14:55:48 +0000154 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800155 rpc->prod_idx = rpc->cons_idx = 0;
156 mutex_init(&rpc->rpc_mutex);
157 init_waitqueue_head(&rpc->wait_release);
158
Stefan Assmann45e829e2009-12-03 06:49:24 -0500159 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800160 set_service_data(dev, rpc);
161
162 return rpc;
163}
164
165/**
166 * aer_remove - clean up resources
167 * @dev: pointer to the pcie_dev data structure
168 *
169 * Invoked when PCI Express bus unloads or AER probe fails.
170 **/
171static void aer_remove(struct pcie_device *dev)
172{
173 struct aer_rpc *rpc = get_service_data(dev);
174
175 if (rpc) {
176 /* If register interrupt service, it must be free. */
177 if (rpc->isr)
178 free_irq(dev->irq, dev);
179
180 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
181
Hidetoshi Seto460d2982010-04-15 13:10:03 +0900182 aer_disable_rootport(rpc);
183 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800184 set_service_data(dev, NULL);
185 }
186}
187
188/**
189 * aer_probe - initialize resources
190 * @dev: pointer to the pcie_dev data structure
191 * @id: pointer to the service id data structure
192 *
193 * Invoked when PCI Express bus loads AER service driver.
194 **/
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900195static int __devinit aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800196{
197 int status;
198 struct aer_rpc *rpc;
199 struct device *device = &dev->device;
200
201 /* Init */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900202 status = aer_init(dev);
203 if (status)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800204 return status;
205
206 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900207 rpc = aer_alloc_rpc(dev);
208 if (!rpc) {
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 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900215 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
216 if (status) {
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;
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900238 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800239 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 */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900244 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
245 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
246 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800247
248 /* Assert Secondary Bus Reset */
249 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
Alexander Duyck4352aa52010-03-25 13:03:30 -0700250 p2p_ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800251 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
252
Alexander Duyck4352aa52010-03-25 13:03:30 -0700253 /*
254 * we should send hot reset message for 2ms to allow it time to
255 * propogate to all downstream ports
256 */
257 msleep(2);
258
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800259 /* De-assert Secondary Bus Reset */
Alexander Duyck4352aa52010-03-25 13:03:30 -0700260 p2p_ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800261 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
262
263 /*
264 * System software must wait for at least 100ms from the end
265 * of a reset of one or more device before it is permitted
266 * to issue Configuration Requests to those devices.
267 */
268 msleep(200);
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600269 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800270
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900271 /* Clear Root Error Status */
272 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
273 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
274
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800275 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900276 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
277 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
278 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800279
280 return PCI_ERS_RESULT_RECOVERED;
281}
282
283/**
284 * aer_error_detected - update severity status
285 * @dev: pointer to Root Port's pci_dev data structure
286 * @error: error severity being notified by port bus
287 *
288 * Invoked by Port Bus driver during error recovery.
289 **/
290static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
291 enum pci_channel_state error)
292{
293 /* Root Port has no impact. Always recovers. */
294 return PCI_ERS_RESULT_CAN_RECOVER;
295}
296
297/**
298 * aer_error_resume - clean up corresponding error status bits
299 * @dev: pointer to Root Port's pci_dev data structure
300 *
301 * Invoked by Port Bus driver during nonfatal recovery.
302 **/
303static void aer_error_resume(struct pci_dev *dev)
304{
305 int pos;
306 u32 status, mask;
307 u16 reg16;
308
309 /* Clean up Root device status */
Kenji Kaneshige39a53062009-11-11 14:31:38 +0900310 pos = pci_pcie_cap(dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800311 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
312 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
313
314 /* Clean AER Root Error Status */
Jesse Barnes09276782008-10-18 17:33:19 -0700315 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800316 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
317 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
318 if (dev->error_state == pci_channel_io_normal)
319 status &= ~mask; /* Clear corresponding nonfatal bits */
320 else
321 status &= mask; /* Clear corresponding fatal bits */
322 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
323}
324
325/**
326 * aer_service_init - register AER root service driver
327 *
328 * Invoked when AER root service driver is loaded.
329 **/
330static int __init aer_service_init(void)
331{
Randy Dunlap7f785762007-10-05 13:17:58 -0700332 if (pcie_aer_disable)
333 return -ENXIO;
Andi Kleen3e77a3f2009-09-16 22:40:22 +0200334 if (!pci_msi_enabled())
335 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100336 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800337}
338
339/**
340 * aer_service_exit - unregister AER root service driver
341 *
342 * Invoked when AER root service driver is unloaded.
343 **/
344static void __exit aer_service_exit(void)
345{
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100346 pcie_port_service_unregister(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800347}
348
349module_init(aer_service_init);
350module_exit(aer_service_exit);