debugfs: Modified default dir of debugfs for debugging UHCI.
[linux/fpc-iii.git] / drivers / pci / pcie / aer / aerdrv.c
blob4770f13b3ca182d8381ef95b43805f286ca74d5e
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"
29 #include "../../pci.h"
32 * Version Information
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"
37 MODULE_AUTHOR(DRIVER_AUTHOR);
38 MODULE_DESCRIPTION(DRIVER_DESC);
39 MODULE_LICENSE("GPL");
41 static int __devinit aer_probe (struct pcie_device *dev);
42 static void aer_remove(struct pcie_device *dev);
43 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
44 enum pci_channel_state error);
45 static void aer_error_resume(struct pci_dev *dev);
46 static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
48 static struct pci_error_handlers aer_error_handlers = {
49 .error_detected = aer_error_detected,
50 .resume = aer_error_resume,
53 static struct pcie_port_service_driver aerdriver = {
54 .name = "aer",
55 .port_type = PCIE_ANY_PORT,
56 .service = PCIE_PORT_SERVICE_AER,
58 .probe = aer_probe,
59 .remove = aer_remove,
61 .err_handler = &aer_error_handlers,
63 .reset_link = aer_root_reset,
66 static int pcie_aer_disable;
68 void pci_no_aer(void)
70 pcie_aer_disable = 1; /* has priority over 'forceload' */
73 /**
74 * aer_irq - Root Port's ISR
75 * @irq: IRQ assigned to Root Port
76 * @context: pointer to Root Port data structure
78 * Invoked when Root Port detects AER messages.
79 **/
80 irqreturn_t aer_irq(int irq, void *context)
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;
89 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
91 * Must lock access to Root Error Status Reg, Root Error ID Reg,
92 * and Root error producer/consumer index
94 spin_lock_irqsave(&rpc->e_lock, flags);
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;
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);
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) {
113 * Error Storm Condition - possibly the same error occurred.
114 * Drop the error.
116 spin_unlock_irqrestore(&rpc->e_lock, flags);
117 return IRQ_HANDLED;
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);
124 /* Invoke DPC handler */
125 schedule_work(&rpc->dpc_handler);
127 return IRQ_HANDLED;
129 EXPORT_SYMBOL_GPL(aer_irq);
132 * aer_alloc_rpc - allocate Root Port data structure
133 * @dev: pointer to the pcie_dev data structure
135 * Invoked when Root Port's AER service is loaded.
137 static struct aer_rpc* aer_alloc_rpc(struct pcie_device *dev)
139 struct aer_rpc *rpc;
141 if (!(rpc = kzalloc(sizeof(struct aer_rpc),
142 GFP_KERNEL)))
143 return NULL;
146 * Initialize Root lock access, e_lock, to Root Error Status Reg,
147 * Root Error ID Reg, and Root error producer/consumer index.
149 spin_lock_init(&rpc->e_lock);
151 rpc->rpd = dev;
152 INIT_WORK(&rpc->dpc_handler, aer_isr);
153 rpc->prod_idx = rpc->cons_idx = 0;
154 mutex_init(&rpc->rpc_mutex);
155 init_waitqueue_head(&rpc->wait_release);
157 /* Use PCIE bus function to store rpc into PCIE device */
158 set_service_data(dev, rpc);
160 return rpc;
164 * aer_remove - clean up resources
165 * @dev: pointer to the pcie_dev data structure
167 * Invoked when PCI Express bus unloads or AER probe fails.
169 static void aer_remove(struct pcie_device *dev)
171 struct aer_rpc *rpc = get_service_data(dev);
173 if (rpc) {
174 /* If register interrupt service, it must be free. */
175 if (rpc->isr)
176 free_irq(dev->irq, dev);
178 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
180 aer_delete_rootport(rpc);
181 set_service_data(dev, NULL);
186 * aer_probe - initialize resources
187 * @dev: pointer to the pcie_dev data structure
188 * @id: pointer to the service id data structure
190 * Invoked when PCI Express bus loads AER service driver.
192 static int __devinit aer_probe (struct pcie_device *dev)
194 int status;
195 struct aer_rpc *rpc;
196 struct device *device = &dev->device;
198 /* Init */
199 if ((status = aer_init(dev)))
200 return status;
202 /* Alloc rpc data structure */
203 if (!(rpc = aer_alloc_rpc(dev))) {
204 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
205 aer_remove(dev);
206 return -ENOMEM;
209 /* Request IRQ ISR */
210 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
211 dev))) {
212 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
213 aer_remove(dev);
214 return status;
217 rpc->isr = 1;
219 aer_enable_rootport(rpc);
221 return status;
225 * aer_root_reset - reset link on Root Port
226 * @dev: pointer to Root Port's pci_dev data structure
228 * Invoked by Port Bus driver when performing link reset at Root Port.
230 static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
232 u16 p2p_ctrl;
233 u32 status;
234 int pos;
236 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
238 /* Disable Root's interrupt in response to error messages */
239 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, 0);
241 /* Assert Secondary Bus Reset */
242 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &p2p_ctrl);
243 p2p_ctrl |= PCI_CB_BRIDGE_CTL_CB_RESET;
244 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
246 /* De-assert Secondary Bus Reset */
247 p2p_ctrl &= ~PCI_CB_BRIDGE_CTL_CB_RESET;
248 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, p2p_ctrl);
251 * System software must wait for at least 100ms from the end
252 * of a reset of one or more device before it is permitted
253 * to issue Configuration Requests to those devices.
255 msleep(200);
256 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
258 /* Enable Root Port's interrupt in response to error messages */
259 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
260 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
261 pci_write_config_dword(dev,
262 pos + PCI_ERR_ROOT_COMMAND,
263 ROOT_PORT_INTR_ON_MESG_MASK);
265 return PCI_ERS_RESULT_RECOVERED;
269 * aer_error_detected - update severity status
270 * @dev: pointer to Root Port's pci_dev data structure
271 * @error: error severity being notified by port bus
273 * Invoked by Port Bus driver during error recovery.
275 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
276 enum pci_channel_state error)
278 /* Root Port has no impact. Always recovers. */
279 return PCI_ERS_RESULT_CAN_RECOVER;
283 * aer_error_resume - clean up corresponding error status bits
284 * @dev: pointer to Root Port's pci_dev data structure
286 * Invoked by Port Bus driver during nonfatal recovery.
288 static void aer_error_resume(struct pci_dev *dev)
290 int pos;
291 u32 status, mask;
292 u16 reg16;
294 /* Clean up Root device status */
295 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
296 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
297 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
299 /* Clean AER Root Error Status */
300 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
301 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
302 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
303 if (dev->error_state == pci_channel_io_normal)
304 status &= ~mask; /* Clear corresponding nonfatal bits */
305 else
306 status &= mask; /* Clear corresponding fatal bits */
307 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
311 * aer_service_init - register AER root service driver
313 * Invoked when AER root service driver is loaded.
315 static int __init aer_service_init(void)
317 if (pcie_aer_disable)
318 return -ENXIO;
319 return pcie_port_service_register(&aerdriver);
323 * aer_service_exit - unregister AER root service driver
325 * Invoked when AER root service driver is unloaded.
327 static void __exit aer_service_exit(void)
329 pcie_port_service_unregister(&aerdriver);
332 module_init(aer_service_init);
333 module_exit(aer_service_exit);