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 47542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev, 48542aeb9cSKeith Busch enum pci_channel_state state, 49542aeb9cSKeith 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*a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, state) || 56*a6bd101bSKeith Busch !dev->driver || 572e28bc84SOza Pawandeep !dev->driver->err_handler || 582e28bc84SOza Pawandeep !dev->driver->err_handler->error_detected) { 592e28bc84SOza Pawandeep /* 60bfcb79fcSKeith Busch * If any device in the subtree does not have an error_detected 61bfcb79fcSKeith Busch * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent 62bfcb79fcSKeith Busch * error callbacks of "any" device in the subtree, and will 63bfcb79fcSKeith Busch * exit in the disconnected error state. 642e28bc84SOza Pawandeep */ 652e28bc84SOza Pawandeep if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) 662e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NO_AER_DRIVER; 672e28bc84SOza Pawandeep else 682e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NONE; 692e28bc84SOza Pawandeep } else { 702e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 71542aeb9cSKeith Busch vote = err_handler->error_detected(dev, state); 722e28bc84SOza Pawandeep } 737b42d97eSKeith Busch pci_uevent_ers(dev, vote); 74542aeb9cSKeith Busch *result = merge_result(*result, vote); 752e28bc84SOza Pawandeep device_unlock(&dev->dev); 762e28bc84SOza Pawandeep return 0; 772e28bc84SOza Pawandeep } 782e28bc84SOza Pawandeep 79542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data) 80542aeb9cSKeith Busch { 81542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_frozen, data); 82542aeb9cSKeith Busch } 83542aeb9cSKeith Busch 84542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data) 85542aeb9cSKeith Busch { 86542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_normal, data); 87542aeb9cSKeith Busch } 88542aeb9cSKeith Busch 892e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data) 902e28bc84SOza Pawandeep { 91542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 922e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 932e28bc84SOza Pawandeep 942e28bc84SOza Pawandeep device_lock(&dev->dev); 952e28bc84SOza Pawandeep if (!dev->driver || 962e28bc84SOza Pawandeep !dev->driver->err_handler || 972e28bc84SOza Pawandeep !dev->driver->err_handler->mmio_enabled) 982e28bc84SOza Pawandeep goto out; 992e28bc84SOza Pawandeep 1002e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1012e28bc84SOza Pawandeep vote = err_handler->mmio_enabled(dev); 102542aeb9cSKeith Busch *result = merge_result(*result, vote); 1032e28bc84SOza Pawandeep out: 1042e28bc84SOza Pawandeep device_unlock(&dev->dev); 1052e28bc84SOza Pawandeep return 0; 1062e28bc84SOza Pawandeep } 1072e28bc84SOza Pawandeep 1082e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data) 1092e28bc84SOza Pawandeep { 110542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 1112e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1122e28bc84SOza Pawandeep 1132e28bc84SOza Pawandeep device_lock(&dev->dev); 1142e28bc84SOza Pawandeep if (!dev->driver || 1152e28bc84SOza Pawandeep !dev->driver->err_handler || 1162e28bc84SOza Pawandeep !dev->driver->err_handler->slot_reset) 1172e28bc84SOza Pawandeep goto out; 1182e28bc84SOza Pawandeep 1192e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1202e28bc84SOza Pawandeep vote = err_handler->slot_reset(dev); 121542aeb9cSKeith Busch *result = merge_result(*result, vote); 1222e28bc84SOza Pawandeep out: 1232e28bc84SOza Pawandeep device_unlock(&dev->dev); 1242e28bc84SOza Pawandeep return 0; 1252e28bc84SOza Pawandeep } 1262e28bc84SOza Pawandeep 1272e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data) 1282e28bc84SOza Pawandeep { 1292e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1302e28bc84SOza Pawandeep 1312e28bc84SOza Pawandeep device_lock(&dev->dev); 132*a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, pci_channel_io_normal) || 133*a6bd101bSKeith Busch !dev->driver || 1342e28bc84SOza Pawandeep !dev->driver->err_handler || 1352e28bc84SOza Pawandeep !dev->driver->err_handler->resume) 1362e28bc84SOza Pawandeep goto out; 1372e28bc84SOza Pawandeep 1382e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1392e28bc84SOza Pawandeep err_handler->resume(dev); 1402e28bc84SOza Pawandeep out: 1417b42d97eSKeith Busch pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED); 1422e28bc84SOza Pawandeep device_unlock(&dev->dev); 1432e28bc84SOza Pawandeep return 0; 1442e28bc84SOza Pawandeep } 1452e28bc84SOza Pawandeep 1462e28bc84SOza Pawandeep /** 1472e28bc84SOza Pawandeep * default_reset_link - default reset function 1482e28bc84SOza Pawandeep * @dev: pointer to pci_dev data structure 1492e28bc84SOza Pawandeep * 1502e28bc84SOza Pawandeep * Invoked when performing link reset on a Downstream Port or a 1512e28bc84SOza Pawandeep * Root Port with no aer driver. 1522e28bc84SOza Pawandeep */ 1532e28bc84SOza Pawandeep static pci_ers_result_t default_reset_link(struct pci_dev *dev) 1542e28bc84SOza Pawandeep { 15518426238SSinan Kaya int rc; 15618426238SSinan Kaya 157c4eed62aSKeith Busch rc = pci_bus_error_reset(dev); 1582e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "downstream link has been reset\n"); 15918426238SSinan Kaya return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 1602e28bc84SOza Pawandeep } 1612e28bc84SOza Pawandeep 1620b91439dSOza Pawandeep static pci_ers_result_t reset_link(struct pci_dev *dev, u32 service) 1632e28bc84SOza Pawandeep { 1642e28bc84SOza Pawandeep pci_ers_result_t status; 1652e28bc84SOza Pawandeep struct pcie_port_service_driver *driver = NULL; 1662e28bc84SOza Pawandeep 167bfcb79fcSKeith Busch driver = pcie_port_find_service(dev, service); 1682e28bc84SOza Pawandeep if (driver && driver->reset_link) { 169bfcb79fcSKeith Busch status = driver->reset_link(dev); 170bfcb79fcSKeith Busch } else if (dev->has_secondary_link) { 171bfcb79fcSKeith Busch status = default_reset_link(dev); 1722e28bc84SOza Pawandeep } else { 1732e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "no link-reset support at upstream device %s\n", 174bfcb79fcSKeith Busch pci_name(dev)); 1752e28bc84SOza Pawandeep return PCI_ERS_RESULT_DISCONNECT; 1762e28bc84SOza Pawandeep } 1772e28bc84SOza Pawandeep 1782e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) { 1792e28bc84SOza Pawandeep pci_printk(KERN_DEBUG, dev, "link reset at upstream device %s failed\n", 180bfcb79fcSKeith Busch pci_name(dev)); 1812e28bc84SOza Pawandeep return PCI_ERS_RESULT_DISCONNECT; 1822e28bc84SOza Pawandeep } 1832e28bc84SOza Pawandeep 1842e28bc84SOza Pawandeep return status; 1852e28bc84SOza Pawandeep } 1862e28bc84SOza Pawandeep 187bdb5ac85SKeith Busch void pcie_do_recovery(struct pci_dev *dev, enum pci_channel_state state, 188bdb5ac85SKeith Busch u32 service) 1892e28bc84SOza Pawandeep { 190542aeb9cSKeith Busch pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER; 191542aeb9cSKeith Busch struct pci_bus *bus; 1922e28bc84SOza Pawandeep 193bfcb79fcSKeith Busch /* 194bfcb79fcSKeith Busch * Error recovery runs on all subordinates of the first downstream port. 195bfcb79fcSKeith Busch * If the downstream port detected the error, it is cleared at the end. 196bfcb79fcSKeith Busch */ 197bfcb79fcSKeith Busch if (!(pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT || 198bfcb79fcSKeith Busch pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)) 199bfcb79fcSKeith Busch dev = dev->bus->self; 200542aeb9cSKeith Busch bus = dev->subordinate; 201bfcb79fcSKeith Busch 202542aeb9cSKeith Busch pci_dbg(dev, "broadcast error_detected message\n"); 203542aeb9cSKeith Busch if (state == pci_channel_io_frozen) 204542aeb9cSKeith Busch pci_walk_bus(bus, report_frozen_detected, &status); 205542aeb9cSKeith Busch else 206542aeb9cSKeith Busch pci_walk_bus(bus, report_normal_detected, &status); 2072e28bc84SOza Pawandeep 208bdb5ac85SKeith Busch if (state == pci_channel_io_frozen && 209bdb5ac85SKeith Busch reset_link(dev, service) != PCI_ERS_RESULT_RECOVERED) 210bdb5ac85SKeith Busch goto failed; 211bdb5ac85SKeith Busch 212542aeb9cSKeith Busch if (status == PCI_ERS_RESULT_CAN_RECOVER) { 213542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 214542aeb9cSKeith Busch pci_dbg(dev, "broadcast mmio_enabled message\n"); 215542aeb9cSKeith Busch pci_walk_bus(bus, report_mmio_enabled, &status); 216542aeb9cSKeith Busch } 2172e28bc84SOza Pawandeep 2182e28bc84SOza Pawandeep if (status == PCI_ERS_RESULT_NEED_RESET) { 2192e28bc84SOza Pawandeep /* 2202e28bc84SOza Pawandeep * TODO: Should call platform-specific 2212e28bc84SOza Pawandeep * functions to reset slot before calling 2222e28bc84SOza Pawandeep * drivers' slot_reset callbacks? 2232e28bc84SOza Pawandeep */ 224542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 225542aeb9cSKeith Busch pci_dbg(dev, "broadcast slot_reset message\n"); 226542aeb9cSKeith Busch pci_walk_bus(bus, report_slot_reset, &status); 2272e28bc84SOza Pawandeep } 2282e28bc84SOza Pawandeep 2292e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) 2302e28bc84SOza Pawandeep goto failed; 2312e28bc84SOza Pawandeep 232542aeb9cSKeith Busch pci_dbg(dev, "broadcast resume message\n"); 233542aeb9cSKeith Busch pci_walk_bus(bus, report_resume, &status); 2342e28bc84SOza Pawandeep 235bfcb79fcSKeith Busch pci_aer_clear_device_status(dev); 236bfcb79fcSKeith Busch pci_cleanup_aer_uncorrect_error_status(dev); 2372e28bc84SOza Pawandeep pci_info(dev, "AER: Device recovery successful\n"); 2382e28bc84SOza Pawandeep return; 2392e28bc84SOza Pawandeep 2402e28bc84SOza Pawandeep failed: 2412e28bc84SOza Pawandeep pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT); 2422e28bc84SOza Pawandeep 2432e28bc84SOza Pawandeep /* TODO: Should kernel panic here? */ 2442e28bc84SOza Pawandeep pci_info(dev, "AER: Device recovery failed\n"); 2452e28bc84SOza Pawandeep } 246