GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / pci / pcie / aer / aerdrv.c
blobf409948e1a9bb614cfe5fedc1dc652285f8e0307
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/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/pm.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/pcieport_if.h>
28 #include <linux/slab.h>
30 #include "aerdrv.h"
31 #include "../../pci.h"
34 * Version Information
36 #define DRIVER_VERSION "v1.0"
37 #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
38 #define DRIVER_DESC "Root Port Advanced Error Reporting Driver"
39 MODULE_AUTHOR(DRIVER_AUTHOR);
40 MODULE_DESCRIPTION(DRIVER_DESC);
41 MODULE_LICENSE("GPL");
43 static int __devinit aer_probe(struct pcie_device *dev);
44 static void aer_remove(struct pcie_device *dev);
45 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
46 enum pci_channel_state error);
47 static void aer_error_resume(struct pci_dev *dev);
48 static pci_ers_result_t aer_root_reset(struct pci_dev *dev);
50 static struct pci_error_handlers aer_error_handlers = {
51 .error_detected = aer_error_detected,
52 .resume = aer_error_resume,
55 static struct pcie_port_service_driver aerdriver = {
56 .name = "aer",
57 .port_type = PCI_EXP_TYPE_ROOT_PORT,
58 .service = PCIE_PORT_SERVICE_AER,
60 .probe = aer_probe,
61 .remove = aer_remove,
63 .err_handler = &aer_error_handlers,
65 .reset_link = aer_root_reset,
68 static int pcie_aer_disable;
70 void pci_no_aer(void)
72 pcie_aer_disable = 1; /* has priority over 'forceload' */
75 bool pci_aer_available(void)
77 return !pcie_aer_disable && pci_msi_enabled();
80 static int set_device_error_reporting(struct pci_dev *dev, void *data)
82 bool enable = *((bool *)data);
84 if ((dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
85 (dev->pcie_type == PCI_EXP_TYPE_UPSTREAM) ||
86 (dev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)) {
87 if (enable)
88 pci_enable_pcie_error_reporting(dev);
89 else
90 pci_disable_pcie_error_reporting(dev);
93 if (enable)
94 pcie_set_ecrc_checking(dev);
96 return 0;
99 /**
100 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
101 * @dev: pointer to root port's pci_dev data structure
102 * @enable: true = enable error reporting, false = disable error reporting.
104 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
105 bool enable)
107 set_device_error_reporting(dev, &enable);
109 if (!dev->subordinate)
110 return;
111 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
115 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
116 * @rpc: pointer to a Root Port data structure
118 * Invoked when PCIe bus loads AER service driver.
120 static void aer_enable_rootport(struct aer_rpc *rpc)
122 struct pci_dev *pdev = rpc->rpd->port;
123 int pos, aer_pos;
124 u16 reg16;
125 u32 reg32;
127 pos = pci_pcie_cap(pdev);
128 /* Clear PCIe Capability's Device Status */
129 pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, &reg16);
130 pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
132 /* Disable system error generation in response to error messages */
133 pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, &reg16);
134 reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
135 pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
137 aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
138 /* Clear error status */
139 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
140 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
141 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
142 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
143 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
144 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
147 * Enable error reporting for the root port device and downstream port
148 * devices.
150 set_downstream_devices_error_reporting(pdev, true);
152 /* Enable Root Port's interrupt in response to error messages */
153 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
154 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
155 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
159 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
160 * @rpc: pointer to a Root Port data structure
162 * Invoked when PCIe bus unloads AER service driver.
164 static void aer_disable_rootport(struct aer_rpc *rpc)
166 struct pci_dev *pdev = rpc->rpd->port;
167 u32 reg32;
168 int pos;
171 * Disable error reporting for the root port device and downstream port
172 * devices.
174 set_downstream_devices_error_reporting(pdev, false);
176 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
177 /* Disable Root's interrupt in response to error messages */
178 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
179 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
180 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
182 /* Clear Root's error status reg */
183 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
184 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
188 * aer_irq - Root Port's ISR
189 * @irq: IRQ assigned to Root Port
190 * @context: pointer to Root Port data structure
192 * Invoked when Root Port detects AER messages.
194 irqreturn_t aer_irq(int irq, void *context)
196 unsigned int status, id;
197 struct pcie_device *pdev = (struct pcie_device *)context;
198 struct aer_rpc *rpc = get_service_data(pdev);
199 int next_prod_idx;
200 unsigned long flags;
201 int pos;
203 pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR);
205 * Must lock access to Root Error Status Reg, Root Error ID Reg,
206 * and Root error producer/consumer index
208 spin_lock_irqsave(&rpc->e_lock, flags);
210 /* Read error status */
211 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
212 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
213 spin_unlock_irqrestore(&rpc->e_lock, flags);
214 return IRQ_NONE;
217 /* Read error source and clear error status */
218 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
219 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
221 /* Store error source for later DPC handler */
222 next_prod_idx = rpc->prod_idx + 1;
223 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
224 next_prod_idx = 0;
225 if (next_prod_idx == rpc->cons_idx) {
227 * Error Storm Condition - possibly the same error occurred.
228 * Drop the error.
230 spin_unlock_irqrestore(&rpc->e_lock, flags);
231 return IRQ_HANDLED;
233 rpc->e_sources[rpc->prod_idx].status = status;
234 rpc->e_sources[rpc->prod_idx].id = id;
235 rpc->prod_idx = next_prod_idx;
236 spin_unlock_irqrestore(&rpc->e_lock, flags);
238 /* Invoke DPC handler */
239 schedule_work(&rpc->dpc_handler);
241 return IRQ_HANDLED;
243 EXPORT_SYMBOL_GPL(aer_irq);
246 * aer_alloc_rpc - allocate Root Port data structure
247 * @dev: pointer to the pcie_dev data structure
249 * Invoked when Root Port's AER service is loaded.
251 static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
253 struct aer_rpc *rpc;
255 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
256 if (!rpc)
257 return NULL;
259 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
260 spin_lock_init(&rpc->e_lock);
262 rpc->rpd = dev;
263 INIT_WORK(&rpc->dpc_handler, aer_isr);
264 mutex_init(&rpc->rpc_mutex);
265 init_waitqueue_head(&rpc->wait_release);
267 /* Use PCIe bus function to store rpc into PCIe device */
268 set_service_data(dev, rpc);
270 return rpc;
274 * aer_remove - clean up resources
275 * @dev: pointer to the pcie_dev data structure
277 * Invoked when PCI Express bus unloads or AER probe fails.
279 static void aer_remove(struct pcie_device *dev)
281 struct aer_rpc *rpc = get_service_data(dev);
283 if (rpc) {
284 /* If register interrupt service, it must be free. */
285 if (rpc->isr)
286 free_irq(dev->irq, dev);
288 wait_event(rpc->wait_release, rpc->prod_idx == rpc->cons_idx);
290 aer_disable_rootport(rpc);
291 kfree(rpc);
292 set_service_data(dev, NULL);
297 * aer_probe - initialize resources
298 * @dev: pointer to the pcie_dev data structure
299 * @id: pointer to the service id data structure
301 * Invoked when PCI Express bus loads AER service driver.
303 static int __devinit aer_probe(struct pcie_device *dev)
305 int status;
306 struct aer_rpc *rpc;
307 struct device *device = &dev->device;
309 /* Init */
310 status = aer_init(dev);
311 if (status)
312 return status;
314 /* Alloc rpc data structure */
315 rpc = aer_alloc_rpc(dev);
316 if (!rpc) {
317 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
318 aer_remove(dev);
319 return -ENOMEM;
322 /* Request IRQ ISR */
323 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
324 if (status) {
325 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
326 aer_remove(dev);
327 return status;
330 rpc->isr = 1;
332 aer_enable_rootport(rpc);
334 return status;
338 * aer_root_reset - reset link on Root Port
339 * @dev: pointer to Root Port's pci_dev data structure
341 * Invoked by Port Bus driver when performing link reset at Root Port.
343 static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
345 u32 reg32;
346 int pos;
348 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
350 /* Disable Root's interrupt in response to error messages */
351 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
352 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
353 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
355 aer_do_secondary_bus_reset(dev);
356 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
358 /* Clear Root Error Status */
359 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
360 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
362 /* Enable Root Port's interrupt in response to error messages */
363 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
364 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
365 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
367 return PCI_ERS_RESULT_RECOVERED;
371 * aer_error_detected - update severity status
372 * @dev: pointer to Root Port's pci_dev data structure
373 * @error: error severity being notified by port bus
375 * Invoked by Port Bus driver during error recovery.
377 static pci_ers_result_t aer_error_detected(struct pci_dev *dev,
378 enum pci_channel_state error)
380 /* Root Port has no impact. Always recovers. */
381 return PCI_ERS_RESULT_CAN_RECOVER;
385 * aer_error_resume - clean up corresponding error status bits
386 * @dev: pointer to Root Port's pci_dev data structure
388 * Invoked by Port Bus driver during nonfatal recovery.
390 static void aer_error_resume(struct pci_dev *dev)
392 int pos;
393 u32 status, mask;
394 u16 reg16;
396 /* Clean up Root device status */
397 pos = pci_pcie_cap(dev);
398 pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &reg16);
399 pci_write_config_word(dev, pos + PCI_EXP_DEVSTA, reg16);
401 /* Clean AER Root Error Status */
402 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
403 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
404 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
405 if (dev->error_state == pci_channel_io_normal)
406 status &= ~mask; /* Clear corresponding nonfatal bits */
407 else
408 status &= mask; /* Clear corresponding fatal bits */
409 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
413 * aer_service_init - register AER root service driver
415 * Invoked when AER root service driver is loaded.
417 static int __init aer_service_init(void)
419 if (!pci_aer_available())
420 return -ENXIO;
421 return pcie_port_service_register(&aerdriver);
425 * aer_service_exit - unregister AER root service driver
427 * Invoked when AER root service driver is unloaded.
429 static void __exit aer_service_exit(void)
431 pcie_port_service_unregister(&aerdriver);
434 module_init(aer_service_init);
435 module_exit(aer_service_exit);