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