Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 1 | /* |
| 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> |
Rafael J. Wysocki | 415e12b | 2011-01-07 00:55:09 +0100 | [diff] [blame] | 20 | #include <linux/pci-acpi.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 21 | #include <linux/sched.h> |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/pm.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/pcieport_if.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 30 | |
| 31 | #include "aerdrv.h" |
Alex Chiang | 5d9526d | 2008-06-04 11:39:07 -0600 | [diff] [blame] | 32 | #include "../../pci.h" |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Version Information |
| 36 | */ |
| 37 | #define DRIVER_VERSION "v1.0" |
| 38 | #define DRIVER_AUTHOR "tom.l.nguyen@intel.com" |
| 39 | #define DRIVER_DESC "Root Port Advanced Error Reporting Driver" |
| 40 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 41 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
Bill Pemberton | 15856ad | 2012-11-21 15:35:00 -0500 | [diff] [blame] | 44 | static int aer_probe(struct pcie_device *dev); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 45 | static void aer_remove(struct pcie_device *dev); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 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); |
| 50 | |
Stephen Hemminger | 4945302 | 2012-09-07 09:33:14 -0700 | [diff] [blame] | 51 | static const struct pci_error_handlers aer_error_handlers = { |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 52 | .error_detected = aer_error_detected, |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 53 | .resume = aer_error_resume, |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 54 | }; |
| 55 | |
Sam Ravnborg | c1996c2 | 2007-02-27 10:22:00 +0100 | [diff] [blame] | 56 | static struct pcie_port_service_driver aerdriver = { |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 57 | .name = "aer", |
Kenji Kaneshige | 694f88e | 2009-11-25 21:06:15 +0900 | [diff] [blame] | 58 | .port_type = PCI_EXP_TYPE_ROOT_PORT, |
Rafael J. Wysocki | 2210636 | 2009-01-13 14:46:46 +0100 | [diff] [blame] | 59 | .service = PCIE_PORT_SERVICE_AER, |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 60 | |
| 61 | .probe = aer_probe, |
| 62 | .remove = aer_remove, |
| 63 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 64 | .err_handler = &aer_error_handlers, |
| 65 | |
| 66 | .reset_link = aer_root_reset, |
| 67 | }; |
| 68 | |
Randy Dunlap | 7f78576 | 2007-10-05 13:17:58 -0700 | [diff] [blame] | 69 | static int pcie_aer_disable; |
| 70 | |
| 71 | void pci_no_aer(void) |
| 72 | { |
Bjorn Helgaas | 7ece141 | 2016-09-06 16:24:37 -0500 | [diff] [blame^] | 73 | pcie_aer_disable = 1; |
Randy Dunlap | 7f78576 | 2007-10-05 13:17:58 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Rafael J. Wysocki | f1a7bfa | 2010-08-21 01:50:52 +0200 | [diff] [blame] | 76 | bool pci_aer_available(void) |
| 77 | { |
| 78 | return !pcie_aer_disable && pci_msi_enabled(); |
| 79 | } |
| 80 | |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 81 | static int set_device_error_reporting(struct pci_dev *dev, void *data) |
| 82 | { |
| 83 | bool enable = *((bool *)data); |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 84 | int type = pci_pcie_type(dev); |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 85 | |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 86 | if ((type == PCI_EXP_TYPE_ROOT_PORT) || |
| 87 | (type == PCI_EXP_TYPE_UPSTREAM) || |
| 88 | (type == PCI_EXP_TYPE_DOWNSTREAM)) { |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 89 | if (enable) |
| 90 | pci_enable_pcie_error_reporting(dev); |
| 91 | else |
| 92 | pci_disable_pcie_error_reporting(dev); |
| 93 | } |
| 94 | |
| 95 | if (enable) |
| 96 | pcie_set_ecrc_checking(dev); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports. |
| 103 | * @dev: pointer to root port's pci_dev data structure |
| 104 | * @enable: true = enable error reporting, false = disable error reporting. |
| 105 | */ |
| 106 | static void set_downstream_devices_error_reporting(struct pci_dev *dev, |
| 107 | bool enable) |
| 108 | { |
| 109 | set_device_error_reporting(dev, &enable); |
| 110 | |
| 111 | if (!dev->subordinate) |
| 112 | return; |
| 113 | pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * aer_enable_rootport - enable Root Port's interrupts when receiving messages |
| 118 | * @rpc: pointer to a Root Port data structure |
| 119 | * |
| 120 | * Invoked when PCIe bus loads AER service driver. |
| 121 | */ |
| 122 | static void aer_enable_rootport(struct aer_rpc *rpc) |
| 123 | { |
| 124 | struct pci_dev *pdev = rpc->rpd->port; |
Jiang Liu | 43bd4ee | 2012-07-24 17:20:11 +0800 | [diff] [blame] | 125 | int aer_pos; |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 126 | u16 reg16; |
| 127 | u32 reg32; |
| 128 | |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 129 | /* Clear PCIe Capability's Device Status */ |
Jiang Liu | 43bd4ee | 2012-07-24 17:20:11 +0800 | [diff] [blame] | 130 | pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, ®16); |
| 131 | pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16); |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 132 | |
| 133 | /* Disable system error generation in response to error messages */ |
Jiang Liu | 43bd4ee | 2012-07-24 17:20:11 +0800 | [diff] [blame] | 134 | pcie_capability_clear_word(pdev, PCI_EXP_RTCTL, |
| 135 | SYSTEM_ERROR_INTR_ON_MESG_MASK); |
Hidetoshi Seto | 843f469 | 2010-04-15 13:10:53 +0900 | [diff] [blame] | 136 | |
| 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, ®32); |
| 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, ®32); |
| 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, ®32); |
| 144 | pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32); |
| 145 | |
| 146 | /* |
| 147 | * Enable error reporting for the root port device and downstream port |
| 148 | * devices. |
| 149 | */ |
| 150 | set_downstream_devices_error_reporting(pdev, true); |
| 151 | |
| 152 | /* Enable Root Port's interrupt in response to error messages */ |
| 153 | pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, ®32); |
| 154 | reg32 |= ROOT_PORT_INTR_ON_MESG_MASK; |
| 155 | pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * aer_disable_rootport - disable Root Port's interrupts when receiving messages |
| 160 | * @rpc: pointer to a Root Port data structure |
| 161 | * |
| 162 | * Invoked when PCIe bus unloads AER service driver. |
| 163 | */ |
| 164 | static void aer_disable_rootport(struct aer_rpc *rpc) |
| 165 | { |
| 166 | struct pci_dev *pdev = rpc->rpd->port; |
| 167 | u32 reg32; |
| 168 | int pos; |
| 169 | |
| 170 | /* |
| 171 | * Disable error reporting for the root port device and downstream port |
| 172 | * devices. |
| 173 | */ |
| 174 | set_downstream_devices_error_reporting(pdev, false); |
| 175 | |
| 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, ®32); |
| 179 | reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK; |
| 180 | pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32); |
| 181 | |
| 182 | /* Clear Root's error status reg */ |
| 183 | pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32); |
| 184 | pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32); |
| 185 | } |
| 186 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 187 | /** |
| 188 | * aer_irq - Root Port's ISR |
| 189 | * @irq: IRQ assigned to Root Port |
| 190 | * @context: pointer to Root Port data structure |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 191 | * |
| 192 | * Invoked when Root Port detects AER messages. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 193 | */ |
Huang Ying | 634deb0 | 2009-04-24 10:45:23 +0800 | [diff] [blame] | 194 | irqreturn_t aer_irq(int irq, void *context) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 195 | { |
| 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; |
| 202 | |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 203 | pos = pci_find_ext_capability(pdev->port, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 204 | /* |
| 205 | * Must lock access to Root Error Status Reg, Root Error ID Reg, |
| 206 | * and Root error producer/consumer index |
| 207 | */ |
| 208 | spin_lock_irqsave(&rpc->e_lock, flags); |
| 209 | |
| 210 | /* Read error status */ |
| 211 | pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status); |
Hidetoshi Seto | e167bfc | 2010-04-15 13:18:26 +0900 | [diff] [blame] | 212 | if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) { |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 213 | spin_unlock_irqrestore(&rpc->e_lock, flags); |
| 214 | return IRQ_NONE; |
| 215 | } |
| 216 | |
| 217 | /* Read error source and clear error status */ |
Hidetoshi Seto | f647a44 | 2010-04-15 13:17:33 +0900 | [diff] [blame] | 218 | pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 219 | pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status); |
| 220 | |
| 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) { |
| 226 | /* |
| 227 | * Error Storm Condition - possibly the same error occurred. |
| 228 | * Drop the error. |
| 229 | */ |
| 230 | spin_unlock_irqrestore(&rpc->e_lock, flags); |
| 231 | return IRQ_HANDLED; |
| 232 | } |
| 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); |
| 237 | |
| 238 | /* Invoke DPC handler */ |
| 239 | schedule_work(&rpc->dpc_handler); |
| 240 | |
| 241 | return IRQ_HANDLED; |
| 242 | } |
Huang Ying | 634deb0 | 2009-04-24 10:45:23 +0800 | [diff] [blame] | 243 | EXPORT_SYMBOL_GPL(aer_irq); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * aer_alloc_rpc - allocate Root Port data structure |
| 247 | * @dev: pointer to the pcie_dev data structure |
| 248 | * |
| 249 | * Invoked when Root Port's AER service is loaded. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 250 | */ |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 251 | static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 252 | { |
| 253 | struct aer_rpc *rpc; |
| 254 | |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 255 | rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL); |
| 256 | if (!rpc) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 257 | return NULL; |
| 258 | |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 259 | /* Initialize Root lock access, e_lock, to Root Error Status Reg */ |
Milind Arun Choudhary | f5609d7 | 2007-07-09 11:55:54 -0700 | [diff] [blame] | 260 | spin_lock_init(&rpc->e_lock); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 261 | |
| 262 | rpc->rpd = dev; |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 263 | INIT_WORK(&rpc->dpc_handler, aer_isr); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 264 | mutex_init(&rpc->rpc_mutex); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 265 | |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 266 | /* Use PCIe bus function to store rpc into PCIe device */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 267 | set_service_data(dev, rpc); |
| 268 | |
| 269 | return rpc; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * aer_remove - clean up resources |
| 274 | * @dev: pointer to the pcie_dev data structure |
| 275 | * |
| 276 | * Invoked when PCI Express bus unloads or AER probe fails. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 277 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 278 | static void aer_remove(struct pcie_device *dev) |
| 279 | { |
| 280 | struct aer_rpc *rpc = get_service_data(dev); |
| 281 | |
| 282 | if (rpc) { |
| 283 | /* If register interrupt service, it must be free. */ |
| 284 | if (rpc->isr) |
| 285 | free_irq(dev->irq, dev); |
| 286 | |
Sebastian Andrzej Siewior | 4ae2182 | 2016-01-25 10:08:00 -0600 | [diff] [blame] | 287 | flush_work(&rpc->dpc_handler); |
Hidetoshi Seto | 460d298 | 2010-04-15 13:10:03 +0900 | [diff] [blame] | 288 | aer_disable_rootport(rpc); |
| 289 | kfree(rpc); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 290 | set_service_data(dev, NULL); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * aer_probe - initialize resources |
| 296 | * @dev: pointer to the pcie_dev data structure |
| 297 | * @id: pointer to the service id data structure |
| 298 | * |
| 299 | * Invoked when PCI Express bus loads AER service driver. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 300 | */ |
Bill Pemberton | 15856ad | 2012-11-21 15:35:00 -0500 | [diff] [blame] | 301 | static int aer_probe(struct pcie_device *dev) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 302 | { |
| 303 | int status; |
| 304 | struct aer_rpc *rpc; |
| 305 | struct device *device = &dev->device; |
| 306 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 307 | /* Alloc rpc data structure */ |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 308 | rpc = aer_alloc_rpc(dev); |
| 309 | if (!rpc) { |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 310 | dev_printk(KERN_DEBUG, device, "alloc rpc failed\n"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 311 | aer_remove(dev); |
| 312 | return -ENOMEM; |
| 313 | } |
| 314 | |
| 315 | /* Request IRQ ISR */ |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 316 | status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev); |
| 317 | if (status) { |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 318 | dev_printk(KERN_DEBUG, device, "request IRQ failed\n"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 319 | aer_remove(dev); |
| 320 | return status; |
| 321 | } |
| 322 | |
| 323 | rpc->isr = 1; |
| 324 | |
| 325 | aer_enable_rootport(rpc); |
| 326 | |
| 327 | return status; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * aer_root_reset - reset link on Root Port |
| 332 | * @dev: pointer to Root Port's pci_dev data structure |
| 333 | * |
| 334 | * Invoked by Port Bus driver when performing link reset at Root Port. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 335 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 336 | static pci_ers_result_t aer_root_reset(struct pci_dev *dev) |
| 337 | { |
Hidetoshi Seto | c6d34ed | 2010-04-15 13:09:13 +0900 | [diff] [blame] | 338 | u32 reg32; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 339 | int pos; |
| 340 | |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 341 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 342 | |
| 343 | /* Disable Root's interrupt in response to error messages */ |
Hidetoshi Seto | c6d34ed | 2010-04-15 13:09:13 +0900 | [diff] [blame] | 344 | pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, ®32); |
| 345 | reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK; |
| 346 | pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 347 | |
Alex Williamson | 1b95ce8 | 2013-08-08 14:10:20 -0600 | [diff] [blame] | 348 | pci_reset_bridge_secondary_bus(dev); |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 349 | dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 350 | |
Hidetoshi Seto | c6d34ed | 2010-04-15 13:09:13 +0900 | [diff] [blame] | 351 | /* Clear Root Error Status */ |
| 352 | pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, ®32); |
| 353 | pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32); |
| 354 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 355 | /* Enable Root Port's interrupt in response to error messages */ |
Hidetoshi Seto | c6d34ed | 2010-04-15 13:09:13 +0900 | [diff] [blame] | 356 | pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, ®32); |
| 357 | reg32 |= ROOT_PORT_INTR_ON_MESG_MASK; |
| 358 | pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 359 | |
| 360 | return PCI_ERS_RESULT_RECOVERED; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * aer_error_detected - update severity status |
| 365 | * @dev: pointer to Root Port's pci_dev data structure |
| 366 | * @error: error severity being notified by port bus |
| 367 | * |
| 368 | * Invoked by Port Bus driver during error recovery. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 369 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 370 | static pci_ers_result_t aer_error_detected(struct pci_dev *dev, |
| 371 | enum pci_channel_state error) |
| 372 | { |
| 373 | /* Root Port has no impact. Always recovers. */ |
| 374 | return PCI_ERS_RESULT_CAN_RECOVER; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * aer_error_resume - clean up corresponding error status bits |
| 379 | * @dev: pointer to Root Port's pci_dev data structure |
| 380 | * |
| 381 | * Invoked by Port Bus driver during nonfatal recovery. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 382 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 383 | static void aer_error_resume(struct pci_dev *dev) |
| 384 | { |
| 385 | int pos; |
| 386 | u32 status, mask; |
| 387 | u16 reg16; |
| 388 | |
| 389 | /* Clean up Root device status */ |
Jiang Liu | 43bd4ee | 2012-07-24 17:20:11 +0800 | [diff] [blame] | 390 | pcie_capability_read_word(dev, PCI_EXP_DEVSTA, ®16); |
| 391 | pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 392 | |
| 393 | /* Clean AER Root Error Status */ |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 394 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 395 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status); |
| 396 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask); |
| 397 | if (dev->error_state == pci_channel_io_normal) |
| 398 | status &= ~mask; /* Clear corresponding nonfatal bits */ |
| 399 | else |
| 400 | status &= mask; /* Clear corresponding fatal bits */ |
| 401 | pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * aer_service_init - register AER root service driver |
| 406 | * |
| 407 | * Invoked when AER root service driver is loaded. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 408 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 409 | static int __init aer_service_init(void) |
| 410 | { |
Rafael J. Wysocki | b22c3d8 | 2010-09-20 18:50:00 +0200 | [diff] [blame] | 411 | if (!pci_aer_available() || aer_acpi_firmware_first()) |
Andi Kleen | 3e77a3f | 2009-09-16 22:40:22 +0200 | [diff] [blame] | 412 | return -ENXIO; |
Sam Ravnborg | c1996c2 | 2007-02-27 10:22:00 +0100 | [diff] [blame] | 413 | return pcie_port_service_register(&aerdriver); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * aer_service_exit - unregister AER root service driver |
| 418 | * |
| 419 | * Invoked when AER root service driver is unloaded. |
Hidetoshi Seto | f6d3780 | 2010-04-15 13:22:11 +0900 | [diff] [blame] | 420 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 421 | static void __exit aer_service_exit(void) |
| 422 | { |
Sam Ravnborg | c1996c2 | 2007-02-27 10:22:00 +0100 | [diff] [blame] | 423 | pcie_port_service_unregister(&aerdriver); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | module_init(aer_service_init); |
| 427 | module_exit(aer_service_exit); |