blob: 57c41204c549887732d47c780c385536171efd20 [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
51/*
52 * PCI Express bus's AER Root service driver data structure
53 */
54static struct pcie_port_service_id aer_id[] = {
55 {
56 .vendor = PCI_ANY_ID,
57 .device = PCI_ANY_ID,
58 .port_type = PCIE_RC_PORT,
59 .service_type = PCIE_PORT_SERVICE_AER,
60 },
61 { /* end: all zeroes */ }
62};
63
64static struct pci_error_handlers aer_error_handlers = {
65 .error_detected = aer_error_detected,
66 .resume = aer_error_resume,
67};
68
Sam Ravnborgc1996c22007-02-27 10:22:00 +010069static struct pcie_port_service_driver aerdriver = {
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080070 .name = "aer",
71 .id_table = &aer_id[0],
72
73 .probe = aer_probe,
74 .remove = aer_remove,
75
76 .suspend = aer_suspend,
77 .resume = aer_resume,
78
79 .err_handler = &aer_error_handlers,
80
81 .reset_link = aer_root_reset,
82};
83
Randy Dunlap7f785762007-10-05 13:17:58 -070084static int pcie_aer_disable;
85
86void pci_no_aer(void)
87{
88 pcie_aer_disable = 1; /* has priority over 'forceload' */
89}
90
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080091/**
92 * aer_irq - Root Port's ISR
93 * @irq: IRQ assigned to Root Port
94 * @context: pointer to Root Port data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080095 *
96 * Invoked when Root Port detects AER messages.
97 **/
David Howells7d12e782006-10-05 14:55:46 +010098static irqreturn_t aer_irq(int irq, void *context)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080099{
100 unsigned int status, id;
101 struct pcie_device *pdev = (struct pcie_device *)context;
102 struct aer_rpc *rpc = get_service_data(pdev);
103 int next_prod_idx;
104 unsigned long flags;
105 int pos;
106
Jesse Barnes09276782008-10-18 17:33:19 -0700107 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800108 /*
109 * Must lock access to Root Error Status Reg, Root Error ID Reg,
110 * and Root error producer/consumer index
111 */
112 spin_lock_irqsave(&rpc->e_lock, flags);
113
114 /* Read error status */
115 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
116 if (!(status & ROOT_ERR_STATUS_MASKS)) {
117 spin_unlock_irqrestore(&rpc->e_lock, flags);
118 return IRQ_NONE;
119 }
120
121 /* Read error source and clear error status */
122 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_COR_SRC, &id);
123 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
124
125 /* Store error source for later DPC handler */
126 next_prod_idx = rpc->prod_idx + 1;
127 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
128 next_prod_idx = 0;
129 if (next_prod_idx == rpc->cons_idx) {
130 /*
131 * Error Storm Condition - possibly the same error occurred.
132 * Drop the error.
133 */
134 spin_unlock_irqrestore(&rpc->e_lock, flags);
135 return IRQ_HANDLED;
136 }
137 rpc->e_sources[rpc->prod_idx].status = status;
138 rpc->e_sources[rpc->prod_idx].id = id;
139 rpc->prod_idx = next_prod_idx;
140 spin_unlock_irqrestore(&rpc->e_lock, flags);
141
142 /* Invoke DPC handler */
143 schedule_work(&rpc->dpc_handler);
144
145 return IRQ_HANDLED;
146}
147
148/**
149 * aer_alloc_rpc - allocate Root Port data structure
150 * @dev: pointer to the pcie_dev data structure
151 *
152 * Invoked when Root Port's AER service is loaded.
153 **/
154static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
155{
156 struct aer_rpc *rpc;
157
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700158 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800159 GFP_KERNEL)))
160 return NULL;
161
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800162 /*
163 * Initialize Root lock access, e_lock, to Root Error Status Reg,
164 * Root Error ID Reg, and Root error producer/consumer index.
165 */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -0700166 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800167
168 rpc->rpd = dev;
David Howells65f27f32006-11-22 14:55:48 +0000169 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800170 rpc->prod_idx = rpc->cons_idx = 0;
171 mutex_init(&rpc->rpc_mutex);
172 init_waitqueue_head(&rpc->wait_release);
173
174 /* Use PCIE bus function to store rpc into PCIE device */
175 set_service_data(dev, rpc);
176
177 return rpc;
178}
179
180/**
181 * aer_remove - clean up resources
182 * @dev: pointer to the pcie_dev data structure
183 *
184 * Invoked when PCI Express bus unloads or AER probe fails.
185 **/
186static void aer_remove(struct pcie_device *dev)
187{
188 struct aer_rpc *rpc = get_service_data(dev);
189
190 if (rpc) {
191 /* If register interrupt service, it must be free. */
192 if (rpc->isr)
193 free_irq(dev->irq, dev);
194
195 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
196
197 aer_delete_rootport(rpc);
198 set_service_data(dev, NULL);
199 }
200}
201
202/**
203 * aer_probe - initialize resources
204 * @dev: pointer to the pcie_dev data structure
205 * @id: pointer to the service id data structure
206 *
207 * Invoked when PCI Express bus loads AER service driver.
208 **/
Rafael J. Wysocki0516c8b2009-01-13 14:44:19 +0100209static int __devinit aer_probe (struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800210{
211 int status;
212 struct aer_rpc *rpc;
213 struct device *device = &dev->device;
214
215 /* Init */
216 if ((status = aer_init(dev)))
217 return status;
218
219 /* Alloc rpc data structure */
220 if (!(rpc = aer_alloc_rpc(dev))) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600221 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800222 aer_remove(dev);
223 return -ENOMEM;
224 }
225
226 /* Request IRQ ISR */
Thomas Gleixner38515e92007-02-14 00:33:16 -0800227 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800228 dev))) {
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600229 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800230 aer_remove(dev);
231 return status;
232 }
233
234 rpc->isr = 1;
235
236 aer_enable_rootport(rpc);
237
238 return status;
239}
240
241/**
242 * aer_root_reset - reset link on Root Port
243 * @dev: pointer to Root Port's pci_dev data structure
244 *
245 * Invoked by Port Bus driver when performing link reset at Root Port.
246 **/
247static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
248{
249 u16 p2p_ctrl;
250 u32 status;
251 int pos;
252
Jesse Barnes09276782008-10-18 17:33:19 -0700253 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800254
255 /* Disable Root's interrupt in response to error messages */
256 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
257
258 /* Assert Secondary Bus Reset */
259 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
260 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
261 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
262
263 /* De-assert Secondary Bus Reset */
264 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
265 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
266
267 /*
268 * System software must wait for at least 100ms from the end
269 * of a reset of one or more device before it is permitted
270 * to issue Configuration Requests to those devices.
271 */
272 msleep(200);
Bjorn Helgaas531f2542008-06-13 10:52:12 -0600273 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800274
275 /* Enable Root Port's interrupt in response to error messages */
276 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
277 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
278 pci_write_config_dword(dev,
279 pos + PCI_ERR_ROOT_COMMAND,
280 ROOT_PORT_INTR_ON_MESG_MASK);
281
282 return PCI_ERS_RESULT_RECOVERED;
283}
284
285/**
286 * aer_error_detected - update severity status
287 * @dev: pointer to Root Port's pci_dev data structure
288 * @error: error severity being notified by port bus
289 *
290 * Invoked by Port Bus driver during error recovery.
291 **/
292static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
293 enum pci_channel_state error)
294{
295 /* Root Port has no impact. Always recovers. */
296 return PCI_ERS_RESULT_CAN_RECOVER;
297}
298
299/**
300 * aer_error_resume - clean up corresponding error status bits
301 * @dev: pointer to Root Port's pci_dev data structure
302 *
303 * Invoked by Port Bus driver during nonfatal recovery.
304 **/
305static void aer_error_resume(struct pci_dev *dev)
306{
307 int pos;
308 u32 status, mask;
309 u16 reg16;
310
311 /* Clean up Root device status */
312 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
313 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
314 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
315
316 /* Clean AER Root Error Status */
Jesse Barnes09276782008-10-18 17:33:19 -0700317 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800318 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
319 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
320 if (dev->error_state == pci_channel_io_normal)
321 status &= ~mask; /* Clear corresponding nonfatal bits */
322 else
323 status &= mask; /* Clear corresponding fatal bits */
324 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
325}
326
327/**
328 * aer_service_init - register AER root service driver
329 *
330 * Invoked when AER root service driver is loaded.
331 **/
332static int __init aer_service_init(void)
333{
Randy Dunlap7f785762007-10-05 13:17:58 -0700334 if (pcie_aer_disable)
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);