blob: 32ade5af927e21c11e008be87ae7380c64a1932a [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);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080043static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
44 enum pci_channel_state error);
45static void aer_error_resume(struct pci_dev *dev);
46static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
47
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080048static struct pci_error_handlers aer_error_handlers = {
49 .error_detected = aer_error_detected,
50 .resume = aer_error_resume,
51};
52
Sam Ravnborgc1996c22007-02-27 10:22:00 +010053static struct pcie_port_service_driver aerdriver = {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080054 .name = "aer",
Rafael J. Wysocki22106362009-01-13 14:46:46 +010055 .port_type = PCIE_ANY_PORT,
56 .service = PCIE_PORT_SERVICE_AER,
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080057
58 .probe = aer_probe,
59 .remove = aer_remove,
60
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080061 .err_handler = &aer_error_handlers,
62
63 .reset_link = aer_root_reset,
64};
65
Randy Dunlap7f785762007-10-05 13:17:58 -070066static int pcie_aer_disable;
67
68void pci_no_aer(void)
69{
70 pcie_aer_disable = 1; /* has priority over 'forceload' */
71}
72
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080073/**
74 * aer_irq - Root Port's ISR
75 * @irq: IRQ assigned to Root Port
76 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080077 *
78 * Invoked when Root Port detects AER messages.
79 **/
David Howells7d12e782006-10-05 14:55:46 +010080static irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080081{
82 unsigned int status, id;
83 struct pcie_device *pdev = (struct pcie_device *)context;
84 struct aer_rpc *rpc = get_service_data(pdev);
85 int next_prod_idx;
86 unsigned long flags;
87 int pos;
88
Jesse Barnes09276782008-10-18 17:33:19 -070089 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080090 /*
91 * Must lock access to Root Error Status Reg, Root Error ID Reg,
92 * and Root error producer/consumer index
93 */
94 spin_lock_irqsave(&rpc->e_lock, flags);
95
96 /* Read error status */
97 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
98 if (!(status & ROOT_ERR_STATUS_MASKS)) {
99 spin_unlock_irqrestore(&rpc->e_lock, flags);
100 return IRQ_NONE;
101 }
102
103 /* Read error source and clear error status */
104 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id);
105 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
106
107 /* Store error source for later DPC handler */
108 next_prod_idx = rpc->prod_idx + 1;
109 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
110 next_prod_idx = 0;
111 if (next_prod_idx == rpc->cons_idx) {
112 /*
113 * Error Storm Condition - possibly the same error occurred.
114 * Drop the error.
115 */
116 spin_unlock_irqrestore(&rpc->e_lock, flags);
117 return IRQ_HANDLED;
118 }
119 rpc->e_sources[rpc->prod_idx].status = status;
120 rpc->e_sources[rpc->prod_idx].id = id;
121 rpc->prod_idx = next_prod_idx;
122 spin_unlock_irqrestore(&rpc->e_lock, flags);
123
124 /* Invoke DPC handler */
125 schedule_work(&rpc->dpc_handler);
126
127 return IRQ_HANDLED;
128}
129
130/**
131 * aer_alloc_rpc - allocate Root Port data structure
132 * @dev: pointer to the pcie_dev data structure
133 *
134 * Invoked when Root Port's AER service is loaded.
135 **/
136static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
137{
138 struct aer_rpc *rpc;
139
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700140 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800141 GFP_KERNEL)))
142 return NULL;
143
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800144 /*
145 * Initialize Root lock access, e_lock, to Root Error Status Reg,
146 * Root Error ID Reg, and Root error producer/consumer index.
147 */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700148 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800149
150 rpc->rpd = dev;
David Howells65f27f32006-11-22 14:55:48 +0000151 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800152 rpc->prod_idx = rpc->cons_idx = 0;
153 mutex_init(&rpc->rpc_mutex);
154 init_waitqueue_head(&rpc->wait_release);
155
156 /* Use PCIE bus function to store rpc into PCIE device */
157 set_service_data(dev, rpc);
158
159 return rpc;
160}
161
162/**
163 * aer_remove - clean up resources
164 * @dev: pointer to the pcie_dev data structure
165 *
166 * Invoked when PCI Express bus unloads or AER probe fails.
167 **/
168static void aer_remove(struct pcie_device *dev)
169{
170 struct aer_rpc *rpc = get_service_data(dev);
171
172 if (rpc) {
173 /* If register interrupt service, it must be free. */
174 if (rpc->isr)
175 free_irq(dev->irq, dev);
176
177 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
178
179 aer_delete_rootport(rpc);
180 set_service_data(dev, NULL);
181 }
182}
183
184/**
185 * aer_probe - initialize resources
186 * @dev: pointer to the pcie_dev data structure
187 * @id: pointer to the service id data structure
188 *
189 * Invoked when PCI Express bus loads AER service driver.
190 **/
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +0100191static int __devinit aer_probe (struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800192{
193 int status;
194 struct aer_rpc *rpc;
195 struct device *device = &dev->device;
196
197 /* Init */
198 if ((status = aer_init(dev)))
199 return status;
200
201 /* Alloc rpc data structure */
202 if (!(rpc = aer_alloc_rpc(dev))) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600203 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800204 aer_remove(dev);
205 return -ENOMEM;
206 }
207
208 /* Request IRQ ISR */
Thomas Gleixner38515e92007-02-14 00:33:16 -0800209 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800210 dev))) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600211 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800212 aer_remove(dev);
213 return status;
214 }
215
216 rpc->isr = 1;
217
218 aer_enable_rootport(rpc);
219
220 return status;
221}
222
223/**
224 * aer_root_reset - reset link on Root Port
225 * @dev: pointer to Root Port's pci_dev data structure
226 *
227 * Invoked by Port Bus driver when performing link reset at Root Port.
228 **/
229static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
230{
231 u16 p2p_ctrl;
232 u32 status;
233 int pos;
234
Jesse Barnes09276782008-10-18 17:33:19 -0700235 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800236
237 /* Disable Root's interrupt in response to error messages */
238 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
239
240 /* Assert Secondary Bus Reset */
241 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
242 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
243 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
244
245 /* De-assert Secondary Bus Reset */
246 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
247 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
248
249 /*
250 * System software must wait for at least 100ms from the end
251 * of a reset of one or more device before it is permitted
252 * to issue Configuration Requests to those devices.
253 */
254 msleep(200);
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600255 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800256
257 /* Enable Root Port's interrupt in response to error messages */
258 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
259 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
260 pci_write_config_dword(dev,
261 pos + PCI_ERR_ROOT_COMMAND,
262 ROOT_PORT_INTR_ON_MESG_MASK);
263
264 return PCI_ERS_RESULT_RECOVERED;
265}
266
267/**
268 * aer_error_detected - update severity status
269 * @dev: pointer to Root Port's pci_dev data structure
270 * @error: error severity being notified by port bus
271 *
272 * Invoked by Port Bus driver during error recovery.
273 **/
274static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
275 enum pci_channel_state error)
276{
277 /* Root Port has no impact. Always recovers. */
278 return PCI_ERS_RESULT_CAN_RECOVER;
279}
280
281/**
282 * aer_error_resume - clean up corresponding error status bits
283 * @dev: pointer to Root Port's pci_dev data structure
284 *
285 * Invoked by Port Bus driver during nonfatal recovery.
286 **/
287static void aer_error_resume(struct pci_dev *dev)
288{
289 int pos;
290 u32 status, mask;
291 u16 reg16;
292
293 /* Clean up Root device status */
294 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
295 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
296 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
297
298 /* Clean AER Root Error Status */
Jesse Barnes09276782008-10-18 17:33:19 -0700299 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800300 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
301 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
302 if (dev->error_state == pci_channel_io_normal)
303 status &= ~mask; /* Clear corresponding nonfatal bits */
304 else
305 status &= mask; /* Clear corresponding fatal bits */
306 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
307}
308
309/**
310 * aer_service_init - register AER root service driver
311 *
312 * Invoked when AER root service driver is loaded.
313 **/
314static int __init aer_service_init(void)
315{
Randy Dunlap7f785762007-10-05 13:17:58 -0700316 if (pcie_aer_disable)
317 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100318 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800319}
320
321/**
322 * aer_service_exit - unregister AER root service driver
323 *
324 * Invoked when AER root service driver is unloaded.
325 **/
326static void __exit aer_service_exit(void)
327{
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100328 pcie_port_service_unregister(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800329}
330
331module_init(aer_service_init);
332module_exit(aer_service_exit);