1 // SPDX-License-Identifier: GPL-2.0
3 * This file implements the error recovery as a core part of PCIe error
4 * reporting. When a PCIe error is delivered, an error message will be
5 * collected and printed to console, then, an error recovery procedure
6 * will be executed by following the PCI error recovery rules.
8 * Copyright (C) 2006 Intel Corp.
9 * Tom Long Nguyen (tom.l.nguyen@intel.com)
10 * Zhang Yanmin (yanmin.zhang@intel.com)
13 #define dev_fmt(fmt) "AER: " fmt
15 #include <linux/pci.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/aer.h>
23 static pci_ers_result_t merge_result(enum pci_ers_result orig,
24 enum pci_ers_result new)
26 if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
27 return PCI_ERS_RESULT_NO_AER_DRIVER;
29 if (new == PCI_ERS_RESULT_NONE)
33 case PCI_ERS_RESULT_CAN_RECOVER:
34 case PCI_ERS_RESULT_RECOVERED:
37 case PCI_ERS_RESULT_DISCONNECT:
38 if (new == PCI_ERS_RESULT_NEED_RESET)
39 orig = PCI_ERS_RESULT_NEED_RESET;
48 static int report_error_detected(struct pci_dev *dev,
49 pci_channel_state_t state,
50 enum pci_ers_result *result)
52 pci_ers_result_t vote;
53 const struct pci_error_handlers *err_handler;
55 device_lock(&dev->dev);
56 if (!pci_dev_set_io_state(dev, state) ||
58 !dev->driver->err_handler ||
59 !dev->driver->err_handler->error_detected) {
61 * If any device in the subtree does not have an error_detected
62 * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
63 * error callbacks of "any" device in the subtree, and will
64 * exit in the disconnected error state.
66 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
67 vote = PCI_ERS_RESULT_NO_AER_DRIVER;
68 pci_info(dev, "can't recover (no error_detected callback)\n");
70 vote = PCI_ERS_RESULT_NONE;
73 err_handler = dev->driver->err_handler;
74 vote = err_handler->error_detected(dev, state);
76 pci_uevent_ers(dev, vote);
77 *result = merge_result(*result, vote);
78 device_unlock(&dev->dev);
82 static int report_frozen_detected(struct pci_dev *dev, void *data)
84 return report_error_detected(dev, pci_channel_io_frozen, data);
87 static int report_normal_detected(struct pci_dev *dev, void *data)
89 return report_error_detected(dev, pci_channel_io_normal, data);
92 static int report_mmio_enabled(struct pci_dev *dev, void *data)
94 pci_ers_result_t vote, *result = data;
95 const struct pci_error_handlers *err_handler;
97 device_lock(&dev->dev);
99 !dev->driver->err_handler ||
100 !dev->driver->err_handler->mmio_enabled)
103 err_handler = dev->driver->err_handler;
104 vote = err_handler->mmio_enabled(dev);
105 *result = merge_result(*result, vote);
107 device_unlock(&dev->dev);
111 static int report_slot_reset(struct pci_dev *dev, void *data)
113 pci_ers_result_t vote, *result = data;
114 const struct pci_error_handlers *err_handler;
116 device_lock(&dev->dev);
118 !dev->driver->err_handler ||
119 !dev->driver->err_handler->slot_reset)
122 err_handler = dev->driver->err_handler;
123 vote = err_handler->slot_reset(dev);
124 *result = merge_result(*result, vote);
126 device_unlock(&dev->dev);
130 static int report_resume(struct pci_dev *dev, void *data)
132 const struct pci_error_handlers *err_handler;
134 device_lock(&dev->dev);
135 if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
137 !dev->driver->err_handler ||
138 !dev->driver->err_handler->resume)
141 err_handler = dev->driver->err_handler;
142 err_handler->resume(dev);
144 pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
145 device_unlock(&dev->dev);
150 * pci_walk_bridge - walk bridges potentially AER affected
151 * @bridge: bridge which may be a Port or an RCEC
152 * @cb: callback to be called for each device found
153 * @userdata: arbitrary pointer to be passed to callback
155 * If the device provided is a bridge, walk the subordinate bus, including
156 * any bridged devices on buses under this bus. Call the provided callback
157 * on each device found.
159 * If the device provided has no subordinate bus, e.g., an RCEC, call the
160 * callback on the device itself.
162 static void pci_walk_bridge(struct pci_dev *bridge,
163 int (*cb)(struct pci_dev *, void *),
166 if (bridge->subordinate)
167 pci_walk_bus(bridge->subordinate, cb, userdata);
169 cb(bridge, userdata);
172 pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
173 pci_channel_state_t state,
174 pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
176 int type = pci_pcie_type(dev);
177 struct pci_dev *bridge;
178 pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
181 * If the error was detected by a Root Port, Downstream Port, or
182 * RCEC, recovery runs on the device itself. For Ports, that also
183 * includes any subordinate devices.
185 * If it was detected by another device (Endpoint, etc), recovery
186 * runs on the device and anything else under the same Port, i.e.,
187 * everything under "bridge".
189 if (type == PCI_EXP_TYPE_ROOT_PORT ||
190 type == PCI_EXP_TYPE_DOWNSTREAM ||
191 type == PCI_EXP_TYPE_RC_EC)
194 bridge = pci_upstream_bridge(dev);
196 pci_dbg(bridge, "broadcast error_detected message\n");
197 if (state == pci_channel_io_frozen) {
198 pci_walk_bridge(bridge, report_frozen_detected, &status);
199 if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) {
200 pci_warn(bridge, "subordinate device reset failed\n");
204 pci_walk_bridge(bridge, report_normal_detected, &status);
207 if (status == PCI_ERS_RESULT_CAN_RECOVER) {
208 status = PCI_ERS_RESULT_RECOVERED;
209 pci_dbg(bridge, "broadcast mmio_enabled message\n");
210 pci_walk_bridge(bridge, report_mmio_enabled, &status);
213 if (status == PCI_ERS_RESULT_NEED_RESET) {
215 * TODO: Should call platform-specific
216 * functions to reset slot before calling
217 * drivers' slot_reset callbacks?
219 status = PCI_ERS_RESULT_RECOVERED;
220 pci_dbg(bridge, "broadcast slot_reset message\n");
221 pci_walk_bridge(bridge, report_slot_reset, &status);
224 if (status != PCI_ERS_RESULT_RECOVERED)
227 pci_dbg(bridge, "broadcast resume message\n");
228 pci_walk_bridge(bridge, report_resume, &status);
230 if (pcie_aer_is_native(bridge))
231 pcie_clear_device_status(bridge);
232 pci_aer_clear_nonfatal_status(bridge);
233 pci_info(bridge, "device recovery successful\n");
237 pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
239 /* TODO: Should kernel panic here? */
240 pci_info(bridge, "device recovery failed\n");