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> 16002bf2fbSStanislaw Gruszka #include <linux/pm_runtime.h> 172e28bc84SOza Pawandeep #include <linux/module.h> 182e28bc84SOza Pawandeep #include <linux/kernel.h> 192e28bc84SOza Pawandeep #include <linux/errno.h> 202e28bc84SOza Pawandeep #include <linux/aer.h> 212e28bc84SOza Pawandeep #include "portdrv.h" 222e28bc84SOza Pawandeep #include "../pci.h" 232e28bc84SOza Pawandeep 242e28bc84SOza Pawandeep static pci_ers_result_t merge_result(enum pci_ers_result orig, 252e28bc84SOza Pawandeep enum pci_ers_result new) 262e28bc84SOza Pawandeep { 272e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NO_AER_DRIVER) 282e28bc84SOza Pawandeep return PCI_ERS_RESULT_NO_AER_DRIVER; 292e28bc84SOza Pawandeep 302e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NONE) 312e28bc84SOza Pawandeep return orig; 322e28bc84SOza Pawandeep 332e28bc84SOza Pawandeep switch (orig) { 342e28bc84SOza Pawandeep case PCI_ERS_RESULT_CAN_RECOVER: 352e28bc84SOza Pawandeep case PCI_ERS_RESULT_RECOVERED: 362e28bc84SOza Pawandeep orig = new; 372e28bc84SOza Pawandeep break; 382e28bc84SOza Pawandeep case PCI_ERS_RESULT_DISCONNECT: 392e28bc84SOza Pawandeep if (new == PCI_ERS_RESULT_NEED_RESET) 402e28bc84SOza Pawandeep orig = PCI_ERS_RESULT_NEED_RESET; 412e28bc84SOza Pawandeep break; 422e28bc84SOza Pawandeep default: 432e28bc84SOza Pawandeep break; 442e28bc84SOza Pawandeep } 452e28bc84SOza Pawandeep 462e28bc84SOza Pawandeep return orig; 472e28bc84SOza Pawandeep } 482e28bc84SOza Pawandeep 49542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev, 5016d79cd4SLuc Van Oostenryck pci_channel_state_t state, 51542aeb9cSKeith Busch enum pci_ers_result *result) 522e28bc84SOza Pawandeep { 53171d149cSBjorn Helgaas struct pci_driver *pdrv; 542e28bc84SOza Pawandeep pci_ers_result_t vote; 552e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 562e28bc84SOza Pawandeep 572e28bc84SOza Pawandeep device_lock(&dev->dev); 58e0217c5bSBjorn Helgaas pdrv = dev->driver; 595e69a33cSChristoph Hellwig if (pci_dev_is_disconnected(dev)) { 605e69a33cSChristoph Hellwig vote = PCI_ERS_RESULT_DISCONNECT; 615e69a33cSChristoph Hellwig } else if (!pci_dev_set_io_state(dev, state)) { 625e69a33cSChristoph Hellwig pci_info(dev, "can't recover (state transition %u -> %u invalid)\n", 635e69a33cSChristoph Hellwig dev->error_state, state); 645e69a33cSChristoph Hellwig vote = PCI_ERS_RESULT_NONE; 655e69a33cSChristoph Hellwig } else if (!pdrv || !pdrv->err_handler || 66171d149cSBjorn Helgaas !pdrv->err_handler->error_detected) { 672e28bc84SOza Pawandeep /* 68bfcb79fcSKeith Busch * If any device in the subtree does not have an error_detected 69bfcb79fcSKeith Busch * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent 70bfcb79fcSKeith Busch * error callbacks of "any" device in the subtree, and will 71bfcb79fcSKeith Busch * exit in the disconnected error state. 722e28bc84SOza Pawandeep */ 7301daacfbSYicong Yang if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { 742e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NO_AER_DRIVER; 758d077c3cSBjorn Helgaas pci_info(dev, "can't recover (no error_detected callback)\n"); 7601daacfbSYicong Yang } else { 772e28bc84SOza Pawandeep vote = PCI_ERS_RESULT_NONE; 7801daacfbSYicong Yang } 792e28bc84SOza Pawandeep } else { 80171d149cSBjorn Helgaas err_handler = pdrv->err_handler; 81542aeb9cSKeith Busch vote = err_handler->error_detected(dev, state); 822e28bc84SOza Pawandeep } 837b42d97eSKeith Busch pci_uevent_ers(dev, vote); 84542aeb9cSKeith Busch *result = merge_result(*result, vote); 852e28bc84SOza Pawandeep device_unlock(&dev->dev); 862e28bc84SOza Pawandeep return 0; 872e28bc84SOza Pawandeep } 882e28bc84SOza Pawandeep 89002bf2fbSStanislaw Gruszka static int pci_pm_runtime_get_sync(struct pci_dev *pdev, void *data) 90002bf2fbSStanislaw Gruszka { 91002bf2fbSStanislaw Gruszka pm_runtime_get_sync(&pdev->dev); 92002bf2fbSStanislaw Gruszka return 0; 93002bf2fbSStanislaw Gruszka } 94002bf2fbSStanislaw Gruszka 95002bf2fbSStanislaw Gruszka static int pci_pm_runtime_put(struct pci_dev *pdev, void *data) 96002bf2fbSStanislaw Gruszka { 97002bf2fbSStanislaw Gruszka pm_runtime_put(&pdev->dev); 98002bf2fbSStanislaw Gruszka return 0; 99002bf2fbSStanislaw Gruszka } 100002bf2fbSStanislaw Gruszka 101542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data) 102542aeb9cSKeith Busch { 103542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_frozen, data); 104542aeb9cSKeith Busch } 105542aeb9cSKeith Busch 106542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data) 107542aeb9cSKeith Busch { 108542aeb9cSKeith Busch return report_error_detected(dev, pci_channel_io_normal, data); 109542aeb9cSKeith Busch } 110542aeb9cSKeith Busch 1112e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data) 1122e28bc84SOza Pawandeep { 113171d149cSBjorn Helgaas struct pci_driver *pdrv; 114542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 1152e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1162e28bc84SOza Pawandeep 1172e28bc84SOza Pawandeep device_lock(&dev->dev); 118e0217c5bSBjorn Helgaas pdrv = dev->driver; 119*c9758cc4SIlpo Järvinen if (!pdrv || !pdrv->err_handler || !pdrv->err_handler->mmio_enabled) 1202e28bc84SOza Pawandeep goto out; 1212e28bc84SOza Pawandeep 122171d149cSBjorn Helgaas err_handler = pdrv->err_handler; 1232e28bc84SOza Pawandeep vote = err_handler->mmio_enabled(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_slot_reset(struct pci_dev *dev, void *data) 1312e28bc84SOza Pawandeep { 132171d149cSBjorn Helgaas struct pci_driver *pdrv; 133542aeb9cSKeith Busch pci_ers_result_t vote, *result = data; 1342e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1352e28bc84SOza Pawandeep 1362e28bc84SOza Pawandeep device_lock(&dev->dev); 137e0217c5bSBjorn Helgaas pdrv = dev->driver; 138*c9758cc4SIlpo Järvinen if (!pdrv || !pdrv->err_handler || !pdrv->err_handler->slot_reset) 1392e28bc84SOza Pawandeep goto out; 1402e28bc84SOza Pawandeep 141171d149cSBjorn Helgaas err_handler = pdrv->err_handler; 1422e28bc84SOza Pawandeep vote = err_handler->slot_reset(dev); 143542aeb9cSKeith Busch *result = merge_result(*result, vote); 1442e28bc84SOza Pawandeep out: 1452e28bc84SOza Pawandeep device_unlock(&dev->dev); 1462e28bc84SOza Pawandeep return 0; 1472e28bc84SOza Pawandeep } 1482e28bc84SOza Pawandeep 1492e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data) 1502e28bc84SOza Pawandeep { 151171d149cSBjorn Helgaas struct pci_driver *pdrv; 1522e28bc84SOza Pawandeep const struct pci_error_handlers *err_handler; 1532e28bc84SOza Pawandeep 1542e28bc84SOza Pawandeep device_lock(&dev->dev); 155e0217c5bSBjorn Helgaas pdrv = dev->driver; 156a6bd101bSKeith Busch if (!pci_dev_set_io_state(dev, pci_channel_io_normal) || 157*c9758cc4SIlpo Järvinen !pdrv || !pdrv->err_handler || !pdrv->err_handler->resume) 1582e28bc84SOza Pawandeep goto out; 1592e28bc84SOza Pawandeep 160171d149cSBjorn Helgaas err_handler = pdrv->err_handler; 1612e28bc84SOza Pawandeep err_handler->resume(dev); 1622e28bc84SOza Pawandeep out: 1637b42d97eSKeith Busch pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED); 1642e28bc84SOza Pawandeep device_unlock(&dev->dev); 1652e28bc84SOza Pawandeep return 0; 1662e28bc84SOza Pawandeep } 1672e28bc84SOza Pawandeep 16805e9ae19SSean V Kelley /** 16905e9ae19SSean V Kelley * pci_walk_bridge - walk bridges potentially AER affected 17057908622SQiuxu Zhuo * @bridge: bridge which may be a Port, an RCEC, or an RCiEP 17105e9ae19SSean V Kelley * @cb: callback to be called for each device found 17205e9ae19SSean V Kelley * @userdata: arbitrary pointer to be passed to callback 17305e9ae19SSean V Kelley * 17405e9ae19SSean V Kelley * If the device provided is a bridge, walk the subordinate bus, including 17505e9ae19SSean V Kelley * any bridged devices on buses under this bus. Call the provided callback 17605e9ae19SSean V Kelley * on each device found. 177a175102bSSean V Kelley * 17857908622SQiuxu Zhuo * If the device provided has no subordinate bus, e.g., an RCEC or RCiEP, 17957908622SQiuxu Zhuo * call the callback on the device itself. 18005e9ae19SSean V Kelley */ 18105e9ae19SSean V Kelley static void pci_walk_bridge(struct pci_dev *bridge, 18205e9ae19SSean V Kelley int (*cb)(struct pci_dev *, void *), 18305e9ae19SSean V Kelley void *userdata) 18405e9ae19SSean V Kelley { 18505e9ae19SSean V Kelley if (bridge->subordinate) 18605e9ae19SSean V Kelley pci_walk_bus(bridge->subordinate, cb, userdata); 187a175102bSSean V Kelley else 188a175102bSSean V Kelley cb(bridge, userdata); 18905e9ae19SSean V Kelley } 19005e9ae19SSean V Kelley 191e8e5ff2aSKuppuswamy Sathyanarayanan pci_ers_result_t pcie_do_recovery(struct pci_dev *dev, 19216d79cd4SLuc Van Oostenryck pci_channel_state_t state, 1938f1bbfbcSSean V Kelley pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev)) 1942e28bc84SOza Pawandeep { 195480ef7cbSSean V Kelley int type = pci_pcie_type(dev); 1960791721dSSean V Kelley struct pci_dev *bridge; 1970791721dSSean V Kelley pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER; 198aa344bc8SSean V Kelley struct pci_host_bridge *host = pci_find_host_bridge(dev->bus); 1992e28bc84SOza Pawandeep 200bfcb79fcSKeith Busch /* 20157908622SQiuxu Zhuo * If the error was detected by a Root Port, Downstream Port, RCEC, 20257908622SQiuxu Zhuo * or RCiEP, recovery runs on the device itself. For Ports, that 20357908622SQiuxu Zhuo * also includes any subordinate devices. 204a175102bSSean V Kelley * 205a175102bSSean V Kelley * If it was detected by another device (Endpoint, etc), recovery 206a175102bSSean V Kelley * runs on the device and anything else under the same Port, i.e., 207a175102bSSean V Kelley * everything under "bridge". 208bfcb79fcSKeith Busch */ 2093d7d8fc7SSean V Kelley if (type == PCI_EXP_TYPE_ROOT_PORT || 210a175102bSSean V Kelley type == PCI_EXP_TYPE_DOWNSTREAM || 21157908622SQiuxu Zhuo type == PCI_EXP_TYPE_RC_EC || 21257908622SQiuxu Zhuo type == PCI_EXP_TYPE_RC_END) 2130791721dSSean V Kelley bridge = dev; 2143d7d8fc7SSean V Kelley else 2153d7d8fc7SSean V Kelley bridge = pci_upstream_bridge(dev); 216bfcb79fcSKeith Busch 217002bf2fbSStanislaw Gruszka pci_walk_bridge(bridge, pci_pm_runtime_get_sync, NULL); 218002bf2fbSStanislaw Gruszka 2190791721dSSean V Kelley pci_dbg(bridge, "broadcast error_detected message\n"); 220b5dfbeacSKuppuswamy Sathyanarayanan if (state == pci_channel_io_frozen) { 22105e9ae19SSean V Kelley pci_walk_bridge(bridge, report_frozen_detected, &status); 222387c72cdSKeith Busch if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) { 2230791721dSSean V Kelley pci_warn(bridge, "subordinate device reset failed\n"); 224bdb5ac85SKeith Busch goto failed; 225b6cf1a42SKuppuswamy Sathyanarayanan } 226b5dfbeacSKuppuswamy Sathyanarayanan } else { 22705e9ae19SSean V Kelley pci_walk_bridge(bridge, report_normal_detected, &status); 228b5dfbeacSKuppuswamy Sathyanarayanan } 229bdb5ac85SKeith Busch 230542aeb9cSKeith Busch if (status == PCI_ERS_RESULT_CAN_RECOVER) { 231542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 2320791721dSSean V Kelley pci_dbg(bridge, "broadcast mmio_enabled message\n"); 23305e9ae19SSean V Kelley pci_walk_bridge(bridge, report_mmio_enabled, &status); 234542aeb9cSKeith Busch } 2352e28bc84SOza Pawandeep 2362e28bc84SOza Pawandeep if (status == PCI_ERS_RESULT_NEED_RESET) { 2372e28bc84SOza Pawandeep /* 2382e28bc84SOza Pawandeep * TODO: Should call platform-specific 2392e28bc84SOza Pawandeep * functions to reset slot before calling 2402e28bc84SOza Pawandeep * drivers' slot_reset callbacks? 2412e28bc84SOza Pawandeep */ 242542aeb9cSKeith Busch status = PCI_ERS_RESULT_RECOVERED; 2430791721dSSean V Kelley pci_dbg(bridge, "broadcast slot_reset message\n"); 24405e9ae19SSean V Kelley pci_walk_bridge(bridge, report_slot_reset, &status); 2452e28bc84SOza Pawandeep } 2462e28bc84SOza Pawandeep 2472e28bc84SOza Pawandeep if (status != PCI_ERS_RESULT_RECOVERED) 2482e28bc84SOza Pawandeep goto failed; 2492e28bc84SOza Pawandeep 2500791721dSSean V Kelley pci_dbg(bridge, "broadcast resume message\n"); 25105e9ae19SSean V Kelley pci_walk_bridge(bridge, report_resume, &status); 2522e28bc84SOza Pawandeep 253aa344bc8SSean V Kelley /* 2547d7cbeabSKeith Busch * If we have native control of AER, clear error status in the device 2557d7cbeabSKeith Busch * that detected the error. If the platform retained control of AER, 2567d7cbeabSKeith Busch * it is responsible for clearing this status. In that case, the 2577d7cbeabSKeith Busch * signaling device may not even be visible to the OS. 258aa344bc8SSean V Kelley */ 259aa344bc8SSean V Kelley if (host->native_aer || pcie_ports_native) { 2607d7cbeabSKeith Busch pcie_clear_device_status(dev); 2617d7cbeabSKeith Busch pci_aer_clear_nonfatal_status(dev); 262aa344bc8SSean V Kelley } 263002bf2fbSStanislaw Gruszka 264002bf2fbSStanislaw Gruszka pci_walk_bridge(bridge, pci_pm_runtime_put, NULL); 265002bf2fbSStanislaw Gruszka 2660791721dSSean V Kelley pci_info(bridge, "device recovery successful\n"); 267e8e5ff2aSKuppuswamy Sathyanarayanan return status; 2682e28bc84SOza Pawandeep 2692e28bc84SOza Pawandeep failed: 270002bf2fbSStanislaw Gruszka pci_walk_bridge(bridge, pci_pm_runtime_put, NULL); 271002bf2fbSStanislaw Gruszka 2720791721dSSean V Kelley pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT); 2732e28bc84SOza Pawandeep 2742e28bc84SOza Pawandeep /* TODO: Should kernel panic here? */ 2750791721dSSean V Kelley pci_info(bridge, "device recovery failed\n"); 276e8e5ff2aSKuppuswamy Sathyanarayanan 277e8e5ff2aSKuppuswamy Sathyanarayanan return status; 2782e28bc84SOza Pawandeep } 279