scsi_eh.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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_complete_request() to delete the block layer timer and
  44. raise SCSI_SOFTIRQ
  45. SCSI_SOFTIRQ handler scsi_softirq calls scsi_decide_disposition() to
  46. determine what to do with the command. scsi_decide_disposition()
  47. looks at the scmd->result value and sense data to determine what to do
  48. 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, SCSI_EH_CANCEL_CMD) 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. - scsi_setup_cmd_retry()
  200. - move from local eh_work_q to local eh_done_q
  201. :LOCKING: none
  202. :CONCURRENCY: at most one thread per separate eh_work_q to
  203. keep queue manipulation lockless
  204. 4. EH completes
  205. :ACTION: scsi_eh_flush_done_q() retries scmds or notifies upper
  206. layer of failure. May be called concurrently but must have
  207. a no more than one thread per separate eh_work_q to
  208. manipulate the queue locklessly
  209. - scmd is removed from eh_done_q and scmd->eh_entry is cleared
  210. - if retry is necessary, scmd is requeued using
  211. scsi_queue_insert()
  212. - otherwise, scsi_finish_command() is invoked for scmd
  213. - zero shost->host_failed
  214. :LOCKING: queue or finish function performs appropriate locking
  215. 2.1.3 Flow of control
  216. ^^^^^^^^^^^^^^^^^^^^^^
  217. EH through fine-grained callbacks start from scsi_unjam_host().
  218. ``scsi_unjam_host``
  219. 1. Lock shost->host_lock, splice_init shost->eh_cmd_q into local
  220. eh_work_q and unlock host_lock. Note that shost->eh_cmd_q is
  221. cleared by this action.
  222. 2. Invoke scsi_eh_get_sense.
  223. ``scsi_eh_get_sense``
  224. This action is taken for each error-completed
  225. (!SCSI_EH_CANCEL_CMD) commands without valid sense data. Most
  226. SCSI transports/LLDDs automatically acquire sense data on
  227. command failures (autosense). Autosense is recommended for
  228. performance reasons and as sense information could get out of
  229. sync between occurrence of CHECK CONDITION and this action.
  230. Note that if autosense is not supported, scmd->sense_buffer
  231. contains invalid sense data when error-completing the scmd
  232. with scsi_done(). scsi_decide_disposition() always returns
  233. FAILED in such cases thus invoking SCSI EH. When the scmd
  234. reaches here, sense data is acquired and
  235. scsi_decide_disposition() is called again.
  236. 1. Invoke scsi_request_sense() which issues REQUEST_SENSE
  237. command. If fails, no action. Note that taking no action
  238. causes higher-severity recovery to be taken for the scmd.
  239. 2. Invoke scsi_decide_disposition() on the scmd
  240. - SUCCESS
  241. scmd->retries is set to scmd->allowed preventing
  242. scsi_eh_flush_done_q() from retrying the scmd and
  243. scsi_eh_finish_cmd() is invoked.
  244. - NEEDS_RETRY
  245. scsi_eh_finish_cmd() invoked
  246. - otherwise
  247. No action.
  248. 3. If !list_empty(&eh_work_q), invoke scsi_eh_abort_cmds().
  249. ``scsi_eh_abort_cmds``
  250. This action is taken for each timed out command when
  251. no_async_abort is enabled in the host template.
  252. hostt->eh_abort_handler() is invoked for each scmd. The
  253. handler returns SUCCESS if it has succeeded to make LLDD and
  254. all related hardware forget about the scmd.
  255. If a timedout scmd is successfully aborted and the sdev is
  256. either offline or ready, scsi_eh_finish_cmd() is invoked for
  257. the scmd. Otherwise, the scmd is left in eh_work_q for
  258. higher-severity actions.
  259. Note that both offline and ready status mean that the sdev is
  260. ready to process new scmds, where processing also implies
  261. immediate failing; thus, if a sdev is in one of the two
  262. states, no further recovery action is needed.
  263. Device readiness is tested using scsi_eh_tur() which issues
  264. TEST_UNIT_READY command. Note that the scmd must have been
  265. aborted successfully before reusing it for TEST_UNIT_READY.
  266. 4. If !list_empty(&eh_work_q), invoke scsi_eh_ready_devs()
  267. ``scsi_eh_ready_devs``
  268. This function takes four increasingly more severe measures to
  269. make failed sdevs ready for new commands.
  270. 1. Invoke scsi_eh_stu()
  271. ``scsi_eh_stu``
  272. For each sdev which has failed scmds with valid sense data
  273. of which scsi_check_sense()'s verdict is FAILED,
  274. START_STOP_UNIT command is issued w/ start=1. Note that
  275. as we explicitly choose error-completed scmds, it is known
  276. that lower layers have forgotten about the scmd and we can
  277. reuse it for STU.
  278. If STU succeeds and the sdev is either offline or ready,
  279. all failed scmds on the sdev are EH-finished with
  280. scsi_eh_finish_cmd().
  281. *NOTE* If hostt->eh_abort_handler() isn't implemented or
  282. failed, we may still have timed out scmds at this point
  283. and STU doesn't make lower layers forget about those
  284. scmds. Yet, this function EH-finish all scmds on the sdev
  285. if STU succeeds leaving lower layers in an inconsistent
  286. state. It seems that STU action should be taken only when
  287. a sdev has no timed out scmd.
  288. 2. If !list_empty(&eh_work_q), invoke scsi_eh_bus_device_reset().
  289. ``scsi_eh_bus_device_reset``
  290. This action is very similar to scsi_eh_stu() except that,
  291. instead of issuing STU, hostt->eh_device_reset_handler()
  292. is used. Also, as we're not issuing SCSI commands and
  293. resetting clears all scmds on the sdev, there is no need
  294. to choose error-completed scmds.
  295. 3. If !list_empty(&eh_work_q), invoke scsi_eh_bus_reset()
  296. ``scsi_eh_bus_reset``
  297. hostt->eh_bus_reset_handler() is invoked for each channel
  298. with failed scmds. If bus reset succeeds, all failed
  299. scmds on all ready or offline sdevs on the channel are
  300. EH-finished.
  301. 4. If !list_empty(&eh_work_q), invoke scsi_eh_host_reset()
  302. ``scsi_eh_host_reset``
  303. This is the last resort. hostt->eh_host_reset_handler()
  304. is invoked. If host reset succeeds, all failed scmds on
  305. all ready or offline sdevs on the host are EH-finished.
  306. 5. If !list_empty(&eh_work_q), invoke scsi_eh_offline_sdevs()
  307. ``scsi_eh_offline_sdevs``
  308. Take all sdevs which still have unrecovered scmds offline
  309. and EH-finish the scmds.
  310. 5. Invoke scsi_eh_flush_done_q().
  311. ``scsi_eh_flush_done_q``
  312. At this point all scmds are recovered (or given up) and
  313. put on eh_done_q by scsi_eh_finish_cmd(). This function
  314. flushes eh_done_q by either retrying or notifying upper
  315. layer of failure of the scmds.
  316. 2.2 EH through transportt->eh_strategy_handler()
  317. ------------------------------------------------
  318. transportt->eh_strategy_handler() is invoked in the place of
  319. scsi_unjam_host() and it is responsible for whole recovery process.
  320. On completion, the handler should have made lower layers forget about
  321. all failed scmds and either ready for new commands or offline. Also,
  322. it should perform SCSI EH maintenance chores to maintain integrity of
  323. SCSI midlayer. IOW, of the steps described in [2-1-2], all steps
  324. except for #1 must be implemented by eh_strategy_handler().
  325. 2.2.1 Pre transportt->eh_strategy_handler() SCSI midlayer conditions
  326. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  327. The following conditions are true on entry to the handler.
  328. - Each failed scmd's eh_flags field is set appropriately.
  329. - Each failed scmd is linked on scmd->eh_cmd_q by scmd->eh_entry.
  330. - SHOST_RECOVERY is set.
  331. - shost->host_failed == shost->host_busy
  332. 2.2.2 Post transportt->eh_strategy_handler() SCSI midlayer conditions
  333. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  334. The following conditions must be true on exit from the handler.
  335. - shost->host_failed is zero.
  336. - Each scmd is in such a state that scsi_setup_cmd_retry() on the
  337. scmd doesn't make any difference.
  338. - shost->eh_cmd_q is cleared.
  339. - Each scmd->eh_entry is cleared.
  340. - Either scsi_queue_insert() or scsi_finish_command() is called on
  341. each scmd. Note that the handler is free to use scmd->retries and
  342. ->allowed to limit the number of retries.
  343. 2.2.3 Things to consider
  344. ^^^^^^^^^^^^^^^^^^^^^^^^
  345. - Know that timed out scmds are still active on lower layers. Make
  346. lower layers forget about them before doing anything else with
  347. those scmds.
  348. - For consistency, when accessing/modifying shost data structure,
  349. grab shost->host_lock.
  350. - On completion, each failed sdev must have forgotten about all
  351. active scmds.
  352. - On completion, each failed sdev must be ready for new commands or
  353. offline.
  354. Tejun Heo
  355. [email protected]
  356. 11th September 2005