Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * This file implements the error recovery as a core part of PCIe error
  4 * reporting. When a PCIe error is delivered, an error message will be
  5 * collected and printed to console, then, an error recovery procedure
  6 * will be executed by following the PCI error recovery rules.
  7 *
  8 * Copyright (C) 2006 Intel Corp.
  9 *	Tom Long Nguyen (tom.l.nguyen@intel.com)
 10 *	Zhang Yanmin (yanmin.zhang@intel.com)
 11 */
 12
 13#define dev_fmt(fmt) "AER: " fmt
 14
 15#include <linux/pci.h>
 16#include <linux/module.h>
 17#include <linux/kernel.h>
 18#include <linux/errno.h>
 19#include <linux/aer.h>
 20#include "portdrv.h"
 21#include "../pci.h"
 22
 23static pci_ers_result_t merge_result(enum pci_ers_result orig,
 24				  enum pci_ers_result new)
 25{
 26	if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
 27		return PCI_ERS_RESULT_NO_AER_DRIVER;
 28
 29	if (new == PCI_ERS_RESULT_NONE)
 30		return orig;
 31
 32	switch (orig) {
 33	case PCI_ERS_RESULT_CAN_RECOVER:
 34	case PCI_ERS_RESULT_RECOVERED:
 35		orig = new;
 36		break;
 37	case PCI_ERS_RESULT_DISCONNECT:
 38		if (new == PCI_ERS_RESULT_NEED_RESET)
 39			orig = PCI_ERS_RESULT_NEED_RESET;
 40		break;
 41	default:
 42		break;
 43	}
 44
 45	return orig;
 46}
 47
 48static int report_error_detected(struct pci_dev *dev,
 49				 pci_channel_state_t state,
 50				 enum pci_ers_result *result)
 51{
 52	struct pci_driver *pdrv;
 53	pci_ers_result_t vote;
 54	const struct pci_error_handlers *err_handler;
 55
 56	device_lock(&dev->dev);
 57	pdrv = dev->driver;
 58	if (pci_dev_is_disconnected(dev)) {
 59		vote = PCI_ERS_RESULT_DISCONNECT;
 60	} else if (!pci_dev_set_io_state(dev, state)) {
 61		pci_info(dev, "can't recover (state transition %u -> %u invalid)\n",
 62			dev->error_state, state);
 63		vote = PCI_ERS_RESULT_NONE;
 64	} else if (!pdrv || !pdrv->err_handler ||
 65		   !pdrv->err_handler->error_detected) {
 66		/*
 67		 * If any device in the subtree does not have an error_detected
 68		 * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
 69		 * error callbacks of "any" device in the subtree, and will
 70		 * exit in the disconnected error state.
 71		 */
 72		if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
 73			vote = PCI_ERS_RESULT_NO_AER_DRIVER;
 74			pci_info(dev, "can't recover (no error_detected callback)\n");
 75		} else {
 76			vote = PCI_ERS_RESULT_NONE;
 77		}
 78	} else {
 79		err_handler = pdrv->err_handler;
 80		vote = err_handler->error_detected(dev, state);
 81	}
 82	pci_uevent_ers(dev, vote);
 83	*result = merge_result(*result, vote);
 84	device_unlock(&dev->dev);
 85	return 0;
 86}
 87
 88static int report_frozen_detected(struct pci_dev *dev, void *data)
 89{
 90	return report_error_detected(dev, pci_channel_io_frozen, data);
 91}
 92
 93static int report_normal_detected(struct pci_dev *dev, void *data)
 94{
 95	return report_error_detected(dev, pci_channel_io_normal, data);
 96}
 97
 98static int report_mmio_enabled(struct pci_dev *dev, void *data)
 99{
100	struct pci_driver *pdrv;
101	pci_ers_result_t vote, *result = data;
102	const struct pci_error_handlers *err_handler;
103
104	device_lock(&dev->dev);
105	pdrv = dev->driver;
106	if (!pdrv ||
107		!pdrv->err_handler ||
108		!pdrv->err_handler->mmio_enabled)
109		goto out;
110
111	err_handler = pdrv->err_handler;
112	vote = err_handler->mmio_enabled(dev);
113	*result = merge_result(*result, vote);
114out:
115	device_unlock(&dev->dev);
116	return 0;
117}
118
119static int report_slot_reset(struct pci_dev *dev, void *data)
120{
121	struct pci_driver *pdrv;
122	pci_ers_result_t vote, *result = data;
123	const struct pci_error_handlers *err_handler;
124
125	device_lock(&dev->dev);
126	pdrv = dev->driver;
127	if (!pdrv ||
128		!pdrv->err_handler ||
129		!pdrv->err_handler->slot_reset)
130		goto out;
131
132	err_handler = pdrv->err_handler;
133	vote = err_handler->slot_reset(dev);
134	*result = merge_result(*result, vote);
135out:
136	device_unlock(&dev->dev);
137	return 0;
138}
139
140static int report_resume(struct pci_dev *dev, void *data)
141{
142	struct pci_driver *pdrv;
143	const struct pci_error_handlers *err_handler;
144
145	device_lock(&dev->dev);
146	pdrv = dev->driver;
147	if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
148		!pdrv ||
149		!pdrv->err_handler ||
150		!pdrv->err_handler->resume)
151		goto out;
152
153	err_handler = pdrv->err_handler;
154	err_handler->resume(dev);
155out:
156	pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
157	device_unlock(&dev->dev);
158	return 0;
159}
160
161/**
162 * pci_walk_bridge - walk bridges potentially AER affected
163 * @bridge:	bridge which may be a Port, an RCEC, or an RCiEP
164 * @cb:		callback to be called for each device found
165 * @userdata:	arbitrary pointer to be passed to callback
166 *
167 * If the device provided is a bridge, walk the subordinate bus, including
168 * any bridged devices on buses under this bus.  Call the provided callback
169 * on each device found.
170 *
171 * If the device provided has no subordinate bus, e.g., an RCEC or RCiEP,
172 * call the callback on the device itself.
173 */
174static void pci_walk_bridge(struct pci_dev *bridge,
175			    int (*cb)(struct pci_dev *, void *),
176			    void *userdata)
177{
178	if (bridge->subordinate)
179		pci_walk_bus(bridge->subordinate, cb, userdata);
180	else
181		cb(bridge, userdata);
182}
183
184pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
185		pci_channel_state_t state,
186		pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
187{
188	int type = pci_pcie_type(dev);
189	struct pci_dev *bridge;
190	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
191	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
192
193	/*
194	 * If the error was detected by a Root Port, Downstream Port, RCEC,
195	 * or RCiEP, recovery runs on the device itself.  For Ports, that
196	 * also includes any subordinate devices.
197	 *
198	 * If it was detected by another device (Endpoint, etc), recovery
199	 * runs on the device and anything else under the same Port, i.e.,
200	 * everything under "bridge".
201	 */
202	if (type == PCI_EXP_TYPE_ROOT_PORT ||
203	    type == PCI_EXP_TYPE_DOWNSTREAM ||
204	    type == PCI_EXP_TYPE_RC_EC ||
205	    type == PCI_EXP_TYPE_RC_END)
206		bridge = dev;
207	else
208		bridge = pci_upstream_bridge(dev);
209
210	pci_dbg(bridge, "broadcast error_detected message\n");
211	if (state == pci_channel_io_frozen) {
212		pci_walk_bridge(bridge, report_frozen_detected, &status);
213		if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) {
214			pci_warn(bridge, "subordinate device reset failed\n");
215			goto failed;
216		}
217	} else {
218		pci_walk_bridge(bridge, report_normal_detected, &status);
219	}
220
221	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
222		status = PCI_ERS_RESULT_RECOVERED;
223		pci_dbg(bridge, "broadcast mmio_enabled message\n");
224		pci_walk_bridge(bridge, report_mmio_enabled, &status);
225	}
226
227	if (status == PCI_ERS_RESULT_NEED_RESET) {
228		/*
229		 * TODO: Should call platform-specific
230		 * functions to reset slot before calling
231		 * drivers' slot_reset callbacks?
232		 */
233		status = PCI_ERS_RESULT_RECOVERED;
234		pci_dbg(bridge, "broadcast slot_reset message\n");
235		pci_walk_bridge(bridge, report_slot_reset, &status);
236	}
237
238	if (status != PCI_ERS_RESULT_RECOVERED)
239		goto failed;
240
241	pci_dbg(bridge, "broadcast resume message\n");
242	pci_walk_bridge(bridge, report_resume, &status);
243
244	/*
245	 * If we have native control of AER, clear error status in the device
246	 * that detected the error.  If the platform retained control of AER,
247	 * it is responsible for clearing this status.  In that case, the
248	 * signaling device may not even be visible to the OS.
249	 */
250	if (host->native_aer || pcie_ports_native) {
251		pcie_clear_device_status(dev);
252		pci_aer_clear_nonfatal_status(dev);
253	}
254	pci_info(bridge, "device recovery successful\n");
255	return status;
256
257failed:
258	pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
259
260	/* TODO: Should kernel panic here? */
261	pci_info(bridge, "device recovery failed\n");
262
263	return status;
264}
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * This file implements the error recovery as a core part of PCIe error
  4 * reporting. When a PCIe error is delivered, an error message will be
  5 * collected and printed to console, then, an error recovery procedure
  6 * will be executed by following the PCI error recovery rules.
  7 *
  8 * Copyright (C) 2006 Intel Corp.
  9 *	Tom Long Nguyen (tom.l.nguyen@intel.com)
 10 *	Zhang Yanmin (yanmin.zhang@intel.com)
 11 */
 12
 13#define dev_fmt(fmt) "AER: " fmt
 14
 15#include <linux/pci.h>
 16#include <linux/module.h>
 17#include <linux/kernel.h>
 18#include <linux/errno.h>
 19#include <linux/aer.h>
 20#include "portdrv.h"
 21#include "../pci.h"
 22
 23static pci_ers_result_t merge_result(enum pci_ers_result orig,
 24				  enum pci_ers_result new)
 25{
 26	if (new == PCI_ERS_RESULT_NO_AER_DRIVER)
 27		return PCI_ERS_RESULT_NO_AER_DRIVER;
 28
 29	if (new == PCI_ERS_RESULT_NONE)
 30		return orig;
 31
 32	switch (orig) {
 33	case PCI_ERS_RESULT_CAN_RECOVER:
 34	case PCI_ERS_RESULT_RECOVERED:
 35		orig = new;
 36		break;
 37	case PCI_ERS_RESULT_DISCONNECT:
 38		if (new == PCI_ERS_RESULT_NEED_RESET)
 39			orig = PCI_ERS_RESULT_NEED_RESET;
 40		break;
 41	default:
 42		break;
 43	}
 44
 45	return orig;
 46}
 47
 48static int report_error_detected(struct pci_dev *dev,
 49				 pci_channel_state_t state,
 50				 enum pci_ers_result *result)
 51{
 
 52	pci_ers_result_t vote;
 53	const struct pci_error_handlers *err_handler;
 54
 55	device_lock(&dev->dev);
 56	if (!pci_dev_set_io_state(dev, state) ||
 57		!dev->driver ||
 58		!dev->driver->err_handler ||
 59		!dev->driver->err_handler->error_detected) {
 
 
 
 
 
 60		/*
 61		 * If any device in the subtree does not have an error_detected
 62		 * callback, PCI_ERS_RESULT_NO_AER_DRIVER prevents subsequent
 63		 * error callbacks of "any" device in the subtree, and will
 64		 * exit in the disconnected error state.
 65		 */
 66		if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
 67			vote = PCI_ERS_RESULT_NO_AER_DRIVER;
 68			pci_info(dev, "can't recover (no error_detected callback)\n");
 69		} else {
 70			vote = PCI_ERS_RESULT_NONE;
 71		}
 72	} else {
 73		err_handler = dev->driver->err_handler;
 74		vote = err_handler->error_detected(dev, state);
 75	}
 76	pci_uevent_ers(dev, vote);
 77	*result = merge_result(*result, vote);
 78	device_unlock(&dev->dev);
 79	return 0;
 80}
 81
 82static int report_frozen_detected(struct pci_dev *dev, void *data)
 83{
 84	return report_error_detected(dev, pci_channel_io_frozen, data);
 85}
 86
 87static int report_normal_detected(struct pci_dev *dev, void *data)
 88{
 89	return report_error_detected(dev, pci_channel_io_normal, data);
 90}
 91
 92static int report_mmio_enabled(struct pci_dev *dev, void *data)
 93{
 
 94	pci_ers_result_t vote, *result = data;
 95	const struct pci_error_handlers *err_handler;
 96
 97	device_lock(&dev->dev);
 98	if (!dev->driver ||
 99		!dev->driver->err_handler ||
100		!dev->driver->err_handler->mmio_enabled)
 
101		goto out;
102
103	err_handler = dev->driver->err_handler;
104	vote = err_handler->mmio_enabled(dev);
105	*result = merge_result(*result, vote);
106out:
107	device_unlock(&dev->dev);
108	return 0;
109}
110
111static int report_slot_reset(struct pci_dev *dev, void *data)
112{
 
113	pci_ers_result_t vote, *result = data;
114	const struct pci_error_handlers *err_handler;
115
116	device_lock(&dev->dev);
117	if (!dev->driver ||
118		!dev->driver->err_handler ||
119		!dev->driver->err_handler->slot_reset)
 
120		goto out;
121
122	err_handler = dev->driver->err_handler;
123	vote = err_handler->slot_reset(dev);
124	*result = merge_result(*result, vote);
125out:
126	device_unlock(&dev->dev);
127	return 0;
128}
129
130static int report_resume(struct pci_dev *dev, void *data)
131{
 
132	const struct pci_error_handlers *err_handler;
133
134	device_lock(&dev->dev);
 
135	if (!pci_dev_set_io_state(dev, pci_channel_io_normal) ||
136		!dev->driver ||
137		!dev->driver->err_handler ||
138		!dev->driver->err_handler->resume)
139		goto out;
140
141	err_handler = dev->driver->err_handler;
142	err_handler->resume(dev);
143out:
144	pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
145	device_unlock(&dev->dev);
146	return 0;
147}
148
149/**
150 * pci_walk_bridge - walk bridges potentially AER affected
151 * @bridge:	bridge which may be a Port, an RCEC, or an RCiEP
152 * @cb:		callback to be called for each device found
153 * @userdata:	arbitrary pointer to be passed to callback
154 *
155 * If the device provided is a bridge, walk the subordinate bus, including
156 * any bridged devices on buses under this bus.  Call the provided callback
157 * on each device found.
158 *
159 * If the device provided has no subordinate bus, e.g., an RCEC or RCiEP,
160 * call the callback on the device itself.
161 */
162static void pci_walk_bridge(struct pci_dev *bridge,
163			    int (*cb)(struct pci_dev *, void *),
164			    void *userdata)
165{
166	if (bridge->subordinate)
167		pci_walk_bus(bridge->subordinate, cb, userdata);
168	else
169		cb(bridge, userdata);
170}
171
172pci_ers_result_t pcie_do_recovery(struct pci_dev *dev,
173		pci_channel_state_t state,
174		pci_ers_result_t (*reset_subordinates)(struct pci_dev *pdev))
175{
176	int type = pci_pcie_type(dev);
177	struct pci_dev *bridge;
178	pci_ers_result_t status = PCI_ERS_RESULT_CAN_RECOVER;
179	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
180
181	/*
182	 * If the error was detected by a Root Port, Downstream Port, RCEC,
183	 * or RCiEP, recovery runs on the device itself.  For Ports, that
184	 * also includes any subordinate devices.
185	 *
186	 * If it was detected by another device (Endpoint, etc), recovery
187	 * runs on the device and anything else under the same Port, i.e.,
188	 * everything under "bridge".
189	 */
190	if (type == PCI_EXP_TYPE_ROOT_PORT ||
191	    type == PCI_EXP_TYPE_DOWNSTREAM ||
192	    type == PCI_EXP_TYPE_RC_EC ||
193	    type == PCI_EXP_TYPE_RC_END)
194		bridge = dev;
195	else
196		bridge = pci_upstream_bridge(dev);
197
198	pci_dbg(bridge, "broadcast error_detected message\n");
199	if (state == pci_channel_io_frozen) {
200		pci_walk_bridge(bridge, report_frozen_detected, &status);
201		if (reset_subordinates(bridge) != PCI_ERS_RESULT_RECOVERED) {
202			pci_warn(bridge, "subordinate device reset failed\n");
203			goto failed;
204		}
205	} else {
206		pci_walk_bridge(bridge, report_normal_detected, &status);
207	}
208
209	if (status == PCI_ERS_RESULT_CAN_RECOVER) {
210		status = PCI_ERS_RESULT_RECOVERED;
211		pci_dbg(bridge, "broadcast mmio_enabled message\n");
212		pci_walk_bridge(bridge, report_mmio_enabled, &status);
213	}
214
215	if (status == PCI_ERS_RESULT_NEED_RESET) {
216		/*
217		 * TODO: Should call platform-specific
218		 * functions to reset slot before calling
219		 * drivers' slot_reset callbacks?
220		 */
221		status = PCI_ERS_RESULT_RECOVERED;
222		pci_dbg(bridge, "broadcast slot_reset message\n");
223		pci_walk_bridge(bridge, report_slot_reset, &status);
224	}
225
226	if (status != PCI_ERS_RESULT_RECOVERED)
227		goto failed;
228
229	pci_dbg(bridge, "broadcast resume message\n");
230	pci_walk_bridge(bridge, report_resume, &status);
231
232	/*
233	 * If we have native control of AER, clear error status in the device
234	 * that detected the error.  If the platform retained control of AER,
235	 * it is responsible for clearing this status.  In that case, the
236	 * signaling device may not even be visible to the OS.
237	 */
238	if (host->native_aer || pcie_ports_native) {
239		pcie_clear_device_status(dev);
240		pci_aer_clear_nonfatal_status(dev);
241	}
242	pci_info(bridge, "device recovery successful\n");
243	return status;
244
245failed:
246	pci_uevent_ers(bridge, PCI_ERS_RESULT_DISCONNECT);
247
248	/* TODO: Should kernel panic here? */
249	pci_info(bridge, "device recovery failed\n");
250
251	return status;
252}