xref: /linux/drivers/pci/pcie/err.c (revision 01daacfb9035e5b86d43a01f11a0614648f306c1)
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/kernel.h>
162e28bc84SOza Pawandeep #include <linux/errno.h>
172e28bc84SOza Pawandeep #include <linux/aer.h>
182e28bc84SOza Pawandeep #include "portdrv.h"
192e28bc84SOza Pawandeep #include "../pci.h"
202e28bc84SOza Pawandeep 
212e28bc84SOza Pawandeep static pci_ers_result_t merge_result(enum pci_ers_result orig,
222e28bc84SOza Pawandeep 				  enum pci_ers_result new)
232e28bc84SOza Pawandeep {
242e28bc84SOza Pawandeep 	if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
252e28bc84SOza Pawandeep 		return PCI_ERS_RESULT_NO_AER_DRIVER;
262e28bc84SOza Pawandeep 
272e28bc84SOza Pawandeep 	if (new == PCI_ERS_RESULT_NONE)
282e28bc84SOza Pawandeep 		return orig;
292e28bc84SOza Pawandeep 
302e28bc84SOza Pawandeep 	switch (orig) {
312e28bc84SOza Pawandeep 	case PCI_ERS_RESULT_CAN_RECOVER:
322e28bc84SOza Pawandeep 	case PCI_ERS_RESULT_RECOVERED:
332e28bc84SOza Pawandeep 		orig = new;
342e28bc84SOza Pawandeep 		break;
352e28bc84SOza Pawandeep 	case PCI_ERS_RESULT_DISCONNECT:
362e28bc84SOza Pawandeep 		if (new == PCI_ERS_RESULT_NEED_RESET)
372e28bc84SOza Pawandeep 			orig = PCI_ERS_RESULT_NEED_RESET;
382e28bc84SOza Pawandeep 		break;
392e28bc84SOza Pawandeep 	default:
402e28bc84SOza Pawandeep 		break;
412e28bc84SOza Pawandeep 	}
422e28bc84SOza Pawandeep 
432e28bc84SOza Pawandeep 	return orig;
442e28bc84SOza Pawandeep }
452e28bc84SOza Pawandeep 
46542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev,
47542aeb9cSKeith Busch 				 enum pci_channel_state state,
48542aeb9cSKeith Busch 				 enum pci_ers_result *result)
492e28bc84SOza Pawandeep {
502e28bc84SOza Pawandeep 	pci_ers_result_t vote;
512e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
522e28bc84SOza Pawandeep 
532e28bc84SOza Pawandeep 	device_lock(&dev->dev);
54a6bd101bSKeith Busch 	if (!pci_dev_set_io_state(dev, state) ||
55a6bd101bSKeith Busch 		!dev->driver ||
562e28bc84SOza Pawandeep 		!dev->driver->err_handler ||
572e28bc84SOza Pawandeep 		!dev->driver->err_handler->error_detected) {
582e28bc84SOza Pawandeep 		/*
59bfcb79fcSKeith Busch 		 * If any device in the subtree does not have an error_detected
60bfcb79fcSKeith Busch 		 * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
61bfcb79fcSKeith Busch 		 * error callbacks of "any" device in the subtree, and will
62bfcb79fcSKeith Busch 		 * exit in the disconnected error state.
632e28bc84SOza Pawandeep 		 */
64*01daacfbSYicong Yang 		if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
652e28bc84SOza Pawandeep 			vote = PCI_ERS_RESULT_NO_AER_DRIVER;
66*01daacfbSYicong Yang 			pci_info(dev, "AER: Can't recover (no error_detected callback)\n");
67*01daacfbSYicong Yang 		} else {
682e28bc84SOza Pawandeep 			vote = PCI_ERS_RESULT_NONE;
69*01daacfbSYicong Yang 		}
702e28bc84SOza Pawandeep 	} else {
712e28bc84SOza Pawandeep 		err_handler = dev->driver->err_handler;
72542aeb9cSKeith Busch 		vote = err_handler->error_detected(dev, state);
732e28bc84SOza Pawandeep 	}
747b42d97eSKeith Busch 	pci_uevent_ers(dev, vote);
75542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
762e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
772e28bc84SOza Pawandeep 	return 0;
782e28bc84SOza Pawandeep }
792e28bc84SOza Pawandeep 
80542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data)
81542aeb9cSKeith Busch {
82542aeb9cSKeith Busch 	return report_error_detected(dev, pci_channel_io_frozen, data);
83542aeb9cSKeith Busch }
84542aeb9cSKeith Busch 
85542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data)
86542aeb9cSKeith Busch {
87542aeb9cSKeith Busch 	return report_error_detected(dev, pci_channel_io_normal, data);
88542aeb9cSKeith Busch }
89542aeb9cSKeith Busch 
902e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data)
912e28bc84SOza Pawandeep {
92542aeb9cSKeith Busch 	pci_ers_result_t vote, *result = data;
932e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
942e28bc84SOza Pawandeep 
952e28bc84SOza Pawandeep 	device_lock(&dev->dev);
962e28bc84SOza Pawandeep 	if (!dev->driver ||
972e28bc84SOza Pawandeep 		!dev->driver->err_handler ||
982e28bc84SOza Pawandeep 		!dev->driver->err_handler->mmio_enabled)
992e28bc84SOza Pawandeep 		goto out;
1002e28bc84SOza Pawandeep 
1012e28bc84SOza Pawandeep 	err_handler = dev->driver->err_handler;
1022e28bc84SOza Pawandeep 	vote = err_handler->mmio_enabled(dev);
103542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
1042e28bc84SOza Pawandeep out:
1052e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1062e28bc84SOza Pawandeep 	return 0;
1072e28bc84SOza Pawandeep }
1082e28bc84SOza Pawandeep 
1092e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data)
1102e28bc84SOza Pawandeep {
111542aeb9cSKeith Busch 	pci_ers_result_t vote, *result = data;
1122e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1132e28bc84SOza Pawandeep 
1142e28bc84SOza Pawandeep 	device_lock(&dev->dev);
1152e28bc84SOza Pawandeep 	if (!dev->driver ||
1162e28bc84SOza Pawandeep 		!dev->driver->err_handler ||
1172e28bc84SOza Pawandeep 		!dev->driver->err_handler->slot_reset)
1182e28bc84SOza Pawandeep 		goto out;
1192e28bc84SOza Pawandeep 
1202e28bc84SOza Pawandeep 	err_handler = dev->driver->err_handler;
1212e28bc84SOza Pawandeep 	vote = err_handler->slot_reset(dev);
122542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
1232e28bc84SOza Pawandeep out:
1242e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1252e28bc84SOza Pawandeep 	return 0;
1262e28bc84SOza Pawandeep }
1272e28bc84SOza Pawandeep 
1282e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data)
1292e28bc84SOza Pawandeep {
1302e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1312e28bc84SOza Pawandeep 
1322e28bc84SOza Pawandeep 	device_lock(&dev->dev);
133a6bd101bSKeith Busch 	if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
134a6bd101bSKeith Busch 		!dev->driver ||
1352e28bc84SOza Pawandeep 		!dev->driver->err_handler ||
1362e28bc84SOza Pawandeep 		!dev->driver->err_handler->resume)
1372e28bc84SOza Pawandeep 		goto out;
1382e28bc84SOza Pawandeep 
1392e28bc84SOza Pawandeep 	err_handler = dev->driver->err_handler;
1402e28bc84SOza Pawandeep 	err_handler->resume(dev);
1412e28bc84SOza Pawandeep out:
1427b42d97eSKeith Busch 	pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
1432e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1442e28bc84SOza Pawandeep 	return 0;
1452e28bc84SOza Pawandeep }
1462e28bc84SOza Pawandeep 
1472e28bc84SOza Pawandeep /**
1482e28bc84SOza Pawandeep  * default_reset_link - default reset function
1492e28bc84SOza Pawandeep  * @dev: pointer to pci_dev data structure
1502e28bc84SOza Pawandeep  *
1512e28bc84SOza Pawandeep  * Invoked when performing link reset on a Downstream Port or a
1522e28bc84SOza Pawandeep  * Root Port with no aer driver.
1532e28bc84SOza Pawandeep  */
1542e28bc84SOza Pawandeep static pci_ers_result_t default_reset_link(struct pci_dev *dev)
1552e28bc84SOza Pawandeep {
15618426238SSinan Kaya 	int rc;
15718426238SSinan Kaya 
158c4eed62aSKeith Busch 	rc = pci_bus_error_reset(dev);
1592e28bc84SOza Pawandeep 	pci_printk(KERN_DEBUG, dev, "downstream link has been reset\n");
16018426238SSinan Kaya 	return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
1612e28bc84SOza Pawandeep }
1622e28bc84SOza Pawandeep 
1630b91439dSOza Pawandeep static pci_ers_result_t reset_link(struct pci_dev *dev, u32 service)
1642e28bc84SOza Pawandeep {
1652e28bc84SOza Pawandeep 	pci_ers_result_t status;
1662e28bc84SOza Pawandeep 	struct pcie_port_service_driver *driver = NULL;
1672e28bc84SOza Pawandeep 
168bfcb79fcSKeith Busch 	driver = pcie_port_find_service(dev, service);
1692e28bc84SOza Pawandeep 	if (driver && driver->reset_link) {
170bfcb79fcSKeith Busch 		status = driver->reset_link(dev);
171ca784104SMika Westerberg 	} else if (pcie_downstream_port(dev)) {
172bfcb79fcSKeith Busch 		status = default_reset_link(dev);
1732e28bc84SOza Pawandeep 	} else {
1742e28bc84SOza Pawandeep 		pci_printk(KERN_DEBUG, dev, "no link-reset support at upstream device %s\n",
175bfcb79fcSKeith Busch 			pci_name(dev));
1762e28bc84SOza Pawandeep 		return PCI_ERS_RESULT_DISCONNECT;
1772e28bc84SOza Pawandeep 	}
1782e28bc84SOza Pawandeep 
1792e28bc84SOza Pawandeep 	if (status != PCI_ERS_RESULT_RECOVERED) {
1802e28bc84SOza Pawandeep 		pci_printk(KERN_DEBUG, dev, "link reset at upstream device %s failed\n",
181bfcb79fcSKeith Busch 			pci_name(dev));
1822e28bc84SOza Pawandeep 		return PCI_ERS_RESULT_DISCONNECT;
1832e28bc84SOza Pawandeep 	}
1842e28bc84SOza Pawandeep 
1852e28bc84SOza Pawandeep 	return status;
1862e28bc84SOza Pawandeep }
1872e28bc84SOza Pawandeep 
188bdb5ac85SKeith Busch void pcie_do_recovery(struct pci_dev *dev, enum pci_channel_state state,
189bdb5ac85SKeith Busch 		      u32 service)
1902e28bc84SOza Pawandeep {
191542aeb9cSKeith Busch 	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
192542aeb9cSKeith Busch 	struct pci_bus *bus;
1932e28bc84SOza Pawandeep 
194bfcb79fcSKeith Busch 	/*
195bfcb79fcSKeith Busch 	 * Error recovery runs on all subordinates of the first downstream port.
196bfcb79fcSKeith Busch 	 * If the downstream port detected the error, it is cleared at the end.
197bfcb79fcSKeith Busch 	 */
198bfcb79fcSKeith Busch 	if (!(pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
199bfcb79fcSKeith Busch 	      pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM))
200bfcb79fcSKeith Busch 		dev = dev->bus->self;
201542aeb9cSKeith Busch 	bus = dev->subordinate;
202bfcb79fcSKeith Busch 
203542aeb9cSKeith Busch 	pci_dbg(dev, "broadcast error_detected message\n");
204542aeb9cSKeith Busch 	if (state == pci_channel_io_frozen)
205542aeb9cSKeith Busch 		pci_walk_bus(bus, report_frozen_detected, &status);
206542aeb9cSKeith Busch 	else
207542aeb9cSKeith Busch 		pci_walk_bus(bus, report_normal_detected, &status);
2082e28bc84SOza Pawandeep 
209bdb5ac85SKeith Busch 	if (state == pci_channel_io_frozen &&
210bdb5ac85SKeith Busch 	    reset_link(dev, service) != PCI_ERS_RESULT_RECOVERED)
211bdb5ac85SKeith Busch 		goto failed;
212bdb5ac85SKeith Busch 
213542aeb9cSKeith Busch 	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
214542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
215542aeb9cSKeith Busch 		pci_dbg(dev, "broadcast mmio_enabled message\n");
216542aeb9cSKeith Busch 		pci_walk_bus(bus, report_mmio_enabled, &status);
217542aeb9cSKeith Busch 	}
2182e28bc84SOza Pawandeep 
2192e28bc84SOza Pawandeep 	if (status == PCI_ERS_RESULT_NEED_RESET) {
2202e28bc84SOza Pawandeep 		/*
2212e28bc84SOza Pawandeep 		 * TODO: Should call platform-specific
2222e28bc84SOza Pawandeep 		 * functions to reset slot before calling
2232e28bc84SOza Pawandeep 		 * drivers' slot_reset callbacks?
2242e28bc84SOza Pawandeep 		 */
225542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
226542aeb9cSKeith Busch 		pci_dbg(dev, "broadcast slot_reset message\n");
227542aeb9cSKeith Busch 		pci_walk_bus(bus, report_slot_reset, &status);
2282e28bc84SOza Pawandeep 	}
2292e28bc84SOza Pawandeep 
2302e28bc84SOza Pawandeep 	if (status != PCI_ERS_RESULT_RECOVERED)
2312e28bc84SOza Pawandeep 		goto failed;
2322e28bc84SOza Pawandeep 
233542aeb9cSKeith Busch 	pci_dbg(dev, "broadcast resume message\n");
234542aeb9cSKeith Busch 	pci_walk_bus(bus, report_resume, &status);
2352e28bc84SOza Pawandeep 
236bfcb79fcSKeith Busch 	pci_aer_clear_device_status(dev);
237bfcb79fcSKeith Busch 	pci_cleanup_aer_uncorrect_error_status(dev);
2382e28bc84SOza Pawandeep 	pci_info(dev, "AER: Device recovery successful\n");
2392e28bc84SOza Pawandeep 	return;
2402e28bc84SOza Pawandeep 
2412e28bc84SOza Pawandeep failed:
2422e28bc84SOza Pawandeep 	pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT);
2432e28bc84SOza Pawandeep 
2442e28bc84SOza Pawandeep 	/* TODO: Should kernel panic here? */
2452e28bc84SOza Pawandeep 	pci_info(dev, "AER: Device recovery failed\n");
2462e28bc84SOza Pawandeep }
247