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