xref: /linux/drivers/pci/pcie/err.c (revision 0791721d800790e6e533bd8467df67f0dc4f2fec)
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 
149e8e5ff2aSKuppuswamy Sathyanarayanan pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
15016d79cd4SLuc Van Oostenryck 		pci_channel_state_t state,
1518f1bbfbcSSean V Kelley 		pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
1522e28bc84SOza Pawandeep {
153480ef7cbSSean V Kelley 	int type = pci_pcie_type(dev);
154*0791721dSSean V Kelley 	struct pci_dev *bridge;
155542aeb9cSKeith Busch 	struct pci_bus *bus;
156*0791721dSSean V Kelley 	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
1572e28bc84SOza Pawandeep 
158bfcb79fcSKeith Busch 	/*
159*0791721dSSean V Kelley 	 * Error recovery runs on all subordinates of the bridge.  If the
160*0791721dSSean V Kelley 	 * bridge detected the error, it is cleared at the end.
161bfcb79fcSKeith Busch 	 */
162480ef7cbSSean V Kelley 	if (!(type == PCI_EXP_TYPE_ROOT_PORT ||
163480ef7cbSSean V Kelley 	      type == PCI_EXP_TYPE_DOWNSTREAM))
164*0791721dSSean V Kelley 		bridge = pci_upstream_bridge(dev);
165*0791721dSSean V Kelley 	else
166*0791721dSSean V Kelley 		bridge = dev;
167bfcb79fcSKeith Busch 
168*0791721dSSean V Kelley 	bus = bridge->subordinate;
169*0791721dSSean V Kelley 	pci_dbg(bridge, "broadcast error_detected message\n");
170b5dfbeacSKuppuswamy Sathyanarayanan 	if (state == pci_channel_io_frozen) {
171542aeb9cSKeith Busch 		pci_walk_bus(bus, report_frozen_detected, &status);
172*0791721dSSean V Kelley 		status = reset_subordinates(bridge);
173b6cf1a42SKuppuswamy Sathyanarayanan 		if (status != PCI_ERS_RESULT_RECOVERED) {
174*0791721dSSean V Kelley 			pci_warn(bridge, "subordinate device reset failed\n");
175bdb5ac85SKeith Busch 			goto failed;
176b6cf1a42SKuppuswamy Sathyanarayanan 		}
177b5dfbeacSKuppuswamy Sathyanarayanan 	} else {
178b5dfbeacSKuppuswamy Sathyanarayanan 		pci_walk_bus(bus, report_normal_detected, &status);
179b5dfbeacSKuppuswamy Sathyanarayanan 	}
180bdb5ac85SKeith Busch 
181542aeb9cSKeith Busch 	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
182542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
183*0791721dSSean V Kelley 		pci_dbg(bridge, "broadcast mmio_enabled message\n");
184542aeb9cSKeith Busch 		pci_walk_bus(bus, report_mmio_enabled, &status);
185542aeb9cSKeith Busch 	}
1862e28bc84SOza Pawandeep 
1872e28bc84SOza Pawandeep 	if (status == PCI_ERS_RESULT_NEED_RESET) {
1882e28bc84SOza Pawandeep 		/*
1892e28bc84SOza Pawandeep 		 * TODO: Should call platform-specific
1902e28bc84SOza Pawandeep 		 * functions to reset slot before calling
1912e28bc84SOza Pawandeep 		 * drivers' slot_reset callbacks?
1922e28bc84SOza Pawandeep 		 */
193542aeb9cSKeith Busch 		status = PCI_ERS_RESULT_RECOVERED;
194*0791721dSSean V Kelley 		pci_dbg(bridge, "broadcast slot_reset message\n");
195542aeb9cSKeith Busch 		pci_walk_bus(bus, report_slot_reset, &status);
1962e28bc84SOza Pawandeep 	}
1972e28bc84SOza Pawandeep 
1982e28bc84SOza Pawandeep 	if (status != PCI_ERS_RESULT_RECOVERED)
1992e28bc84SOza Pawandeep 		goto failed;
2002e28bc84SOza Pawandeep 
201*0791721dSSean V Kelley 	pci_dbg(bridge, "broadcast resume message\n");
202542aeb9cSKeith Busch 	pci_walk_bus(bus, report_resume, &status);
2032e28bc84SOza Pawandeep 
204*0791721dSSean V Kelley 	if (pcie_aer_is_native(bridge))
205*0791721dSSean V Kelley 		pcie_clear_device_status(bridge);
206*0791721dSSean V Kelley 	pci_aer_clear_nonfatal_status(bridge);
207*0791721dSSean V Kelley 	pci_info(bridge, "device recovery successful\n");
208e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2092e28bc84SOza Pawandeep 
2102e28bc84SOza Pawandeep failed:
211*0791721dSSean V Kelley 	pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
2122e28bc84SOza Pawandeep 
2132e28bc84SOza Pawandeep 	/* TODO: Should kernel panic here? */
214*0791721dSSean V Kelley 	pci_info(bridge, "device recovery failed\n");
215e8e5ff2aSKuppuswamy Sathyanarayanan 
216e8e5ff2aSKuppuswamy Sathyanarayanan 	return status;
2172e28bc84SOza Pawandeep }
218