blob: b7a3aa602d40260f4c0ce23d960104234ee794c6 [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
41static int __devinit aer_probe (struct pcie_device *dev,
42 const struct pcie_port_service_id *id );
43static void aer_remove(struct pcie_device *dev);
44static int aer_suspend(struct pcie_device *dev, pm_message_t state)
45{return 0;}
46static int aer_resume(struct pcie_device *dev) {return 0;}
47static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
48 enum pci_channel_state error);
49static void aer_error_resume(struct pci_dev *dev);
50static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
51
52/*
53 * PCI Express bus's AER Root service driver data structure
54 */
55static struct pcie_port_service_id aer_id[] = {
56 {
57 .vendor = PCI_ANY_ID,
58 .device = PCI_ANY_ID,
59 .port_type = PCIE_RC_PORT,
60 .service_type = PCIE_PORT_SERVICE_AER,
61 },
62 { /* end: all zeroes */ }
63};
64
65static struct pci_error_handlers aer_error_handlers = {
66 .error_detected = aer_error_detected,
67 .resume = aer_error_resume,
68};
69
Sam Ravnborgc1996c22007-02-27 10:22:00 +010070static struct pcie_port_service_driver aerdriver = {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080071 .name = "aer",
72 .id_table = &aer_id[0],
73
74 .probe = aer_probe,
75 .remove = aer_remove,
76
77 .suspend = aer_suspend,
78 .resume = aer_resume,
79
80 .err_handler = &aer_error_handlers,
81
82 .reset_link = aer_root_reset,
83};
84
Randy Dunlap7f785762007-10-05 13:17:58 -070085static int pcie_aer_disable;
86
87void pci_no_aer(void)
88{
89 pcie_aer_disable = 1; /* has priority over 'forceload' */
90}
91
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080092/**
93 * aer_irq - Root Port's ISR
94 * @irq: IRQ assigned to Root Port
95 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080096 *
97 * Invoked when Root Port detects AER messages.
98 **/
David Howells7d12e782006-10-05 14:55:46 +010099static irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800100{
101 unsigned int status, id;
102 struct pcie_device *pdev = (struct pcie_device *)context;
103 struct aer_rpc *rpc = get_service_data(pdev);
104 int next_prod_idx;
105 unsigned long flags;
106 int pos;
107
108 pos = pci_find_aer_capability(pdev->port);
109 /*
110 * Must lock access to Root Error Status Reg, Root Error ID Reg,
111 * and Root error producer/consumer index
112 */
113 spin_lock_irqsave(&rpc->e_lock, flags);
114
115 /* Read error status */
116 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
117 if (!(status & ROOT_ERR_STATUS_MASKS)) {
118 spin_unlock_irqrestore(&rpc->e_lock, flags);
119 return IRQ_NONE;
120 }
121
122 /* Read error source and clear error status */
123 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id);
124 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
125
126 /* Store error source for later DPC handler */
127 next_prod_idx = rpc->prod_idx + 1;
128 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
129 next_prod_idx = 0;
130 if (next_prod_idx == rpc->cons_idx) {
131 /*
132 * Error Storm Condition - possibly the same error occurred.
133 * Drop the error.
134 */
135 spin_unlock_irqrestore(&rpc->e_lock, flags);
136 return IRQ_HANDLED;
137 }
138 rpc->e_sources[rpc->prod_idx].status = status;
139 rpc->e_sources[rpc->prod_idx].id = id;
140 rpc->prod_idx = next_prod_idx;
141 spin_unlock_irqrestore(&rpc->e_lock, flags);
142
143 /* Invoke DPC handler */
144 schedule_work(&rpc->dpc_handler);
145
146 return IRQ_HANDLED;
147}
148
149/**
150 * aer_alloc_rpc - allocate Root Port data structure
151 * @dev: pointer to the pcie_dev data structure
152 *
153 * Invoked when Root Port's AER service is loaded.
154 **/
155static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
156{
157 struct aer_rpc *rpc;
158
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700159 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800160 GFP_KERNEL)))
161 return NULL;
162
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800163 /*
164 * Initialize Root lock access, e_lock, to Root Error Status Reg,
165 * Root Error ID Reg, and Root error producer/consumer index.
166 */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700167 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800168
169 rpc->rpd = dev;
David Howells65f27f32006-11-22 14:55:48 +0000170 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800171 rpc->prod_idx = rpc->cons_idx = 0;
172 mutex_init(&rpc->rpc_mutex);
173 init_waitqueue_head(&rpc->wait_release);
174
175 /* Use PCIE bus function to store rpc into PCIE device */
176 set_service_data(dev, rpc);
177
178 return rpc;
179}
180
181/**
182 * aer_remove - clean up resources
183 * @dev: pointer to the pcie_dev data structure
184 *
185 * Invoked when PCI Express bus unloads or AER probe fails.
186 **/
187static void aer_remove(struct pcie_device *dev)
188{
189 struct aer_rpc *rpc = get_service_data(dev);
190
191 if (rpc) {
192 /* If register interrupt service, it must be free. */
193 if (rpc->isr)
194 free_irq(dev->irq, dev);
195
196 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
197
198 aer_delete_rootport(rpc);
199 set_service_data(dev, NULL);
200 }
201}
202
203/**
204 * aer_probe - initialize resources
205 * @dev: pointer to the pcie_dev data structure
206 * @id: pointer to the service id data structure
207 *
208 * Invoked when PCI Express bus loads AER service driver.
209 **/
210static int __devinit aer_probe (struct pcie_device *dev,
211 const struct pcie_port_service_id *id )
212{
213 int status;
214 struct aer_rpc *rpc;
215 struct device *device = &dev->device;
216
217 /* Init */
218 if ((status = aer_init(dev)))
219 return status;
220
221 /* Alloc rpc data structure */
222 if (!(rpc = aer_alloc_rpc(dev))) {
223 printk(KERN_DEBUG "%s: Alloc rpc fails on PCIE device[%s]\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800224 __func__, device->bus_id);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800225 aer_remove(dev);
226 return -ENOMEM;
227 }
228
229 /* Request IRQ ISR */
Thomas Gleixner38515e92007-02-14 00:33:16 -0800230 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800231 dev))) {
232 printk(KERN_DEBUG "%s: Request ISR fails on PCIE device[%s]\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800233 __func__, device->bus_id);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800234 aer_remove(dev);
235 return status;
236 }
237
238 rpc->isr = 1;
239
240 aer_enable_rootport(rpc);
241
242 return status;
243}
244
245/**
246 * aer_root_reset - reset link on Root Port
247 * @dev: pointer to Root Port's pci_dev data structure
248 *
249 * Invoked by Port Bus driver when performing link reset at Root Port.
250 **/
251static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
252{
253 u16 p2p_ctrl;
254 u32 status;
255 int pos;
256
257 pos = pci_find_aer_capability(dev);
258
259 /* Disable Root's interrupt in response to error messages */
260 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
261
262 /* Assert Secondary Bus Reset */
263 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
264 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
265 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
266
267 /* De-assert Secondary Bus Reset */
268 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
269 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
270
271 /*
272 * System software must wait for at least 100ms from the end
273 * of a reset of one or more device before it is permitted
274 * to issue Configuration Requests to those devices.
275 */
276 msleep(200);
277 printk(KERN_DEBUG "Complete link reset at Root[%s]\n", dev->dev.bus_id);
278
279 /* Enable Root Port's interrupt in response to error messages */
280 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
281 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
282 pci_write_config_dword(dev,
283 pos + PCI_ERR_ROOT_COMMAND,
284 ROOT_PORT_INTR_ON_MESG_MASK);
285
286 return PCI_ERS_RESULT_RECOVERED;
287}
288
289/**
290 * aer_error_detected - update severity status
291 * @dev: pointer to Root Port's pci_dev data structure
292 * @error: error severity being notified by port bus
293 *
294 * Invoked by Port Bus driver during error recovery.
295 **/
296static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
297 enum pci_channel_state error)
298{
299 /* Root Port has no impact. Always recovers. */
300 return PCI_ERS_RESULT_CAN_RECOVER;
301}
302
303/**
304 * aer_error_resume - clean up corresponding error status bits
305 * @dev: pointer to Root Port's pci_dev data structure
306 *
307 * Invoked by Port Bus driver during nonfatal recovery.
308 **/
309static void aer_error_resume(struct pci_dev *dev)
310{
311 int pos;
312 u32 status, mask;
313 u16 reg16;
314
315 /* Clean up Root device status */
316 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
317 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
318 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
319
320 /* Clean AER Root Error Status */
321 pos = pci_find_aer_capability(dev);
322 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
323 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
324 if (dev->error_state == pci_channel_io_normal)
325 status &= ~mask; /* Clear corresponding nonfatal bits */
326 else
327 status &= mask; /* Clear corresponding fatal bits */
328 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
329}
330
331/**
332 * aer_service_init - register AER root service driver
333 *
334 * Invoked when AER root service driver is loaded.
335 **/
336static int __init aer_service_init(void)
337{
Randy Dunlap7f785762007-10-05 13:17:58 -0700338 if (pcie_aer_disable)
339 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100340 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800341}
342
343/**
344 * aer_service_exit - unregister AER root service driver
345 *
346 * Invoked when AER root service driver is unloaded.
347 **/
348static void __exit aer_service_exit(void)
349{
Sam Ravnborgc1996c22007-02-27 10:22:00 +0100350 pcie_port_service_unregister(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800351}
352
353module_init(aer_service_init);
354module_exit(aer_service_exit);