1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright IBM Corp. 2012
4  *
5  *  Author(s):
6  *    Jan Glauber <[email protected]>
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 		return false;
58 	default:
59 		return true;
60 	}
61 }
62 
is_passed_through(struct pci_dev * pdev)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 
is_driver_supported(struct pci_driver * driver)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 
zpci_event_notify_error_detected(struct pci_dev * pdev,struct pci_driver * driver)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 
zpci_event_do_error_state_clear(struct pci_dev * pdev,struct pci_driver * driver)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 
zpci_event_do_reset(struct pci_dev * pdev,struct pci_driver * driver)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  */
zpci_event_attempt_error_recovery(struct pci_dev * pdev)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  */
zpci_event_io_failure(struct pci_dev * pdev,pci_channel_state_t es)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 
__zpci_event_error(struct zpci_ccdf_err * ccdf)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 
zpci_event_error(void * data)317 void zpci_event_error(void *data)
318 {
319 	if (zpci_is_enabled())
320 		__zpci_event_error(data);
321 }
322 
zpci_event_hard_deconfigured(struct zpci_dev * zdev,u32 fh)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 
__zpci_event_availability(struct zpci_ccdf_avail * ccdf)338 static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
339 {
340 	struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
341 	bool existing_zdev = !!zdev;
342 	enum zpci_state state;
343 
344 	zpci_dbg(3, "avl fid:%x, fh:%x, pec:%x\n",
345 		 ccdf->fid, ccdf->fh, ccdf->pec);
346 
347 	if (existing_zdev)
348 		mutex_lock(&zdev->state_lock);
349 
350 	switch (ccdf->pec) {
351 	case 0x0301: /* Reserved|Standby -> Configured */
352 		if (!zdev) {
353 			zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_CONFIGURED);
354 			if (IS_ERR(zdev))
355 				break;
356 			if (zpci_add_device(zdev)) {
357 				kfree(zdev);
358 				break;
359 			}
360 		} else {
361 			/* the configuration request may be stale */
362 			if (zdev->state != ZPCI_FN_STATE_STANDBY)
363 				break;
364 			zdev->state = ZPCI_FN_STATE_CONFIGURED;
365 		}
366 		zpci_scan_configured_device(zdev, ccdf->fh);
367 		break;
368 	case 0x0302: /* Reserved -> Standby */
369 		if (!zdev) {
370 			zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_STANDBY);
371 			if (IS_ERR(zdev))
372 				break;
373 			if (zpci_add_device(zdev)) {
374 				kfree(zdev);
375 				break;
376 			}
377 		} else {
378 			zpci_update_fh(zdev, ccdf->fh);
379 		}
380 		break;
381 	case 0x0303: /* Deconfiguration requested */
382 		if (zdev) {
383 			/* The event may have been queued before we configured
384 			 * the device.
385 			 */
386 			if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
387 				break;
388 			zpci_update_fh(zdev, ccdf->fh);
389 			zpci_deconfigure_device(zdev);
390 		}
391 		break;
392 	case 0x0304: /* Configured -> Standby|Reserved */
393 		if (zdev) {
394 			/* The event may have been queued before we configured
395 			 * the device.:
396 			 */
397 			if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
398 				zpci_event_hard_deconfigured(zdev, ccdf->fh);
399 			/* The 0x0304 event may immediately reserve the device */
400 			if (!clp_get_state(zdev->fid, &state) &&
401 			    state == ZPCI_FN_STATE_RESERVED) {
402 				zpci_device_reserved(zdev);
403 			}
404 		}
405 		break;
406 	case 0x0306: /* 0x308 or 0x302 for multiple devices */
407 		zpci_remove_reserved_devices();
408 		zpci_scan_devices();
409 		break;
410 	case 0x0308: /* Standby -> Reserved */
411 		if (!zdev)
412 			break;
413 		zpci_device_reserved(zdev);
414 		break;
415 	default:
416 		break;
417 	}
418 	if (existing_zdev) {
419 		mutex_unlock(&zdev->state_lock);
420 		zpci_zdev_put(zdev);
421 	}
422 }
423 
zpci_event_availability(void * data)424 void zpci_event_availability(void *data)
425 {
426 	if (zpci_is_enabled())
427 		__zpci_event_availability(data);
428 }
429