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 138d077c3cSBjorn Helgaas #define dev_fmt(fmt) "AER: " fmt 148d077c3cSBjorn Helgaas 152e28bc84SOza Pawandeep #include <linux/pci.h> 162e28bc84SOza Pawandeep #include <linux/module.h> 172e28bc84SOza Pawandeep #include <linux/kernel.h> 182e28bc84SOza Pawandeep #include <linux/errno.h> 192e28bc84SOza Pawandeep #include <linux/aer.h> 202e28bc84SOza Pawandeep #include "portdrv.h" 212e28bc84SOza Pawandeep #include "../pci.h" 222e28bc84SOza Pawandeep 232e28bc84SOza Pawandeep static pci_ers_result_t merge_result(enum pci_ers_result orig, 242e28bc84SOza Pawandeep enum pci_ers_result new) 252e28bc84SOza Pawandeep { 262e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NO_AER_DRIVER) 272e28bc84SOza Pawandeep return PCI_ERS_RESULT_NO_AER_DRIVER; 282e28bc84SOza Pawandeep 292e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NONE) 302e28bc84SOza Pawandeep return orig; 312e28bc84SOza Pawandeep 322e28bc84SOza Pawandeep switch (orig) { 332e28bc84SOza Pawandeep case PCI_ERS_RESULT_CAN_RECOVER: 342e28bc84SOza Pawandeep case PCI_ERS_RESULT_RECOVERED: 352e28bc84SOza Pawandeep orig = new; 362e28bc84SOza Pawandeep break; 372e28bc84SOza Pawandeep case PCI_ERS_RESULT_DISCONNECT: 382e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NEED_RESET) 392e28bc84SOza Pawandeep orig = PCI_ERS_RESULT_NEED_RESET; 402e28bc84SOza Pawandeep break; 412e28bc84SOza Pawandeep default: 422e28bc84SOza Pawandeep break; 432e28bc84SOza Pawandeep } 442e28bc84SOza Pawandeep 452e28bc84SOza Pawandeep return orig; 462e28bc84SOza Pawandeep } 472e28bc84SOza Pawandeep 48542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev, 4916d79cd4SLuc Van Oostenryck pci_channel_state_t state, 50542aeb9cSKeith Busch enum pci_ers_result *result) 512e28bc84SOza Pawandeep { 522e28bc84SOza Pawandeep pci_ers_result_t vote; 532e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 542e28bc84SOza Pawandeep 552e28bc84SOza Pawandeep device_lock(&dev->dev); 56a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, state) || 57a6bd101bSKeith Busch !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 */ 6601daacfbSYicong Yang if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { 672e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NO_AER_DRIVER; 688d077c3cSBjorn Helgaas pci_info(dev, "can't recover (no error_detected callback)\n"); 6901daacfbSYicong Yang } else { 702e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NONE; 7101daacfbSYicong Yang } 722e28bc84SOza Pawandeep } else { 732e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 74542aeb9cSKeith Busch vote = err_handler->error_detected(dev, state); 752e28bc84SOza Pawandeep } 767b42d97eSKeith Busch pci_uevent_ers(dev, vote); 77542aeb9cSKeith Busch *result = merge_result(*result, vote); 782e28bc84SOza Pawandeep device_unlock(&dev->dev); 792e28bc84SOza Pawandeep return 0; 802e28bc84SOza Pawandeep } 812e28bc84SOza Pawandeep 82542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data) 83542aeb9cSKeith Busch { 84542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_frozen, data); 85542aeb9cSKeith Busch } 86542aeb9cSKeith Busch 87542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data) 88542aeb9cSKeith Busch { 89542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_normal, data); 90542aeb9cSKeith Busch } 91542aeb9cSKeith Busch 922e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data) 932e28bc84SOza Pawandeep { 94542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 952e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 962e28bc84SOza Pawandeep 972e28bc84SOza Pawandeep device_lock(&dev->dev); 982e28bc84SOza Pawandeep if (!dev->driver || 992e28bc84SOza Pawandeep !dev->driver->err_handler || 1002e28bc84SOza Pawandeep !dev->driver->err_handler->mmio_enabled) 1012e28bc84SOza Pawandeep goto out; 1022e28bc84SOza Pawandeep 1032e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1042e28bc84SOza Pawandeep vote = err_handler->mmio_enabled(dev); 105542aeb9cSKeith Busch *result = merge_result(*result, vote); 1062e28bc84SOza Pawandeep out: 1072e28bc84SOza Pawandeep device_unlock(&dev->dev); 1082e28bc84SOza Pawandeep return 0; 1092e28bc84SOza Pawandeep } 1102e28bc84SOza Pawandeep 1112e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data) 1122e28bc84SOza Pawandeep { 113542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 1142e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1152e28bc84SOza Pawandeep 1162e28bc84SOza Pawandeep device_lock(&dev->dev); 1172e28bc84SOza Pawandeep if (!dev->driver || 1182e28bc84SOza Pawandeep !dev->driver->err_handler || 1192e28bc84SOza Pawandeep !dev->driver->err_handler->slot_reset) 1202e28bc84SOza Pawandeep goto out; 1212e28bc84SOza Pawandeep 1222e28bc84SOza Pawandeep err_handler = dev->driver->err_handler; 1232e28bc84SOza Pawandeep vote = err_handler->slot_reset(dev); 124542aeb9cSKeith Busch *result = merge_result(*result, vote); 1252e28bc84SOza Pawandeep out: 1262e28bc84SOza Pawandeep device_unlock(&dev->dev); 1272e28bc84SOza Pawandeep return 0; 1282e28bc84SOza Pawandeep } 1292e28bc84SOza Pawandeep 1302e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data) 1312e28bc84SOza Pawandeep { 1322e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1332e28bc84SOza Pawandeep 1342e28bc84SOza Pawandeep device_lock(&dev->dev); 135a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, pci_channel_io_normal) || 136a6bd101bSKeith Busch !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 out: 1447b42d97eSKeith Busch pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED); 1452e28bc84SOza Pawandeep device_unlock(&dev->dev); 1462e28bc84SOza Pawandeep return 0; 1472e28bc84SOza Pawandeep } 1482e28bc84SOza Pawandeep 149e8e5ff2aSKuppuswamy Sathyanarayanan pci_ers_result_t pcie_do_recovery(struct pci_dev *dev, 15016d79cd4SLuc Van Oostenryck pci_channel_state_t state, 151*8f1bbfbcSSean V Kelley pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev)) 1522e28bc84SOza Pawandeep { 153542aeb9cSKeith Busch pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER; 154542aeb9cSKeith Busch struct pci_bus *bus; 1552e28bc84SOza Pawandeep 156bfcb79fcSKeith Busch /* 157bfcb79fcSKeith Busch * Error recovery runs on all subordinates of the first downstream port. 158bfcb79fcSKeith Busch * If the downstream port detected the error, it is cleared at the end. 159bfcb79fcSKeith Busch */ 160bfcb79fcSKeith Busch if (!(pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT || 161bfcb79fcSKeith Busch pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)) 162bfcb79fcSKeith Busch dev = dev->bus->self; 163542aeb9cSKeith Busch bus = dev->subordinate; 164bfcb79fcSKeith Busch 165542aeb9cSKeith Busch pci_dbg(dev, "broadcast error_detected message\n"); 166b5dfbeacSKuppuswamy Sathyanarayanan if (state == pci_channel_io_frozen) { 167542aeb9cSKeith Busch pci_walk_bus(bus, report_frozen_detected, &status); 168*8f1bbfbcSSean V Kelley status = reset_subordinates(dev); 169b6cf1a42SKuppuswamy Sathyanarayanan if (status != PCI_ERS_RESULT_RECOVERED) { 170*8f1bbfbcSSean V Kelley pci_warn(dev, "subordinate device reset failed\n"); 171bdb5ac85SKeith Busch goto failed; 172b6cf1a42SKuppuswamy Sathyanarayanan } 173b5dfbeacSKuppuswamy Sathyanarayanan } else { 174b5dfbeacSKuppuswamy Sathyanarayanan pci_walk_bus(bus, report_normal_detected, &status); 175b5dfbeacSKuppuswamy Sathyanarayanan } 176bdb5ac85SKeith Busch 177542aeb9cSKeith Busch if (status == PCI_ERS_RESULT_CAN_RECOVER) { 178542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 179542aeb9cSKeith Busch pci_dbg(dev, "broadcast mmio_enabled message\n"); 180542aeb9cSKeith Busch pci_walk_bus(bus, report_mmio_enabled, &status); 181542aeb9cSKeith Busch } 1822e28bc84SOza Pawandeep 1832e28bc84SOza Pawandeep if (status == PCI_ERS_RESULT_NEED_RESET) { 1842e28bc84SOza Pawandeep /* 1852e28bc84SOza Pawandeep * TODO: Should call platform-specific 1862e28bc84SOza Pawandeep * functions to reset slot before calling 1872e28bc84SOza Pawandeep * drivers' slot_reset callbacks? 1882e28bc84SOza Pawandeep */ 189542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 190542aeb9cSKeith Busch pci_dbg(dev, "broadcast slot_reset message\n"); 191542aeb9cSKeith Busch pci_walk_bus(bus, report_slot_reset, &status); 1922e28bc84SOza Pawandeep } 1932e28bc84SOza Pawandeep 1942e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) 1952e28bc84SOza Pawandeep goto failed; 1962e28bc84SOza Pawandeep 197542aeb9cSKeith Busch pci_dbg(dev, "broadcast resume message\n"); 198542aeb9cSKeith Busch pci_walk_bus(bus, report_resume, &status); 1992e28bc84SOza Pawandeep 200068c29a2SJonathan Cameron if (pcie_aer_is_native(dev)) 201600a5b4fSBjorn Helgaas pcie_clear_device_status(dev); 202894020fdSKuppuswamy Sathyanarayanan pci_aer_clear_nonfatal_status(dev); 2038d077c3cSBjorn Helgaas pci_info(dev, "device recovery successful\n"); 204e8e5ff2aSKuppuswamy Sathyanarayanan return status; 2052e28bc84SOza Pawandeep 2062e28bc84SOza Pawandeep failed: 2072e28bc84SOza Pawandeep pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT); 2082e28bc84SOza Pawandeep 2092e28bc84SOza Pawandeep /* TODO: Should kernel panic here? */ 2108d077c3cSBjorn Helgaas pci_info(dev, "device recovery failed\n"); 211e8e5ff2aSKuppuswamy Sathyanarayanan 212e8e5ff2aSKuppuswamy Sathyanarayanan return status; 2132e28bc84SOza Pawandeep } 214