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/kernel.h> 162e28bc84SOza Pawandeep #include <linux/errno.h> 172e28bc84SOza Pawandeep #include <linux/aer.h> 182e28bc84SOza Pawandeep #include "portdrv.h" 192e28bc84SOza Pawandeep #include "../pci.h" 202e28bc84SOza Pawandeep 212e28bc84SOza Pawandeep static pci_ers_result_t merge_result(enum pci_ers_result orig, 222e28bc84SOza Pawandeep enum pci_ers_result new) 232e28bc84SOza Pawandeep { 242e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NO_AER_DRIVER) 252e28bc84SOza Pawandeep return PCI_ERS_RESULT_NO_AER_DRIVER; 262e28bc84SOza Pawandeep 272e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NONE) 282e28bc84SOza Pawandeep return orig; 292e28bc84SOza Pawandeep 302e28bc84SOza Pawandeep switch (orig) { 312e28bc84SOza Pawandeep case PCI_ERS_RESULT_CAN_RECOVER: 322e28bc84SOza Pawandeep case PCI_ERS_RESULT_RECOVERED: 332e28bc84SOza Pawandeep orig = new; 342e28bc84SOza Pawandeep break; 352e28bc84SOza Pawandeep case PCI_ERS_RESULT_DISCONNECT: 362e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NEED_RESET) 372e28bc84SOza Pawandeep orig = PCI_ERS_RESULT_NEED_RESET; 382e28bc84SOza Pawandeep break; 392e28bc84SOza Pawandeep default: 402e28bc84SOza Pawandeep break; 412e28bc84SOza Pawandeep } 422e28bc84SOza Pawandeep 432e28bc84SOza Pawandeep return orig; 442e28bc84SOza Pawandeep } 452e28bc84SOza Pawandeep 46542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev, 47542aeb9cSKeith Busch enum pci_channel_state state, 48542aeb9cSKeith Busch enum pci_ers_result *result) 492e28bc84SOza Pawandeep { 502e28bc84SOza Pawandeep pci_ers_result_t vote; 512e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 522e28bc84SOza Pawandeep 532e28bc84SOza Pawandeep device_lock(&dev->dev); 54*a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, state) || 55*a6bd101bSKeith Busch !dev->driver || 562e28bc84SOza Pawandeep !dev->driver->err_handler || 572e28bc84SOza Pawandeep !dev->driver->err_handler->error_detected) { 582e28bc84SOza Pawandeep /* 59bfcb79fcSKeith Busch * If any device in the subtree does not have an error_detected 60bfcb79fcSKeith Busch * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent 61bfcb79fcSKeith Busch * error callbacks of "any" device in the subtree, and will 62bfcb79fcSKeith Busch * exit in the disconnected error state. 632e28bc84SOza Pawandeep */ 642e28bc84SOza Pawandeep if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) 652e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NO_AER_DRIVER; 662e28bc84SOza Pawandeep else 672e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NONE; 682e28bc84SOza Pawandeep } else { 692e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 70542aeb9cSKeith Busch vote = err_handler->error_detected(dev, state); 712e28bc84SOza Pawandeep } 727b42d97eSKeith Busch pci_uevent_ers(dev, vote); 73542aeb9cSKeith Busch *result = merge_result(*result, vote); 742e28bc84SOza Pawandeep device_unlock(&dev->dev); 752e28bc84SOza Pawandeep return 0; 762e28bc84SOza Pawandeep } 772e28bc84SOza Pawandeep 78542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data) 79542aeb9cSKeith Busch { 80542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_frozen, data); 81542aeb9cSKeith Busch } 82542aeb9cSKeith Busch 83542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data) 84542aeb9cSKeith Busch { 85542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_normal, data); 86542aeb9cSKeith Busch } 87542aeb9cSKeith Busch 882e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data) 892e28bc84SOza Pawandeep { 90542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 912e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 922e28bc84SOza Pawandeep 932e28bc84SOza Pawandeep device_lock(&dev->dev); 942e28bc84SOza Pawandeep if (!dev->driver || 952e28bc84SOza Pawandeep !dev->driver->err_handler || 962e28bc84SOza Pawandeep !dev->driver->err_handler->mmio_enabled) 972e28bc84SOza Pawandeep goto out; 982e28bc84SOza Pawandeep 992e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1002e28bc84SOza Pawandeep vote = err_handler->mmio_enabled(dev); 101542aeb9cSKeith Busch *result = merge_result(*result, vote); 1022e28bc84SOza Pawandeep out: 1032e28bc84SOza Pawandeep device_unlock(&dev->dev); 1042e28bc84SOza Pawandeep return 0; 1052e28bc84SOza Pawandeep } 1062e28bc84SOza Pawandeep 1072e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data) 1082e28bc84SOza Pawandeep { 109542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 1102e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1112e28bc84SOza Pawandeep 1122e28bc84SOza Pawandeep device_lock(&dev->dev); 1132e28bc84SOza Pawandeep if (!dev->driver || 1142e28bc84SOza Pawandeep !dev->driver->err_handler || 1152e28bc84SOza Pawandeep !dev->driver->err_handler->slot_reset) 1162e28bc84SOza Pawandeep goto out; 1172e28bc84SOza Pawandeep 1182e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1192e28bc84SOza Pawandeep vote = err_handler->slot_reset(dev); 120542aeb9cSKeith Busch *result = merge_result(*result, vote); 1212e28bc84SOza Pawandeep out: 1222e28bc84SOza Pawandeep device_unlock(&dev->dev); 1232e28bc84SOza Pawandeep return 0; 1242e28bc84SOza Pawandeep } 1252e28bc84SOza Pawandeep 1262e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data) 1272e28bc84SOza Pawandeep { 1282e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1292e28bc84SOza Pawandeep 1302e28bc84SOza Pawandeep device_lock(&dev->dev); 131*a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, pci_channel_io_normal) || 132*a6bd101bSKeith Busch !dev->driver || 1332e28bc84SOza Pawandeep !dev->driver->err_handler || 1342e28bc84SOza Pawandeep !dev->driver->err_handler->resume) 1352e28bc84SOza Pawandeep goto out; 1362e28bc84SOza Pawandeep 1372e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1382e28bc84SOza Pawandeep err_handler->resume(dev); 1392e28bc84SOza Pawandeep out: 1407b42d97eSKeith Busch pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED); 1412e28bc84SOza Pawandeep device_unlock(&dev->dev); 1422e28bc84SOza Pawandeep return 0; 1432e28bc84SOza Pawandeep } 1442e28bc84SOza Pawandeep 1452e28bc84SOza Pawandeep /** 1462e28bc84SOza Pawandeep * default_reset_link - default reset function 1472e28bc84SOza Pawandeep * @dev: pointer to pci_dev data structure 1482e28bc84SOza Pawandeep * 1492e28bc84SOza Pawandeep * Invoked when performing link reset on a Downstream Port or a 1502e28bc84SOza Pawandeep * Root Port with no aer driver. 1512e28bc84SOza Pawandeep */ 1522e28bc84SOza Pawandeep static pci_ers_result_t default_reset_link(struct pci_dev *dev) 1532e28bc84SOza Pawandeep { 15418426238SSinan Kaya int rc; 15518426238SSinan Kaya 156c4eed62aSKeith Busch rc = pci_bus_error_reset(dev); 1572e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "downstream link has been reset\n"); 15818426238SSinan Kaya return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 1592e28bc84SOza Pawandeep } 1602e28bc84SOza Pawandeep 1610b91439dSOza Pawandeep static pci_ers_result_t reset_link(struct pci_dev *dev, u32 service) 1622e28bc84SOza Pawandeep { 1632e28bc84SOza Pawandeep pci_ers_result_t status; 1642e28bc84SOza Pawandeep struct pcie_port_service_driver *driver = NULL; 1652e28bc84SOza Pawandeep 166bfcb79fcSKeith Busch driver = pcie_port_find_service(dev, service); 1672e28bc84SOza Pawandeep if (driver && driver->reset_link) { 168bfcb79fcSKeith Busch status = driver->reset_link(dev); 169bfcb79fcSKeith Busch } else if (dev->has_secondary_link) { 170bfcb79fcSKeith Busch status = default_reset_link(dev); 1712e28bc84SOza Pawandeep } else { 1722e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "no link-reset support at upstream device %s\n", 173bfcb79fcSKeith Busch pci_name(dev)); 1742e28bc84SOza Pawandeep return PCI_ERS_RESULT_DISCONNECT; 1752e28bc84SOza Pawandeep } 1762e28bc84SOza Pawandeep 1772e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) { 1782e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "link reset at upstream device %s failed\n", 179bfcb79fcSKeith Busch pci_name(dev)); 1802e28bc84SOza Pawandeep return PCI_ERS_RESULT_DISCONNECT; 1812e28bc84SOza Pawandeep } 1822e28bc84SOza Pawandeep 1832e28bc84SOza Pawandeep return status; 1842e28bc84SOza Pawandeep } 1852e28bc84SOza Pawandeep 186bdb5ac85SKeith Busch void pcie_do_recovery(struct pci_dev *dev, enum pci_channel_state state, 187bdb5ac85SKeith Busch u32 service) 1882e28bc84SOza Pawandeep { 189542aeb9cSKeith Busch pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER; 190542aeb9cSKeith Busch struct pci_bus *bus; 1912e28bc84SOza Pawandeep 192bfcb79fcSKeith Busch /* 193bfcb79fcSKeith Busch * Error recovery runs on all subordinates of the first downstream port. 194bfcb79fcSKeith Busch * If the downstream port detected the error, it is cleared at the end. 195bfcb79fcSKeith Busch */ 196bfcb79fcSKeith Busch if (!(pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT || 197bfcb79fcSKeith Busch pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)) 198bfcb79fcSKeith Busch dev = dev->bus->self; 199542aeb9cSKeith Busch bus = dev->subordinate; 200bfcb79fcSKeith Busch 201542aeb9cSKeith Busch pci_dbg(dev, "broadcast error_detected message\n"); 202542aeb9cSKeith Busch if (state == pci_channel_io_frozen) 203542aeb9cSKeith Busch pci_walk_bus(bus, report_frozen_detected, &status); 204542aeb9cSKeith Busch else 205542aeb9cSKeith Busch pci_walk_bus(bus, report_normal_detected, &status); 2062e28bc84SOza Pawandeep 207bdb5ac85SKeith Busch if (state == pci_channel_io_frozen && 208bdb5ac85SKeith Busch reset_link(dev, service) != PCI_ERS_RESULT_RECOVERED) 209bdb5ac85SKeith Busch goto failed; 210bdb5ac85SKeith Busch 211542aeb9cSKeith Busch if (status == PCI_ERS_RESULT_CAN_RECOVER) { 212542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 213542aeb9cSKeith Busch pci_dbg(dev, "broadcast mmio_enabled message\n"); 214542aeb9cSKeith Busch pci_walk_bus(bus, report_mmio_enabled, &status); 215542aeb9cSKeith Busch } 2162e28bc84SOza Pawandeep 2172e28bc84SOza Pawandeep if (status == PCI_ERS_RESULT_NEED_RESET) { 2182e28bc84SOza Pawandeep /* 2192e28bc84SOza Pawandeep * TODO: Should call platform-specific 2202e28bc84SOza Pawandeep * functions to reset slot before calling 2212e28bc84SOza Pawandeep * drivers' slot_reset callbacks? 2222e28bc84SOza Pawandeep */ 223542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 224542aeb9cSKeith Busch pci_dbg(dev, "broadcast slot_reset message\n"); 225542aeb9cSKeith Busch pci_walk_bus(bus, report_slot_reset, &status); 2262e28bc84SOza Pawandeep } 2272e28bc84SOza Pawandeep 2282e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) 2292e28bc84SOza Pawandeep goto failed; 2302e28bc84SOza Pawandeep 231542aeb9cSKeith Busch pci_dbg(dev, "broadcast resume message\n"); 232542aeb9cSKeith Busch pci_walk_bus(bus, report_resume, &status); 2332e28bc84SOza Pawandeep 234bfcb79fcSKeith Busch pci_aer_clear_device_status(dev); 235bfcb79fcSKeith Busch pci_cleanup_aer_uncorrect_error_status(dev); 2362e28bc84SOza Pawandeep pci_info(dev, "AER: Device recovery successful\n"); 2372e28bc84SOza Pawandeep return; 2382e28bc84SOza Pawandeep 2392e28bc84SOza Pawandeep failed: 2402e28bc84SOza Pawandeep pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT); 2412e28bc84SOza Pawandeep 2422e28bc84SOza Pawandeep /* TODO: Should kernel panic here? */ 2432e28bc84SOza Pawandeep pci_info(dev, "AER: Device recovery failed\n"); 2442e28bc84SOza Pawandeep } 245