pci-error-recovery.rst 20 KB

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