xref: /linux/drivers/pci/pcie/err.c (revision 171d149ce8d11f7fafbd4f338f14d472a153c3d4)
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 {
52*171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
532e28bc84SOza Pawandeep 	pci_ers_result_t vote;
542e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
552e28bc84SOza Pawandeep 
562e28bc84SOza Pawandeep 	device_lock(&dev->dev);
57*171d149cSBjorn Helgaas 	pdrv = dev->driver;
58a6bd101bSKeith Busch 	if (!pci_dev_set_io_state(dev, state) ||
59*171d149cSBjorn Helgaas 		!pdrv ||
60*171d149cSBjorn Helgaas 		!pdrv->err_handler ||
61*171d149cSBjorn Helgaas 		!pdrv->err_handler->error_detected) {
622e28bc84SOza Pawandeep 		/*
63bfcb79fcSKeith Busch 		 * If any device in the subtree does not have an error_detected
64bfcb79fcSKeith Busch 		 * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
65bfcb79fcSKeith Busch 		 * error callbacks of "any" device in the subtree, and will
66bfcb79fcSKeith Busch 		 * exit in the disconnected error state.
672e28bc84SOza Pawandeep 		 */
6801daacfbSYicong Yang 		if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
692e28bc84SOza Pawandeep 			vote = PCI_ERS_RESULT_NO_AER_DRIVER;
708d077c3cSBjorn Helgaas 			pci_info(dev, "can't recover (no error_detected callback)\n");
7101daacfbSYicong Yang 		} else {
722e28bc84SOza Pawandeep 			vote = PCI_ERS_RESULT_NONE;
7301daacfbSYicong Yang 		}
742e28bc84SOza Pawandeep 	} else {
75*171d149cSBjorn Helgaas 		err_handler = pdrv->err_handler;
76542aeb9cSKeith Busch 		vote = err_handler->error_detected(dev, state);
772e28bc84SOza Pawandeep 	}
787b42d97eSKeith Busch 	pci_uevent_ers(dev, vote);
79542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
802e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
812e28bc84SOza Pawandeep 	return 0;
822e28bc84SOza Pawandeep }
832e28bc84SOza Pawandeep 
84542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data)
85542aeb9cSKeith Busch {
86542aeb9cSKeith Busch 	return report_error_detected(dev, pci_channel_io_frozen, data);
87542aeb9cSKeith Busch }
88542aeb9cSKeith Busch 
89542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data)
90542aeb9cSKeith Busch {
91542aeb9cSKeith Busch 	return report_error_detected(dev, pci_channel_io_normal, data);
92542aeb9cSKeith Busch }
93542aeb9cSKeith Busch 
942e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data)
952e28bc84SOza Pawandeep {
96*171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
97542aeb9cSKeith Busch 	pci_ers_result_t vote, *result = data;
982e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
992e28bc84SOza Pawandeep 
1002e28bc84SOza Pawandeep 	device_lock(&dev->dev);
101*171d149cSBjorn Helgaas 	pdrv = dev->driver;
102*171d149cSBjorn Helgaas 	if (!pdrv ||
103*171d149cSBjorn Helgaas 		!pdrv->err_handler ||
104*171d149cSBjorn Helgaas 		!pdrv->err_handler->mmio_enabled)
1052e28bc84SOza Pawandeep 		goto out;
1062e28bc84SOza Pawandeep 
107*171d149cSBjorn Helgaas 	err_handler = pdrv->err_handler;
1082e28bc84SOza Pawandeep 	vote = err_handler->mmio_enabled(dev);
109542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
1102e28bc84SOza Pawandeep out:
1112e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1122e28bc84SOza Pawandeep 	return 0;
1132e28bc84SOza Pawandeep }
1142e28bc84SOza Pawandeep 
1152e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data)
1162e28bc84SOza Pawandeep {
117*171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
118542aeb9cSKeith Busch 	pci_ers_result_t vote, *result = data;
1192e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1202e28bc84SOza Pawandeep 
1212e28bc84SOza Pawandeep 	device_lock(&dev->dev);
122*171d149cSBjorn Helgaas 	pdrv = dev->driver;
123*171d149cSBjorn Helgaas 	if (!pdrv ||
124*171d149cSBjorn Helgaas 		!pdrv->err_handler ||
125*171d149cSBjorn Helgaas 		!pdrv->err_handler->slot_reset)
1262e28bc84SOza Pawandeep 		goto out;
1272e28bc84SOza Pawandeep 
128*171d149cSBjorn Helgaas 	err_handler = pdrv->err_handler;
1292e28bc84SOza Pawandeep 	vote = err_handler->slot_reset(dev);
130542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
1312e28bc84SOza Pawandeep out:
1322e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1332e28bc84SOza Pawandeep 	return 0;
1342e28bc84SOza Pawandeep }
1352e28bc84SOza Pawandeep 
1362e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data)
1372e28bc84SOza Pawandeep {
138*171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
1392e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1402e28bc84SOza Pawandeep 
1412e28bc84SOza Pawandeep 	device_lock(&dev->dev);
142*171d149cSBjorn Helgaas 	pdrv = dev->driver;
143a6bd101bSKeith Busch 	if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
144*171d149cSBjorn Helgaas 		!pdrv ||
145*171d149cSBjorn Helgaas 		!pdrv->err_handler ||
146*171d149cSBjorn Helgaas 		!pdrv->err_handler->resume)
1472e28bc84SOza Pawandeep 		goto out;
1482e28bc84SOza Pawandeep 
149*171d149cSBjorn Helgaas 	err_handler = pdrv->err_handler;
1502e28bc84SOza Pawandeep 	err_handler->resume(dev);
1512e28bc84SOza Pawandeep out:
1527b42d97eSKeith Busch 	pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
1532e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1542e28bc84SOza Pawandeep 	return 0;
1552e28bc84SOza Pawandeep }
1562e28bc84SOza Pawandeep 
15705e9ae19SSean V Kelley /**
15805e9ae19SSean V Kelley  * pci_walk_bridge - walk bridges potentially AER affected
15957908622SQiuxu Zhuo  * @bridge:	bridge which may be a Port, an RCEC, or an RCiEP
16005e9ae19SSean V Kelley  * @cb:		callback to be called for each device found
16105e9ae19SSean V Kelley  * @userdata:	arbitrary pointer to be passed to callback
16205e9ae19SSean V Kelley  *
16305e9ae19SSean V Kelley  * If the device provided is a bridge, walk the subordinate bus, including
16405e9ae19SSean V Kelley  * any bridged devices on buses under this bus.  Call the provided callback
16505e9ae19SSean V Kelley  * on each device found.
166a175102bSSean V Kelley  *
16757908622SQiuxu Zhuo  * If the device provided has no subordinate bus, e.g., an RCEC or RCiEP,
16857908622SQiuxu Zhuo  * call the callback on the device itself.
16905e9ae19SSean V Kelley  */
17005e9ae19SSean V Kelley static void pci_walk_bridge(struct pci_dev *bridge,
17105e9ae19SSean V Kelley 			    int (*cb)(struct pci_dev *, void *),
17205e9ae19SSean V Kelley 			    void *userdata)
17305e9ae19SSean V Kelley {
17405e9ae19SSean V Kelley 	if (bridge->subordinate)
17505e9ae19SSean V Kelley 		pci_walk_bus(bridge->subordinate, cb, userdata);
176a175102bSSean V Kelley 	else
177a175102bSSean V Kelley 		cb(bridge, userdata);
17805e9ae19SSean V Kelley }
17905e9ae19SSean V Kelley 
180e8e5ff2aSKuppuswamy Sathyanarayanan pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
18116d79cd4SLuc Van Oostenryck 		pci_channel_state_t state,
1828f1bbfbcSSean V Kelley 		pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
1832e28bc84SOza Pawandeep {
184480ef7cbSSean V Kelley 	int type = pci_pcie_type(dev);
1850791721dSSean V Kelley 	struct pci_dev *bridge;
1860791721dSSean V Kelley 	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
187aa344bc8SSean V Kelley 	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
1882e28bc84SOza Pawandeep 
189bfcb79fcSKeith Busch 	/*
19057908622SQiuxu Zhuo 	 * If the error was detected by a Root Port, Downstream Port, RCEC,
19157908622SQiuxu Zhuo 	 * or RCiEP, recovery runs on the device itself.  For Ports, that
19257908622SQiuxu Zhuo 	 * also includes any subordinate devices.
193a175102bSSean V Kelley 	 *
194a175102bSSean V Kelley 	 * If it was detected by another device (Endpoint, etc), recovery
195a175102bSSean V Kelley 	 * runs on the device and anything else under the same Port, i.e.,
196a175102bSSean V Kelley 	 * everything under "bridge".
197bfcb79fcSKeith Busch 	 */
1983d7d8fc7SSean V Kelley 	if (type == PCI_EXP_TYPE_ROOT_PORT ||
199a175102bSSean V Kelley 	    type == PCI_EXP_TYPE_DOWNSTREAM ||
20057908622SQiuxu Zhuo 	    type == PCI_EXP_TYPE_RC_EC ||
20157908622SQiuxu Zhuo 	    type == PCI_EXP_TYPE_RC_END)
2020791721dSSean V Kelley 		bridge = dev;
2033d7d8fc7SSean V Kelley 	else
2043d7d8fc7SSean V Kelley 		bridge = pci_upstream_bridge(dev);
205bfcb79fcSKeith Busch 
2060791721dSSean V Kelley 	pci_dbg(bridge, "broadcast error_detected message\n");
207b5dfbeacSKuppuswamy Sathyanarayanan 	if (state == pci_channel_io_frozen) {
20805e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_frozen_detected, &status);
209387c72cdSKeith Busch 		if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) {
2100791721dSSean V Kelley 			pci_warn(bridge, "subordinate device reset failed\n");
211bdb5ac85SKeith Busch 			goto failed;
212b6cf1a42SKuppuswamy Sathyanarayanan 		}
213b5dfbeacSKuppuswamy Sathyanarayanan 	} else {
21405e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_normal_detected, &status);
215b5dfbeacSKuppuswamy Sathyanarayanan 	}
216bdb5ac85SKeith Busch 
217542aeb9cSKeith Busch 	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
218542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
2190791721dSSean V Kelley 		pci_dbg(bridge, "broadcast mmio_enabled message\n");
22005e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_mmio_enabled, &status);
221542aeb9cSKeith Busch 	}
2222e28bc84SOza Pawandeep 
2232e28bc84SOza Pawandeep 	if (status == PCI_ERS_RESULT_NEED_RESET) {
2242e28bc84SOza Pawandeep 		/*
2252e28bc84SOza Pawandeep 		 * TODO: Should call platform-specific
2262e28bc84SOza Pawandeep 		 * functions to reset slot before calling
2272e28bc84SOza Pawandeep 		 * drivers' slot_reset callbacks?
2282e28bc84SOza Pawandeep 		 */
229542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
2300791721dSSean V Kelley 		pci_dbg(bridge, "broadcast slot_reset message\n");
23105e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_slot_reset, &status);
2322e28bc84SOza Pawandeep 	}
2332e28bc84SOza Pawandeep 
2342e28bc84SOza Pawandeep 	if (status != PCI_ERS_RESULT_RECOVERED)
2352e28bc84SOza Pawandeep 		goto failed;
2362e28bc84SOza Pawandeep 
2370791721dSSean V Kelley 	pci_dbg(bridge, "broadcast resume message\n");
23805e9ae19SSean V Kelley 	pci_walk_bridge(bridge, report_resume, &status);
2392e28bc84SOza Pawandeep 
240aa344bc8SSean V Kelley 	/*
2417d7cbeabSKeith Busch 	 * If we have native control of AER, clear error status in the device
2427d7cbeabSKeith Busch 	 * that detected the error.  If the platform retained control of AER,
2437d7cbeabSKeith Busch 	 * it is responsible for clearing this status.  In that case, the
2447d7cbeabSKeith Busch 	 * signaling device may not even be visible to the OS.
245aa344bc8SSean V Kelley 	 */
246aa344bc8SSean V Kelley 	if (host->native_aer || pcie_ports_native) {
2477d7cbeabSKeith Busch 		pcie_clear_device_status(dev);
2487d7cbeabSKeith Busch 		pci_aer_clear_nonfatal_status(dev);
249aa344bc8SSean V Kelley 	}
2500791721dSSean V Kelley 	pci_info(bridge, "device recovery successful\n");
251e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2522e28bc84SOza Pawandeep 
2532e28bc84SOza Pawandeep failed:
2540791721dSSean V Kelley 	pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
2552e28bc84SOza Pawandeep 
2562e28bc84SOza Pawandeep 	/* TODO: Should kernel panic here? */
2570791721dSSean V Kelley 	pci_info(bridge, "device recovery failed\n");
258e8e5ff2aSKuppuswamy Sathyanarayanan 
259e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2602e28bc84SOza Pawandeep }
261