xref: /linux/drivers/pci/pcie/err.c (revision a175102b0a82fc57853a9e611c42d1d6172e5180)
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 
14905e9ae19SSean V Kelley /**
15005e9ae19SSean V Kelley  * pci_walk_bridge - walk bridges potentially AER affected
151*a175102bSSean V Kelley  * @bridge:	bridge which may be a Port or an RCEC
15205e9ae19SSean V Kelley  * @cb:		callback to be called for each device found
15305e9ae19SSean V Kelley  * @userdata:	arbitrary pointer to be passed to callback
15405e9ae19SSean V Kelley  *
15505e9ae19SSean V Kelley  * If the device provided is a bridge, walk the subordinate bus, including
15605e9ae19SSean V Kelley  * any bridged devices on buses under this bus.  Call the provided callback
15705e9ae19SSean V Kelley  * on each device found.
158*a175102bSSean V Kelley  *
159*a175102bSSean V Kelley  * If the device provided has no subordinate bus, e.g., an RCEC, call the
160*a175102bSSean V Kelley  * callback on the device itself.
16105e9ae19SSean V Kelley  */
16205e9ae19SSean V Kelley static void pci_walk_bridge(struct pci_dev *bridge,
16305e9ae19SSean V Kelley 			    int (*cb)(struct pci_dev *, void *),
16405e9ae19SSean V Kelley 			    void *userdata)
16505e9ae19SSean V Kelley {
16605e9ae19SSean V Kelley 	if (bridge->subordinate)
16705e9ae19SSean V Kelley 		pci_walk_bus(bridge->subordinate, cb, userdata);
168*a175102bSSean V Kelley 	else
169*a175102bSSean V Kelley 		cb(bridge, userdata);
17005e9ae19SSean V Kelley }
17105e9ae19SSean V Kelley 
172e8e5ff2aSKuppuswamy Sathyanarayanan pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
17316d79cd4SLuc Van Oostenryck 		pci_channel_state_t state,
1748f1bbfbcSSean V Kelley 		pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
1752e28bc84SOza Pawandeep {
176480ef7cbSSean V Kelley 	int type = pci_pcie_type(dev);
1770791721dSSean V Kelley 	struct pci_dev *bridge;
1780791721dSSean V Kelley 	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
179aa344bc8SSean V Kelley 	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
1802e28bc84SOza Pawandeep 
181bfcb79fcSKeith Busch 	/*
182*a175102bSSean V Kelley 	 * If the error was detected by a Root Port, Downstream Port, or
183*a175102bSSean V Kelley 	 * RCEC, recovery runs on the device itself.  For Ports, that also
184*a175102bSSean V Kelley 	 * includes any subordinate devices.
185*a175102bSSean V Kelley 	 *
186*a175102bSSean V Kelley 	 * If it was detected by another device (Endpoint, etc), recovery
187*a175102bSSean V Kelley 	 * runs on the device and anything else under the same Port, i.e.,
188*a175102bSSean V Kelley 	 * everything under "bridge".
189bfcb79fcSKeith Busch 	 */
1903d7d8fc7SSean V Kelley 	if (type == PCI_EXP_TYPE_ROOT_PORT ||
191*a175102bSSean V Kelley 	    type == PCI_EXP_TYPE_DOWNSTREAM ||
192*a175102bSSean V Kelley 	    type == PCI_EXP_TYPE_RC_EC)
1930791721dSSean V Kelley 		bridge = dev;
1943d7d8fc7SSean V Kelley 	else
1953d7d8fc7SSean V Kelley 		bridge = pci_upstream_bridge(dev);
196bfcb79fcSKeith Busch 
1970791721dSSean V Kelley 	pci_dbg(bridge, "broadcast error_detected message\n");
198b5dfbeacSKuppuswamy Sathyanarayanan 	if (state == pci_channel_io_frozen) {
19905e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_frozen_detected, &status);
2000791721dSSean V Kelley 		status = reset_subordinates(bridge);
201b6cf1a42SKuppuswamy Sathyanarayanan 		if (status != PCI_ERS_RESULT_RECOVERED) {
2020791721dSSean V Kelley 			pci_warn(bridge, "subordinate device reset failed\n");
203bdb5ac85SKeith Busch 			goto failed;
204b6cf1a42SKuppuswamy Sathyanarayanan 		}
205b5dfbeacSKuppuswamy Sathyanarayanan 	} else {
20605e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_normal_detected, &status);
207b5dfbeacSKuppuswamy Sathyanarayanan 	}
208bdb5ac85SKeith Busch 
209542aeb9cSKeith Busch 	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
210542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
2110791721dSSean V Kelley 		pci_dbg(bridge, "broadcast mmio_enabled message\n");
21205e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_mmio_enabled, &status);
213542aeb9cSKeith Busch 	}
2142e28bc84SOza Pawandeep 
2152e28bc84SOza Pawandeep 	if (status == PCI_ERS_RESULT_NEED_RESET) {
2162e28bc84SOza Pawandeep 		/*
2172e28bc84SOza Pawandeep 		 * TODO: Should call platform-specific
2182e28bc84SOza Pawandeep 		 * functions to reset slot before calling
2192e28bc84SOza Pawandeep 		 * drivers' slot_reset callbacks?
2202e28bc84SOza Pawandeep 		 */
221542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
2220791721dSSean V Kelley 		pci_dbg(bridge, "broadcast slot_reset message\n");
22305e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_slot_reset, &status);
2242e28bc84SOza Pawandeep 	}
2252e28bc84SOza Pawandeep 
2262e28bc84SOza Pawandeep 	if (status != PCI_ERS_RESULT_RECOVERED)
2272e28bc84SOza Pawandeep 		goto failed;
2282e28bc84SOza Pawandeep 
2290791721dSSean V Kelley 	pci_dbg(bridge, "broadcast resume message\n");
23005e9ae19SSean V Kelley 	pci_walk_bridge(bridge, report_resume, &status);
2312e28bc84SOza Pawandeep 
232aa344bc8SSean V Kelley 	/*
233aa344bc8SSean V Kelley 	 * If we have native control of AER, clear error status in the Root
234aa344bc8SSean V Kelley 	 * Port or Downstream Port that signaled the error.  If the
235aa344bc8SSean V Kelley 	 * platform retained control of AER, it is responsible for clearing
236aa344bc8SSean V Kelley 	 * this status.  In that case, the signaling device may not even be
237aa344bc8SSean V Kelley 	 * visible to the OS.
238aa344bc8SSean V Kelley 	 */
239aa344bc8SSean V Kelley 	if (host->native_aer || pcie_ports_native) {
2400791721dSSean V Kelley 		pcie_clear_device_status(bridge);
2410791721dSSean V Kelley 		pci_aer_clear_nonfatal_status(bridge);
242aa344bc8SSean V Kelley 	}
2430791721dSSean V Kelley 	pci_info(bridge, "device recovery successful\n");
244e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2452e28bc84SOza Pawandeep 
2462e28bc84SOza Pawandeep failed:
2470791721dSSean V Kelley 	pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
2482e28bc84SOza Pawandeep 
2492e28bc84SOza Pawandeep 	/* TODO: Should kernel panic here? */
2500791721dSSean V Kelley 	pci_info(bridge, "device recovery failed\n");
251e8e5ff2aSKuppuswamy Sathyanarayanan 
252e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2532e28bc84SOza Pawandeep }
254