blob: f7c62453ae002a3bf1bd15941184b2416532c506 [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>
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +010020#include <linux/pci-acpi.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040021#include <linux/sched.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080022#include <linux/kernel.h>
23#include <linux/errno.h>
24#include <linux/pm.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
28#include <linux/pcieport_if.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080030
31#include "aerdrv.h"
Alex Chiang5d9526d2008-06-04 11:39:07 -060032#include "../../pci.h"
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080033
34/*
35 * Version Information
36 */
37#define DRIVER_VERSION "v1.0"
38#define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
39#define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
40MODULE_AUTHOR(DRIVER_AUTHOR);
41MODULE_DESCRIPTION(DRIVER_DESC);
42MODULE_LICENSE("GPL");
43
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090044static int __devinit aer_probe(struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080045static void aer_remove(struct pcie_device *dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080046static 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,
Hidetoshi Setoc9a91882009-09-07 17:07:29 +090053 .resume = aer_error_resume,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080054};
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",
Kenji Kaneshige694f88e2009-11-25 21:06:15 +090058 .port_type = PCI_EXP_TYPE_ROOT_PORT,
Rafael J. Wysocki22106362009-01-13 14:46:46 +010059 .service = PCIE_PORT_SERVICE_AER,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080060
61 .probe = aer_probe,
62 .remove = aer_remove,
63
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080064 .err_handler = &aer_error_handlers,
65
66 .reset_link = aer_root_reset,
67};
68
Randy Dunlap7f785762007-10-05 13:17:58 -070069static int pcie_aer_disable;
70
71void pci_no_aer(void)
72{
73 pcie_aer_disable = 1; /* has priority over 'forceload' */
74}
75
Rafael J. Wysockif1a7bfa2010-08-21 01:50:52 +020076bool pci_aer_available(void)
77{
78 return !pcie_aer_disable && pci_msi_enabled();
79}
80
Hidetoshi Seto843f4692010-04-15 13:10:53 +090081static int set_device_error_reporting(struct pci_dev *dev, void *data)
82{
83 bool enable = *((bool *)data);
Yijing Wang62f87c02012-07-24 17:20:03 +080084 int type = pci_pcie_type(dev);
Hidetoshi Seto843f4692010-04-15 13:10:53 +090085
Yijing Wang62f87c02012-07-24 17:20:03 +080086 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
87 (type == PCI_EXP_TYPE_UPSTREAM) ||
88 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
Hidetoshi Seto843f4692010-04-15 13:10:53 +090089 if (enable)
90 pci_enable_pcie_error_reporting(dev);
91 else
92 pci_disable_pcie_error_reporting(dev);
93 }
94
95 if (enable)
96 pcie_set_ecrc_checking(dev);
97
98 return 0;
99}
100
101/**
102 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
103 * @dev: pointer to root port's pci_dev data structure
104 * @enable: true = enable error reporting, false = disable error reporting.
105 */
106static void set_downstream_devices_error_reporting(struct pci_dev *dev,
107 bool enable)
108{
109 set_device_error_reporting(dev, &enable);
110
111 if (!dev->subordinate)
112 return;
113 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
114}
115
116/**
117 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
118 * @rpc: pointer to a Root Port data structure
119 *
120 * Invoked when PCIe bus loads AER service driver.
121 */
122static void aer_enable_rootport(struct aer_rpc *rpc)
123{
124 struct pci_dev *pdev = rpc->rpd->port;
125 int pos, aer_pos;
126 u16 reg16;
127 u32 reg32;
128
129 pos = pci_pcie_cap(pdev);
130 /* Clear PCIe Capability's Device Status */
131 pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, &reg16);
132 pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
133
134 /* Disable system error generation in response to error messages */
135 pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, &reg16);
136 reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
137 pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
138
139 aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
140 /* Clear error status */
141 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
142 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
143 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
144 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
145 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
146 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
147
148 /*
149 * Enable error reporting for the root port device and downstream port
150 * devices.
151 */
152 set_downstream_devices_error_reporting(pdev, true);
153
154 /* Enable Root Port's interrupt in response to error messages */
155 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
156 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
157 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
158}
159
160/**
161 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
162 * @rpc: pointer to a Root Port data structure
163 *
164 * Invoked when PCIe bus unloads AER service driver.
165 */
166static void aer_disable_rootport(struct aer_rpc *rpc)
167{
168 struct pci_dev *pdev = rpc->rpd->port;
169 u32 reg32;
170 int pos;
171
172 /*
173 * Disable error reporting for the root port device and downstream port
174 * devices.
175 */
176 set_downstream_devices_error_reporting(pdev, false);
177
178 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
179 /* Disable Root's interrupt in response to error messages */
180 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
181 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
182 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
183
184 /* Clear Root's error status reg */
185 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
186 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
187}
188
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800189/**
190 * aer_irq - Root Port's ISR
191 * @irq: IRQ assigned to Root Port
192 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800193 *
194 * Invoked when Root Port detects AER messages.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900195 */
Huang Ying634deb02009-04-24 10:45:23 +0800196irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800197{
198 unsigned int status, id;
199 struct pcie_device *pdev = (struct pcie_device *)context;
200 struct aer_rpc *rpc = get_service_data(pdev);
201 int next_prod_idx;
202 unsigned long flags;
203 int pos;
204
Jesse Barnes09276782008-10-18 17:33:19 -0700205 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800206 /*
207 * Must lock access to Root Error Status Reg, Root Error ID Reg,
208 * and Root error producer/consumer index
209 */
210 spin_lock_irqsave(&rpc->e_lock, flags);
211
212 /* Read error status */
213 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
Hidetoshi Setoe167bfc2010-04-15 13:18:26 +0900214 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800215 spin_unlock_irqrestore(&rpc->e_lock, flags);
216 return IRQ_NONE;
217 }
218
219 /* Read error source and clear error status */
Hidetoshi Setof647a442010-04-15 13:17:33 +0900220 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800221 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
222
223 /* Store error source for later DPC handler */
224 next_prod_idx = rpc->prod_idx + 1;
225 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
226 next_prod_idx = 0;
227 if (next_prod_idx == rpc->cons_idx) {
228 /*
229 * Error Storm Condition - possibly the same error occurred.
230 * Drop the error.
231 */
232 spin_unlock_irqrestore(&rpc->e_lock, flags);
233 return IRQ_HANDLED;
234 }
235 rpc->e_sources[rpc->prod_idx].status = status;
236 rpc->e_sources[rpc->prod_idx].id = id;
237 rpc->prod_idx = next_prod_idx;
238 spin_unlock_irqrestore(&rpc->e_lock, flags);
239
240 /* Invoke DPC handler */
241 schedule_work(&rpc->dpc_handler);
242
243 return IRQ_HANDLED;
244}
Huang Ying634deb02009-04-24 10:45:23 +0800245EXPORT_SYMBOL_GPL(aer_irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800246
247/**
248 * aer_alloc_rpc - allocate Root Port data structure
249 * @dev: pointer to the pcie_dev data structure
250 *
251 * Invoked when Root Port's AER service is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900252 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900253static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800254{
255 struct aer_rpc *rpc;
256
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900257 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
258 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800259 return NULL;
260
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900261 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700262 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800263
264 rpc->rpd = dev;
David Howells65f27f32006-11-22 14:55:48 +0000265 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800266 mutex_init(&rpc->rpc_mutex);
267 init_waitqueue_head(&rpc->wait_release);
268
Stefan Assmann45e829e2009-12-03 06:49:24 -0500269 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800270 set_service_data(dev, rpc);
271
272 return rpc;
273}
274
275/**
276 * aer_remove - clean up resources
277 * @dev: pointer to the pcie_dev data structure
278 *
279 * Invoked when PCI Express bus unloads or AER probe fails.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900280 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800281static void aer_remove(struct pcie_device *dev)
282{
283 struct aer_rpc *rpc = get_service_data(dev);
284
285 if (rpc) {
286 /* If register interrupt service, it must be free. */
287 if (rpc->isr)
288 free_irq(dev->irq, dev);
289
290 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
291
Hidetoshi Seto460d2982010-04-15 13:10:03 +0900292 aer_disable_rootport(rpc);
293 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800294 set_service_data(dev, NULL);
295 }
296}
297
298/**
299 * aer_probe - initialize resources
300 * @dev: pointer to the pcie_dev data structure
301 * @id: pointer to the service id data structure
302 *
303 * Invoked when PCI Express bus loads AER service driver.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900304 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900305static int __devinit aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800306{
307 int status;
308 struct aer_rpc *rpc;
309 struct device *device = &dev->device;
310
311 /* Init */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900312 status = aer_init(dev);
313 if (status)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800314 return status;
315
316 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900317 rpc = aer_alloc_rpc(dev);
318 if (!rpc) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600319 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800320 aer_remove(dev);
321 return -ENOMEM;
322 }
323
324 /* Request IRQ ISR */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +0900325 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
326 if (status) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600327 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800328 aer_remove(dev);
329 return status;
330 }
331
332 rpc->isr = 1;
333
334 aer_enable_rootport(rpc);
335
336 return status;
337}
338
339/**
340 * aer_root_reset - reset link on Root Port
341 * @dev: pointer to Root Port's pci_dev data structure
342 *
343 * Invoked by Port Bus driver when performing link reset at Root Port.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900344 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800345static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
346{
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900347 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800348 int pos;
349
Jesse Barnes09276782008-10-18 17:33:19 -0700350 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800351
352 /* Disable Root's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900353 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
354 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
355 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800356
Hidetoshi Seto89713422010-04-15 13:21:27 +0900357 aer_do_secondary_bus_reset(dev);
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600358 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800359
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900360 /* Clear Root Error Status */
361 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
362 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
363
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800364 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +0900365 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
366 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
367 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800368
369 return PCI_ERS_RESULT_RECOVERED;
370}
371
372/**
373 * aer_error_detected - update severity status
374 * @dev: pointer to Root Port's pci_dev data structure
375 * @error: error severity being notified by port bus
376 *
377 * Invoked by Port Bus driver during error recovery.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900378 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800379static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
380 enum pci_channel_state error)
381{
382 /* Root Port has no impact. Always recovers. */
383 return PCI_ERS_RESULT_CAN_RECOVER;
384}
385
386/**
387 * aer_error_resume - clean up corresponding error status bits
388 * @dev: pointer to Root Port's pci_dev data structure
389 *
390 * Invoked by Port Bus driver during nonfatal recovery.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900391 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800392static void aer_error_resume(struct pci_dev *dev)
393{
394 int pos;
395 u32 status, mask;
396 u16 reg16;
397
398 /* Clean up Root device status */
Kenji Kaneshige39a53062009-11-11 14:31:38 +0900399 pos = pci_pcie_cap(dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800400 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
401 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
402
403 /* Clean AER Root Error Status */
Jesse Barnes09276782008-10-18 17:33:19 -0700404 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800405 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
406 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
407 if (dev->error_state == pci_channel_io_normal)
408 status &= ~mask; /* Clear corresponding nonfatal bits */
409 else
410 status &= mask; /* Clear corresponding fatal bits */
411 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
412}
413
414/**
415 * aer_service_init - register AER root service driver
416 *
417 * Invoked when AER root service driver is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900418 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800419static int __init aer_service_init(void)
420{
Rafael J. Wysockib22c3d82010-09-20 18:50:00 +0200421 if (!pci_aer_available() || aer_acpi_firmware_first())
Andi Kleen3e77a3f2009-09-16 22:40:22 +0200422 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100423 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800424}
425
426/**
427 * aer_service_exit - unregister AER root service driver
428 *
429 * Invoked when AER root service driver is unloaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +0900430 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800431static void __exit aer_service_exit(void)
432{
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100433 pcie_port_service_unregister(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800434}
435
436module_init(aer_service_init);
437module_exit(aer_service_exit);