Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/pci/pcie/aer/aerdrv_core.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 core part of PCI-Express AER. When an pci-express |
| 9 | * error is delivered, an error message will be collected and printed to |
| 10 | * console, then, an error recovery procedure will be executed by following |
| 11 | * the pci error recovery rules. |
| 12 | * |
| 13 | * Copyright (C) 2006 Intel Corp. |
| 14 | * Tom Long Nguyen (tom.l.nguyen@intel.com) |
| 15 | * Zhang Yanmin (yanmin.zhang@intel.com) |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/pci.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/pm.h> |
| 24 | #include <linux/suspend.h> |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 25 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 27 | #include "aerdrv.h" |
| 28 | |
| 29 | static int forceload; |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 30 | static int nosourceid; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 31 | module_param(forceload, bool, 0); |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 32 | module_param(nosourceid, bool, 0); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 33 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 34 | int pci_enable_pcie_error_reporting(struct pci_dev *dev) |
| 35 | { |
| 36 | u16 reg16 = 0; |
| 37 | int pos; |
| 38 | |
Matt Domsch | 0584396 | 2009-11-02 11:51:24 -0600 | [diff] [blame] | 39 | if (dev->aer_firmware_first) |
| 40 | return -EIO; |
| 41 | |
Yu Zhao | 270c66b | 2008-10-19 20:35:20 +0800 | [diff] [blame] | 42 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 43 | if (!pos) |
| 44 | return -EIO; |
| 45 | |
Kenji Kaneshige | 39a5306 | 2009-11-11 14:31:38 +0900 | [diff] [blame] | 46 | pos = pci_pcie_cap(dev); |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 47 | if (!pos) |
| 48 | return -EIO; |
| 49 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 50 | pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16); |
| 51 | reg16 = reg16 | |
| 52 | PCI_EXP_DEVCTL_CERE | |
| 53 | PCI_EXP_DEVCTL_NFERE | |
| 54 | PCI_EXP_DEVCTL_FERE | |
| 55 | PCI_EXP_DEVCTL_URRE; |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 56 | pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16); |
| 57 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 61 | |
| 62 | int pci_disable_pcie_error_reporting(struct pci_dev *dev) |
| 63 | { |
| 64 | u16 reg16 = 0; |
| 65 | int pos; |
| 66 | |
Matt Domsch | 0584396 | 2009-11-02 11:51:24 -0600 | [diff] [blame] | 67 | if (dev->aer_firmware_first) |
| 68 | return -EIO; |
| 69 | |
Kenji Kaneshige | 39a5306 | 2009-11-11 14:31:38 +0900 | [diff] [blame] | 70 | pos = pci_pcie_cap(dev); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 71 | if (!pos) |
| 72 | return -EIO; |
| 73 | |
| 74 | pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16); |
| 75 | reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE | |
| 76 | PCI_EXP_DEVCTL_NFERE | |
| 77 | PCI_EXP_DEVCTL_FERE | |
| 78 | PCI_EXP_DEVCTL_URRE); |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 79 | pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16); |
| 80 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 81 | return 0; |
| 82 | } |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 84 | |
| 85 | int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev) |
| 86 | { |
| 87 | int pos; |
Andrew Patterson | 6cdfd99 | 2009-12-03 10:28:20 -0700 | [diff] [blame] | 88 | u32 status; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 89 | |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 90 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 91 | if (!pos) |
| 92 | return -EIO; |
| 93 | |
| 94 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status); |
Andrew Patterson | 6cdfd99 | 2009-12-03 10:28:20 -0700 | [diff] [blame] | 95 | if (status) |
| 96 | pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 100 | EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 101 | |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 102 | static int set_device_error_reporting(struct pci_dev *dev, void *data) |
Andrew Patterson | 1f9f13c | 2009-02-20 16:04:59 -0700 | [diff] [blame] | 103 | { |
| 104 | bool enable = *((bool *)data); |
| 105 | |
Kenji Kaneshige | 694f88e | 2009-11-25 21:06:15 +0900 | [diff] [blame] | 106 | if ((dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT) || |
| 107 | (dev->pcie_type == PCI_EXP_TYPE_UPSTREAM) || |
| 108 | (dev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)) { |
Andrew Patterson | 43c1640 | 2009-04-22 16:52:09 -0600 | [diff] [blame] | 109 | if (enable) |
| 110 | pci_enable_pcie_error_reporting(dev); |
| 111 | else |
| 112 | pci_disable_pcie_error_reporting(dev); |
| 113 | } |
Andrew Patterson | 1f9f13c | 2009-02-20 16:04:59 -0700 | [diff] [blame] | 114 | |
| 115 | if (enable) |
Andrew Patterson | 43c1640 | 2009-04-22 16:52:09 -0600 | [diff] [blame] | 116 | pcie_set_ecrc_checking(dev); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 117 | |
| 118 | return 0; |
Andrew Patterson | 1f9f13c | 2009-02-20 16:04:59 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports. |
| 123 | * @dev: pointer to root port's pci_dev data structure |
| 124 | * @enable: true = enable error reporting, false = disable error reporting. |
| 125 | */ |
| 126 | static void set_downstream_devices_error_reporting(struct pci_dev *dev, |
| 127 | bool enable) |
| 128 | { |
| 129 | set_device_error_reporting(dev, &enable); |
Alex Chiang | cb4cb4a | 2009-03-05 19:28:40 -0700 | [diff] [blame] | 130 | |
| 131 | if (!dev->subordinate) |
| 132 | return; |
Andrew Patterson | 1f9f13c | 2009-02-20 16:04:59 -0700 | [diff] [blame] | 133 | pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable); |
| 134 | } |
| 135 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 136 | static inline int compare_device_id(struct pci_dev *dev, |
| 137 | struct aer_err_info *e_info) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 138 | { |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 139 | if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) { |
| 140 | /* |
| 141 | * Device ID match |
| 142 | */ |
| 143 | return 1; |
| 144 | } |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 145 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 149 | static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev) |
| 150 | { |
| 151 | if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) { |
| 152 | e_info->dev[e_info->error_dev_num] = dev; |
| 153 | e_info->error_dev_num++; |
| 154 | return 1; |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return 0; |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 161 | #define PCI_BUS(x) (((x) >> 8) & 0xff) |
| 162 | |
| 163 | static int find_device_iter(struct pci_dev *dev, void *data) |
| 164 | { |
| 165 | int pos; |
| 166 | u32 status; |
| 167 | u32 mask; |
| 168 | u16 reg16; |
| 169 | int result; |
| 170 | struct aer_err_info *e_info = (struct aer_err_info *)data; |
| 171 | |
| 172 | /* |
| 173 | * When bus id is equal to 0, it might be a bad id |
| 174 | * reported by root port. |
| 175 | */ |
| 176 | if (!nosourceid && (PCI_BUS(e_info->id) != 0)) { |
| 177 | result = compare_device_id(dev, e_info); |
| 178 | if (result) |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 179 | add_error_device(e_info, dev); |
| 180 | |
| 181 | /* |
| 182 | * If there is no multiple error, we stop |
| 183 | * or continue based on the id comparing. |
| 184 | */ |
Hidetoshi Seto | 273024d | 2009-09-07 17:16:20 +0900 | [diff] [blame] | 185 | if (!e_info->multi_error_valid) |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 186 | return result; |
| 187 | |
| 188 | /* |
| 189 | * If there are multiple errors and id does match, |
| 190 | * We need continue to search other devices under |
| 191 | * the root port. Return 0 means that. |
| 192 | */ |
| 193 | if (result) |
| 194 | return 0; |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 198 | * When either |
| 199 | * 1) nosourceid==y; |
| 200 | * 2) bus id is equal to 0. Some ports might lose the bus |
| 201 | * id of error source id; |
| 202 | * 3) There are multiple errors and prior id comparing fails; |
| 203 | * We check AER status registers to find the initial reporter. |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 204 | */ |
| 205 | if (atomic_read(&dev->enable_cnt) == 0) |
| 206 | return 0; |
Kenji Kaneshige | 39a5306 | 2009-11-11 14:31:38 +0900 | [diff] [blame] | 207 | pos = pci_pcie_cap(dev); |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 208 | if (!pos) |
| 209 | return 0; |
| 210 | /* Check if AER is enabled */ |
| 211 | pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16); |
| 212 | if (!(reg16 & ( |
| 213 | PCI_EXP_DEVCTL_CERE | |
| 214 | PCI_EXP_DEVCTL_NFERE | |
| 215 | PCI_EXP_DEVCTL_FERE | |
| 216 | PCI_EXP_DEVCTL_URRE))) |
| 217 | return 0; |
| 218 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
| 219 | if (!pos) |
| 220 | return 0; |
| 221 | |
| 222 | status = 0; |
| 223 | mask = 0; |
| 224 | if (e_info->severity == AER_CORRECTABLE) { |
Hidetoshi Seto | 0d90c3a | 2009-09-07 17:12:25 +0900 | [diff] [blame] | 225 | pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status); |
| 226 | pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask); |
| 227 | if (status & ~mask) { |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 228 | add_error_device(e_info, dev); |
| 229 | goto added; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 230 | } |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 231 | } else { |
Hidetoshi Seto | 0d90c3a | 2009-09-07 17:12:25 +0900 | [diff] [blame] | 232 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status); |
| 233 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask); |
| 234 | if (status & ~mask) { |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 235 | add_error_device(e_info, dev); |
| 236 | goto added; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | return 0; |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 241 | |
| 242 | added: |
Hidetoshi Seto | 273024d | 2009-09-07 17:16:20 +0900 | [diff] [blame] | 243 | if (e_info->multi_error_valid) |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 244 | return 0; |
| 245 | else |
| 246 | return 1; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * find_source_device - search through device hierarchy for source device |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 251 | * @parent: pointer to Root Port pci_dev data structure |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 252 | * @err_info: including detailed error information such like id |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 253 | * |
| 254 | * Invoked when error is detected at the Root Port. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 255 | */ |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 256 | static void find_source_device(struct pci_dev *parent, |
| 257 | struct aer_err_info *e_info) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 258 | { |
| 259 | struct pci_dev *dev = parent; |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 260 | int result; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 261 | |
| 262 | /* Is Root Port an agent that sends error message? */ |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 263 | result = find_device_iter(dev, e_info); |
| 264 | if (result) |
| 265 | return; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 266 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 267 | pci_walk_bus(parent->subordinate, find_device_iter, e_info); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 268 | } |
| 269 | |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 270 | static int report_error_detected(struct pci_dev *dev, void *data) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 271 | { |
| 272 | pci_ers_result_t vote; |
| 273 | struct pci_error_handlers *err_handler; |
| 274 | struct aer_broadcast_data *result_data; |
| 275 | result_data = (struct aer_broadcast_data *) data; |
| 276 | |
| 277 | dev->error_state = result_data->state; |
| 278 | |
| 279 | if (!dev->driver || |
| 280 | !dev->driver->err_handler || |
| 281 | !dev->driver->err_handler->error_detected) { |
| 282 | if (result_data->state == pci_channel_io_frozen && |
| 283 | !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) { |
| 284 | /* |
| 285 | * In case of fatal recovery, if one of down- |
| 286 | * stream device has no driver. We might be |
| 287 | * unable to recover because a later insmod |
| 288 | * of a driver for this device is unaware of |
| 289 | * its hw state. |
| 290 | */ |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 291 | dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n", |
| 292 | dev->driver ? |
| 293 | "no AER-aware driver" : "no driver"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 294 | } |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 295 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | err_handler = dev->driver->err_handler; |
| 299 | vote = err_handler->error_detected(dev, result_data->state); |
| 300 | result_data->result = merge_result(result_data->result, vote); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 301 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 302 | } |
| 303 | |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 304 | static int report_mmio_enabled(struct pci_dev *dev, void *data) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 305 | { |
| 306 | pci_ers_result_t vote; |
| 307 | struct pci_error_handlers *err_handler; |
| 308 | struct aer_broadcast_data *result_data; |
| 309 | result_data = (struct aer_broadcast_data *) data; |
| 310 | |
| 311 | if (!dev->driver || |
| 312 | !dev->driver->err_handler || |
| 313 | !dev->driver->err_handler->mmio_enabled) |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 314 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 315 | |
| 316 | err_handler = dev->driver->err_handler; |
| 317 | vote = err_handler->mmio_enabled(dev); |
| 318 | result_data->result = merge_result(result_data->result, vote); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 319 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 320 | } |
| 321 | |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 322 | static int report_slot_reset(struct pci_dev *dev, void *data) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 323 | { |
| 324 | pci_ers_result_t vote; |
| 325 | struct pci_error_handlers *err_handler; |
| 326 | struct aer_broadcast_data *result_data; |
| 327 | result_data = (struct aer_broadcast_data *) data; |
| 328 | |
| 329 | if (!dev->driver || |
| 330 | !dev->driver->err_handler || |
| 331 | !dev->driver->err_handler->slot_reset) |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 332 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 333 | |
| 334 | err_handler = dev->driver->err_handler; |
| 335 | vote = err_handler->slot_reset(dev); |
| 336 | result_data->result = merge_result(result_data->result, vote); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 337 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 338 | } |
| 339 | |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 340 | static int report_resume(struct pci_dev *dev, void *data) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 341 | { |
| 342 | struct pci_error_handlers *err_handler; |
| 343 | |
| 344 | dev->error_state = pci_channel_io_normal; |
| 345 | |
| 346 | if (!dev->driver || |
| 347 | !dev->driver->err_handler || |
Hidetoshi Seto | b0b801d | 2008-12-01 16:31:06 +0900 | [diff] [blame] | 348 | !dev->driver->err_handler->resume) |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 349 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 350 | |
| 351 | err_handler = dev->driver->err_handler; |
| 352 | err_handler->resume(dev); |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 353 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * broadcast_error_message - handle message broadcast to downstream drivers |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 358 | * @dev: pointer to from where in a hierarchy message is broadcasted down |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 359 | * @state: error state |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 360 | * @error_mesg: message to print |
| 361 | * @cb: callback to be broadcasted |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 362 | * |
| 363 | * Invoked during error recovery process. Once being invoked, the content |
| 364 | * of error severity will be broadcasted to all downstream drivers in a |
| 365 | * hierarchy in question. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 366 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 367 | static pci_ers_result_t broadcast_error_message(struct pci_dev *dev, |
| 368 | enum pci_channel_state state, |
| 369 | char *error_mesg, |
Zhang, Yanmin | 70298c6 | 2009-06-16 13:34:38 +0800 | [diff] [blame] | 370 | int (*cb)(struct pci_dev *, void *)) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 371 | { |
| 372 | struct aer_broadcast_data result_data; |
| 373 | |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 374 | dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 375 | result_data.state = state; |
| 376 | if (cb == report_error_detected) |
| 377 | result_data.result = PCI_ERS_RESULT_CAN_RECOVER; |
| 378 | else |
| 379 | result_data.result = PCI_ERS_RESULT_RECOVERED; |
| 380 | |
| 381 | if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) { |
| 382 | /* |
| 383 | * If the error is reported by a bridge, we think this error |
| 384 | * is related to the downstream link of the bridge, so we |
| 385 | * do error recovery on all subordinates of the bridge instead |
| 386 | * of the bridge and clear the error status of the bridge. |
| 387 | */ |
| 388 | if (cb == report_error_detected) |
| 389 | dev->error_state = state; |
| 390 | pci_walk_bus(dev->subordinate, cb, &result_data); |
| 391 | if (cb == report_resume) { |
| 392 | pci_cleanup_aer_uncorrect_error_status(dev); |
| 393 | dev->error_state = pci_channel_io_normal; |
| 394 | } |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 395 | } else { |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 396 | /* |
| 397 | * If the error is reported by an end point, we think this |
| 398 | * error is related to the upstream link of the end point. |
| 399 | */ |
| 400 | pci_walk_bus(dev->bus, cb, &result_data); |
| 401 | } |
| 402 | |
| 403 | return result_data.result; |
| 404 | } |
| 405 | |
| 406 | struct find_aer_service_data { |
| 407 | struct pcie_port_service_driver *aer_driver; |
| 408 | int is_downstream; |
| 409 | }; |
| 410 | |
| 411 | static int find_aer_service_iter(struct device *device, void *data) |
| 412 | { |
| 413 | struct device_driver *driver; |
| 414 | struct pcie_port_service_driver *service_driver; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 415 | struct find_aer_service_data *result; |
| 416 | |
| 417 | result = (struct find_aer_service_data *) data; |
| 418 | |
| 419 | if (device->bus == &pcie_port_bus_type) { |
Kenji Kaneshige | 694f88e | 2009-11-25 21:06:15 +0900 | [diff] [blame] | 420 | struct pcie_device *pcie = to_pcie_device(device); |
Rafael J. Wysocki | 2210636 | 2009-01-13 14:46:46 +0100 | [diff] [blame] | 421 | |
Kenji Kaneshige | 694f88e | 2009-11-25 21:06:15 +0900 | [diff] [blame] | 422 | if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 423 | result->is_downstream = 1; |
| 424 | |
| 425 | driver = device->driver; |
| 426 | if (driver) { |
| 427 | service_driver = to_service_driver(driver); |
Rafael J. Wysocki | 2210636 | 2009-01-13 14:46:46 +0100 | [diff] [blame] | 428 | if (service_driver->service == PCIE_PORT_SERVICE_AER) { |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 429 | result->aer_driver = service_driver; |
| 430 | return 1; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static void find_aer_service(struct pci_dev *dev, |
| 439 | struct find_aer_service_data *data) |
| 440 | { |
Greg Kroah-Hartman | b19441a | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 441 | int retval; |
| 442 | retval = device_for_each_child(&dev->dev, data, find_aer_service_iter); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static pci_ers_result_t reset_link(struct pcie_device *aerdev, |
| 446 | struct pci_dev *dev) |
| 447 | { |
| 448 | struct pci_dev *udev; |
| 449 | pci_ers_result_t status; |
| 450 | struct find_aer_service_data data; |
| 451 | |
| 452 | if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) |
| 453 | udev = dev; |
| 454 | else |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 455 | udev = dev->bus->self; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 456 | |
| 457 | data.is_downstream = 0; |
| 458 | data.aer_driver = NULL; |
| 459 | find_aer_service(udev, &data); |
| 460 | |
| 461 | /* |
| 462 | * Use the aer driver of the error agent firstly. |
| 463 | * If it hasn't the aer driver, use the root port's |
| 464 | */ |
| 465 | if (!data.aer_driver || !data.aer_driver->reset_link) { |
| 466 | if (data.is_downstream && |
| 467 | aerdev->device.driver && |
| 468 | to_service_driver(aerdev->device.driver)->reset_link) { |
| 469 | data.aer_driver = |
| 470 | to_service_driver(aerdev->device.driver); |
| 471 | } else { |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 472 | dev_printk(KERN_DEBUG, &dev->dev, "no link-reset " |
| 473 | "support\n"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 474 | return PCI_ERS_RESULT_DISCONNECT; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | status = data.aer_driver->reset_link(udev); |
| 479 | if (status != PCI_ERS_RESULT_RECOVERED) { |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 480 | dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream " |
| 481 | "device %s failed\n", pci_name(udev)); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 482 | return PCI_ERS_RESULT_DISCONNECT; |
| 483 | } |
| 484 | |
| 485 | return status; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * do_recovery - handle nonfatal/fatal error recovery process |
| 490 | * @aerdev: pointer to a pcie_device data structure of root port |
| 491 | * @dev: pointer to a pci_dev data structure of agent detecting an error |
| 492 | * @severity: error severity type |
| 493 | * |
| 494 | * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast |
| 495 | * error detected message to all downstream drivers within a hierarchy in |
| 496 | * question and return the returned code. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 497 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 498 | static pci_ers_result_t do_recovery(struct pcie_device *aerdev, |
| 499 | struct pci_dev *dev, |
| 500 | int severity) |
| 501 | { |
| 502 | pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED; |
| 503 | enum pci_channel_state state; |
| 504 | |
| 505 | if (severity == AER_FATAL) |
| 506 | state = pci_channel_io_frozen; |
| 507 | else |
| 508 | state = pci_channel_io_normal; |
| 509 | |
| 510 | status = broadcast_error_message(dev, |
| 511 | state, |
| 512 | "error_detected", |
| 513 | report_error_detected); |
| 514 | |
| 515 | if (severity == AER_FATAL) { |
| 516 | result = reset_link(aerdev, dev); |
| 517 | if (result != PCI_ERS_RESULT_RECOVERED) { |
| 518 | /* TODO: Should panic here? */ |
| 519 | return result; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (status == PCI_ERS_RESULT_CAN_RECOVER) |
| 524 | status = broadcast_error_message(dev, |
| 525 | state, |
| 526 | "mmio_enabled", |
| 527 | report_mmio_enabled); |
| 528 | |
| 529 | if (status == PCI_ERS_RESULT_NEED_RESET) { |
| 530 | /* |
| 531 | * TODO: Should call platform-specific |
| 532 | * functions to reset slot before calling |
| 533 | * drivers' slot_reset callbacks? |
| 534 | */ |
| 535 | status = broadcast_error_message(dev, |
| 536 | state, |
| 537 | "slot_reset", |
| 538 | report_slot_reset); |
| 539 | } |
| 540 | |
| 541 | if (status == PCI_ERS_RESULT_RECOVERED) |
| 542 | broadcast_error_message(dev, |
| 543 | state, |
| 544 | "resume", |
| 545 | report_resume); |
| 546 | |
| 547 | return status; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * handle_error_source - handle logging error into an event log |
| 552 | * @aerdev: pointer to pcie_device data structure of the root port |
| 553 | * @dev: pointer to pci_dev data structure of error source device |
| 554 | * @info: comprehensive error information |
| 555 | * |
| 556 | * Invoked when an error being detected by Root Port. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 557 | */ |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 558 | static void handle_error_source(struct pcie_device *aerdev, |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 559 | struct pci_dev *dev, |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 560 | struct aer_err_info *info) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 561 | { |
| 562 | pci_ers_result_t status = 0; |
| 563 | int pos; |
| 564 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 565 | if (info->severity == AER_CORRECTABLE) { |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 566 | /* |
| 567 | * Correctable error does not need software intevention. |
| 568 | * No need to go through error recovery process. |
| 569 | */ |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 570 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 571 | if (pos) |
| 572 | pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 573 | info->status); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 574 | } else { |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 575 | status = do_recovery(aerdev, dev, info->severity); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 576 | if (status == PCI_ERS_RESULT_RECOVERED) { |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 577 | dev_printk(KERN_DEBUG, &dev->dev, "AER driver " |
| 578 | "successfully recovered\n"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 579 | } else { |
| 580 | /* TODO: Should kernel panic here? */ |
Bjorn Helgaas | 531f254 | 2008-06-13 10:52:12 -0600 | [diff] [blame] | 581 | dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't " |
| 582 | "recover\n"); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * aer_enable_rootport - enable Root Port's interrupts when receiving messages |
| 589 | * @rpc: pointer to a Root Port data structure |
| 590 | * |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 591 | * Invoked when PCIe bus loads AER service driver. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 592 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 593 | void aer_enable_rootport(struct aer_rpc *rpc) |
| 594 | { |
| 595 | struct pci_dev *pdev = rpc->rpd->port; |
| 596 | int pos, aer_pos; |
| 597 | u16 reg16; |
| 598 | u32 reg32; |
| 599 | |
Kenji Kaneshige | 39a5306 | 2009-11-11 14:31:38 +0900 | [diff] [blame] | 600 | pos = pci_pcie_cap(pdev); |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 601 | /* Clear PCIe Capability's Device Status */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 602 | pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, ®16); |
| 603 | pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16); |
| 604 | |
| 605 | /* Disable system error generation in response to error messages */ |
| 606 | pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, ®16); |
| 607 | reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK); |
| 608 | pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16); |
| 609 | |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 610 | aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 611 | /* Clear error status */ |
| 612 | pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, ®32); |
| 613 | pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32); |
| 614 | pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, ®32); |
| 615 | pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32); |
| 616 | pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, ®32); |
| 617 | pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32); |
| 618 | |
Andrew Patterson | 1f9f13c | 2009-02-20 16:04:59 -0700 | [diff] [blame] | 619 | /* |
| 620 | * Enable error reporting for the root port device and downstream port |
| 621 | * devices. |
| 622 | */ |
| 623 | set_downstream_devices_error_reporting(pdev, true); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 624 | |
| 625 | /* Enable Root Port's interrupt in response to error messages */ |
Hidetoshi Seto | c6d34ed | 2010-04-15 13:09:13 +0900 | [diff] [blame^] | 626 | pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, ®32); |
| 627 | reg32 |= ROOT_PORT_INTR_ON_MESG_MASK; |
| 628 | pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
| 632 | * disable_root_aer - disable Root Port's interrupts when receiving messages |
| 633 | * @rpc: pointer to a Root Port data structure |
| 634 | * |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 635 | * Invoked when PCIe bus unloads AER service driver. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 636 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 637 | static void disable_root_aer(struct aer_rpc *rpc) |
| 638 | { |
| 639 | struct pci_dev *pdev = rpc->rpd->port; |
| 640 | u32 reg32; |
| 641 | int pos; |
| 642 | |
Andrew Patterson | 1f9f13c | 2009-02-20 16:04:59 -0700 | [diff] [blame] | 643 | /* |
| 644 | * Disable error reporting for the root port device and downstream port |
| 645 | * devices. |
| 646 | */ |
| 647 | set_downstream_devices_error_reporting(pdev, false); |
| 648 | |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 649 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 650 | /* Disable Root's interrupt in response to error messages */ |
Hidetoshi Seto | c6d34ed | 2010-04-15 13:09:13 +0900 | [diff] [blame^] | 651 | pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, ®32); |
| 652 | reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK; |
| 653 | pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 654 | |
| 655 | /* Clear Root's error status reg */ |
| 656 | pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32); |
| 657 | pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * get_e_source - retrieve an error source |
| 662 | * @rpc: pointer to the root port which holds an error |
| 663 | * |
| 664 | * Invoked by DPC handler to consume an error. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 665 | */ |
Hidetoshi Seto | c9a9188 | 2009-09-07 17:07:29 +0900 | [diff] [blame] | 666 | static struct aer_err_source *get_e_source(struct aer_rpc *rpc) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 667 | { |
| 668 | struct aer_err_source *e_source; |
| 669 | unsigned long flags; |
| 670 | |
| 671 | /* Lock access to Root error producer/consumer index */ |
| 672 | spin_lock_irqsave(&rpc->e_lock, flags); |
| 673 | if (rpc->prod_idx == rpc->cons_idx) { |
| 674 | spin_unlock_irqrestore(&rpc->e_lock, flags); |
| 675 | return NULL; |
| 676 | } |
| 677 | e_source = &rpc->e_sources[rpc->cons_idx]; |
| 678 | rpc->cons_idx++; |
| 679 | if (rpc->cons_idx == AER_ERROR_SOURCES_MAX) |
| 680 | rpc->cons_idx = 0; |
| 681 | spin_unlock_irqrestore(&rpc->e_lock, flags); |
| 682 | |
| 683 | return e_source; |
| 684 | } |
| 685 | |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 686 | /** |
| 687 | * get_device_error_info - read error status from dev and store it to info |
| 688 | * @dev: pointer to the device expected to have a error record |
| 689 | * @info: pointer to structure to store the error record |
| 690 | * |
| 691 | * Return 1 on success, 0 on error. |
| 692 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 693 | static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info) |
| 694 | { |
Hidetoshi Seto | e7a0d92 | 2009-09-07 17:13:42 +0900 | [diff] [blame] | 695 | int pos, temp; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 696 | |
Hidetoshi Seto | 1b4ffcf | 2009-09-07 17:09:58 +0900 | [diff] [blame] | 697 | info->status = 0; |
Hidetoshi Seto | 273024d | 2009-09-07 17:16:20 +0900 | [diff] [blame] | 698 | info->tlp_header_valid = 0; |
Hidetoshi Seto | 1b4ffcf | 2009-09-07 17:09:58 +0900 | [diff] [blame] | 699 | |
Jesse Barnes | 0927678 | 2008-10-18 17:33:19 -0700 | [diff] [blame] | 700 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 701 | |
| 702 | /* The device might not support AER */ |
| 703 | if (!pos) |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 704 | return 1; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 705 | |
| 706 | if (info->severity == AER_CORRECTABLE) { |
| 707 | pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, |
| 708 | &info->status); |
Hidetoshi Seto | 0d90c3a | 2009-09-07 17:12:25 +0900 | [diff] [blame] | 709 | pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, |
| 710 | &info->mask); |
| 711 | if (!(info->status & ~info->mask)) |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 712 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 713 | } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE || |
| 714 | info->severity == AER_NONFATAL) { |
| 715 | |
| 716 | /* Link is still healthy for IO reads */ |
| 717 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, |
| 718 | &info->status); |
Hidetoshi Seto | 0d90c3a | 2009-09-07 17:12:25 +0900 | [diff] [blame] | 719 | pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, |
| 720 | &info->mask); |
| 721 | if (!(info->status & ~info->mask)) |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 722 | return 0; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 723 | |
Hidetoshi Seto | e7a0d92 | 2009-09-07 17:13:42 +0900 | [diff] [blame] | 724 | /* Get First Error Pointer */ |
| 725 | pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp); |
Hidetoshi Seto | 273024d | 2009-09-07 17:16:20 +0900 | [diff] [blame] | 726 | info->first_error = PCI_ERR_CAP_FEP(temp); |
Hidetoshi Seto | e7a0d92 | 2009-09-07 17:13:42 +0900 | [diff] [blame] | 727 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 728 | if (info->status & AER_LOG_TLP_MASKS) { |
Hidetoshi Seto | 273024d | 2009-09-07 17:16:20 +0900 | [diff] [blame] | 729 | info->tlp_header_valid = 1; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 730 | pci_read_config_dword(dev, |
| 731 | pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0); |
| 732 | pci_read_config_dword(dev, |
| 733 | pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1); |
| 734 | pci_read_config_dword(dev, |
| 735 | pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2); |
| 736 | pci_read_config_dword(dev, |
| 737 | pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3); |
| 738 | } |
| 739 | } |
| 740 | |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 741 | return 1; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 742 | } |
| 743 | |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 744 | static inline void aer_process_err_devices(struct pcie_device *p_device, |
| 745 | struct aer_err_info *e_info) |
| 746 | { |
| 747 | int i; |
| 748 | |
| 749 | if (!e_info->dev[0]) { |
| 750 | dev_printk(KERN_DEBUG, &p_device->port->dev, |
| 751 | "can't find device of ID%04x\n", |
| 752 | e_info->id); |
| 753 | } |
| 754 | |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 755 | /* Report all before handle them, not to lost records by reset etc. */ |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 756 | for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) { |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 757 | if (get_device_error_info(e_info->dev[i], e_info)) |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 758 | aer_print_error(e_info->dev[i], e_info); |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 759 | } |
| 760 | for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) { |
| 761 | if (get_device_error_info(e_info->dev[i], e_info)) |
| 762 | handle_error_source(p_device, e_info->dev[i], e_info); |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 766 | /** |
| 767 | * aer_isr_one_error - consume an error detected by root port |
| 768 | * @p_device: pointer to error root port service device |
| 769 | * @e_src: pointer to an error source |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 770 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 771 | static void aer_isr_one_error(struct pcie_device *p_device, |
| 772 | struct aer_err_source *e_src) |
| 773 | { |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 774 | struct aer_err_info *e_info; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 775 | int i; |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 776 | |
| 777 | /* struct aer_err_info might be big, so we allocate it with slab */ |
| 778 | e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL); |
| 779 | if (e_info == NULL) { |
| 780 | dev_printk(KERN_DEBUG, &p_device->port->dev, |
| 781 | "Can't allocate mem when processing AER errors\n"); |
| 782 | return; |
| 783 | } |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 784 | |
| 785 | /* |
| 786 | * There is a possibility that both correctable error and |
| 787 | * uncorrectable error being logged. Report correctable error first. |
| 788 | */ |
| 789 | for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) { |
| 790 | if (i > 4) |
| 791 | break; |
| 792 | if (!(e_src->status & i)) |
| 793 | continue; |
| 794 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 795 | memset(e_info, 0, sizeof(struct aer_err_info)); |
| 796 | |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 797 | /* Init comprehensive error information */ |
| 798 | if (i & PCI_ERR_ROOT_COR_RCV) { |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 799 | e_info->id = ERR_COR_ID(e_src->id); |
| 800 | e_info->severity = AER_CORRECTABLE; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 801 | } else { |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 802 | e_info->id = ERR_UNCOR_ID(e_src->id); |
| 803 | e_info->severity = ((e_src->status >> 6) & 1); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 804 | } |
| 805 | if (e_src->status & |
| 806 | (PCI_ERR_ROOT_MULTI_COR_RCV | |
| 807 | PCI_ERR_ROOT_MULTI_UNCOR_RCV)) |
Hidetoshi Seto | 273024d | 2009-09-07 17:16:20 +0900 | [diff] [blame] | 808 | e_info->multi_error_valid = 1; |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 809 | |
Hidetoshi Seto | 79e4b89 | 2009-09-07 17:16:45 +0900 | [diff] [blame] | 810 | aer_print_port_info(p_device->port, e_info); |
| 811 | |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 812 | find_source_device(p_device->port, e_info); |
Zhang, Yanmin | 3d5505c | 2009-06-16 13:35:16 +0800 | [diff] [blame] | 813 | aer_process_err_devices(p_device, e_info); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 814 | } |
Zhang, Yanmin | 28eb27c | 2009-06-16 13:35:11 +0800 | [diff] [blame] | 815 | |
| 816 | kfree(e_info); |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /** |
| 820 | * aer_isr - consume errors detected by root port |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 821 | * @work: definition of this work item |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 822 | * |
| 823 | * Invoked, as DPC, when root port records new detected error |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 824 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 825 | void aer_isr(struct work_struct *work) |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 826 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 827 | struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler); |
| 828 | struct pcie_device *p_device = rpc->rpd; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 829 | struct aer_err_source *e_src; |
| 830 | |
| 831 | mutex_lock(&rpc->rpc_mutex); |
| 832 | e_src = get_e_source(rpc); |
| 833 | while (e_src) { |
| 834 | aer_isr_one_error(p_device, e_src); |
| 835 | e_src = get_e_source(rpc); |
| 836 | } |
| 837 | mutex_unlock(&rpc->rpc_mutex); |
| 838 | |
| 839 | wake_up(&rpc->wait_release); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * aer_delete_rootport - disable root port aer and delete service data |
| 844 | * @rpc: pointer to a root port device being deleted |
| 845 | * |
| 846 | * Invoked when AER service unloaded on a specific Root Port |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 847 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 848 | void aer_delete_rootport(struct aer_rpc *rpc) |
| 849 | { |
| 850 | /* Disable root port AER itself */ |
| 851 | disable_root_aer(rpc); |
| 852 | |
| 853 | kfree(rpc); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * aer_init - provide AER initialization |
| 858 | * @dev: pointer to AER pcie device |
| 859 | * |
| 860 | * Invoked when AER service driver is loaded. |
Randy Dunlap | d885c6b | 2007-11-28 09:04:23 -0800 | [diff] [blame] | 861 | */ |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 862 | int aer_init(struct pcie_device *dev) |
| 863 | { |
Matt Domsch | 0584396 | 2009-11-02 11:51:24 -0600 | [diff] [blame] | 864 | if (dev->port->aer_firmware_first) { |
| 865 | dev_printk(KERN_DEBUG, &dev->device, |
| 866 | "PCIe errors handled by platform firmware.\n"); |
| 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | if (aer_osc_setup(dev)) |
| 871 | goto out; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 872 | |
Hidetoshi Seto | b1c089b | 2009-09-07 17:16:59 +0900 | [diff] [blame] | 873 | return 0; |
Matt Domsch | 0584396 | 2009-11-02 11:51:24 -0600 | [diff] [blame] | 874 | out: |
| 875 | if (forceload) { |
| 876 | dev_printk(KERN_DEBUG, &dev->device, |
| 877 | "aerdrv forceload requested.\n"); |
| 878 | dev->port->aer_firmware_first = 0; |
| 879 | return 0; |
| 880 | } |
| 881 | return -ENXIO; |
Zhang, Yanmin | 6c2b374 | 2006-07-31 15:21:33 +0800 | [diff] [blame] | 882 | } |