Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / pci / pcie / aer / aerdrv.c
blob07c3bdb6edc2ad5b261f3877eed725466f4d895a
1 /*
2 * drivers/pci/pcie/aer/aerdrv.c
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.
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.
12 * Copyright (C) 2006 Intel Corp.
13 * Tom Long Nguyen (tom.l.nguyen@intel.com)
14 * Zhang Yanmin (yanmin.zhang@intel.com)
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>
28 #include "aerdrv.h"
31 * Version Information
33 #define DRIVER_VERSION "v1.0"
34 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
35 #define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
36 MODULE_AUTHOR(DRIVER_AUTHOR);
37 MODULE_DESCRIPTION(DRIVER_DESC);
38 MODULE_LICENSE("GPL");
40 static int __devinit aer_probe (struct pcie_device *dev,
41 const struct pcie_port_service_id *id );
42 static void aer_remove(struct pcie_device *dev);
43 static int aer_suspend(struct pcie_device *dev, pm_message_t state)
44 {return 0;}
45 static int aer_resume(struct pcie_device *dev) {return 0;}
46 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
47 enum pci_channel_state error);
48 static void aer_error_resume(struct pci_dev *dev);
49 static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
52 * PCI Express bus's AER Root service driver data structure
54 static struct pcie_port_service_id aer_id[] = {
56 .vendor = PCI_ANY_ID,
57 .device = PCI_ANY_ID,
58 .port_type = PCIE_RC_PORT,
59 .service_type = PCIE_PORT_SERVICE_AER,
61 { /* end: all zeroes */ }
64 static struct pci_error_handlers aer_error_handlers = {
65 .error_detected = aer_error_detected,
66 .resume = aer_error_resume,
69 static struct pcie_port_service_driver aerdriver = {
70 .name = "aer",
71 .id_table = &aer_id[0],
73 .probe = aer_probe,
74 .remove = aer_remove,
76 .suspend = aer_suspend,
77 .resume = aer_resume,
79 .err_handler = &aer_error_handlers,
81 .reset_link = aer_root_reset,
84 static int pcie_aer_disable;
86 void pci_no_aer(void)
88 pcie_aer_disable = 1; /* has priority over 'forceload' */
91 /**
92 * aer_irq - Root Port's ISR
93 * @irq: IRQ assigned to Root Port
94 * @context: pointer to Root Port data structure
96 * Invoked when Root Port detects AER messages.
97 **/
98 static irqreturn_t aer_irq(int irq, void *context)
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;
107 pos = pci_find_aer_capability(pdev->port);
109 * Must lock access to Root Error Status Reg, Root Error ID Reg,
110 * and Root error producer/consumer index
112 spin_lock_irqsave(&rpc->e_lock, flags);
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;
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);
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) {
131 * Error Storm Condition - possibly the same error occurred.
132 * Drop the error.
134 spin_unlock_irqrestore(&rpc->e_lock, flags);
135 return IRQ_HANDLED;
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);
142 /* Invoke DPC handler */
143 schedule_work(&rpc->dpc_handler);
145 return IRQ_HANDLED;
149 * aer_alloc_rpc - allocate Root Port data structure
150 * @dev: pointer to the pcie_dev data structure
152 * Invoked when Root Port's AER service is loaded.
154 static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
156 struct aer_rpc *rpc;
158 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
159 GFP_KERNEL)))
160 return NULL;
163 * Initialize Root lock access, e_lock, to Root Error Status Reg,
164 * Root Error ID Reg, and Root error producer/consumer index.
166 spin_lock_init(&rpc->e_lock);
168 rpc->rpd = dev;
169 INIT_WORK(&rpc->dpc_handler, aer_isr);
170 rpc->prod_idx = rpc->cons_idx = 0;
171 mutex_init(&rpc->rpc_mutex);
172 init_waitqueue_head(&rpc->wait_release);
174 /* Use PCIE bus function to store rpc into PCIE device */
175 set_service_data(dev, rpc);
177 return rpc;
181 * aer_remove - clean up resources
182 * @dev: pointer to the pcie_dev data structure
184 * Invoked when PCI Express bus unloads or AER probe fails.
186 static void aer_remove(struct pcie_device *dev)
188 struct aer_rpc *rpc = get_service_data(dev);
190 if (rpc) {
191 /* If register interrupt service, it must be free. */
192 if (rpc->isr)
193 free_irq(dev->irq, dev);
195 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
197 aer_delete_rootport(rpc);
198 set_service_data(dev, NULL);
203 * aer_probe - initialize resources
204 * @dev: pointer to the pcie_dev data structure
205 * @id: pointer to the service id data structure
207 * Invoked when PCI Express bus loads AER service driver.
209 static int __devinit aer_probe (struct pcie_device *dev,
210 const struct pcie_port_service_id *id )
212 int status;
213 struct aer_rpc *rpc;
214 struct device *device = &dev->device;
216 /* Init */
217 if ((status = aer_init(dev)))
218 return status;
220 /* Alloc rpc data structure */
221 if (!(rpc = aer_alloc_rpc(dev))) {
222 printk(KERN_DEBUG "%s: Alloc rpc fails on PCIE device[%s]\n",
223 __func__, device->bus_id);
224 aer_remove(dev);
225 return -ENOMEM;
228 /* Request IRQ ISR */
229 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
230 dev))) {
231 printk(KERN_DEBUG "%s: Request ISR fails on PCIE device[%s]\n",
232 __func__, device->bus_id);
233 aer_remove(dev);
234 return status;
237 rpc->isr = 1;
239 aer_enable_rootport(rpc);
241 return status;
245 * aer_root_reset - reset link on Root Port
246 * @dev: pointer to Root Port's pci_dev data structure
248 * Invoked by Port Bus driver when performing link reset at Root Port.
250 static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
252 u16 p2p_ctrl;
253 u32 status;
254 int pos;
256 pos = pci_find_aer_capability(dev);
258 /* Disable Root's interrupt in response to error messages */
259 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
261 /* Assert Secondary Bus Reset */
262 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
263 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
264 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
266 /* De-assert Secondary Bus Reset */
267 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
268 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
271 * System software must wait for at least 100ms from the end
272 * of a reset of one or more device before it is permitted
273 * to issue Configuration Requests to those devices.
275 msleep(200);
276 printk(KERN_DEBUG "Complete link reset at Root[%s]\n", dev->dev.bus_id);
278 /* Enable Root Port's interrupt in response to error messages */
279 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
280 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
281 pci_write_config_dword(dev,
282 pos + PCI_ERR_ROOT_COMMAND,
283 ROOT_PORT_INTR_ON_MESG_MASK);
285 return PCI_ERS_RESULT_RECOVERED;
289 * aer_error_detected - update severity status
290 * @dev: pointer to Root Port's pci_dev data structure
291 * @error: error severity being notified by port bus
293 * Invoked by Port Bus driver during error recovery.
295 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
296 enum pci_channel_state error)
298 /* Root Port has no impact. Always recovers. */
299 return PCI_ERS_RESULT_CAN_RECOVER;
303 * aer_error_resume - clean up corresponding error status bits
304 * @dev: pointer to Root Port's pci_dev data structure
306 * Invoked by Port Bus driver during nonfatal recovery.
308 static void aer_error_resume(struct pci_dev *dev)
310 int pos;
311 u32 status, mask;
312 u16 reg16;
314 /* Clean up Root device status */
315 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
316 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
317 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
319 /* Clean AER Root Error Status */
320 pos = pci_find_aer_capability(dev);
321 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
322 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
323 if (dev->error_state == pci_channel_io_normal)
324 status &= ~mask; /* Clear corresponding nonfatal bits */
325 else
326 status &= mask; /* Clear corresponding fatal bits */
327 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
331 * aer_service_init - register AER root service driver
333 * Invoked when AER root service driver is loaded.
335 static int __init aer_service_init(void)
337 if (pcie_aer_disable)
338 return -ENXIO;
339 return pcie_port_service_register(&aerdriver);
343 * aer_service_exit - unregister AER root service driver
345 * Invoked when AER root service driver is unloaded.
347 static void __exit aer_service_exit(void)
349 pcie_port_service_unregister(&aerdriver);
352 module_init(aer_service_init);
353 module_exit(aer_service_exit);