| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* This file implements the error recovery as a core part of PCIe error |
| 4 |
* reporting. When a PCIe error is delivered, an error message will be |
| 5 |
* collected and printed to console, then, an error recovery procedure |
| 6 |
* will be executed by following the PCI error recovery rules. |
| 7 |
* |
| 8 |
* Copyright (C) 2006 Intel Corp. |
| 9 |
* Tom Long Nguyen ([email protected]) |
| 10 |
* Zhang Yanmin ([email protected]) |
| 11 |
*/ |
| 12 |
|
| 13 |
#define dev_fmt(fmt) "AER: " fmt |
| 14 |
|
| 15 |
#include <linux/pci.h> |
| 16 |
#include <linux/pm_runtime.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include <linux/kernel.h> |
| 19 |
#include <linux/errno.h> |
| 20 |
#include <linux/aer.h> |
| 21 |
#include "portdrv.h" |
| 22 |
#include "../pci.h" |
| 23 |
|
| 24 |
static pci_ers_result_t merge_result(enum pci_ers_result orig, |
| 25 |
enum pci_ers_result new) |
| 26 |
{ |
| 27 |
if (new == PCI_ERS_RESULT_NO_AER_DRIVER) |
| 28 |
return PCI_ERS_RESULT_NO_AER_DRIVER; |
| 29 |
|
| 30 |
if (new == PCI_ERS_RESULT_NONE) |
| 31 |
return orig; |
| 32 |
|
| 33 |
switch (orig) { |
| 34 |
case PCI_ERS_RESULT_CAN_RECOVER: |
| 35 |
case PCI_ERS_RESULT_RECOVERED: |
| 36 |
orig = new; |
| 37 |
break; |
| 38 |
case PCI_ERS_RESULT_DISCONNECT: |
| 39 |
if (new == PCI_ERS_RESULT_NEED_RESET) |
| 40 |
orig = PCI_ERS_RESULT_NEED_RESET; |
| 41 |
break; |
| 42 |
default: |
| 43 |
break; |
| 44 |
} |
| 45 |
|
| 46 |
return orig; |
| 47 |
} |
| 48 |
|
| 49 |
static int report_error_detected(struct pci_dev *dev, |
| 50 |
pci_channel_state_t state, |
| 51 |
enum pci_ers_result *result) |
| 52 |
{ |
| 53 |
struct pci_driver *pdrv; |
| 54 |
pci_ers_result_t vote; |
| 55 |
const struct pci_error_handlers *err_handler; |
| 56 |
|
| 57 |
device_lock(&dev->dev); |
| 58 |
pdrv = dev->driver; |
| 59 |
if (pci_dev_is_disconnected(dev)) { |
| 60 |
vote = PCI_ERS_RESULT_DISCONNECT; |
| 61 |
} else if (!pci_dev_set_io_state(dev, state)) { |
| 62 |
pci_info(dev, "can't recover (state transition %u -> %u invalid)\n", |
| 63 |
dev->error_state, state); |
| 64 |
vote = PCI_ERS_RESULT_NONE; |
| 65 |
} else if (!pdrv || !pdrv->err_handler || |
| 66 |
!pdrv->err_handler->error_detected) { |
| 67 |
/* |
| 68 |
* If any device in the subtree does not have an error_detected |
| 69 |
* callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent |
| 70 |
* error callbacks of "any" device in the subtree, and will |
| 71 |
* exit in the disconnected error state. |
| 72 |
*/ |
| 73 |
if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { |
| 74 |
vote = PCI_ERS_RESULT_NO_AER_DRIVER; |
| 75 |
pci_info(dev, "can't recover (no error_detected callback)\n"); |
| 76 |
} else { |
| 77 |
vote = PCI_ERS_RESULT_NONE; |
| 78 |
} |
| 79 |
} else { |
| 80 |
err_handler = pdrv->err_handler; |
| 81 |
vote = err_handler->error_detected(dev, state); |
| 82 |
} |
| 83 |
pci_uevent_ers(dev, vote); |
| 84 |
*result = merge_result(*result, vote); |
| 85 |
device_unlock(&dev->dev); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static int pci_pm_runtime_get_sync(struct pci_dev *pdev, void *data) |
| 90 |
{ |
| 91 |
pm_runtime_get_sync(&pdev->dev); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static int pci_pm_runtime_put(struct pci_dev *pdev, void *data) |
| 96 |
{ |
| 97 |
pm_runtime_put(&pdev->dev); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
static int report_frozen_detected(struct pci_dev *dev, void *data) |
| 102 |
{ |
| 103 |
return report_error_detected(dev, pci_channel_io_frozen, data); |
| 104 |
} |
| 105 |
|
| 106 |
static int report_normal_detected(struct pci_dev *dev, void *data) |
| 107 |
{ |
| 108 |
return report_error_detected(dev, pci_channel_io_normal, data); |
| 109 |
} |
| 110 |
|
| 111 |
static int report_perm_failure_detected(struct pci_dev *dev, void *data) |
| 112 |
{ |
| 113 |
struct pci_driver *pdrv; |
| 114 |
const struct pci_error_handlers *err_handler; |
| 115 |
|
| 116 |
device_lock(&dev->dev); |
| 117 |
pdrv = dev->driver; |
| 118 |
if (!pdrv || !pdrv->err_handler || !pdrv->err_handler->error_detected) |
| 119 |
goto out; |
| 120 |
|
| 121 |
err_handler = pdrv->err_handler; |
| 122 |
err_handler->error_detected(dev, pci_channel_io_perm_failure); |
| 123 |
out: |
| 124 |
pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT); |
| 125 |
device_unlock(&dev->dev); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static int report_mmio_enabled(struct pci_dev *dev, void *data) |
| 130 |
{ |
| 131 |
struct pci_driver *pdrv; |
| 132 |
pci_ers_result_t vote, *result = data; |
| 133 |
const struct pci_error_handlers *err_handler; |
| 134 |
|
| 135 |
device_lock(&dev->dev); |
| 136 |
pdrv = dev->driver; |
| 137 |
if (!pdrv || !pdrv->err_handler || !pdrv->err_handler->mmio_enabled) |
| 138 |
goto out; |
| 139 |
|
| 140 |
err_handler = pdrv->err_handler; |
| 141 |
vote = err_handler->mmio_enabled(dev); |
| 142 |
*result = merge_result(*result, vote); |
| 143 |
out: |
| 144 |
device_unlock(&dev->dev); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
static int report_slot_reset(struct pci_dev *dev, void *data) |
| 149 |
{ |
| 150 |
struct pci_driver *pdrv; |
| 151 |
pci_ers_result_t vote, *result = data; |
| 152 |
const struct pci_error_handlers *err_handler; |
| 153 |
|
| 154 |
device_lock(&dev->dev); |
| 155 |
pdrv = dev->driver; |
| 156 |
if (!pci_dev_set_io_state(dev, pci_channel_io_normal) || |
| 157 |
!pdrv || !pdrv->err_handler || !pdrv->err_handler->slot_reset) |
| 158 |
goto out; |
| 159 |
|
| 160 |
err_handler = pdrv->err_handler; |
| 161 |
vote = err_handler->slot_reset(dev); |
| 162 |
*result = merge_result(*result, vote); |
| 163 |
out: |
| 164 |
device_unlock(&dev->dev); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
static int report_resume(struct pci_dev *dev, void *data) |
| 169 |
{ |
| 170 |
struct pci_driver *pdrv; |
| 171 |
const struct pci_error_handlers *err_handler; |
| 172 |
|
| 173 |
device_lock(&dev->dev); |
| 174 |
pdrv = dev->driver; |
| 175 |
if (!pci_dev_set_io_state(dev, pci_channel_io_normal) || |
| 176 |
!pdrv || !pdrv->err_handler || !pdrv->err_handler->resume) |
| 177 |
goto out; |
| 178 |
|
| 179 |
err_handler = pdrv->err_handler; |
| 180 |
err_handler->resume(dev); |
| 181 |
out: |
| 182 |
pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED); |
| 183 |
device_unlock(&dev->dev); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* pci_walk_bridge - walk bridges potentially AER affected |
| 189 |
* @bridge: bridge which may be a Port, an RCEC, or an RCiEP |
| 190 |
* @cb: callback to be called for each device found |
| 191 |
* @userdata: arbitrary pointer to be passed to callback |
| 192 |
* |
| 193 |
* If the device provided is a bridge, walk the subordinate bus, including |
| 194 |
* any bridged devices on buses under this bus. Call the provided callback |
| 195 |
* on each device found. |
| 196 |
* |
| 197 |
* If the device provided has no subordinate bus, e.g., an RCEC or RCiEP, |
| 198 |
* call the callback on the device itself. |
| 199 |
*/ |
| 200 |
static void pci_walk_bridge(struct pci_dev *bridge, |
| 201 |
int (*cb)(struct pci_dev *, void *), |
| 202 |
void *userdata) |
| 203 |
{ |
| 204 |
if (bridge->subordinate) |
| 205 |
pci_walk_bus(bridge->subordinate, cb, userdata); |
| 206 |
else |
| 207 |
cb(bridge, userdata); |
| 208 |
} |
| 209 |
|
| 210 |
pci_ers_result_t pcie_do_recovery(struct pci_dev *dev, |
| 211 |
pci_channel_state_t state, |
| 212 |
pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev)) |
| 213 |
{ |
| 214 |
int type = pci_pcie_type(dev); |
| 215 |
struct pci_dev *bridge; |
| 216 |
pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER; |
| 217 |
struct pci_host_bridge *host = pci_find_host_bridge(dev->bus); |
| 218 |
|
| 219 |
/* |
| 220 |
* If the error was detected by a Root Port, Downstream Port, RCEC, |
| 221 |
* or RCiEP, recovery runs on the device itself. For Ports, that |
| 222 |
* also includes any subordinate devices. |
| 223 |
* |
| 224 |
* If it was detected by another device (Endpoint, etc), recovery |
| 225 |
* runs on the device and anything else under the same Port, i.e., |
| 226 |
* everything under "bridge". |
| 227 |
*/ |
| 228 |
if (type == PCI_EXP_TYPE_ROOT_PORT || |
| 229 |
type == PCI_EXP_TYPE_DOWNSTREAM || |
| 230 |
type == PCI_EXP_TYPE_RC_EC || |
| 231 |
type == PCI_EXP_TYPE_RC_END) |
| 232 |
bridge = dev; |
| 233 |
else |
| 234 |
bridge = pci_upstream_bridge(dev); |
| 235 |
|
| 236 |
pci_walk_bridge(bridge, pci_pm_runtime_get_sync, NULL); |
| 237 |
|
| 238 |
pci_dbg(bridge, "broadcast error_detected message\n"); |
| 239 |
if (state == pci_channel_io_frozen) |
| 240 |
pci_walk_bridge(bridge, report_frozen_detected, &status); |
| 241 |
else |
| 242 |
pci_walk_bridge(bridge, report_normal_detected, &status); |
| 243 |
|
| 244 |
if (status == PCI_ERS_RESULT_CAN_RECOVER) { |
| 245 |
status = PCI_ERS_RESULT_RECOVERED; |
| 246 |
pci_dbg(bridge, "broadcast mmio_enabled message\n"); |
| 247 |
pci_walk_bridge(bridge, report_mmio_enabled, &status); |
| 248 |
} |
| 249 |
|
| 250 |
if (status == PCI_ERS_RESULT_NEED_RESET || |
| 251 |
state == pci_channel_io_frozen) { |
| 252 |
if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) { |
| 253 |
pci_warn(bridge, "subordinate device reset failed\n"); |
| 254 |
goto failed; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if (status == PCI_ERS_RESULT_NEED_RESET) { |
| 259 |
/* |
| 260 |
* TODO: Should call platform-specific |
| 261 |
* functions to reset slot before calling |
| 262 |
* drivers' slot_reset callbacks? |
| 263 |
*/ |
| 264 |
status = PCI_ERS_RESULT_RECOVERED; |
| 265 |
pci_dbg(bridge, "broadcast slot_reset message\n"); |
| 266 |
pci_walk_bridge(bridge, report_slot_reset, &status); |
| 267 |
} |
| 268 |
|
| 269 |
if (status != PCI_ERS_RESULT_RECOVERED) |
| 270 |
goto failed; |
| 271 |
|
| 272 |
pci_dbg(bridge, "broadcast resume message\n"); |
| 273 |
pci_walk_bridge(bridge, report_resume, &status); |
| 274 |
|
| 275 |
/* |
| 276 |
* If we have native control of AER, clear error status in the device |
| 277 |
* that detected the error. If the platform retained control of AER, |
| 278 |
* it is responsible for clearing this status. In that case, the |
| 279 |
* signaling device may not even be visible to the OS. |
| 280 |
*/ |
| 281 |
if (host->native_aer || pcie_ports_native) { |
| 282 |
pcie_clear_device_status(dev); |
| 283 |
pci_aer_clear_nonfatal_status(dev); |
| 284 |
} |
| 285 |
|
| 286 |
pci_walk_bridge(bridge, pci_pm_runtime_put, NULL); |
| 287 |
|
| 288 |
pci_info(bridge, "device recovery successful\n"); |
| 289 |
return status; |
| 290 |
|
| 291 |
failed: |
| 292 |
pci_walk_bridge(bridge, pci_pm_runtime_put, NULL); |
| 293 |
|
| 294 |
pci_walk_bridge(bridge, report_perm_failure_detected, NULL); |
| 295 |
|
| 296 |
pci_info(bridge, "device recovery failed\n"); |
| 297 |
|
| 298 |
return status; |
| 299 |
} |