scsi_eh.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =======
  3. SCSI EH
  4. =======
  5. This document describes SCSI midlayer error handling infrastructure.
  6. Please refer to Documentation/scsi/scsi_mid_low_api.rst for more
  7. information regarding SCSI midlayer.
  8. .. TABLE OF CONTENTS
  9. [1] How SCSI commands travel through the midlayer and to EH
  10. [1-1] struct scsi_cmnd
  11. [1-2] How do scmd's get completed?
  12. [1-2-1] Completing a scmd w/ scsi_done
  13. [1-2-2] Completing a scmd w/ timeout
  14. [1-3] How EH takes over
  15. [2] How SCSI EH works
  16. [2-1] EH through fine-grained callbacks
  17. [2-1-1] Overview
  18. [2-1-2] Flow of scmds through EH
  19. [2-1-3] Flow of control
  20. [2-2] EH through transportt->eh_strategy_handler()
  21. [2-2-1] Pre transportt->eh_strategy_handler() SCSI midlayer conditions
  22. [2-2-2] Post transportt->eh_strategy_handler() SCSI midlayer conditions
  23. [2-2-3] Things to consider
  24. 1. How SCSI commands travel through the midlayer and to EH
  25. ==========================================================
  26. 1.1 struct scsi_cmnd
  27. --------------------
  28. Each SCSI command is represented with struct scsi_cmnd (== scmd). A
  29. scmd has two list_head's to link itself into lists. The two are
  30. scmd->list and scmd->eh_entry. The former is used for free list or
  31. per-device allocated scmd list and not of much interest to this EH
  32. discussion. The latter is used for completion and EH lists and unless
  33. otherwise stated scmds are always linked using scmd->eh_entry in this
  34. discussion.
  35. 1.2 How do scmd's get completed?
  36. --------------------------------
  37. Once LLDD gets hold of a scmd, either the LLDD will complete the
  38. command by calling scsi_done callback passed from midlayer when
  39. invoking hostt->queuecommand() or the block layer will time it out.
  40. 1.2.1 Completing a scmd w/ scsi_done
  41. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  42. For all non-EH commands, scsi_done() is the completion callback. It
  43. just calls blk_mq_complete_request() to delete the block layer timer and
  44. raise BLOCK_SOFTIRQ.
  45. The BLOCK_SOFTIRQ indirectly calls scsi_complete(), which calls
  46. scsi_decide_disposition() to determine what to do with the command.
  47. scsi_decide_disposition() looks at the scmd->result value and sense
  48. data to determine what to do with the command.
  49. - SUCCESS
  50. scsi_finish_command() is invoked for the command. The
  51. function does some maintenance chores and then calls
  52. scsi_io_completion() to finish the I/O.
  53. scsi_io_completion() then notifies the block layer on
  54. the completed request by calling blk_end_request and
  55. friends or figures out what to do with the remainder
  56. of the data in case of an error.
  57. - NEEDS_RETRY
  58. - ADD_TO_MLQUEUE
  59. scmd is requeued to blk queue.
  60. - otherwise
  61. scsi_eh_scmd_add(scmd) is invoked for the command. See
  62. [1-3] for details of this function.
  63. 1.2.2 Completing a scmd w/ timeout
  64. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  65. The timeout handler is scsi_timeout(). When a timeout occurs, this function
  66. 1. invokes optional hostt->eh_timed_out() callback. Return value can
  67. be one of
  68. - SCSI_EH_RESET_TIMER
  69. This indicates that more time is required to finish the
  70. command. Timer is restarted.
  71. - SCSI_EH_NOT_HANDLED
  72. eh_timed_out() callback did not handle the command.
  73. Step #2 is taken.
  74. - SCSI_EH_DONE
  75. eh_timed_out() completed the command.
  76. 2. scsi_abort_command() is invoked to schedule an asynchronous abort which may
  77. issue a retry scmd->allowed + 1 times. Asynchronous aborts are not invoked
  78. for commands for which the SCSI_EH_ABORT_SCHEDULED flag is set (this
  79. indicates that the command already had been aborted once, and this is a
  80. retry which failed), when retries are exceeded, or when the EH deadline is
  81. expired. In these cases Step #3 is taken.
  82. 3. scsi_eh_scmd_add(scmd) is invoked for the
  83. command. See [1-4] for more information.
  84. 1.3 Asynchronous command aborts
  85. -------------------------------
  86. After a timeout occurs a command abort is scheduled from
  87. scsi_abort_command(). If the abort is successful the command
  88. will either be retried (if the number of retries is not exhausted)
  89. or terminated with DID_TIME_OUT.
  90. Otherwise scsi_eh_scmd_add() is invoked for the command.
  91. See [1-4] for more information.
  92. 1.4 How EH takes over
  93. ---------------------
  94. scmds enter EH via scsi_eh_scmd_add(), which does the following.
  95. 1. Links scmd->eh_entry to shost->eh_cmd_q
  96. 2. Sets SHOST_RECOVERY bit in shost->shost_state
  97. 3. Increments shost->host_failed
  98. 4. Wakes up SCSI EH thread if shost->host_busy == shost->host_failed
  99. As can be seen above, once any scmd is added to shost->eh_cmd_q,
  100. SHOST_RECOVERY shost_state bit is turned on. This prevents any new
  101. scmd to be issued from blk queue to the host; eventually, all scmds on
  102. the host either complete normally, fail and get added to eh_cmd_q, or
  103. time out and get added to shost->eh_cmd_q.
  104. If all scmds either complete or fail, the number of in-flight scmds
  105. becomes equal to the number of failed scmds - i.e. shost->host_busy ==
  106. shost->host_failed. This wakes up SCSI EH thread. So, once woken up,
  107. SCSI EH thread can expect that all in-flight commands have failed and
  108. are linked on shost->eh_cmd_q.
  109. Note that this does not mean lower layers are quiescent. If a LLDD
  110. completed a scmd with error status, the LLDD and lower layers are
  111. assumed to forget about the scmd at that point. However, if a scmd
  112. has timed out, unless hostt->eh_timed_out() made lower layers forget
  113. about the scmd, which currently no LLDD does, the command is still
  114. active as long as lower layers are concerned and completion could
  115. occur at any time. Of course, all such completions are ignored as the
  116. timer has already expired.
  117. We'll talk about how SCSI EH takes actions to abort - make LLDD
  118. forget about - timed out scmds later.
  119. 2. How SCSI EH works
  120. ====================
  121. LLDD's can implement SCSI EH actions in one of the following two
  122. ways.
  123. - Fine-grained EH callbacks
  124. LLDD can implement fine-grained EH callbacks and let SCSI
  125. midlayer drive error handling and call appropriate callbacks.
  126. This will be discussed further in [2-1].
  127. - eh_strategy_handler() callback
  128. This is one big callback which should perform whole error
  129. handling. As such, it should do all chores the SCSI midlayer
  130. performs during recovery. This will be discussed in [2-2].
  131. Once recovery is complete, SCSI EH resumes normal operation by
  132. calling scsi_restart_operations(), which
  133. 1. Checks if door locking is needed and locks door.
  134. 2. Clears SHOST_RECOVERY shost_state bit
  135. 3. Wakes up waiters on shost->host_wait. This occurs if someone
  136. calls scsi_block_when_processing_errors() on the host.
  137. (*QUESTION* why is it needed? All operations will be blocked
  138. anyway after it reaches blk queue.)
  139. 4. Kicks queues in all devices on the host in the asses
  140. 2.1 EH through fine-grained callbacks
  141. -------------------------------------
  142. 2.1.1 Overview
  143. ^^^^^^^^^^^^^^
  144. If eh_strategy_handler() is not present, SCSI midlayer takes charge
  145. of driving error handling. EH's goals are two - make LLDD, host and
  146. device forget about timed out scmds and make them ready for new
  147. commands. A scmd is said to be recovered if the scmd is forgotten by
  148. lower layers and lower layers are ready to process or fail the scmd
  149. again.
  150. To achieve these goals, EH performs recovery actions with increasing
  151. severity. Some actions are performed by issuing SCSI commands and
  152. others are performed by invoking one of the following fine-grained
  153. hostt EH callbacks. Callbacks may be omitted and omitted ones are
  154. considered to fail always.
  155. ::
  156. int (* eh_abort_handler)(struct scsi_cmnd *);
  157. int (* eh_device_reset_handler)(struct scsi_cmnd *);
  158. int (* eh_bus_reset_handler)(struct scsi_cmnd *);
  159. int (* eh_host_reset_handler)(struct scsi_cmnd *);
  160. Higher-severity actions are taken only when lower-severity actions
  161. cannot recover some of failed scmds. Also, note that failure of the
  162. highest-severity action means EH failure and results in offlining of
  163. all unrecovered devices.
  164. During recovery, the following rules are followed
  165. - Recovery actions are performed on failed scmds on the to do list,
  166. eh_work_q. If a recovery action succeeds for a scmd, recovered
  167. scmds are removed from eh_work_q.
  168. Note that single recovery action on a scmd can recover multiple
  169. scmds. e.g. resetting a device recovers all failed scmds on the
  170. device.
  171. - Higher severity actions are taken iff eh_work_q is not empty after
  172. lower severity actions are complete.
  173. - EH reuses failed scmds to issue commands for recovery. For
  174. timed-out scmds, SCSI EH ensures that LLDD forgets about a scmd
  175. before reusing it for EH commands.
  176. When a scmd is recovered, the scmd is moved from eh_work_q to EH
  177. local eh_done_q using scsi_eh_finish_cmd(). After all scmds are
  178. recovered (eh_work_q is empty), scsi_eh_flush_done_q() is invoked to
  179. either retry or error-finish (notify upper layer of failure) recovered
  180. scmds.
  181. scmds are retried iff its sdev is still online (not offlined during
  182. EH), REQ_FAILFAST is not set and ++scmd->retries is less than
  183. scmd->allowed.
  184. 2.1.2 Flow of scmds through EH
  185. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  186. 1. Error completion / time out
  187. :ACTION: scsi_eh_scmd_add() is invoked for scmd
  188. - add scmd to shost->eh_cmd_q
  189. - set SHOST_RECOVERY
  190. - shost->host_failed++
  191. :LOCKING: shost->host_lock
  192. 2. EH starts
  193. :ACTION: move all scmds to EH's local eh_work_q. shost->eh_cmd_q
  194. is cleared.
  195. :LOCKING: shost->host_lock (not strictly necessary, just for
  196. consistency)
  197. 3. scmd recovered
  198. :ACTION: scsi_eh_finish_cmd() is invoked to EH-finish scmd
  199. - move from local eh_work_q to local eh_done_q
  200. :LOCKING: none
  201. :CONCURRENCY: at most one thread per separate eh_work_q to
  202. keep queue manipulation lockless
  203. 4. EH completes
  204. :ACTION: scsi_eh_flush_done_q() retries scmds or notifies upper
  205. layer of failure. May be called concurrently but must have
  206. a no more than one thread per separate eh_work_q to
  207. manipulate the queue locklessly
  208. - scmd is removed from eh_done_q and scmd->eh_entry is cleared
  209. - if retry is necessary, scmd is requeued using
  210. scsi_queue_insert()
  211. - otherwise, scsi_finish_command() is invoked for scmd
  212. - zero shost->host_failed
  213. :LOCKING: queue or finish function performs appropriate locking
  214. 2.1.3 Flow of control
  215. ^^^^^^^^^^^^^^^^^^^^^^
  216. EH through fine-grained callbacks start from scsi_unjam_host().
  217. ``scsi_unjam_host``
  218. 1. Lock shost->host_lock, splice_init shost->eh_cmd_q into local
  219. eh_work_q and unlock host_lock. Note that shost->eh_cmd_q is
  220. cleared by this action.
  221. 2. Invoke scsi_eh_get_sense.
  222. ``scsi_eh_get_sense``
  223. This action is taken for each error-completed
  224. command without valid sense data. Most
  225. SCSI transports/LLDDs automatically acquire sense data on
  226. command failures (autosense). Autosense is recommended for
  227. performance reasons and as sense information could get out of
  228. sync between occurrence of CHECK CONDITION and this action.
  229. Note that if autosense is not supported, scmd->sense_buffer
  230. contains invalid sense data when error-completing the scmd
  231. with scsi_done(). scsi_decide_disposition() always returns
  232. FAILED in such cases thus invoking SCSI EH. When the scmd
  233. reaches here, sense data is acquired and
  234. scsi_decide_disposition() is called again.
  235. 1. Invoke scsi_request_sense() which issues REQUEST_SENSE
  236. command. If fails, no action. Note that taking no action
  237. causes higher-severity recovery to be taken for the scmd.
  238. 2. Invoke scsi_decide_disposition() on the scmd
  239. - SUCCESS
  240. scmd->retries is set to scmd->allowed preventing
  241. scsi_eh_flush_done_q() from retrying the scmd and
  242. scsi_eh_finish_cmd() is invoked.
  243. - NEEDS_RETRY
  244. scsi_eh_finish_cmd() invoked
  245. - otherwise
  246. No action.
  247. 4. If !list_empty(&eh_work_q), invoke scsi_eh_ready_devs()
  248. ``scsi_eh_ready_devs``
  249. This function takes four increasingly more severe measures to
  250. make failed sdevs ready for new commands.
  251. 1. Invoke scsi_eh_stu()
  252. ``scsi_eh_stu``
  253. For each sdev which has failed scmds with valid sense data
  254. of which scsi_check_sense()'s verdict is FAILED,
  255. START STOP UNIT command is issued w/ start=1. Note that
  256. as we explicitly choose error-completed scmds, it is known
  257. that lower layers have forgotten about the scmd and we can
  258. reuse it for STU.
  259. If STU succeeds and the sdev is either offline or ready,
  260. all failed scmds on the sdev are EH-finished with
  261. scsi_eh_finish_cmd().
  262. *NOTE* If hostt->eh_abort_handler() isn't implemented or
  263. failed, we may still have timed out scmds at this point
  264. and STU doesn't make lower layers forget about those
  265. scmds. Yet, this function EH-finish all scmds on the sdev
  266. if STU succeeds leaving lower layers in an inconsistent
  267. state. It seems that STU action should be taken only when
  268. a sdev has no timed out scmd.
  269. 2. If !list_empty(&eh_work_q), invoke scsi_eh_bus_device_reset().
  270. ``scsi_eh_bus_device_reset``
  271. This action is very similar to scsi_eh_stu() except that,
  272. instead of issuing STU, hostt->eh_device_reset_handler()
  273. is used. Also, as we're not issuing SCSI commands and
  274. resetting clears all scmds on the sdev, there is no need
  275. to choose error-completed scmds.
  276. 3. If !list_empty(&eh_work_q), invoke scsi_eh_bus_reset()
  277. ``scsi_eh_bus_reset``
  278. hostt->eh_bus_reset_handler() is invoked for each channel
  279. with failed scmds. If bus reset succeeds, all failed
  280. scmds on all ready or offline sdevs on the channel are
  281. EH-finished.
  282. 4. If !list_empty(&eh_work_q), invoke scsi_eh_host_reset()
  283. ``scsi_eh_host_reset``
  284. This is the last resort. hostt->eh_host_reset_handler()
  285. is invoked. If host reset succeeds, all failed scmds on
  286. all ready or offline sdevs on the host are EH-finished.
  287. 5. If !list_empty(&eh_work_q), invoke scsi_eh_offline_sdevs()
  288. ``scsi_eh_offline_sdevs``
  289. Take all sdevs which still have unrecovered scmds offline
  290. and EH-finish the scmds.
  291. 5. Invoke scsi_eh_flush_done_q().
  292. ``scsi_eh_flush_done_q``
  293. At this point all scmds are recovered (or given up) and
  294. put on eh_done_q by scsi_eh_finish_cmd(). This function
  295. flushes eh_done_q by either retrying or notifying upper
  296. layer of failure of the scmds.
  297. 2.2 EH through transportt->eh_strategy_handler()
  298. ------------------------------------------------
  299. transportt->eh_strategy_handler() is invoked in the place of
  300. scsi_unjam_host() and it is responsible for whole recovery process.
  301. On completion, the handler should have made lower layers forget about
  302. all failed scmds and either ready for new commands or offline. Also,
  303. it should perform SCSI EH maintenance chores to maintain integrity of
  304. SCSI midlayer. IOW, of the steps described in [2-1-2], all steps
  305. except for #1 must be implemented by eh_strategy_handler().
  306. 2.2.1 Pre transportt->eh_strategy_handler() SCSI midlayer conditions
  307. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  308. The following conditions are true on entry to the handler.
  309. - Each failed scmd's eh_flags field is set appropriately.
  310. - Each failed scmd is linked on scmd->eh_cmd_q by scmd->eh_entry.
  311. - SHOST_RECOVERY is set.
  312. - shost->host_failed == shost->host_busy
  313. 2.2.2 Post transportt->eh_strategy_handler() SCSI midlayer conditions
  314. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  315. The following conditions must be true on exit from the handler.
  316. - shost->host_failed is zero.
  317. - shost->eh_cmd_q is cleared.
  318. - Each scmd->eh_entry is cleared.
  319. - Either scsi_queue_insert() or scsi_finish_command() is called on
  320. each scmd. Note that the handler is free to use scmd->retries and
  321. ->allowed to limit the number of retries.
  322. 2.2.3 Things to consider
  323. ^^^^^^^^^^^^^^^^^^^^^^^^
  324. - Know that timed out scmds are still active on lower layers. Make
  325. lower layers forget about them before doing anything else with
  326. those scmds.
  327. - For consistency, when accessing/modifying shost data structure,
  328. grab shost->host_lock.
  329. - On completion, each failed sdev must have forgotten about all
  330. active scmds.
  331. - On completion, each failed sdev must be ready for new commands or
  332. offline.
  333. Tejun Heo
  334. htejun@gmail.com
  335. 11th September 2005