• 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 zpci_dev * zdev)62 static bool is_passed_through(struct zpci_dev *zdev)
63 {
64 	return zdev->s390_domain;
65 }
66 
is_driver_supported(struct pci_driver * driver)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 
zpci_event_notify_error_detected(struct pci_dev * pdev,struct pci_driver * driver)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 
zpci_event_do_error_state_clear(struct pci_dev * pdev,struct pci_driver * driver)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 	/* The underlying device may have been disabled by the event */
102 	if (!zdev_enabled(zdev))
103 		return PCI_ERS_RESULT_NEED_RESET;
104 
105 	pr_info("%s: Unblocking device access for examination\n", pci_name(pdev));
106 	rc = zpci_reset_load_store_blocked(zdev);
107 	if (rc) {
108 		pr_err("%s: Unblocking device access failed\n", pci_name(pdev));
109 		/* Let's try a full reset instead */
110 		return PCI_ERS_RESULT_NEED_RESET;
111 	}
112 
113 	if (driver->err_handler->mmio_enabled) {
114 		ers_res = driver->err_handler->mmio_enabled(pdev);
115 		if (ers_result_indicates_abort(ers_res)) {
116 			pr_info("%s: Automatic recovery failed after MMIO re-enable\n",
117 				pci_name(pdev));
118 			return ers_res;
119 		} else if (ers_res == PCI_ERS_RESULT_NEED_RESET) {
120 			pr_debug("%s: Driver needs reset to recover\n", pci_name(pdev));
121 			return ers_res;
122 		}
123 	}
124 
125 	pr_debug("%s: Unblocking DMA\n", pci_name(pdev));
126 	rc = zpci_clear_error_state(zdev);
127 	if (!rc) {
128 		pdev->error_state = pci_channel_io_normal;
129 	} else {
130 		pr_err("%s: Unblocking DMA failed\n", pci_name(pdev));
131 		/* Let's try a full reset instead */
132 		return PCI_ERS_RESULT_NEED_RESET;
133 	}
134 
135 	return ers_res;
136 }
137 
zpci_event_do_reset(struct pci_dev * pdev,struct pci_driver * driver)138 static pci_ers_result_t zpci_event_do_reset(struct pci_dev *pdev,
139 					    struct pci_driver *driver)
140 {
141 	pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
142 
143 	pr_info("%s: Initiating reset\n", pci_name(pdev));
144 	if (zpci_hot_reset_device(to_zpci(pdev))) {
145 		pr_err("%s: The reset request failed\n", pci_name(pdev));
146 		return ers_res;
147 	}
148 	pdev->error_state = pci_channel_io_normal;
149 	ers_res = driver->err_handler->slot_reset(pdev);
150 	if (ers_result_indicates_abort(ers_res)) {
151 		pr_info("%s: Automatic recovery failed after slot reset\n", pci_name(pdev));
152 		return ers_res;
153 	}
154 
155 	return ers_res;
156 }
157 
158 /* zpci_event_attempt_error_recovery - Try to recover the given PCI function
159  * @pdev: PCI function to recover currently in the error state
160  *
161  * We follow the scheme outlined in Documentation/PCI/pci-error-recovery.rst.
162  * With the simplification that recovery always happens per function
163  * and the platform determines which functions are affected for
164  * multi-function devices.
165  */
zpci_event_attempt_error_recovery(struct pci_dev * pdev)166 static pci_ers_result_t zpci_event_attempt_error_recovery(struct pci_dev *pdev)
167 {
168 	pci_ers_result_t ers_res = PCI_ERS_RESULT_DISCONNECT;
169 	struct pci_driver *driver;
170 
171 	/*
172 	 * Ensure that the PCI function is not removed concurrently, no driver
173 	 * is unbound or probed and that userspace can't access its
174 	 * configuration space while we perform recovery.
175 	 */
176 	pci_dev_lock(pdev);
177 	if (pdev->error_state == pci_channel_io_perm_failure) {
178 		ers_res = PCI_ERS_RESULT_DISCONNECT;
179 		goto out_unlock;
180 	}
181 	pdev->error_state = pci_channel_io_frozen;
182 
183 	if (is_passed_through(to_zpci(pdev))) {
184 		pr_info("%s: Cannot be recovered in the host because it is a pass-through device\n",
185 			pci_name(pdev));
186 		goto out_unlock;
187 	}
188 
189 	driver = to_pci_driver(pdev->dev.driver);
190 	if (!is_driver_supported(driver)) {
191 		if (!driver)
192 			pr_info("%s: Cannot be recovered because no driver is bound to the device\n",
193 				pci_name(pdev));
194 		else
195 			pr_info("%s: The %s driver bound to the device does not support error recovery\n",
196 				pci_name(pdev),
197 				driver->name);
198 		goto out_unlock;
199 	}
200 
201 	ers_res = zpci_event_notify_error_detected(pdev, driver);
202 	if (ers_result_indicates_abort(ers_res))
203 		goto out_unlock;
204 
205 	if (ers_res == PCI_ERS_RESULT_CAN_RECOVER) {
206 		ers_res = zpci_event_do_error_state_clear(pdev, driver);
207 		if (ers_result_indicates_abort(ers_res))
208 			goto out_unlock;
209 	}
210 
211 	if (ers_res == PCI_ERS_RESULT_NEED_RESET)
212 		ers_res = zpci_event_do_reset(pdev, driver);
213 
214 	if (ers_res != PCI_ERS_RESULT_RECOVERED) {
215 		pr_err("%s: Automatic recovery failed; operator intervention is required\n",
216 		       pci_name(pdev));
217 		goto out_unlock;
218 	}
219 
220 	pr_info("%s: The device is ready to resume operations\n", pci_name(pdev));
221 	if (driver->err_handler->resume)
222 		driver->err_handler->resume(pdev);
223 out_unlock:
224 	pci_dev_unlock(pdev);
225 
226 	return ers_res;
227 }
228 
229 /* zpci_event_io_failure - Report PCI channel failure state to driver
230  * @pdev: PCI function for which to report
231  * @es: PCI channel failure state to report
232  */
zpci_event_io_failure(struct pci_dev * pdev,pci_channel_state_t es)233 static void zpci_event_io_failure(struct pci_dev *pdev, pci_channel_state_t es)
234 {
235 	struct pci_driver *driver;
236 
237 	pci_dev_lock(pdev);
238 	pdev->error_state = es;
239 	/**
240 	 * While vfio-pci's error_detected callback notifies user-space QEMU
241 	 * reacts to this by freezing the guest. In an s390 environment PCI
242 	 * errors are rarely fatal so this is overkill. Instead in the future
243 	 * we will inject the error event and let the guest recover the device
244 	 * itself.
245 	 */
246 	if (is_passed_through(to_zpci(pdev)))
247 		goto out;
248 	driver = to_pci_driver(pdev->dev.driver);
249 	if (driver && driver->err_handler && driver->err_handler->error_detected)
250 		driver->err_handler->error_detected(pdev, pdev->error_state);
251 out:
252 	pci_dev_unlock(pdev);
253 }
254 
__zpci_event_error(struct zpci_ccdf_err * ccdf)255 static void __zpci_event_error(struct zpci_ccdf_err *ccdf)
256 {
257 	struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
258 	struct pci_dev *pdev = NULL;
259 	pci_ers_result_t ers_res;
260 
261 	zpci_dbg(3, "err fid:%x, fh:%x, pec:%x\n",
262 		 ccdf->fid, ccdf->fh, ccdf->pec);
263 	zpci_err("error CCDF:\n");
264 	zpci_err_hex(ccdf, sizeof(*ccdf));
265 
266 	if (zdev) {
267 		zpci_update_fh(zdev, ccdf->fh);
268 		if (zdev->zbus->bus)
269 			pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
270 	}
271 
272 	pr_err("%s: Event 0x%x reports an error for PCI function 0x%x\n",
273 	       pdev ? pci_name(pdev) : "n/a", ccdf->pec, ccdf->fid);
274 
275 	if (!pdev)
276 		goto no_pdev;
277 
278 	switch (ccdf->pec) {
279 	case 0x002a: /* Error event concerns FMB */
280 	case 0x002b:
281 	case 0x002c:
282 		break;
283 	case 0x0040: /* Service Action or Error Recovery Failed */
284 	case 0x003b:
285 		zpci_event_io_failure(pdev, pci_channel_io_perm_failure);
286 		break;
287 	default: /* PCI function left in the error state attempt to recover */
288 		ers_res = zpci_event_attempt_error_recovery(pdev);
289 		if (ers_res != PCI_ERS_RESULT_RECOVERED)
290 			zpci_event_io_failure(pdev, pci_channel_io_perm_failure);
291 		break;
292 	}
293 	pci_dev_put(pdev);
294 no_pdev:
295 	zpci_zdev_put(zdev);
296 }
297 
zpci_event_error(void * data)298 void zpci_event_error(void *data)
299 {
300 	if (zpci_is_enabled())
301 		__zpci_event_error(data);
302 }
303 
zpci_event_hard_deconfigured(struct zpci_dev * zdev,u32 fh)304 static void zpci_event_hard_deconfigured(struct zpci_dev *zdev, u32 fh)
305 {
306 	zpci_update_fh(zdev, fh);
307 	/* Give the driver a hint that the function is
308 	 * already unusable.
309 	 */
310 	zpci_bus_remove_device(zdev, true);
311 	/* Even though the device is already gone we still
312 	 * need to free zPCI resources as part of the disable.
313 	 */
314 	if (zdev->dma_table)
315 		zpci_dma_exit_device(zdev);
316 	if (zdev_enabled(zdev))
317 		zpci_disable_device(zdev);
318 	zdev->state = ZPCI_FN_STATE_STANDBY;
319 }
320 
__zpci_event_availability(struct zpci_ccdf_avail * ccdf)321 static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
322 {
323 	struct zpci_dev *zdev = get_zdev_by_fid(ccdf->fid);
324 	bool existing_zdev = !!zdev;
325 	enum zpci_state state;
326 
327 	zpci_dbg(3, "avl fid:%x, fh:%x, pec:%x\n",
328 		 ccdf->fid, ccdf->fh, ccdf->pec);
329 	switch (ccdf->pec) {
330 	case 0x0301: /* Reserved|Standby -> Configured */
331 		if (!zdev) {
332 			zdev = zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_CONFIGURED);
333 			if (IS_ERR(zdev))
334 				break;
335 		} else {
336 			/* the configuration request may be stale */
337 			if (zdev->state != ZPCI_FN_STATE_STANDBY)
338 				break;
339 			zdev->state = ZPCI_FN_STATE_CONFIGURED;
340 		}
341 		zpci_scan_configured_device(zdev, ccdf->fh);
342 		break;
343 	case 0x0302: /* Reserved -> Standby */
344 		if (!zdev)
345 			zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_STANDBY);
346 		else
347 			zpci_update_fh(zdev, ccdf->fh);
348 		break;
349 	case 0x0303: /* Deconfiguration requested */
350 		if (zdev) {
351 			/* The event may have been queued before we confirgured
352 			 * the device.
353 			 */
354 			if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
355 				break;
356 			zpci_update_fh(zdev, ccdf->fh);
357 			zpci_deconfigure_device(zdev);
358 		}
359 		break;
360 	case 0x0304: /* Configured -> Standby|Reserved */
361 		if (zdev) {
362 			/* The event may have been queued before we confirgured
363 			 * the device.:
364 			 */
365 			if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
366 				zpci_event_hard_deconfigured(zdev, ccdf->fh);
367 			/* The 0x0304 event may immediately reserve the device */
368 			if (!clp_get_state(zdev->fid, &state) &&
369 			    state == ZPCI_FN_STATE_RESERVED) {
370 				zpci_device_reserved(zdev);
371 			}
372 		}
373 		break;
374 	case 0x0306: /* 0x308 or 0x302 for multiple devices */
375 		zpci_remove_reserved_devices();
376 		clp_scan_pci_devices();
377 		break;
378 	case 0x0308: /* Standby -> Reserved */
379 		if (!zdev)
380 			break;
381 		zpci_device_reserved(zdev);
382 		break;
383 	default:
384 		break;
385 	}
386 	if (existing_zdev)
387 		zpci_zdev_put(zdev);
388 }
389 
zpci_event_availability(void * data)390 void zpci_event_availability(void *data)
391 {
392 	if (zpci_is_enabled())
393 		__zpci_event_availability(data);
394 }
395