12e28bc84SOza Pawandeep // SPDX-License-Identifier: GPL-2.0 22e28bc84SOza Pawandeep /* 32e28bc84SOza Pawandeep * This file implements the error recovery as a core part of PCIe error 42e28bc84SOza Pawandeep * reporting. When a PCIe error is delivered, an error message will be 52e28bc84SOza Pawandeep * collected and printed to console, then, an error recovery procedure 62e28bc84SOza Pawandeep * will be executed by following the PCI error recovery rules. 72e28bc84SOza Pawandeep * 82e28bc84SOza Pawandeep * Copyright (C) 2006 Intel Corp. 92e28bc84SOza Pawandeep * Tom Long Nguyen (tom.l.nguyen@intel.com) 102e28bc84SOza Pawandeep * Zhang Yanmin (yanmin.zhang@intel.com) 112e28bc84SOza Pawandeep */ 122e28bc84SOza Pawandeep 132e28bc84SOza Pawandeep #include <linux/pci.h> 142e28bc84SOza Pawandeep #include <linux/module.h> 152e28bc84SOza Pawandeep #include <linux/pci.h> 162e28bc84SOza Pawandeep #include <linux/kernel.h> 172e28bc84SOza Pawandeep #include <linux/errno.h> 182e28bc84SOza Pawandeep #include <linux/aer.h> 192e28bc84SOza Pawandeep #include "portdrv.h" 202e28bc84SOza Pawandeep #include "../pci.h" 212e28bc84SOza Pawandeep 222e28bc84SOza Pawandeep static pci_ers_result_t merge_result(enum pci_ers_result orig, 232e28bc84SOza Pawandeep enum pci_ers_result new) 242e28bc84SOza Pawandeep { 252e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NO_AER_DRIVER) 262e28bc84SOza Pawandeep return PCI_ERS_RESULT_NO_AER_DRIVER; 272e28bc84SOza Pawandeep 282e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NONE) 292e28bc84SOza Pawandeep return orig; 302e28bc84SOza Pawandeep 312e28bc84SOza Pawandeep switch (orig) { 322e28bc84SOza Pawandeep case PCI_ERS_RESULT_CAN_RECOVER: 332e28bc84SOza Pawandeep case PCI_ERS_RESULT_RECOVERED: 342e28bc84SOza Pawandeep orig = new; 352e28bc84SOza Pawandeep break; 362e28bc84SOza Pawandeep case PCI_ERS_RESULT_DISCONNECT: 372e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NEED_RESET) 382e28bc84SOza Pawandeep orig = PCI_ERS_RESULT_NEED_RESET; 392e28bc84SOza Pawandeep break; 402e28bc84SOza Pawandeep default: 412e28bc84SOza Pawandeep break; 422e28bc84SOza Pawandeep } 432e28bc84SOza Pawandeep 442e28bc84SOza Pawandeep return orig; 452e28bc84SOza Pawandeep } 462e28bc84SOza Pawandeep 47*542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev, 48*542aeb9cSKeith Busch enum pci_channel_state state, 49*542aeb9cSKeith Busch enum pci_ers_result *result) 502e28bc84SOza Pawandeep { 512e28bc84SOza Pawandeep pci_ers_result_t vote; 522e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 532e28bc84SOza Pawandeep 542e28bc84SOza Pawandeep device_lock(&dev->dev); 55*542aeb9cSKeith Busch dev->error_state = state; 562e28bc84SOza Pawandeep 572e28bc84SOza Pawandeep if (!dev->driver || 582e28bc84SOza Pawandeep !dev->driver->err_handler || 592e28bc84SOza Pawandeep !dev->driver->err_handler->error_detected) { 602e28bc84SOza Pawandeep /* 61bfcb79fcSKeith Busch * If any device in the subtree does not have an error_detected 62bfcb79fcSKeith Busch * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent 63bfcb79fcSKeith Busch * error callbacks of "any" device in the subtree, and will 64bfcb79fcSKeith Busch * exit in the disconnected error state. 652e28bc84SOza Pawandeep */ 662e28bc84SOza Pawandeep if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) 672e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NO_AER_DRIVER; 682e28bc84SOza Pawandeep else 692e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NONE; 702e28bc84SOza Pawandeep } else { 712e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 72*542aeb9cSKeith Busch vote = err_handler->error_detected(dev, state); 732e28bc84SOza Pawandeep pci_uevent_ers(dev, PCI_ERS_RESULT_NONE); 742e28bc84SOza Pawandeep } 752e28bc84SOza Pawandeep 76*542aeb9cSKeith Busch *result = merge_result(*result, vote); 772e28bc84SOza Pawandeep device_unlock(&dev->dev); 782e28bc84SOza Pawandeep return 0; 792e28bc84SOza Pawandeep } 802e28bc84SOza Pawandeep 81*542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data) 82*542aeb9cSKeith Busch { 83*542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_frozen, data); 84*542aeb9cSKeith Busch } 85*542aeb9cSKeith Busch 86*542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data) 87*542aeb9cSKeith Busch { 88*542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_normal, data); 89*542aeb9cSKeith Busch } 90*542aeb9cSKeith Busch 912e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data) 922e28bc84SOza Pawandeep { 93*542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 942e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 952e28bc84SOza Pawandeep 962e28bc84SOza Pawandeep device_lock(&dev->dev); 972e28bc84SOza Pawandeep if (!dev->driver || 982e28bc84SOza Pawandeep !dev->driver->err_handler || 992e28bc84SOza Pawandeep !dev->driver->err_handler->mmio_enabled) 1002e28bc84SOza Pawandeep goto out; 1012e28bc84SOza Pawandeep 1022e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1032e28bc84SOza Pawandeep vote = err_handler->mmio_enabled(dev); 104*542aeb9cSKeith Busch *result = merge_result(*result, vote); 1052e28bc84SOza Pawandeep out: 1062e28bc84SOza Pawandeep device_unlock(&dev->dev); 1072e28bc84SOza Pawandeep return 0; 1082e28bc84SOza Pawandeep } 1092e28bc84SOza Pawandeep 1102e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data) 1112e28bc84SOza Pawandeep { 112*542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 1132e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1142e28bc84SOza Pawandeep 1152e28bc84SOza Pawandeep device_lock(&dev->dev); 1162e28bc84SOza Pawandeep if (!dev->driver || 1172e28bc84SOza Pawandeep !dev->driver->err_handler || 1182e28bc84SOza Pawandeep !dev->driver->err_handler->slot_reset) 1192e28bc84SOza Pawandeep goto out; 1202e28bc84SOza Pawandeep 1212e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1222e28bc84SOza Pawandeep vote = err_handler->slot_reset(dev); 123*542aeb9cSKeith Busch *result = merge_result(*result, vote); 1242e28bc84SOza Pawandeep out: 1252e28bc84SOza Pawandeep device_unlock(&dev->dev); 1262e28bc84SOza Pawandeep return 0; 1272e28bc84SOza Pawandeep } 1282e28bc84SOza Pawandeep 1292e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data) 1302e28bc84SOza Pawandeep { 1312e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1322e28bc84SOza Pawandeep 1332e28bc84SOza Pawandeep device_lock(&dev->dev); 1342e28bc84SOza Pawandeep dev->error_state = pci_channel_io_normal; 1352e28bc84SOza Pawandeep 1362e28bc84SOza Pawandeep if (!dev->driver || 1372e28bc84SOza Pawandeep !dev->driver->err_handler || 1382e28bc84SOza Pawandeep !dev->driver->err_handler->resume) 1392e28bc84SOza Pawandeep goto out; 1402e28bc84SOza Pawandeep 1412e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1422e28bc84SOza Pawandeep err_handler->resume(dev); 1432e28bc84SOza Pawandeep pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED); 1442e28bc84SOza Pawandeep out: 1452e28bc84SOza Pawandeep device_unlock(&dev->dev); 1462e28bc84SOza Pawandeep return 0; 1472e28bc84SOza Pawandeep } 1482e28bc84SOza Pawandeep 1492e28bc84SOza Pawandeep /** 1502e28bc84SOza Pawandeep * default_reset_link - default reset function 1512e28bc84SOza Pawandeep * @dev: pointer to pci_dev data structure 1522e28bc84SOza Pawandeep * 1532e28bc84SOza Pawandeep * Invoked when performing link reset on a Downstream Port or a 1542e28bc84SOza Pawandeep * Root Port with no aer driver. 1552e28bc84SOza Pawandeep */ 1562e28bc84SOza Pawandeep static pci_ers_result_t default_reset_link(struct pci_dev *dev) 1572e28bc84SOza Pawandeep { 15818426238SSinan Kaya int rc; 15918426238SSinan Kaya 160c4eed62aSKeith Busch rc = pci_bus_error_reset(dev); 1612e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "downstream link has been reset\n"); 16218426238SSinan Kaya return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 1632e28bc84SOza Pawandeep } 1642e28bc84SOza Pawandeep 1650b91439dSOza Pawandeep static pci_ers_result_t reset_link(struct pci_dev *dev, u32 service) 1662e28bc84SOza Pawandeep { 1672e28bc84SOza Pawandeep pci_ers_result_t status; 1682e28bc84SOza Pawandeep struct pcie_port_service_driver *driver = NULL; 1692e28bc84SOza Pawandeep 170bfcb79fcSKeith Busch driver = pcie_port_find_service(dev, service); 1712e28bc84SOza Pawandeep if (driver && driver->reset_link) { 172bfcb79fcSKeith Busch status = driver->reset_link(dev); 173bfcb79fcSKeith Busch } else if (dev->has_secondary_link) { 174bfcb79fcSKeith Busch status = default_reset_link(dev); 1752e28bc84SOza Pawandeep } else { 1762e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "no link-reset support at upstream device %s\n", 177bfcb79fcSKeith Busch pci_name(dev)); 1782e28bc84SOza Pawandeep return PCI_ERS_RESULT_DISCONNECT; 1792e28bc84SOza Pawandeep } 1802e28bc84SOza Pawandeep 1812e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) { 1822e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "link reset at upstream device %s failed\n", 183bfcb79fcSKeith Busch pci_name(dev)); 1842e28bc84SOza Pawandeep return PCI_ERS_RESULT_DISCONNECT; 1852e28bc84SOza Pawandeep } 1862e28bc84SOza Pawandeep 1872e28bc84SOza Pawandeep return status; 1882e28bc84SOza Pawandeep } 1892e28bc84SOza Pawandeep 190bdb5ac85SKeith Busch void pcie_do_recovery(struct pci_dev *dev, enum pci_channel_state state, 191bdb5ac85SKeith Busch u32 service) 1922e28bc84SOza Pawandeep { 193*542aeb9cSKeith Busch pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER; 194*542aeb9cSKeith Busch struct pci_bus *bus; 1952e28bc84SOza Pawandeep 196bfcb79fcSKeith Busch /* 197bfcb79fcSKeith Busch * Error recovery runs on all subordinates of the first downstream port. 198bfcb79fcSKeith Busch * If the downstream port detected the error, it is cleared at the end. 199bfcb79fcSKeith Busch */ 200bfcb79fcSKeith Busch if (!(pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT || 201bfcb79fcSKeith Busch pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)) 202bfcb79fcSKeith Busch dev = dev->bus->self; 203*542aeb9cSKeith Busch bus = dev->subordinate; 204bfcb79fcSKeith Busch 205*542aeb9cSKeith Busch pci_dbg(dev, "broadcast error_detected message\n"); 206*542aeb9cSKeith Busch if (state == pci_channel_io_frozen) 207*542aeb9cSKeith Busch pci_walk_bus(bus, report_frozen_detected, &status); 208*542aeb9cSKeith Busch else 209*542aeb9cSKeith Busch pci_walk_bus(bus, report_normal_detected, &status); 2102e28bc84SOza Pawandeep 211bdb5ac85SKeith Busch if (state == pci_channel_io_frozen && 212bdb5ac85SKeith Busch reset_link(dev, service) != PCI_ERS_RESULT_RECOVERED) 213bdb5ac85SKeith Busch goto failed; 214bdb5ac85SKeith Busch 215*542aeb9cSKeith Busch if (status == PCI_ERS_RESULT_CAN_RECOVER) { 216*542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 217*542aeb9cSKeith Busch pci_dbg(dev, "broadcast mmio_enabled message\n"); 218*542aeb9cSKeith Busch pci_walk_bus(bus, report_mmio_enabled, &status); 219*542aeb9cSKeith Busch } 2202e28bc84SOza Pawandeep 2212e28bc84SOza Pawandeep if (status == PCI_ERS_RESULT_NEED_RESET) { 2222e28bc84SOza Pawandeep /* 2232e28bc84SOza Pawandeep * TODO: Should call platform-specific 2242e28bc84SOza Pawandeep * functions to reset slot before calling 2252e28bc84SOza Pawandeep * drivers' slot_reset callbacks? 2262e28bc84SOza Pawandeep */ 227*542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 228*542aeb9cSKeith Busch pci_dbg(dev, "broadcast slot_reset message\n"); 229*542aeb9cSKeith Busch pci_walk_bus(bus, report_slot_reset, &status); 2302e28bc84SOza Pawandeep } 2312e28bc84SOza Pawandeep 2322e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) 2332e28bc84SOza Pawandeep goto failed; 2342e28bc84SOza Pawandeep 235*542aeb9cSKeith Busch pci_dbg(dev, "broadcast resume message\n"); 236*542aeb9cSKeith Busch pci_walk_bus(bus, report_resume, &status); 2372e28bc84SOza Pawandeep 238bfcb79fcSKeith Busch pci_aer_clear_device_status(dev); 239bfcb79fcSKeith Busch pci_cleanup_aer_uncorrect_error_status(dev); 2402e28bc84SOza Pawandeep pci_info(dev, "AER: Device recovery successful\n"); 2412e28bc84SOza Pawandeep return; 2422e28bc84SOza Pawandeep 2432e28bc84SOza Pawandeep failed: 2442e28bc84SOza Pawandeep pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT); 2452e28bc84SOza Pawandeep 2462e28bc84SOza Pawandeep /* TODO: Should kernel panic here? */ 2472e28bc84SOza Pawandeep pci_info(dev, "AER: Device recovery failed\n"); 2482e28bc84SOza Pawandeep } 249