xref: /linux/Documentation/PCI/pci-error-recovery.rst (revision 68a052239fc4b351e961f698b824f7654a346091)
1.. SPDX-License-Identifier: GPL-2.0
2
3==================
4PCI Error Recovery
5==================
6
7
8:Authors: - Linas Vepstas <linasvepstas@gmail.com>
9          - Richard Lary <rlary@us.ibm.com>
10          - Mike Mason <mmlnx@us.ibm.com>
11
12
13Many PCI bus controllers are able to detect a variety of hardware
14PCI errors on the bus, such as parity errors on the data and address
15buses, as well as SERR and PERR errors.  Some of the more advanced
16chipsets are able to deal with these errors; these include PCIe chipsets,
17and the PCI-host bridges found on IBM Power4, Power5 and Power6-based
18pSeries boxes. A typical action taken is to disconnect the affected device,
19halting all I/O to it.  The goal of a disconnection is to avoid system
20corruption; for example, to halt system memory corruption due to DMAs
21to "wild" addresses. Typically, a reconnection mechanism is also
22offered, so that the affected PCI device(s) are reset and put back
23into working condition. The reset phase requires coordination
24between the affected device drivers and the PCI controller chip.
25This document describes a generic API for notifying device drivers
26of a bus disconnection, and then performing error recovery.
27This API is currently implemented in the 2.6.16 and later kernels.
28
29Reporting and recovery is performed in several steps. First, when
30a PCI hardware error has resulted in a bus disconnect, that event
31is reported as soon as possible to all affected device drivers,
32including multiple instances of a device driver on multi-function
33cards. This allows device drivers to avoid deadlocking in spinloops,
34waiting for some i/o-space register to change, when it never will.
35It also gives the drivers a chance to defer incoming I/O as
36needed.
37
38Next, recovery is performed in several stages. Most of the complexity
39is forced by the need to handle multi-function devices, that is,
40devices that have multiple device drivers associated with them.
41In the first stage, each driver is allowed to indicate what type
42of reset it desires, the choices being a simple re-enabling of I/O
43or requesting a slot reset.
44
45If any driver requests a slot reset, that is what will be done.
46
47After a reset and/or a re-enabling of I/O, all drivers are
48again notified, so that they may then perform any device setup/config
49that may be required.  After these have all completed, a final
50"resume normal operations" event is sent out.
51
52The biggest reason for choosing a kernel-based implementation rather
53than a user-space implementation was the need to deal with bus
54disconnects of PCI devices attached to storage media, and, in particular,
55disconnects from devices holding the root file system.  If the root
56file system is disconnected, a user-space mechanism would have to go
57through a large number of contortions to complete recovery. Almost all
58of the current Linux file systems are not tolerant of disconnection
59from/reconnection to their underlying block device. By contrast,
60bus errors are easy to manage in the device driver. Indeed, most
61device drivers already handle very similar recovery procedures;
62for example, the SCSI-generic layer already provides significant
63mechanisms for dealing with SCSI bus errors and SCSI bus resets.
64
65
66Detailed Design
67===============
68
69Design and implementation details below, based on a chain of
70public email discussions with Ben Herrenschmidt, circa 5 April 2005.
71
72The error recovery API support is exposed to the driver in the form of
73a structure of function pointers pointed to by a new field in struct
74pci_driver. A driver that fails to provide the structure is "non-aware",
75and the actual recovery steps taken are platform dependent.  The
76arch/powerpc implementation will simulate a PCI hotplug remove/add.
77
78This structure has the form::
79
80	struct pci_error_handlers
81	{
82		int (*error_detected)(struct pci_dev *dev, pci_channel_state_t);
83		int (*mmio_enabled)(struct pci_dev *dev);
84		int (*slot_reset)(struct pci_dev *dev);
85		void (*resume)(struct pci_dev *dev);
86		void (*cor_error_detected)(struct pci_dev *dev);
87	};
88
89The possible channel states are::
90
91	typedef enum {
92		pci_channel_io_normal,  /* I/O channel is in normal state */
93		pci_channel_io_frozen,  /* I/O to channel is blocked */
94		pci_channel_io_perm_failure, /* PCI card is dead */
95	} pci_channel_state_t;
96
97Possible return values are::
98
99	enum pci_ers_result {
100		PCI_ERS_RESULT_NONE,        /* no result/none/not supported in device driver */
101		PCI_ERS_RESULT_CAN_RECOVER, /* Device driver can recover without slot reset */
102		PCI_ERS_RESULT_NEED_RESET,  /* Device driver wants slot to be reset. */
103		PCI_ERS_RESULT_DISCONNECT,  /* Device has completely failed, is unrecoverable */
104		PCI_ERS_RESULT_RECOVERED,   /* Device driver is fully recovered and operational */
105	};
106
107A driver does not have to implement all of these callbacks; however,
108if it implements any, it must implement error_detected(). If a callback
109is not implemented, the corresponding feature is considered unsupported.
110For example, if mmio_enabled() and resume() aren't there, then it
111is assumed that the driver does not need these callbacks
112for recovery.  Typically a driver will want to know about
113a slot_reset().
114
115The actual steps taken by a platform to recover from a PCI error
116event will be platform-dependent, but will follow the general
117sequence described below.
118
119STEP 0: Error Event
120-------------------
121A PCI bus error is detected by the PCI hardware.  On powerpc, the slot
122is isolated, in that all I/O is blocked: all reads return 0xffffffff,
123all writes are ignored.
124
125Similarly, on platforms supporting Downstream Port Containment
126(PCIe r7.0 sec 6.2.11), the link to the sub-hierarchy with the
127faulting device is disabled. Any device in the sub-hierarchy
128becomes inaccessible.
129
130STEP 1: Notification
131--------------------
132Platform calls the error_detected() callback on every instance of
133every driver affected by the error.
134
135At this point, the device might not be accessible anymore, depending on
136the platform (the slot will be isolated on powerpc). The driver may
137already have "noticed" the error because of a failing I/O, but this
138is the proper "synchronization point", that is, it gives the driver
139a chance to cleanup, waiting for pending stuff (timers, whatever, etc...)
140to complete; it can take semaphores, schedule, etc... everything but
141touch the device. Within this function and after it returns, the driver
142shouldn't do any new IOs. Called in task context. This is sort of a
143"quiesce" point. See note about interrupts at the end of this doc.
144
145All drivers participating in this system must implement this call.
146The driver must return one of the following result codes:
147
148  - PCI_ERS_RESULT_RECOVERED
149      Driver returns this if it thinks the device is usable despite
150      the error and does not need further intervention.
151  - PCI_ERS_RESULT_CAN_RECOVER
152      Driver returns this if it thinks it might be able to recover
153      the HW by just banging IOs or if it wants to be given
154      a chance to extract some diagnostic information (see
155      mmio_enable, below).
156  - PCI_ERS_RESULT_NEED_RESET
157      Driver returns this if it can't recover without a
158      slot reset.
159  - PCI_ERS_RESULT_DISCONNECT
160      Driver returns this if it doesn't want to recover at all.
161
162The next step taken will depend on the result codes returned by the
163drivers.
164
165If all drivers on the segment/slot return PCI_ERS_RESULT_CAN_RECOVER,
166then the platform should re-enable IOs on the slot (or do nothing in
167particular, if the platform doesn't isolate slots), and recovery
168proceeds to STEP 2 (MMIO Enable).
169
170If any driver requested a slot reset (by returning PCI_ERS_RESULT_NEED_RESET),
171then recovery proceeds to STEP 4 (Slot Reset).
172
173If the platform is unable to recover the slot, the next step
174is STEP 6 (Permanent Failure).
175
176.. note::
177
178   The current powerpc implementation assumes that a device driver will
179   *not* schedule or semaphore in this routine; the current powerpc
180   implementation uses one kernel thread to notify all devices;
181   thus, if one device sleeps/schedules, all devices are affected.
182   Doing better requires complex multi-threaded logic in the error
183   recovery implementation (e.g. waiting for all notification threads
184   to "join" before proceeding with recovery.)  This seems excessively
185   complex and not worth implementing.
186
187   The current powerpc implementation doesn't much care if the device
188   attempts I/O at this point, or not.  I/Os will fail, returning
189   a value of 0xff on read, and writes will be dropped. If more than
190   EEH_MAX_FAILS I/Os are attempted to a frozen adapter, EEH
191   assumes that the device driver has gone into an infinite loop
192   and prints an error to syslog.  A reboot is then required to
193   get the device working again.
194
195STEP 2: MMIO Enabled
196--------------------
197The platform re-enables MMIO to the device (but typically not the
198DMA), and then calls the mmio_enabled() callback on all affected
199device drivers.
200
201This is the "early recovery" call. IOs are allowed again, but DMA is
202not, with some restrictions. This is NOT a callback for the driver to
203start operations again, only to peek/poke at the device, extract diagnostic
204information, if any, and eventually do things like trigger a device local
205reset or some such, but not restart operations. This callback is made if
206all drivers on a segment agree that they can try to recover and if no automatic
207link reset was performed by the HW. If the platform can't just re-enable IOs
208without a slot reset or a link reset, it will not call this callback, and
209instead will have gone directly to STEP 3 (Link Reset) or STEP 4 (Slot Reset).
210
211.. note::
212
213   On platforms supporting Advanced Error Reporting (PCIe r7.0 sec 6.2),
214   the faulting device may already be accessible in STEP 1 (Notification).
215   Drivers should nevertheless defer accesses to STEP 2 (MMIO Enabled)
216   to be compatible with EEH on powerpc and with s390 (where devices are
217   inaccessible until STEP 2).
218
219   On platforms supporting Downstream Port Containment, the link to the
220   sub-hierarchy with the faulting device is re-enabled in STEP 3 (Link
221   Reset). Hence devices in the sub-hierarchy are inaccessible until
222   STEP 4 (Slot Reset).
223
224   For errors such as Surprise Down (PCIe r7.0 sec 6.2.7), the device
225   may not even be accessible in STEP 4 (Slot Reset). Drivers can detect
226   accessibility by checking whether reads from the device return all 1's
227   (PCI_POSSIBLE_ERROR()).
228
229.. note::
230
231   The following is proposed; no platform implements this yet:
232   Proposal: All I/Os should be done _synchronously_ from within
233   this callback, errors triggered by them will be returned via
234   the normal pci_check_whatever() API, no new error_detected()
235   callback will be issued due to an error happening here. However,
236   such an error might cause IOs to be re-blocked for the whole
237   segment, and thus invalidate the recovery that other devices
238   on the same segment might have done, forcing the whole segment
239   into one of the next states, that is, link reset or slot reset.
240
241The driver should return one of the following result codes:
242  - PCI_ERS_RESULT_RECOVERED
243      Driver returns this if it thinks the device is fully
244      functional and thinks it is ready to start
245      normal driver operations again. There is no
246      guarantee that the driver will actually be
247      allowed to proceed, as another driver on the
248      same segment might have failed and thus triggered a
249      slot reset on platforms that support it.
250
251  - PCI_ERS_RESULT_NEED_RESET
252      Driver returns this if it thinks the device is not
253      recoverable in its current state and it needs a slot
254      reset to proceed.
255
256  - PCI_ERS_RESULT_DISCONNECT
257      Same as above. Total failure, no recovery even after
258      reset driver dead. (To be defined more precisely)
259
260The next step taken depends on the results returned by the drivers.
261If all drivers returned PCI_ERS_RESULT_RECOVERED, then the platform
262proceeds to either STEP 3 (Link Reset) or to STEP 5 (Resume Operations).
263
264If any driver returned PCI_ERS_RESULT_NEED_RESET, then the platform
265proceeds to STEP 4 (Slot Reset)
266
267STEP 3: Link Reset
268------------------
269The platform resets the link.  This is a PCIe specific step
270and is done whenever a fatal error has been detected that can be
271"solved" by resetting the link.
272
273STEP 4: Slot Reset
274------------------
275
276In response to a return value of PCI_ERS_RESULT_NEED_RESET, the
277platform will perform a slot reset on the requesting PCI device(s).
278The actual steps taken by a platform to perform a slot reset
279will be platform-dependent. Upon completion of slot reset, the
280platform will call the device slot_reset() callback.
281
282Powerpc platforms implement two levels of slot reset:
283soft reset(default) and fundamental(optional) reset.
284
285Powerpc soft reset consists of asserting the adapter #RST line and then
286restoring the PCI BARs and PCI configuration header to a state
287that is equivalent to what it would be after a fresh system
288power-on followed by power-on BIOS/system firmware initialization.
289Soft reset is also known as hot-reset.
290
291Powerpc fundamental reset is supported by PCIe cards only
292and results in device's state machines, hardware logic, port states and
293configuration registers to initialize to their default conditions.
294
295For most PCI devices, a soft reset will be sufficient for recovery.
296Optional fundamental reset is provided to support a limited number
297of PCIe devices for which a soft reset is not sufficient
298for recovery.
299
300If the platform supports PCI hotplug, then the reset might be
301performed by toggling the slot electrical power off/on.
302
303It is important for the platform to restore the PCI config space
304to the "fresh poweron" state, rather than the "last state". After
305a slot reset, the device driver will almost always use its standard
306device initialization routines, and an unusual config space setup
307may result in hung devices, kernel panics, or silent data corruption.
308
309This call gives drivers the chance to re-initialize the hardware
310(re-download firmware, etc.).  At this point, the driver may assume
311that the card is in a fresh state and is fully functional. The slot
312is unfrozen and the driver has full access to PCI config space,
313memory mapped I/O space and DMA. Interrupts (Legacy, MSI, or MSI-X)
314will also be available.
315
316Drivers should not restart normal I/O processing operations
317at this point.  If all device drivers report success on this
318callback, the platform will call resume() to complete the sequence,
319and let the driver restart normal I/O processing.
320
321A driver can still return a critical failure for this function if
322it can't get the device operational after reset.  If the platform
323previously tried a soft reset, it might now try a hard reset (power
324cycle) and then call slot_reset() again.  If the device still can't
325be recovered, there is nothing more that can be done;  the platform
326will typically report a "permanent failure" in such a case.  The
327device will be considered "dead" in this case.
328
329Drivers for multi-function cards will need to coordinate among
330themselves as to which driver instance will perform any "one-shot"
331or global device initialization. For example, the Symbios sym53cxx2
332driver performs device init only from PCI function 0::
333
334	+       if (PCI_FUNC(pdev->devfn) == 0)
335	+               sym_reset_scsi_bus(np, 0);
336
337Result codes:
338	- PCI_ERS_RESULT_DISCONNECT
339	  Same as above.
340
341Drivers for PCIe cards that require a fundamental reset must
342set the needs_freset bit in the pci_dev structure in their probe function.
343For example, the QLogic qla2xxx driver sets the needs_freset bit for certain
344PCI card types::
345
346	+	/* Set EEH reset type to fundamental if required by hba  */
347	+	if (IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha))
348	+		pdev->needs_freset = 1;
349	+
350
351Platform proceeds either to STEP 5 (Resume Operations) or STEP 6 (Permanent
352Failure).
353
354.. note::
355
356   The current powerpc implementation does not try a power-cycle
357   reset if the driver returned PCI_ERS_RESULT_DISCONNECT.
358   However, it probably should.
359
360
361STEP 5: Resume Operations
362-------------------------
363The platform will call the resume() callback on all affected device
364drivers if all drivers on the segment have returned
365PCI_ERS_RESULT_RECOVERED from one of the 3 previous callbacks.
366The goal of this callback is to tell the driver to restart activity,
367that everything is back and running. This callback does not return
368a result code.
369
370At this point, if a new error happens, the platform will restart
371a new error recovery sequence.
372
373STEP 6: Permanent Failure
374-------------------------
375A "permanent failure" has occurred, and the platform cannot recover
376the device.  The platform will call error_detected() with a
377pci_channel_state_t value of pci_channel_io_perm_failure.
378
379The device driver should, at this point, assume the worst. It should
380cancel all pending I/O, refuse all new I/O, returning -EIO to
381higher layers. The device driver should then clean up all of its
382memory and remove itself from kernel operations, much as it would
383during system shutdown.
384
385The platform will typically notify the system operator of the
386permanent failure in some way.  If the device is hotplug-capable,
387the operator will probably want to remove and replace the device.
388Note, however, not all failures are truly "permanent". Some are
389caused by over-heating, some by a poorly seated card. Many
390PCI error events are caused by software bugs, e.g. DMAs to
391wild addresses or bogus split transactions due to programming
392errors. See the discussion in Documentation/arch/powerpc/eeh-pci-error-recovery.rst
393for additional detail on real-life experience of the causes of
394software errors.
395
396
397Conclusion; General Remarks
398---------------------------
399The way the callbacks are called is platform policy. A platform with
400no slot reset capability may want to just "ignore" drivers that can't
401recover (disconnect them) and try to let other cards on the same segment
402recover. Keep in mind that in most real life cases, though, there will
403be only one driver per segment.
404
405Now, a note about interrupts. If you get an interrupt and your
406device is dead or has been isolated, there is a problem :)
407The current policy is to turn this into a platform policy.
408That is, the recovery API only requires that:
409
410 - There is no guarantee that interrupt delivery can proceed from any
411   device on the segment starting from the error detection and until the
412   slot_reset callback is called, at which point interrupts are expected
413   to be fully operational.
414
415 - There is no guarantee that interrupt delivery is stopped, that is,
416   a driver that gets an interrupt after detecting an error, or that detects
417   an error within the interrupt handler such that it prevents proper
418   ack'ing of the interrupt (and thus removal of the source) should just
419   return IRQ_NOTHANDLED. It's up to the platform to deal with that
420   condition, typically by masking the IRQ source during the duration of
421   the error handling. It is expected that the platform "knows" which
422   interrupts are routed to error-management capable slots and can deal
423   with temporarily disabling that IRQ number during error processing (this
424   isn't terribly complex). That means some IRQ latency for other devices
425   sharing the interrupt, but there is simply no other way. High end
426   platforms aren't supposed to share interrupts between many devices
427   anyway :)
428
429.. note::
430
431   Implementation details for the powerpc platform are discussed in
432   the file Documentation/arch/powerpc/eeh-pci-error-recovery.rst
433
434   As of this writing, there is a growing list of device drivers with
435   patches implementing error recovery. Not all of these patches are in
436   mainline yet. These may be used as "examples":
437
438   - drivers/scsi/ipr
439   - drivers/scsi/sym53c8xx_2
440   - drivers/scsi/qla2xxx
441   - drivers/scsi/lpfc
442   - drivers/next/bnx2.c
443   - drivers/next/e100.c
444   - drivers/net/e1000
445   - drivers/net/e1000e
446   - drivers/net/ixgbe
447   - drivers/net/cxgb3
448   - drivers/net/s2io.c
449
450   The cor_error_detected() callback is invoked in handle_error_source() when
451   the error severity is "correctable". The callback is optional and allows
452   additional logging to be done if desired. See example:
453
454   - drivers/cxl/pci.c
455
456The End
457-------
458