1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2012
4 *
5 * Author(s):
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 */
8
9 #define KMSG_COMPONENT "zpci"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/pci.h>
14 #include <asm/pci_debug.h>
15 #include <asm/pci_dma.h>
16 #include <asm/sclp.h>
17
18 #include "pci_bus.h"
19 #include "pci_report.h"
20
21 /* Content Code Description for PCI Function Error */
22 struct zpci_ccdf_err {
23 u32 reserved1;
24 u32 fh; /* function handle */
25 u32 fid; /* function id */
26 u32 ett : 4; /* expected table type */
27 u32 mvn : 12; /* MSI vector number */
28 u32 dmaas : 8; /* DMA address space */
29 u32 : 6;
30 u32 q : 1; /* event qualifier */
31 u32 rw : 1; /* read/write */
32 u64 faddr; /* failing address */
33 u32 reserved3;
34 u16 reserved4;
35 u16 pec; /* PCI event code */
36 } __packed;
37
38 /* Content Code Description for PCI Function Availability */
39 struct zpci_ccdf_avail {
40 u32 reserved1;
41 u32 fh; /* function handle */
42 u32 fid; /* function id */
43 u32 reserved2;
44 u32 reserved3;
45 u32 reserved4;
46 u32 reserved5;
47 u16 reserved6;
48 u16 pec; /* PCI event code */
49 } __packed;
50
ers_result_indicates_abort(pci_ers_result_t ers_res)51 static inline bool ers_result_indicates_abort(pci_ers_result_t ers_res)
52 {
53 switch (ers_res) {
54 case PCI_ERS_RESULT_CAN_RECOVER:
55 case PCI_ERS_RESULT_RECOVERED:
56 case PCI_ERS_RESULT_NEED_RESET:
57 case PCI_ERS_RESULT_NONE:
58 return false;
59 default:
60 return true;
61 }
62 }
63
is_passed_through(struct pci_dev * pdev)64 static bool is_passed_through(struct pci_dev *pdev)
65 {
66 struct zpci_dev *zdev = to_zpci(pdev);
67 bool ret;
68
69 mutex_lock(&zdev->kzdev_lock);
70 ret = !!zdev->kzdev;
71 mutex_unlock(&zdev->kzdev_lock);
72
73 return ret;
74 }
75
is_driver_supported(struct pci_driver * driver)76 static bool is_driver_supported(struct pci_driver *driver)
77 {
78 if (!driver || !driver->err_handler)
79 return false;
80 if (!driver->err_handler->error_detected)
81 return false;
82 return true;
83 }
84
zpci_event_notify_error_detected(struct pci_dev * pdev,struct pci_driver * driver)85 static pci_ers_result_t zpci_event_notify_error_detected(struct pci_dev *pdev,
86 struct pci_driver *driver)
87 {
88 pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
89
90 ers_res = driver->err_handler->error_detected(pdev, pdev->error_state);
91 if (ers_result_indicates_abort(ers_res))
92 pr_info("%s: Automatic recovery failed after initial reporting\n", pci_name(pdev));
93 else if (ers_res == PCI_ERS_RESULT_NEED_RESET)
94 pr_debug("%s: Driver needs reset to recover\n", pci_name(pdev));
95
96 return ers_res;
97 }
98
zpci_event_do_error_state_clear(struct pci_dev * pdev,struct pci_driver * driver)99 static pci_ers_result_t zpci_event_do_error_state_clear(struct pci_dev *pdev,
100 struct pci_driver *driver)
101 {
102 pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
103 struct zpci_dev *zdev = to_zpci(pdev);
104 int rc;
105
106 /* The underlying device may have been disabled by the event */
107 if (!zdev_enabled(zdev))
108 return PCI_ERS_RESULT_NEED_RESET;
109
110 pr_info("%s: Unblocking device access for examination\n", pci_name(pdev));
111 rc = zpci_reset_load_store_blocked(zdev);
112 if (rc) {
113 pr_err("%s: Unblocking device access failed\n", pci_name(pdev));
114 /* Let's try a full reset instead */
115 return PCI_ERS_RESULT_NEED_RESET;
116 }
117
118 if (driver->err_handler->mmio_enabled)
119 ers_res = driver->err_handler->mmio_enabled(pdev);
120 else
121 ers_res = PCI_ERS_RESULT_NONE;
122
123 if (ers_result_indicates_abort(ers_res)) {
124 pr_info("%s: Automatic recovery failed after MMIO re-enable\n",
125 pci_name(pdev));
126 return ers_res;
127 } else if (ers_res == PCI_ERS_RESULT_NEED_RESET) {
128 pr_debug("%s: Driver needs reset to recover\n", pci_name(pdev));
129 return ers_res;
130 }
131
132 pr_debug("%s: Unblocking DMA\n", pci_name(pdev));
133 rc = zpci_clear_error_state(zdev);
134 if (!rc) {
135 pdev->error_state = pci_channel_io_normal;
136 } else {
137 pr_err("%s: Unblocking DMA failed\n", pci_name(pdev));
138 /* Let's try a full reset instead */
139 return PCI_ERS_RESULT_NEED_RESET;
140 }
141
142 return ers_res;
143 }
144
zpci_event_do_reset(struct pci_dev * pdev,struct pci_driver * driver)145 static pci_ers_result_t zpci_event_do_reset(struct pci_dev *pdev,
146 struct pci_driver *driver)
147 {
148 pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
149
150 pr_info("%s: Initiating reset\n", pci_name(pdev));
151 if (zpci_hot_reset_device(to_zpci(pdev))) {
152 pr_err("%s: The reset request failed\n", pci_name(pdev));
153 return ers_res;
154 }
155 pdev->error_state = pci_channel_io_normal;
156
157 if (driver->err_handler->slot_reset)
158 ers_res = driver->err_handler->slot_reset(pdev);
159 else
160 ers_res = PCI_ERS_RESULT_NONE;
161
162 if (ers_result_indicates_abort(ers_res)) {
163 pr_info("%s: Automatic recovery failed after slot reset\n", pci_name(pdev));
164 return ers_res;
165 }
166
167 return ers_res;
168 }
169
170 /* zpci_event_attempt_error_recovery - Try to recover the given PCI function
171 * @pdev: PCI function to recover currently in the error state
172 *
173 * We follow the scheme outlined in Documentation/PCI/pci-error-recovery.rst.
174 * With the simplification that recovery always happens per function
175 * and the platform determines which functions are affected for
176 * multi-function devices.
177 */
zpci_event_attempt_error_recovery(struct pci_dev * pdev)178 static pci_ers_result_t zpci_event_attempt_error_recovery(struct pci_dev *pdev)
179 {
180 pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
181 struct zpci_dev *zdev = to_zpci(pdev);
182 char *status_str = "success";
183 struct pci_driver *driver;
184
185 /*
186 * Ensure that the PCI function is not removed concurrently, no driver
187 * is unbound or probed and that userspace can't access its
188 * configuration space while we perform recovery.
189 */
190 pci_dev_lock(pdev);
191 if (pdev->error_state == pci_channel_io_perm_failure) {
192 ers_res = PCI_ERS_RESULT_DISCONNECT;
193 goto out_unlock;
194 }
195 pdev->error_state = pci_channel_io_frozen;
196
197 if (is_passed_through(pdev)) {
198 pr_info("%s: Cannot be recovered in the host because it is a pass-through device\n",
199 pci_name(pdev));
200 status_str = "failed (pass-through)";
201 goto out_unlock;
202 }
203
204 driver = to_pci_driver(pdev->dev.driver);
205 if (!is_driver_supported(driver)) {
206 if (!driver) {
207 pr_info("%s: Cannot be recovered because no driver is bound to the device\n",
208 pci_name(pdev));
209 status_str = "failed (no driver)";
210 } else {
211 pr_info("%s: The %s driver bound to the device does not support error recovery\n",
212 pci_name(pdev),
213 driver->name);
214 status_str = "failed (no driver support)";
215 }
216 goto out_unlock;
217 }
218
219 ers_res = zpci_event_notify_error_detected(pdev, driver);
220 if (ers_result_indicates_abort(ers_res)) {
221 status_str = "failed (abort on detection)";
222 goto out_unlock;
223 }
224
225 if (ers_res != PCI_ERS_RESULT_NEED_RESET) {
226 ers_res = zpci_event_do_error_state_clear(pdev, driver);
227 if (ers_result_indicates_abort(ers_res)) {
228 status_str = "failed (abort on MMIO enable)";
229 goto out_unlock;
230 }
231 }
232
233 if (ers_res == PCI_ERS_RESULT_NEED_RESET)
234 ers_res = zpci_event_do_reset(pdev, driver);
235
236 /*
237 * ers_res can be PCI_ERS_RESULT_NONE either because the driver
238 * decided to return it, indicating that it abstains from voting
239 * on how to recover, or because it didn't implement the callback.
240 * Both cases assume, that if there is nothing else causing a
241 * disconnect, we recovered successfully.
242 */
243 if (ers_res == PCI_ERS_RESULT_NONE)
244 ers_res = PCI_ERS_RESULT_RECOVERED;
245
246 if (ers_res != PCI_ERS_RESULT_RECOVERED) {
247 pr_err("%s: Automatic recovery failed; operator intervention is required\n",
248 pci_name(pdev));
249 status_str = "failed (driver can't recover)";
250 goto out_unlock;
251 }
252
253 pr_info("%s: The device is ready to resume operations\n", pci_name(pdev));
254 if (driver->err_handler->resume)
255 driver->err_handler->resume(pdev);
256 out_unlock:
257 pci_dev_unlock(pdev);
258 zpci_report_status(zdev, "recovery", status_str);
259
260 return ers_res;
261 }
262
263 /* zpci_event_io_failure - Report PCI channel failure state to driver
264 * @pdev: PCI function for which to report
265 * @es: PCI channel failure state to report
266 */
zpci_event_io_failure(struct pci_dev * pdev,pci_channel_state_t es)267 static void zpci_event_io_failure(struct pci_dev *pdev, pci_channel_state_t es)
268 {
269 struct pci_driver *driver;
270
271 pci_dev_lock(pdev);
272 pdev->error_state = es;
273 /**
274 * While vfio-pci's error_detected callback notifies user-space QEMU
275 * reacts to this by freezing the guest. In an s390 environment PCI
276 * errors are rarely fatal so this is overkill. Instead in the future
277 * we will inject the error event and let the guest recover the device
278 * itself.
279 */
280 if (is_passed_through(pdev))
281 goto out;
282 driver = to_pci_driver(pdev->dev.driver);
283 if (driver && driver->err_handler && driver->err_handler->error_detected)
284 driver->err_handler->error_detected(pdev, pdev->error_state);
285 out:
286 pci_dev_unlock(pdev);
287 }
288
__zpci_event_error(struct zpci_ccdf_err * ccdf)289 static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
290 {
291 struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
292 struct pci_dev *pdev = NULL;
293 pci_ers_result_t ers_res;
294 u32 fh = 0;
295 int rc;
296
297 zpci_dbg(3, "err fid:%x, fh:%x, pec:%x\n",
298 ccdf->fid, ccdf->fh, ccdf->pec);
299 zpci_err("error CCDF:\n");
300 zpci_err_hex(ccdf, sizeof(*ccdf));
301
302 if (zdev) {
303 mutex_lock(&zdev->state_lock);
304 rc = clp_refresh_fh(zdev->fid, &fh);
305 if (rc)
306 goto no_pdev;
307 if (!fh || ccdf->fh != fh) {
308 /* Ignore events with stale handles */
309 zpci_dbg(3, "err fid:%x, fh:%x (stale %x)\n",
310 ccdf->fid, fh, ccdf->fh);
311 goto no_pdev;
312 }
313 zpci_update_fh(zdev, ccdf->fh);
314 if (zdev->zbus->bus)
315 pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
316 }
317
318 pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
319 pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
320
321 if (!pdev)
322 goto no_pdev;
323
324 switch (ccdf->pec) {
325 case 0x002a: /* Error event concerns FMB */
326 case 0x002b:
327 case 0x002c:
328 break;
329 case 0x0040: /* Service Action or Error Recovery Failed */
330 case 0x003b:
331 zpci_event_io_failure(pdev, pci_channel_io_perm_failure);
332 break;
333 default: /* PCI function left in the error state attempt to recover */
334 ers_res = zpci_event_attempt_error_recovery(pdev);
335 if (ers_res != PCI_ERS_RESULT_RECOVERED)
336 zpci_event_io_failure(pdev, pci_channel_io_perm_failure);
337 break;
338 }
339 pci_dev_put(pdev);
340 no_pdev:
341 if (zdev)
342 mutex_unlock(&zdev->state_lock);
343 zpci_zdev_put(zdev);
344 }
345
zpci_event_error(void * data)346 void zpci_event_error(void *data)
347 {
348 if (zpci_is_enabled())
349 __zpci_event_error(data);
350 }
351
zpci_event_hard_deconfigured(struct zpci_dev * zdev,u32 fh)352 static void zpci_event_hard_deconfigured(struct zpci_dev *zdev, u32 fh)
353 {
354 zpci_update_fh(zdev, fh);
355 /* Give the driver a hint that the function is
356 * already unusable.
357 */
358 zpci_bus_remove_device(zdev, true);
359 /* Even though the device is already gone we still
360 * need to free zPCI resources as part of the disable.
361 */
362 if (zdev_enabled(zdev))
363 zpci_disable_device(zdev);
364 zdev->state = ZPCI_FN_STATE_STANDBY;
365 }
366
zpci_event_reappear(struct zpci_dev * zdev)367 static void zpci_event_reappear(struct zpci_dev *zdev)
368 {
369 lockdep_assert_held(&zdev->state_lock);
370 /*
371 * The zdev is in the reserved state. This means that it was presumed to
372 * go away but there are still undropped references. Now, the platform
373 * announced its availability again. Bring back the lingering zdev
374 * to standby. This is safe because we hold a temporary reference
375 * now so that it won't go away. Account for the re-appearance of the
376 * underlying device by incrementing the reference count.
377 */
378 zdev->state = ZPCI_FN_STATE_STANDBY;
379 zpci_zdev_get(zdev);
380 zpci_dbg(1, "rea fid:%x, fh:%x\n", zdev->fid, zdev->fh);
381 }
382
__zpci_event_availability(struct zpci_ccdf_avail * ccdf)383 static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
384 {
385 struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
386 bool existing_zdev = !!zdev;
387 enum zpci_state state;
388
389 zpci_dbg(3, "avl fid:%x, fh:%x, pec:%x\n",
390 ccdf->fid, ccdf->fh, ccdf->pec);
391
392 if (existing_zdev)
393 mutex_lock(&zdev->state_lock);
394
395 switch (ccdf->pec) {
396 case 0x0301: /* Reserved|Standby -> Configured */
397 if (!zdev) {
398 zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_CONFIGURED);
399 if (IS_ERR(zdev))
400 break;
401 if (zpci_add_device(zdev)) {
402 kfree(zdev);
403 break;
404 }
405 } else {
406 if (zdev->state == ZPCI_FN_STATE_RESERVED)
407 zpci_event_reappear(zdev);
408 /* the configuration request may be stale */
409 else if (zdev->state != ZPCI_FN_STATE_STANDBY)
410 break;
411 zdev->state = ZPCI_FN_STATE_CONFIGURED;
412 }
413 zpci_scan_configured_device(zdev, ccdf->fh);
414 break;
415 case 0x0302: /* Reserved -> Standby */
416 if (!zdev) {
417 zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_STANDBY);
418 if (IS_ERR(zdev))
419 break;
420 if (zpci_add_device(zdev)) {
421 kfree(zdev);
422 break;
423 }
424 } else {
425 if (zdev->state == ZPCI_FN_STATE_RESERVED)
426 zpci_event_reappear(zdev);
427 zpci_update_fh(zdev, ccdf->fh);
428 }
429 break;
430 case 0x0303: /* Deconfiguration requested */
431 if (zdev) {
432 /* The event may have been queued before we configured
433 * the device.
434 */
435 if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
436 break;
437 zpci_update_fh(zdev, ccdf->fh);
438 zpci_deconfigure_device(zdev);
439 }
440 break;
441 case 0x0304: /* Configured -> Standby|Reserved */
442 if (zdev) {
443 /* The event may have been queued before we configured
444 * the device.:
445 */
446 if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
447 zpci_event_hard_deconfigured(zdev, ccdf->fh);
448 /* The 0x0304 event may immediately reserve the device */
449 if (!clp_get_state(zdev->fid, &state) &&
450 state == ZPCI_FN_STATE_RESERVED) {
451 zpci_device_reserved(zdev);
452 }
453 }
454 break;
455 case 0x0306: /* 0x308 or 0x302 for multiple devices */
456 zpci_remove_reserved_devices();
457 zpci_scan_devices();
458 break;
459 case 0x0308: /* Standby -> Reserved */
460 if (!zdev)
461 break;
462 zpci_device_reserved(zdev);
463 break;
464 default:
465 break;
466 }
467 if (existing_zdev) {
468 mutex_unlock(&zdev->state_lock);
469 zpci_zdev_put(zdev);
470 }
471 }
472
zpci_event_availability(void * data)473 void zpci_event_availability(void *data)
474 {
475 if (zpci_is_enabled())
476 __zpci_event_availability(data);
477 }
478