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