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.
13 #include <linux/pci.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/aer.h>
22 struct aer_broadcast_data {
23 enum pci_channel_state state;
24 enum pci_ers_result result;
27 static pci_ers_result_t merge_result(enum pci_ers_result orig,
28 enum pci_ers_result new)
30 if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
31 return PCI_ERS_RESULT_NO_AER_DRIVER;
33 if (new == PCI_ERS_RESULT_NONE)
37 case PCI_ERS_RESULT_CAN_RECOVER:
38 case PCI_ERS_RESULT_RECOVERED:
41 case PCI_ERS_RESULT_DISCONNECT:
42 if (new == PCI_ERS_RESULT_NEED_RESET)
43 orig = PCI_ERS_RESULT_NEED_RESET;
52 static int report_error_detected(struct pci_dev *dev, void *data)
54 pci_ers_result_t vote;
55 const struct pci_error_handlers *err_handler;
56 struct aer_broadcast_data *result_data;
58 result_data = (struct aer_broadcast_data *) data;
60 device_lock(&dev->dev);
61 dev->error_state = result_data->state;
64 !dev->driver->err_handler ||
65 !dev->driver->err_handler->error_detected) {
66 if (result_data->state == pci_channel_io_frozen &&
67 dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
69 * In case of fatal recovery, if one of down-
70 * stream device has no driver. We might be
71 * unable to recover because a later insmod
72 * of a driver for this device is unaware of
75 pci_printk(KERN_DEBUG, dev, "device has %s\n",
77 "no AER-aware driver" : "no driver");
81 * If there's any device in the subtree that does not
82 * have an error_detected callback, returning
83 * PCI_ERS_RESULT_NO_AER_DRIVER prevents calling of
84 * the subsequent mmio_enabled/slot_reset/resume
85 * callbacks of "any" device in the subtree. All the
86 * devices in the subtree are left in the error state
90 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE)
91 vote = PCI_ERS_RESULT_NO_AER_DRIVER;
93 vote = PCI_ERS_RESULT_NONE;
95 err_handler = dev->driver->err_handler;
96 vote = err_handler->error_detected(dev, result_data->state);
97 pci_uevent_ers(dev, PCI_ERS_RESULT_NONE);
100 result_data->result = merge_result(result_data->result, vote);
101 device_unlock(&dev->dev);
105 static int report_mmio_enabled(struct pci_dev *dev, void *data)
107 pci_ers_result_t vote;
108 const struct pci_error_handlers *err_handler;
109 struct aer_broadcast_data *result_data;
111 result_data = (struct aer_broadcast_data *) data;
113 device_lock(&dev->dev);
115 !dev->driver->err_handler ||
116 !dev->driver->err_handler->mmio_enabled)
119 err_handler = dev->driver->err_handler;
120 vote = err_handler->mmio_enabled(dev);
121 result_data->result = merge_result(result_data->result, vote);
123 device_unlock(&dev->dev);
127 static int report_slot_reset(struct pci_dev *dev, void *data)
129 pci_ers_result_t vote;
130 const struct pci_error_handlers *err_handler;
131 struct aer_broadcast_data *result_data;
133 result_data = (struct aer_broadcast_data *) data;
135 device_lock(&dev->dev);
137 !dev->driver->err_handler ||
138 !dev->driver->err_handler->slot_reset)
141 err_handler = dev->driver->err_handler;
142 vote = err_handler->slot_reset(dev);
143 result_data->result = merge_result(result_data->result, vote);
145 device_unlock(&dev->dev);
149 static int report_resume(struct pci_dev *dev, void *data)
151 const struct pci_error_handlers *err_handler;
153 device_lock(&dev->dev);
154 dev->error_state = pci_channel_io_normal;
157 !dev->driver->err_handler ||
158 !dev->driver->err_handler->resume)
161 err_handler = dev->driver->err_handler;
162 err_handler->resume(dev);
163 pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
165 device_unlock(&dev->dev);
170 * default_reset_link - default reset function
171 * @dev: pointer to pci_dev data structure
173 * Invoked when performing link reset on a Downstream Port or a
174 * Root Port with no aer driver.
176 static pci_ers_result_t default_reset_link(struct pci_dev *dev)
180 rc = pci_bridge_secondary_bus_reset(dev);
181 pci_printk(KERN_DEBUG, dev, "downstream link has been reset\n");
182 return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
185 static pci_ers_result_t reset_link(struct pci_dev *dev, u32 service)
187 struct pci_dev *udev;
188 pci_ers_result_t status;
189 struct pcie_port_service_driver *driver = NULL;
191 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
192 /* Reset this port for all subordinates */
195 /* Reset the upstream component (likely downstream port) */
196 udev = dev->bus->self;
199 /* Use the aer driver of the component firstly */
200 driver = pcie_port_find_service(udev, service);
202 if (driver && driver->reset_link) {
203 status = driver->reset_link(udev);
204 } else if (udev->has_secondary_link) {
205 status = default_reset_link(udev);
207 pci_printk(KERN_DEBUG, dev, "no link-reset support at upstream device %s\n",
209 return PCI_ERS_RESULT_DISCONNECT;
212 if (status != PCI_ERS_RESULT_RECOVERED) {
213 pci_printk(KERN_DEBUG, dev, "link reset at upstream device %s failed\n",
215 return PCI_ERS_RESULT_DISCONNECT;
222 * broadcast_error_message - handle message broadcast to downstream drivers
223 * @dev: pointer to from where in a hierarchy message is broadcasted down
224 * @state: error state
225 * @error_mesg: message to print
226 * @cb: callback to be broadcasted
228 * Invoked during error recovery process. Once being invoked, the content
229 * of error severity will be broadcasted to all downstream drivers in a
230 * hierarchy in question.
232 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
233 enum pci_channel_state state,
235 int (*cb)(struct pci_dev *, void *))
237 struct aer_broadcast_data result_data;
239 pci_printk(KERN_DEBUG, dev, "broadcast %s message\n", error_mesg);
240 result_data.state = state;
241 if (cb == report_error_detected)
242 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
244 result_data.result = PCI_ERS_RESULT_RECOVERED;
246 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
248 * If the error is reported by a bridge, we think this error
249 * is related to the downstream link of the bridge, so we
250 * do error recovery on all subordinates of the bridge instead
251 * of the bridge and clear the error status of the bridge.
253 if (cb == report_error_detected)
254 dev->error_state = state;
255 pci_walk_bus(dev->subordinate, cb, &result_data);
256 if (cb == report_resume) {
257 pci_aer_clear_device_status(dev);
258 pci_cleanup_aer_uncorrect_error_status(dev);
259 dev->error_state = pci_channel_io_normal;
263 * If the error is reported by an end point, we think this
264 * error is related to the upstream link of the end point.
265 * The error is non fatal so the bus is ok; just invoke
266 * the callback for the function that logged the error.
268 cb(dev, &result_data);
271 return result_data.result;
275 * pcie_do_fatal_recovery - handle fatal error recovery process
276 * @dev: pointer to a pci_dev data structure of agent detecting an error
278 * Invoked when an error is fatal. Once being invoked, removes the devices
279 * beneath this AER agent, followed by reset link e.g. secondary bus reset
280 * followed by re-enumeration of devices.
282 void pcie_do_fatal_recovery(struct pci_dev *dev, u32 service)
284 struct pci_dev *udev;
285 struct pci_bus *parent;
286 struct pci_dev *pdev, *temp;
287 pci_ers_result_t result;
289 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
292 udev = dev->bus->self;
294 parent = udev->subordinate;
295 pci_lock_rescan_remove();
297 list_for_each_entry_safe_reverse(pdev, temp, &parent->devices,
300 pci_dev_set_disconnected(pdev, NULL);
301 if (pci_has_subordinate(pdev))
302 pci_walk_bus(pdev->subordinate,
303 pci_dev_set_disconnected, NULL);
304 pci_stop_and_remove_bus_device(pdev);
308 result = reset_link(udev, service);
310 if ((service == PCIE_PORT_SERVICE_AER) &&
311 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
313 * If the error is reported by a bridge, we think this error
314 * is related to the downstream link of the bridge, so we
315 * do error recovery on all subordinates of the bridge instead
316 * of the bridge and clear the error status of the bridge.
318 pci_aer_clear_fatal_status(dev);
319 pci_aer_clear_device_status(dev);
322 if (result == PCI_ERS_RESULT_RECOVERED) {
323 if (pcie_wait_for_link(udev, true))
324 pci_rescan_bus(udev->bus);
325 pci_info(dev, "Device recovery from fatal error successful\n");
327 pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT);
328 pci_info(dev, "Device recovery from fatal error failed\n");
332 pci_unlock_rescan_remove();
336 * pcie_do_nonfatal_recovery - handle nonfatal error recovery process
337 * @dev: pointer to a pci_dev data structure of agent detecting an error
339 * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
340 * error detected message to all downstream drivers within a hierarchy in
341 * question and return the returned code.
343 void pcie_do_nonfatal_recovery(struct pci_dev *dev)
345 pci_ers_result_t status;
346 enum pci_channel_state state;
348 state = pci_channel_io_normal;
350 status = broadcast_error_message(dev,
353 report_error_detected);
355 if (status == PCI_ERS_RESULT_CAN_RECOVER)
356 status = broadcast_error_message(dev,
359 report_mmio_enabled);
361 if (status == PCI_ERS_RESULT_NEED_RESET) {
363 * TODO: Should call platform-specific
364 * functions to reset slot before calling
365 * drivers' slot_reset callbacks?
367 status = broadcast_error_message(dev,
373 if (status != PCI_ERS_RESULT_RECOVERED)
376 broadcast_error_message(dev,
381 pci_info(dev, "AER: Device recovery successful\n");
385 pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT);
387 /* TODO: Should kernel panic here? */
388 pci_info(dev, "AER: Device recovery failed\n");