scsi_eh.txt 16 KB

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