Skip to content

Commit 4cdf2f4

Browse files
niklas88Vasily Gorbik
authored andcommitted
s390/pci: implement minimal PCI error recovery
When the platform detects an error on a PCI function or a service action has been performed it is put in the error state and an error event notification is provided to the OS. Currently we treat all error event notifications the same and simply set pdev->error_state = pci_channel_io_perm_failure requiring user intervention such as use of the recover attribute to get the device usable again. Despite requiring a manual step this also has the disadvantage that the device is completely torn down and recreated resulting in higher level devices such as a block or network device being recreated. In case of a block device this also means that it may need to be removed and added to a software raid even if that could otherwise survive with a temporary degradation. This is of course not ideal more so since an error notification with PEC 0x3A indicates that the platform already performed error recovery successfully or that the error state was caused by a service action that is now finished. At least in this case we can assume that the error state can be reset and the function made usable again. So as not to have the disadvantage of a full tear down and recreation we need to coordinate this recovery with the driver. Thankfully there is already a well defined recovery flow for this described in Documentation/PCI/pci-error-recovery.rst. The implementation of this is somewhat straight forward and simplified by the fact that our recovery flow is defined per PCI function. As a reset we use the newly introduced zpci_hot_reset_device() which also takes the PCI function out of the error state. Reviewed-by: Pierre Morel <[email protected]> Acked-by: Matthew Rosato <[email protected]> Signed-off-by: Niklas Schnelle <[email protected]> Signed-off-by: Vasily Gorbik <[email protected]>
1 parent dfd5bb2 commit 4cdf2f4

File tree

3 files changed

+277
-4
lines changed

3 files changed

+277
-4
lines changed

arch/s390/include/asm/pci.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ void zpci_debug_exit(void);
296296
void zpci_debug_init_device(struct zpci_dev *, const char *);
297297
void zpci_debug_exit_device(struct zpci_dev *);
298298

299-
/* Error reporting */
299+
/* Error handling */
300300
int zpci_report_error(struct pci_dev *, struct zpci_report_error_header *);
301+
int zpci_clear_error_state(struct zpci_dev *zdev);
302+
int zpci_reset_load_store_blocked(struct zpci_dev *zdev);
301303

302304
#ifdef CONFIG_NUMA
303305

arch/s390/pci/pci.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,59 @@ int zpci_report_error(struct pci_dev *pdev,
990990
}
991991
EXPORT_SYMBOL(zpci_report_error);
992992

993+
/**
994+
* zpci_clear_error_state() - Clears the zPCI error state of the device
995+
* @zdev: The zdev for which the zPCI error state should be reset
996+
*
997+
* Clear the zPCI error state of the device. If clearing the zPCI error state
998+
* fails the device is left in the error state. In this case it may make sense
999+
* to call zpci_io_perm_failure() on the associated pdev if it exists.
1000+
*
1001+
* Returns: 0 on success, -EIO otherwise
1002+
*/
1003+
int zpci_clear_error_state(struct zpci_dev *zdev)
1004+
{
1005+
u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
1006+
struct zpci_fib fib = {0};
1007+
u8 status;
1008+
int cc;
1009+
1010+
cc = zpci_mod_fc(req, &fib, &status);
1011+
if (cc) {
1012+
zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1013+
return -EIO;
1014+
}
1015+
1016+
return 0;
1017+
}
1018+
1019+
/**
1020+
* zpci_reset_load_store_blocked() - Re-enables L/S from error state
1021+
* @zdev: The zdev for which to unblock load/store access
1022+
*
1023+
* Re-enables load/store access for a PCI function in the error state while
1024+
* keeping DMA blocked. In this state drivers can poke MMIO space to determine
1025+
* if error recovery is possible while catching any rogue DMA access from the
1026+
* device.
1027+
*
1028+
* Returns: 0 on success, -EIO otherwise
1029+
*/
1030+
int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
1031+
{
1032+
u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
1033+
struct zpci_fib fib = {0};
1034+
u8 status;
1035+
int cc;
1036+
1037+
cc = zpci_mod_fc(req, &fib, &status);
1038+
if (cc) {
1039+
zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1040+
return -EIO;
1041+
}
1042+
1043+
return 0;
1044+
}
1045+
9931046
static int zpci_mem_init(void)
9941047
{
9951048
BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||

arch/s390/pci/pci_event.c

Lines changed: 221 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,244 @@ struct zpci_ccdf_avail {
4747
u16 pec; /* PCI event code */
4848
} __packed;
4949

50+
static inline bool ers_result_indicates_abort(pci_ers_result_t ers_res)
51+
{
52+
switch (ers_res) {
53+
case PCI_ERS_RESULT_CAN_RECOVER:
54+
case PCI_ERS_RESULT_RECOVERED:
55+
case PCI_ERS_RESULT_NEED_RESET:
56+
return false;
57+
default:
58+
return true;
59+
}
60+
}
61+
62+
static bool is_passed_through(struct zpci_dev *zdev)
63+
{
64+
return zdev->s390_domain;
65+
}
66+
67+
static bool is_driver_supported(struct pci_driver *driver)
68+
{
69+
if (!driver || !driver->err_handler)
70+
return false;
71+
if (!driver->err_handler->error_detected)
72+
return false;
73+
if (!driver->err_handler->slot_reset)
74+
return false;
75+
if (!driver->err_handler->resume)
76+
return false;
77+
return true;
78+
}
79+
80+
static pci_ers_result_t zpci_event_notify_error_detected(struct pci_dev *pdev,
81+
struct pci_driver *driver)
82+
{
83+
pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
84+
85+
ers_res = driver->err_handler->error_detected(pdev, pdev->error_state);
86+
if (ers_result_indicates_abort(ers_res))
87+
pr_info("%s: Automatic recovery failed after initial reporting\n", pci_name(pdev));
88+
else if (ers_res == PCI_ERS_RESULT_NEED_RESET)
89+
pr_debug("%s: Driver needs reset to recover\n", pci_name(pdev));
90+
91+
return ers_res;
92+
}
93+
94+
static pci_ers_result_t zpci_event_do_error_state_clear(struct pci_dev *pdev,
95+
struct pci_driver *driver)
96+
{
97+
pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
98+
struct zpci_dev *zdev = to_zpci(pdev);
99+
int rc;
100+
101+
pr_info("%s: Unblocking device access for examination\n", pci_name(pdev));
102+
rc = zpci_reset_load_store_blocked(zdev);
103+
if (rc) {
104+
pr_err("%s: Unblocking device access failed\n", pci_name(pdev));
105+
/* Let's try a full reset instead */
106+
return PCI_ERS_RESULT_NEED_RESET;
107+
}
108+
109+
if (driver->err_handler->mmio_enabled) {
110+
ers_res = driver->err_handler->mmio_enabled(pdev);
111+
if (ers_result_indicates_abort(ers_res)) {
112+
pr_info("%s: Automatic recovery failed after MMIO re-enable\n",
113+
pci_name(pdev));
114+
return ers_res;
115+
} else if (ers_res == PCI_ERS_RESULT_NEED_RESET) {
116+
pr_debug("%s: Driver needs reset to recover\n", pci_name(pdev));
117+
return ers_res;
118+
}
119+
}
120+
121+
pr_debug("%s: Unblocking DMA\n", pci_name(pdev));
122+
rc = zpci_clear_error_state(zdev);
123+
if (!rc) {
124+
pdev->error_state = pci_channel_io_normal;
125+
} else {
126+
pr_err("%s: Unblocking DMA failed\n", pci_name(pdev));
127+
/* Let's try a full reset instead */
128+
return PCI_ERS_RESULT_NEED_RESET;
129+
}
130+
131+
return ers_res;
132+
}
133+
134+
static pci_ers_result_t zpci_event_do_reset(struct pci_dev *pdev,
135+
struct pci_driver *driver)
136+
{
137+
pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
138+
139+
pr_info("%s: Initiating reset\n", pci_name(pdev));
140+
if (zpci_hot_reset_device(to_zpci(pdev))) {
141+
pr_err("%s: The reset request failed\n", pci_name(pdev));
142+
return ers_res;
143+
}
144+
pdev->error_state = pci_channel_io_normal;
145+
ers_res = driver->err_handler->slot_reset(pdev);
146+
if (ers_result_indicates_abort(ers_res)) {
147+
pr_info("%s: Automatic recovery failed after slot reset\n", pci_name(pdev));
148+
return ers_res;
149+
}
150+
151+
return ers_res;
152+
}
153+
154+
/* zpci_event_attempt_error_recovery - Try to recover the given PCI function
155+
* @pdev: PCI function to recover currently in the error state
156+
*
157+
* We follow the scheme outlined in Documentation/PCI/pci-error-recovery.rst.
158+
* With the simplification that recovery always happens per function
159+
* and the platform determines which functions are affected for
160+
* multi-function devices.
161+
*/
162+
static pci_ers_result_t zpci_event_attempt_error_recovery(struct pci_dev *pdev)
163+
{
164+
pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
165+
struct pci_driver *driver;
166+
167+
/*
168+
* Ensure that the PCI function is not removed concurrently, no driver
169+
* is unbound or probed and that userspace can't access its
170+
* configuration space while we perform recovery.
171+
*/
172+
pci_dev_lock(pdev);
173+
if (pdev->error_state == pci_channel_io_perm_failure) {
174+
ers_res = PCI_ERS_RESULT_DISCONNECT;
175+
goto out_unlock;
176+
}
177+
pdev->error_state = pci_channel_io_frozen;
178+
179+
if (is_passed_through(to_zpci(pdev))) {
180+
pr_info("%s: Cannot be recovered in the host because it is a pass-through device\n",
181+
pci_name(pdev));
182+
goto out_unlock;
183+
}
184+
185+
driver = to_pci_driver(pdev->dev.driver);
186+
if (!is_driver_supported(driver)) {
187+
if (!driver)
188+
pr_info("%s: Cannot be recovered because no driver is bound to the device\n",
189+
pci_name(pdev));
190+
else
191+
pr_info("%s: The %s driver bound to the device does not support error recovery\n",
192+
pci_name(pdev),
193+
driver->name);
194+
goto out_unlock;
195+
}
196+
197+
ers_res = zpci_event_notify_error_detected(pdev, driver);
198+
if (ers_result_indicates_abort(ers_res))
199+
goto out_unlock;
200+
201+
if (ers_res == PCI_ERS_RESULT_CAN_RECOVER) {
202+
ers_res = zpci_event_do_error_state_clear(pdev, driver);
203+
if (ers_result_indicates_abort(ers_res))
204+
goto out_unlock;
205+
}
206+
207+
if (ers_res == PCI_ERS_RESULT_NEED_RESET)
208+
ers_res = zpci_event_do_reset(pdev, driver);
209+
210+
if (ers_res != PCI_ERS_RESULT_RECOVERED) {
211+
pr_err("%s: Automatic recovery failed; operator intervention is required\n",
212+
pci_name(pdev));
213+
goto out_unlock;
214+
}
215+
216+
pr_info("%s: The device is ready to resume operations\n", pci_name(pdev));
217+
if (driver->err_handler->resume)
218+
driver->err_handler->resume(pdev);
219+
out_unlock:
220+
pci_dev_unlock(pdev);
221+
222+
return ers_res;
223+
}
224+
225+
/* zpci_event_io_failure - Report PCI channel failure state to driver
226+
* @pdev: PCI function for which to report
227+
* @es: PCI channel failure state to report
228+
*/
229+
static void zpci_event_io_failure(struct pci_dev *pdev, pci_channel_state_t es)
230+
{
231+
struct pci_driver *driver;
232+
233+
pci_dev_lock(pdev);
234+
pdev->error_state = es;
235+
/**
236+
* While vfio-pci's error_detected callback notifies user-space QEMU
237+
* reacts to this by freezing the guest. In an s390 environment PCI
238+
* errors are rarely fatal so this is overkill. Instead in the future
239+
* we will inject the error event and let the guest recover the device
240+
* itself.
241+
*/
242+
if (is_passed_through(to_zpci(pdev)))
243+
goto out;
244+
driver = to_pci_driver(pdev->dev.driver);
245+
if (driver && driver->err_handler && driver->err_handler->error_detected)
246+
driver->err_handler->error_detected(pdev, pdev->error_state);
247+
out:
248+
pci_dev_unlock(pdev);
249+
}
250+
50251
static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
51252
{
52253
struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
53254
struct pci_dev *pdev = NULL;
255+
pci_ers_result_t ers_res;
54256

55257
zpci_dbg(3, "err fid:%x, fh:%x, pec:%x\n",
56258
ccdf->fid, ccdf->fh, ccdf->pec);
57259
zpci_err("error CCDF:\n");
58260
zpci_err_hex(ccdf, sizeof(*ccdf));
59261

60-
if (zdev)
61-
pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
262+
if (zdev) {
263+
zpci_update_fh(zdev, ccdf->fh);
264+
if (zdev->zbus->bus)
265+
pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
266+
}
62267

63268
pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
64269
pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
65270

66271
if (!pdev)
67272
return;
68273

69-
pdev->error_state = pci_channel_io_perm_failure;
274+
switch (ccdf->pec) {
275+
case 0x003a: /* Service Action or Error Recovery Successful */
276+
ers_res = zpci_event_attempt_error_recovery(pdev);
277+
if (ers_res != PCI_ERS_RESULT_RECOVERED)
278+
zpci_event_io_failure(pdev, pci_channel_io_perm_failure);
279+
break;
280+
default:
281+
/*
282+
* Mark as frozen not permanently failed because the device
283+
* could be subsequently recovered by the platform.
284+
*/
285+
zpci_event_io_failure(pdev, pci_channel_io_frozen);
286+
break;
287+
}
70288
pci_dev_put(pdev);
71289
}
72290

0 commit comments

Comments
 (0)