xref: /openbmc/linux/drivers/pci/pcie/err.c (revision 46eeaa11bdd1bc9e077bdf741d32ca7235d263c6)
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>
16*1a6efd4cSStanislaw Gruszka #include <linux/pm_runtime.h>
172e28bc84SOza Pawandeep #include <linux/module.h>
182e28bc84SOza Pawandeep #include <linux/kernel.h>
192e28bc84SOza Pawandeep #include <linux/errno.h>
202e28bc84SOza Pawandeep #include <linux/aer.h>
212e28bc84SOza Pawandeep #include "portdrv.h"
222e28bc84SOza Pawandeep #include "../pci.h"
232e28bc84SOza Pawandeep 
merge_result(enum pci_ers_result orig,enum pci_ers_result new)242e28bc84SOza Pawandeep static pci_ers_result_t merge_result(enum pci_ers_result orig,
252e28bc84SOza Pawandeep 				  enum pci_ers_result new)
262e28bc84SOza Pawandeep {
272e28bc84SOza Pawandeep 	if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
282e28bc84SOza Pawandeep 		return PCI_ERS_RESULT_NO_AER_DRIVER;
292e28bc84SOza Pawandeep 
302e28bc84SOza Pawandeep 	if (new == PCI_ERS_RESULT_NONE)
312e28bc84SOza Pawandeep 		return orig;
322e28bc84SOza Pawandeep 
332e28bc84SOza Pawandeep 	switch (orig) {
342e28bc84SOza Pawandeep 	case PCI_ERS_RESULT_CAN_RECOVER:
352e28bc84SOza Pawandeep 	case PCI_ERS_RESULT_RECOVERED:
362e28bc84SOza Pawandeep 		orig = new;
372e28bc84SOza Pawandeep 		break;
382e28bc84SOza Pawandeep 	case PCI_ERS_RESULT_DISCONNECT:
392e28bc84SOza Pawandeep 		if (new == PCI_ERS_RESULT_NEED_RESET)
402e28bc84SOza Pawandeep 			orig = PCI_ERS_RESULT_NEED_RESET;
412e28bc84SOza Pawandeep 		break;
422e28bc84SOza Pawandeep 	default:
432e28bc84SOza Pawandeep 		break;
442e28bc84SOza Pawandeep 	}
452e28bc84SOza Pawandeep 
462e28bc84SOza Pawandeep 	return orig;
472e28bc84SOza Pawandeep }
482e28bc84SOza Pawandeep 
report_error_detected(struct pci_dev * dev,pci_channel_state_t state,enum pci_ers_result * result)49542aeb9cSKeith Busch static int report_error_detected(struct pci_dev *dev,
5016d79cd4SLuc Van Oostenryck 				 pci_channel_state_t state,
51542aeb9cSKeith Busch 				 enum pci_ers_result *result)
522e28bc84SOza Pawandeep {
53171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
542e28bc84SOza Pawandeep 	pci_ers_result_t vote;
552e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
562e28bc84SOza Pawandeep 
572e28bc84SOza Pawandeep 	device_lock(&dev->dev);
58e0217c5bSBjorn Helgaas 	pdrv = dev->driver;
595e69a33cSChristoph Hellwig 	if (pci_dev_is_disconnected(dev)) {
605e69a33cSChristoph Hellwig 		vote = PCI_ERS_RESULT_DISCONNECT;
615e69a33cSChristoph Hellwig 	} else if (!pci_dev_set_io_state(dev, state)) {
625e69a33cSChristoph Hellwig 		pci_info(dev, "can't recover (state transition %u -> %u invalid)\n",
635e69a33cSChristoph Hellwig 			dev->error_state, state);
645e69a33cSChristoph Hellwig 		vote = PCI_ERS_RESULT_NONE;
655e69a33cSChristoph Hellwig 	} else if (!pdrv || !pdrv->err_handler ||
66171d149cSBjorn Helgaas 		   !pdrv->err_handler->error_detected) {
672e28bc84SOza Pawandeep 		/*
68bfcb79fcSKeith Busch 		 * If any device in the subtree does not have an error_detected
69bfcb79fcSKeith Busch 		 * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
70bfcb79fcSKeith Busch 		 * error callbacks of "any" device in the subtree, and will
71bfcb79fcSKeith Busch 		 * exit in the disconnected error state.
722e28bc84SOza Pawandeep 		 */
7301daacfbSYicong Yang 		if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
742e28bc84SOza Pawandeep 			vote = PCI_ERS_RESULT_NO_AER_DRIVER;
758d077c3cSBjorn Helgaas 			pci_info(dev, "can't recover (no error_detected callback)\n");
7601daacfbSYicong Yang 		} else {
772e28bc84SOza Pawandeep 			vote = PCI_ERS_RESULT_NONE;
7801daacfbSYicong Yang 		}
792e28bc84SOza Pawandeep 	} else {
80171d149cSBjorn Helgaas 		err_handler = pdrv->err_handler;
81542aeb9cSKeith Busch 		vote = err_handler->error_detected(dev, state);
822e28bc84SOza Pawandeep 	}
837b42d97eSKeith Busch 	pci_uevent_ers(dev, vote);
84542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
852e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
862e28bc84SOza Pawandeep 	return 0;
872e28bc84SOza Pawandeep }
882e28bc84SOza Pawandeep 
pci_pm_runtime_get_sync(struct pci_dev * pdev,void * data)89*1a6efd4cSStanislaw Gruszka static int pci_pm_runtime_get_sync(struct pci_dev *pdev, void *data)
90*1a6efd4cSStanislaw Gruszka {
91*1a6efd4cSStanislaw Gruszka 	pm_runtime_get_sync(&pdev->dev);
92*1a6efd4cSStanislaw Gruszka 	return 0;
93*1a6efd4cSStanislaw Gruszka }
94*1a6efd4cSStanislaw Gruszka 
pci_pm_runtime_put(struct pci_dev * pdev,void * data)95*1a6efd4cSStanislaw Gruszka static int pci_pm_runtime_put(struct pci_dev *pdev, void *data)
96*1a6efd4cSStanislaw Gruszka {
97*1a6efd4cSStanislaw Gruszka 	pm_runtime_put(&pdev->dev);
98*1a6efd4cSStanislaw Gruszka 	return 0;
99*1a6efd4cSStanislaw Gruszka }
100*1a6efd4cSStanislaw Gruszka 
report_frozen_detected(struct pci_dev * dev,void * data)101542aeb9cSKeith Busch static int report_frozen_detected(struct pci_dev *dev, void *data)
102542aeb9cSKeith Busch {
103542aeb9cSKeith Busch 	return report_error_detected(dev, pci_channel_io_frozen, data);
104542aeb9cSKeith Busch }
105542aeb9cSKeith Busch 
report_normal_detected(struct pci_dev * dev,void * data)106542aeb9cSKeith Busch static int report_normal_detected(struct pci_dev *dev, void *data)
107542aeb9cSKeith Busch {
108542aeb9cSKeith Busch 	return report_error_detected(dev, pci_channel_io_normal, data);
109542aeb9cSKeith Busch }
110542aeb9cSKeith Busch 
report_mmio_enabled(struct pci_dev * dev,void * data)1112e28bc84SOza Pawandeep static int report_mmio_enabled(struct pci_dev *dev, void *data)
1122e28bc84SOza Pawandeep {
113171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
114542aeb9cSKeith Busch 	pci_ers_result_t vote, *result = data;
1152e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1162e28bc84SOza Pawandeep 
1172e28bc84SOza Pawandeep 	device_lock(&dev->dev);
118e0217c5bSBjorn Helgaas 	pdrv = dev->driver;
119171d149cSBjorn Helgaas 	if (!pdrv ||
120171d149cSBjorn Helgaas 		!pdrv->err_handler ||
121171d149cSBjorn Helgaas 		!pdrv->err_handler->mmio_enabled)
1222e28bc84SOza Pawandeep 		goto out;
1232e28bc84SOza Pawandeep 
124171d149cSBjorn Helgaas 	err_handler = pdrv->err_handler;
1252e28bc84SOza Pawandeep 	vote = err_handler->mmio_enabled(dev);
126542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
1272e28bc84SOza Pawandeep out:
1282e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1292e28bc84SOza Pawandeep 	return 0;
1302e28bc84SOza Pawandeep }
1312e28bc84SOza Pawandeep 
report_slot_reset(struct pci_dev * dev,void * data)1322e28bc84SOza Pawandeep static int report_slot_reset(struct pci_dev *dev, void *data)
1332e28bc84SOza Pawandeep {
134171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
135542aeb9cSKeith Busch 	pci_ers_result_t vote, *result = data;
1362e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1372e28bc84SOza Pawandeep 
1382e28bc84SOza Pawandeep 	device_lock(&dev->dev);
139e0217c5bSBjorn Helgaas 	pdrv = dev->driver;
140171d149cSBjorn Helgaas 	if (!pdrv ||
141171d149cSBjorn Helgaas 		!pdrv->err_handler ||
142171d149cSBjorn Helgaas 		!pdrv->err_handler->slot_reset)
1432e28bc84SOza Pawandeep 		goto out;
1442e28bc84SOza Pawandeep 
145171d149cSBjorn Helgaas 	err_handler = pdrv->err_handler;
1462e28bc84SOza Pawandeep 	vote = err_handler->slot_reset(dev);
147542aeb9cSKeith Busch 	*result = merge_result(*result, vote);
1482e28bc84SOza Pawandeep out:
1492e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1502e28bc84SOza Pawandeep 	return 0;
1512e28bc84SOza Pawandeep }
1522e28bc84SOza Pawandeep 
report_resume(struct pci_dev * dev,void * data)1532e28bc84SOza Pawandeep static int report_resume(struct pci_dev *dev, void *data)
1542e28bc84SOza Pawandeep {
155171d149cSBjorn Helgaas 	struct pci_driver *pdrv;
1562e28bc84SOza Pawandeep 	const struct pci_error_handlers *err_handler;
1572e28bc84SOza Pawandeep 
1582e28bc84SOza Pawandeep 	device_lock(&dev->dev);
159e0217c5bSBjorn Helgaas 	pdrv = dev->driver;
160a6bd101bSKeith Busch 	if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
161171d149cSBjorn Helgaas 		!pdrv ||
162171d149cSBjorn Helgaas 		!pdrv->err_handler ||
163171d149cSBjorn Helgaas 		!pdrv->err_handler->resume)
1642e28bc84SOza Pawandeep 		goto out;
1652e28bc84SOza Pawandeep 
166171d149cSBjorn Helgaas 	err_handler = pdrv->err_handler;
1672e28bc84SOza Pawandeep 	err_handler->resume(dev);
1682e28bc84SOza Pawandeep out:
1697b42d97eSKeith Busch 	pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
1702e28bc84SOza Pawandeep 	device_unlock(&dev->dev);
1712e28bc84SOza Pawandeep 	return 0;
1722e28bc84SOza Pawandeep }
1732e28bc84SOza Pawandeep 
17405e9ae19SSean V Kelley /**
17505e9ae19SSean V Kelley  * pci_walk_bridge - walk bridges potentially AER affected
17657908622SQiuxu Zhuo  * @bridge:	bridge which may be a Port, an RCEC, or an RCiEP
17705e9ae19SSean V Kelley  * @cb:		callback to be called for each device found
17805e9ae19SSean V Kelley  * @userdata:	arbitrary pointer to be passed to callback
17905e9ae19SSean V Kelley  *
18005e9ae19SSean V Kelley  * If the device provided is a bridge, walk the subordinate bus, including
18105e9ae19SSean V Kelley  * any bridged devices on buses under this bus.  Call the provided callback
18205e9ae19SSean V Kelley  * on each device found.
183a175102bSSean V Kelley  *
18457908622SQiuxu Zhuo  * If the device provided has no subordinate bus, e.g., an RCEC or RCiEP,
18557908622SQiuxu Zhuo  * call the callback on the device itself.
18605e9ae19SSean V Kelley  */
pci_walk_bridge(struct pci_dev * bridge,int (* cb)(struct pci_dev *,void *),void * userdata)18705e9ae19SSean V Kelley static void pci_walk_bridge(struct pci_dev *bridge,
18805e9ae19SSean V Kelley 			    int (*cb)(struct pci_dev *, void *),
18905e9ae19SSean V Kelley 			    void *userdata)
19005e9ae19SSean V Kelley {
19105e9ae19SSean V Kelley 	if (bridge->subordinate)
19205e9ae19SSean V Kelley 		pci_walk_bus(bridge->subordinate, cb, userdata);
193a175102bSSean V Kelley 	else
194a175102bSSean V Kelley 		cb(bridge, userdata);
19505e9ae19SSean V Kelley }
19605e9ae19SSean V Kelley 
pcie_do_recovery(struct pci_dev * dev,pci_channel_state_t state,pci_ers_result_t (* reset_subordinates)(struct pci_dev * pdev))197e8e5ff2aSKuppuswamy Sathyanarayanan pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
19816d79cd4SLuc Van Oostenryck 		pci_channel_state_t state,
1998f1bbfbcSSean V Kelley 		pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
2002e28bc84SOza Pawandeep {
201480ef7cbSSean V Kelley 	int type = pci_pcie_type(dev);
2020791721dSSean V Kelley 	struct pci_dev *bridge;
2030791721dSSean V Kelley 	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
204aa344bc8SSean V Kelley 	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
2052e28bc84SOza Pawandeep 
206bfcb79fcSKeith Busch 	/*
20757908622SQiuxu Zhuo 	 * If the error was detected by a Root Port, Downstream Port, RCEC,
20857908622SQiuxu Zhuo 	 * or RCiEP, recovery runs on the device itself.  For Ports, that
20957908622SQiuxu Zhuo 	 * also includes any subordinate devices.
210a175102bSSean V Kelley 	 *
211a175102bSSean V Kelley 	 * If it was detected by another device (Endpoint, etc), recovery
212a175102bSSean V Kelley 	 * runs on the device and anything else under the same Port, i.e.,
213a175102bSSean V Kelley 	 * everything under "bridge".
214bfcb79fcSKeith Busch 	 */
2153d7d8fc7SSean V Kelley 	if (type == PCI_EXP_TYPE_ROOT_PORT ||
216a175102bSSean V Kelley 	    type == PCI_EXP_TYPE_DOWNSTREAM ||
21757908622SQiuxu Zhuo 	    type == PCI_EXP_TYPE_RC_EC ||
21857908622SQiuxu Zhuo 	    type == PCI_EXP_TYPE_RC_END)
2190791721dSSean V Kelley 		bridge = dev;
2203d7d8fc7SSean V Kelley 	else
2213d7d8fc7SSean V Kelley 		bridge = pci_upstream_bridge(dev);
222bfcb79fcSKeith Busch 
223*1a6efd4cSStanislaw Gruszka 	pci_walk_bridge(bridge, pci_pm_runtime_get_sync, NULL);
224*1a6efd4cSStanislaw Gruszka 
2250791721dSSean V Kelley 	pci_dbg(bridge, "broadcast error_detected message\n");
226b5dfbeacSKuppuswamy Sathyanarayanan 	if (state == pci_channel_io_frozen) {
22705e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_frozen_detected, &status);
228387c72cdSKeith Busch 		if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) {
2290791721dSSean V Kelley 			pci_warn(bridge, "subordinate device reset failed\n");
230bdb5ac85SKeith Busch 			goto failed;
231b6cf1a42SKuppuswamy Sathyanarayanan 		}
232b5dfbeacSKuppuswamy Sathyanarayanan 	} else {
23305e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_normal_detected, &status);
234b5dfbeacSKuppuswamy Sathyanarayanan 	}
235bdb5ac85SKeith Busch 
236542aeb9cSKeith Busch 	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
237542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
2380791721dSSean V Kelley 		pci_dbg(bridge, "broadcast mmio_enabled message\n");
23905e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_mmio_enabled, &status);
240542aeb9cSKeith Busch 	}
2412e28bc84SOza Pawandeep 
2422e28bc84SOza Pawandeep 	if (status == PCI_ERS_RESULT_NEED_RESET) {
2432e28bc84SOza Pawandeep 		/*
2442e28bc84SOza Pawandeep 		 * TODO: Should call platform-specific
2452e28bc84SOza Pawandeep 		 * functions to reset slot before calling
2462e28bc84SOza Pawandeep 		 * drivers' slot_reset callbacks?
2472e28bc84SOza Pawandeep 		 */
248542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
2490791721dSSean V Kelley 		pci_dbg(bridge, "broadcast slot_reset message\n");
25005e9ae19SSean V Kelley 		pci_walk_bridge(bridge, report_slot_reset, &status);
2512e28bc84SOza Pawandeep 	}
2522e28bc84SOza Pawandeep 
2532e28bc84SOza Pawandeep 	if (status != PCI_ERS_RESULT_RECOVERED)
2542e28bc84SOza Pawandeep 		goto failed;
2552e28bc84SOza Pawandeep 
2560791721dSSean V Kelley 	pci_dbg(bridge, "broadcast resume message\n");
25705e9ae19SSean V Kelley 	pci_walk_bridge(bridge, report_resume, &status);
2582e28bc84SOza Pawandeep 
259aa344bc8SSean V Kelley 	/*
2607d7cbeabSKeith Busch 	 * If we have native control of AER, clear error status in the device
2617d7cbeabSKeith Busch 	 * that detected the error.  If the platform retained control of AER,
2627d7cbeabSKeith Busch 	 * it is responsible for clearing this status.  In that case, the
2637d7cbeabSKeith Busch 	 * signaling device may not even be visible to the OS.
264aa344bc8SSean V Kelley 	 */
265aa344bc8SSean V Kelley 	if (host->native_aer || pcie_ports_native) {
2667d7cbeabSKeith Busch 		pcie_clear_device_status(dev);
2677d7cbeabSKeith Busch 		pci_aer_clear_nonfatal_status(dev);
268aa344bc8SSean V Kelley 	}
269*1a6efd4cSStanislaw Gruszka 
270*1a6efd4cSStanislaw Gruszka 	pci_walk_bridge(bridge, pci_pm_runtime_put, NULL);
271*1a6efd4cSStanislaw Gruszka 
2720791721dSSean V Kelley 	pci_info(bridge, "device recovery successful\n");
273e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2742e28bc84SOza Pawandeep 
2752e28bc84SOza Pawandeep failed:
276*1a6efd4cSStanislaw Gruszka 	pci_walk_bridge(bridge, pci_pm_runtime_put, NULL);
277*1a6efd4cSStanislaw Gruszka 
2780791721dSSean V Kelley 	pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
2792e28bc84SOza Pawandeep 
2802e28bc84SOza Pawandeep 	/* TODO: Should kernel panic here? */
2810791721dSSean V Kelley 	pci_info(bridge, "device recovery failed\n");
282e8e5ff2aSKuppuswamy Sathyanarayanan 
283e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2842e28bc84SOza Pawandeep }
285