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