1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * scsi_error.c Copyright (C) 1997 Eric Youngdale 4 * 5 * SCSI error/timeout handling 6 * Initial versions: Eric Youngdale. Based upon conversations with 7 * Leonard Zubkoff and David Miller at Linux Expo, 8 * ideas originating from all over the place. 9 * 10 * Restructured scsi_unjam_host and associated functions. 11 * September 04, 2002 Mike Anderson (andmike@us.ibm.com) 12 * 13 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and 14 * minor cleanups. 15 * September 30, 2002 Mike Anderson (andmike@us.ibm.com) 16 */ 17 18 #include <linux/module.h> 19 #include <linux/sched.h> 20 #include <linux/gfp.h> 21 #include <linux/timer.h> 22 #include <linux/string.h> 23 #include <linux/kernel.h> 24 #include <linux/freezer.h> 25 #include <linux/kthread.h> 26 #include <linux/interrupt.h> 27 #include <linux/blkdev.h> 28 #include <linux/delay.h> 29 #include <linux/jiffies.h> 30 31 #include <scsi/scsi.h> 32 #include <scsi/scsi_cmnd.h> 33 #include <scsi/scsi_dbg.h> 34 #include <scsi/scsi_device.h> 35 #include <scsi/scsi_driver.h> 36 #include <scsi/scsi_eh.h> 37 #include <scsi/scsi_common.h> 38 #include <scsi/scsi_transport.h> 39 #include <scsi/scsi_host.h> 40 #include <scsi/scsi_ioctl.h> 41 #include <scsi/scsi_dh.h> 42 #include <scsi/scsi_devinfo.h> 43 #include <scsi/sg.h> 44 45 #include "scsi_priv.h" 46 #include "scsi_logging.h" 47 #include "scsi_transport_api.h" 48 49 #include <trace/events/scsi.h> 50 51 #include <asm/unaligned.h> 52 53 /* 54 * These should *probably* be handled by the host itself. 55 * Since it is allowed to sleep, it probably should. 56 */ 57 #define BUS_RESET_SETTLE_TIME (10) 58 #define HOST_RESET_SETTLE_TIME (10) 59 60 static int scsi_eh_try_stu(struct scsi_cmnd *scmd); 61 static enum scsi_disposition scsi_try_to_abort_cmd(struct scsi_host_template *, 62 struct scsi_cmnd *); 63 64 void scsi_eh_wakeup(struct Scsi_Host *shost) 65 { 66 lockdep_assert_held(shost->host_lock); 67 68 if (scsi_host_busy(shost) == shost->host_failed) { 69 trace_scsi_eh_wakeup(shost); 70 wake_up_process(shost->ehandler); 71 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost, 72 "Waking error handler thread\n")); 73 } 74 } 75 76 /** 77 * scsi_schedule_eh - schedule EH for SCSI host 78 * @shost: SCSI host to invoke error handling on. 79 * 80 * Schedule SCSI EH without scmd. 81 */ 82 void scsi_schedule_eh(struct Scsi_Host *shost) 83 { 84 unsigned long flags; 85 86 spin_lock_irqsave(shost->host_lock, flags); 87 88 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 || 89 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) { 90 shost->host_eh_scheduled++; 91 scsi_eh_wakeup(shost); 92 } 93 94 spin_unlock_irqrestore(shost->host_lock, flags); 95 } 96 EXPORT_SYMBOL_GPL(scsi_schedule_eh); 97 98 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost) 99 { 100 if (!shost->last_reset || shost->eh_deadline == -1) 101 return 0; 102 103 /* 104 * 32bit accesses are guaranteed to be atomic 105 * (on all supported architectures), so instead 106 * of using a spinlock we can as well double check 107 * if eh_deadline has been set to 'off' during the 108 * time_before call. 109 */ 110 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) && 111 shost->eh_deadline > -1) 112 return 0; 113 114 return 1; 115 } 116 117 static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd) 118 { 119 if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT) 120 return true; 121 122 return ++cmd->retries <= cmd->allowed; 123 } 124 125 static bool scsi_eh_should_retry_cmd(struct scsi_cmnd *cmd) 126 { 127 struct scsi_device *sdev = cmd->device; 128 struct Scsi_Host *host = sdev->host; 129 130 if (host->hostt->eh_should_retry_cmd) 131 return host->hostt->eh_should_retry_cmd(cmd); 132 133 return true; 134 } 135 136 /** 137 * scmd_eh_abort_handler - Handle command aborts 138 * @work: command to be aborted. 139 * 140 * Note: this function must be called only for a command that has timed out. 141 * Because the block layer marks a request as complete before it calls 142 * scsi_timeout(), a .scsi_done() call from the LLD for a command that has 143 * timed out do not have any effect. Hence it is safe to call 144 * scsi_finish_command() from this function. 145 */ 146 void 147 scmd_eh_abort_handler(struct work_struct *work) 148 { 149 struct scsi_cmnd *scmd = 150 container_of(work, struct scsi_cmnd, abort_work.work); 151 struct scsi_device *sdev = scmd->device; 152 struct Scsi_Host *shost = sdev->host; 153 enum scsi_disposition rtn; 154 unsigned long flags; 155 156 if (scsi_host_eh_past_deadline(shost)) { 157 SCSI_LOG_ERROR_RECOVERY(3, 158 scmd_printk(KERN_INFO, scmd, 159 "eh timeout, not aborting\n")); 160 goto out; 161 } 162 163 SCSI_LOG_ERROR_RECOVERY(3, 164 scmd_printk(KERN_INFO, scmd, 165 "aborting command\n")); 166 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd); 167 if (rtn != SUCCESS) { 168 SCSI_LOG_ERROR_RECOVERY(3, 169 scmd_printk(KERN_INFO, scmd, 170 "cmd abort %s\n", 171 (rtn == FAST_IO_FAIL) ? 172 "not send" : "failed")); 173 goto out; 174 } 175 set_host_byte(scmd, DID_TIME_OUT); 176 if (scsi_host_eh_past_deadline(shost)) { 177 SCSI_LOG_ERROR_RECOVERY(3, 178 scmd_printk(KERN_INFO, scmd, 179 "eh timeout, not retrying " 180 "aborted command\n")); 181 goto out; 182 } 183 184 spin_lock_irqsave(shost->host_lock, flags); 185 list_del_init(&scmd->eh_entry); 186 187 /* 188 * If the abort succeeds, and there is no further 189 * EH action, clear the ->last_reset time. 190 */ 191 if (list_empty(&shost->eh_abort_list) && 192 list_empty(&shost->eh_cmd_q)) 193 if (shost->eh_deadline != -1) 194 shost->last_reset = 0; 195 196 spin_unlock_irqrestore(shost->host_lock, flags); 197 198 if (!scsi_noretry_cmd(scmd) && 199 scsi_cmd_retry_allowed(scmd) && 200 scsi_eh_should_retry_cmd(scmd)) { 201 SCSI_LOG_ERROR_RECOVERY(3, 202 scmd_printk(KERN_WARNING, scmd, 203 "retry aborted command\n")); 204 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY); 205 } else { 206 SCSI_LOG_ERROR_RECOVERY(3, 207 scmd_printk(KERN_WARNING, scmd, 208 "finish aborted command\n")); 209 scsi_finish_command(scmd); 210 } 211 return; 212 213 out: 214 spin_lock_irqsave(shost->host_lock, flags); 215 list_del_init(&scmd->eh_entry); 216 spin_unlock_irqrestore(shost->host_lock, flags); 217 218 scsi_eh_scmd_add(scmd); 219 } 220 221 /** 222 * scsi_abort_command - schedule a command abort 223 * @scmd: scmd to abort. 224 * 225 * We only need to abort commands after a command timeout 226 */ 227 static int 228 scsi_abort_command(struct scsi_cmnd *scmd) 229 { 230 struct scsi_device *sdev = scmd->device; 231 struct Scsi_Host *shost = sdev->host; 232 unsigned long flags; 233 234 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) { 235 /* 236 * Retry after abort failed, escalate to next level. 237 */ 238 SCSI_LOG_ERROR_RECOVERY(3, 239 scmd_printk(KERN_INFO, scmd, 240 "previous abort failed\n")); 241 BUG_ON(delayed_work_pending(&scmd->abort_work)); 242 return FAILED; 243 } 244 245 spin_lock_irqsave(shost->host_lock, flags); 246 if (shost->eh_deadline != -1 && !shost->last_reset) 247 shost->last_reset = jiffies; 248 BUG_ON(!list_empty(&scmd->eh_entry)); 249 list_add_tail(&scmd->eh_entry, &shost->eh_abort_list); 250 spin_unlock_irqrestore(shost->host_lock, flags); 251 252 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED; 253 SCSI_LOG_ERROR_RECOVERY(3, 254 scmd_printk(KERN_INFO, scmd, "abort scheduled\n")); 255 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100); 256 return SUCCESS; 257 } 258 259 /** 260 * scsi_eh_reset - call into ->eh_action to reset internal counters 261 * @scmd: scmd to run eh on. 262 * 263 * The scsi driver might be carrying internal state about the 264 * devices, so we need to call into the driver to reset the 265 * internal state once the error handler is started. 266 */ 267 static void scsi_eh_reset(struct scsi_cmnd *scmd) 268 { 269 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) { 270 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd); 271 if (sdrv->eh_reset) 272 sdrv->eh_reset(scmd); 273 } 274 } 275 276 static void scsi_eh_inc_host_failed(struct rcu_head *head) 277 { 278 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu); 279 struct Scsi_Host *shost = scmd->device->host; 280 unsigned long flags; 281 282 spin_lock_irqsave(shost->host_lock, flags); 283 shost->host_failed++; 284 scsi_eh_wakeup(shost); 285 spin_unlock_irqrestore(shost->host_lock, flags); 286 } 287 288 /** 289 * scsi_eh_scmd_add - add scsi cmd to error handling. 290 * @scmd: scmd to run eh on. 291 */ 292 void scsi_eh_scmd_add(struct scsi_cmnd *scmd) 293 { 294 struct Scsi_Host *shost = scmd->device->host; 295 unsigned long flags; 296 int ret; 297 298 WARN_ON_ONCE(!shost->ehandler); 299 300 spin_lock_irqsave(shost->host_lock, flags); 301 if (scsi_host_set_state(shost, SHOST_RECOVERY)) { 302 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY); 303 WARN_ON_ONCE(ret); 304 } 305 if (shost->eh_deadline != -1 && !shost->last_reset) 306 shost->last_reset = jiffies; 307 308 scsi_eh_reset(scmd); 309 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q); 310 spin_unlock_irqrestore(shost->host_lock, flags); 311 /* 312 * Ensure that all tasks observe the host state change before the 313 * host_failed change. 314 */ 315 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed); 316 } 317 318 /** 319 * scsi_timeout - Timeout function for normal scsi commands. 320 * @req: request that is timing out. 321 * 322 * Notes: 323 * We do not need to lock this. There is the potential for a race 324 * only in that the normal completion handling might run, but if the 325 * normal completion function determines that the timer has already 326 * fired, then it mustn't do anything. 327 */ 328 enum blk_eh_timer_return scsi_timeout(struct request *req) 329 { 330 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req); 331 enum blk_eh_timer_return rtn = BLK_EH_DONE; 332 struct Scsi_Host *host = scmd->device->host; 333 334 trace_scsi_dispatch_cmd_timeout(scmd); 335 scsi_log_completion(scmd, TIMEOUT_ERROR); 336 337 if (host->eh_deadline != -1 && !host->last_reset) 338 host->last_reset = jiffies; 339 340 if (host->hostt->eh_timed_out) 341 rtn = host->hostt->eh_timed_out(scmd); 342 343 if (rtn == BLK_EH_DONE) { 344 /* 345 * Set the command to complete first in order to prevent a real 346 * completion from releasing the command while error handling 347 * is using it. If the command was already completed, then the 348 * lower level driver beat the timeout handler, and it is safe 349 * to return without escalating error recovery. 350 * 351 * If timeout handling lost the race to a real completion, the 352 * block layer may ignore that due to a fake timeout injection, 353 * so return RESET_TIMER to allow error handling another shot 354 * at this command. 355 */ 356 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state)) 357 return BLK_EH_RESET_TIMER; 358 if (scsi_abort_command(scmd) != SUCCESS) { 359 set_host_byte(scmd, DID_TIME_OUT); 360 scsi_eh_scmd_add(scmd); 361 } 362 } 363 364 return rtn; 365 } 366 367 /** 368 * scsi_block_when_processing_errors - Prevent cmds from being queued. 369 * @sdev: Device on which we are performing recovery. 370 * 371 * Description: 372 * We block until the host is out of error recovery, and then check to 373 * see whether the host or the device is offline. 374 * 375 * Return value: 376 * 0 when dev was taken offline by error recovery. 1 OK to proceed. 377 */ 378 int scsi_block_when_processing_errors(struct scsi_device *sdev) 379 { 380 int online; 381 382 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host)); 383 384 online = scsi_device_online(sdev); 385 386 return online; 387 } 388 EXPORT_SYMBOL(scsi_block_when_processing_errors); 389 390 #ifdef CONFIG_SCSI_LOGGING 391 /** 392 * scsi_eh_prt_fail_stats - Log info on failures. 393 * @shost: scsi host being recovered. 394 * @work_q: Queue of scsi cmds to process. 395 */ 396 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost, 397 struct list_head *work_q) 398 { 399 struct scsi_cmnd *scmd; 400 struct scsi_device *sdev; 401 int total_failures = 0; 402 int cmd_failed = 0; 403 int cmd_cancel = 0; 404 int devices_failed = 0; 405 406 shost_for_each_device(sdev, shost) { 407 list_for_each_entry(scmd, work_q, eh_entry) { 408 if (scmd->device == sdev) { 409 ++total_failures; 410 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) 411 ++cmd_cancel; 412 else 413 ++cmd_failed; 414 } 415 } 416 417 if (cmd_cancel || cmd_failed) { 418 SCSI_LOG_ERROR_RECOVERY(3, 419 shost_printk(KERN_INFO, shost, 420 "%s: cmds failed: %d, cancel: %d\n", 421 __func__, cmd_failed, 422 cmd_cancel)); 423 cmd_cancel = 0; 424 cmd_failed = 0; 425 ++devices_failed; 426 } 427 } 428 429 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost, 430 "Total of %d commands on %d" 431 " devices require eh work\n", 432 total_failures, devices_failed)); 433 } 434 #endif 435 436 /** 437 * scsi_report_lun_change - Set flag on all *other* devices on the same target 438 * to indicate that a UNIT ATTENTION is expected. 439 * @sdev: Device reporting the UNIT ATTENTION 440 */ 441 static void scsi_report_lun_change(struct scsi_device *sdev) 442 { 443 sdev->sdev_target->expecting_lun_change = 1; 444 } 445 446 /** 447 * scsi_report_sense - Examine scsi sense information and log messages for 448 * certain conditions, also issue uevents for some of them. 449 * @sdev: Device reporting the sense code 450 * @sshdr: sshdr to be examined 451 */ 452 static void scsi_report_sense(struct scsi_device *sdev, 453 struct scsi_sense_hdr *sshdr) 454 { 455 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */ 456 457 if (sshdr->sense_key == UNIT_ATTENTION) { 458 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) { 459 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED; 460 sdev_printk(KERN_WARNING, sdev, 461 "Inquiry data has changed"); 462 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) { 463 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED; 464 scsi_report_lun_change(sdev); 465 sdev_printk(KERN_WARNING, sdev, 466 "LUN assignments on this target have " 467 "changed. The Linux SCSI layer does not " 468 "automatically remap LUN assignments.\n"); 469 } else if (sshdr->asc == 0x3f) 470 sdev_printk(KERN_WARNING, sdev, 471 "Operating parameters on this target have " 472 "changed. The Linux SCSI layer does not " 473 "automatically adjust these parameters.\n"); 474 475 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) { 476 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED; 477 sdev_printk(KERN_WARNING, sdev, 478 "Warning! Received an indication that the " 479 "LUN reached a thin provisioning soft " 480 "threshold.\n"); 481 } 482 483 if (sshdr->asc == 0x29) { 484 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED; 485 /* 486 * Do not print message if it is an expected side-effect 487 * of runtime PM. 488 */ 489 if (!sdev->silence_suspend) 490 sdev_printk(KERN_WARNING, sdev, 491 "Power-on or device reset occurred\n"); 492 } 493 494 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) { 495 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED; 496 sdev_printk(KERN_WARNING, sdev, 497 "Mode parameters changed"); 498 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) { 499 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED; 500 sdev_printk(KERN_WARNING, sdev, 501 "Asymmetric access state changed"); 502 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) { 503 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED; 504 sdev_printk(KERN_WARNING, sdev, 505 "Capacity data has changed"); 506 } else if (sshdr->asc == 0x2a) 507 sdev_printk(KERN_WARNING, sdev, 508 "Parameters changed"); 509 } 510 511 if (evt_type != SDEV_EVT_MAXBITS) { 512 set_bit(evt_type, sdev->pending_events); 513 schedule_work(&sdev->event_work); 514 } 515 } 516 517 static inline void set_scsi_ml_byte(struct scsi_cmnd *cmd, u8 status) 518 { 519 cmd->result = (cmd->result & 0xffff00ff) | (status << 8); 520 } 521 522 /** 523 * scsi_check_sense - Examine scsi cmd sense 524 * @scmd: Cmd to have sense checked. 525 * 526 * Return value: 527 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE 528 * 529 * Notes: 530 * When a deferred error is detected the current command has 531 * not been executed and needs retrying. 532 */ 533 enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd) 534 { 535 struct scsi_device *sdev = scmd->device; 536 struct scsi_sense_hdr sshdr; 537 538 if (! scsi_command_normalize_sense(scmd, &sshdr)) 539 return FAILED; /* no valid sense data */ 540 541 scsi_report_sense(sdev, &sshdr); 542 543 if (scsi_sense_is_deferred(&sshdr)) 544 return NEEDS_RETRY; 545 546 if (sdev->handler && sdev->handler->check_sense) { 547 enum scsi_disposition rc; 548 549 rc = sdev->handler->check_sense(sdev, &sshdr); 550 if (rc != SCSI_RETURN_NOT_HANDLED) 551 return rc; 552 /* handler does not care. Drop down to default handling */ 553 } 554 555 if (scmd->cmnd[0] == TEST_UNIT_READY && 556 scmd->submitter != SUBMITTED_BY_SCSI_ERROR_HANDLER) 557 /* 558 * nasty: for mid-layer issued TURs, we need to return the 559 * actual sense data without any recovery attempt. For eh 560 * issued ones, we need to try to recover and interpret 561 */ 562 return SUCCESS; 563 564 /* 565 * Previous logic looked for FILEMARK, EOM or ILI which are 566 * mainly associated with tapes and returned SUCCESS. 567 */ 568 if (sshdr.response_code == 0x70) { 569 /* fixed format */ 570 if (scmd->sense_buffer[2] & 0xe0) 571 return SUCCESS; 572 } else { 573 /* 574 * descriptor format: look for "stream commands sense data 575 * descriptor" (see SSC-3). Assume single sense data 576 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG. 577 */ 578 if ((sshdr.additional_length > 3) && 579 (scmd->sense_buffer[8] == 0x4) && 580 (scmd->sense_buffer[11] & 0xe0)) 581 return SUCCESS; 582 } 583 584 switch (sshdr.sense_key) { 585 case NO_SENSE: 586 return SUCCESS; 587 case RECOVERED_ERROR: 588 return /* soft_error */ SUCCESS; 589 590 case ABORTED_COMMAND: 591 if (sshdr.asc == 0x10) /* DIF */ 592 return SUCCESS; 593 594 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF) 595 return ADD_TO_MLQUEUE; 596 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 && 597 sdev->sdev_bflags & BLIST_RETRY_ASC_C1) 598 return ADD_TO_MLQUEUE; 599 600 return NEEDS_RETRY; 601 case NOT_READY: 602 case UNIT_ATTENTION: 603 /* 604 * if we are expecting a cc/ua because of a bus reset that we 605 * performed, treat this just as a retry. otherwise this is 606 * information that we should pass up to the upper-level driver 607 * so that we can deal with it there. 608 */ 609 if (scmd->device->expecting_cc_ua) { 610 /* 611 * Because some device does not queue unit 612 * attentions correctly, we carefully check 613 * additional sense code and qualifier so as 614 * not to squash media change unit attention. 615 */ 616 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) { 617 scmd->device->expecting_cc_ua = 0; 618 return NEEDS_RETRY; 619 } 620 } 621 /* 622 * we might also expect a cc/ua if another LUN on the target 623 * reported a UA with an ASC/ASCQ of 3F 0E - 624 * REPORTED LUNS DATA HAS CHANGED. 625 */ 626 if (scmd->device->sdev_target->expecting_lun_change && 627 sshdr.asc == 0x3f && sshdr.ascq == 0x0e) 628 return NEEDS_RETRY; 629 /* 630 * if the device is in the process of becoming ready, we 631 * should retry. 632 */ 633 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01)) 634 return NEEDS_RETRY; 635 /* 636 * if the device is not started, we need to wake 637 * the error handler to start the motor 638 */ 639 if (scmd->device->allow_restart && 640 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02)) 641 return FAILED; 642 /* 643 * Pass the UA upwards for a determination in the completion 644 * functions. 645 */ 646 return SUCCESS; 647 648 /* these are not supported */ 649 case DATA_PROTECT: 650 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) { 651 /* Thin provisioning hard threshold reached */ 652 set_scsi_ml_byte(scmd, SCSIML_STAT_NOSPC); 653 return SUCCESS; 654 } 655 fallthrough; 656 case COPY_ABORTED: 657 case VOLUME_OVERFLOW: 658 case MISCOMPARE: 659 case BLANK_CHECK: 660 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE); 661 return SUCCESS; 662 663 case MEDIUM_ERROR: 664 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */ 665 sshdr.asc == 0x13 || /* AMNF DATA FIELD */ 666 sshdr.asc == 0x14) { /* RECORD NOT FOUND */ 667 set_scsi_ml_byte(scmd, SCSIML_STAT_MED_ERROR); 668 return SUCCESS; 669 } 670 return NEEDS_RETRY; 671 672 case HARDWARE_ERROR: 673 if (scmd->device->retry_hwerror) 674 return ADD_TO_MLQUEUE; 675 else 676 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE); 677 fallthrough; 678 679 case ILLEGAL_REQUEST: 680 if (sshdr.asc == 0x20 || /* Invalid command operation code */ 681 sshdr.asc == 0x21 || /* Logical block address out of range */ 682 sshdr.asc == 0x22 || /* Invalid function */ 683 sshdr.asc == 0x24 || /* Invalid field in cdb */ 684 sshdr.asc == 0x26 || /* Parameter value invalid */ 685 sshdr.asc == 0x27) { /* Write protected */ 686 set_scsi_ml_byte(scmd, SCSIML_STAT_TGT_FAILURE); 687 } 688 return SUCCESS; 689 690 default: 691 return SUCCESS; 692 } 693 } 694 EXPORT_SYMBOL_GPL(scsi_check_sense); 695 696 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev) 697 { 698 struct scsi_host_template *sht = sdev->host->hostt; 699 struct scsi_device *tmp_sdev; 700 701 if (!sht->track_queue_depth || 702 sdev->queue_depth >= sdev->max_queue_depth) 703 return; 704 705 if (time_before(jiffies, 706 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period)) 707 return; 708 709 if (time_before(jiffies, 710 sdev->last_queue_full_time + sdev->queue_ramp_up_period)) 711 return; 712 713 /* 714 * Walk all devices of a target and do 715 * ramp up on them. 716 */ 717 shost_for_each_device(tmp_sdev, sdev->host) { 718 if (tmp_sdev->channel != sdev->channel || 719 tmp_sdev->id != sdev->id || 720 tmp_sdev->queue_depth == sdev->max_queue_depth) 721 continue; 722 723 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1); 724 sdev->last_queue_ramp_up = jiffies; 725 } 726 } 727 728 static void scsi_handle_queue_full(struct scsi_device *sdev) 729 { 730 struct scsi_host_template *sht = sdev->host->hostt; 731 struct scsi_device *tmp_sdev; 732 733 if (!sht->track_queue_depth) 734 return; 735 736 shost_for_each_device(tmp_sdev, sdev->host) { 737 if (tmp_sdev->channel != sdev->channel || 738 tmp_sdev->id != sdev->id) 739 continue; 740 /* 741 * We do not know the number of commands that were at 742 * the device when we got the queue full so we start 743 * from the highest possible value and work our way down. 744 */ 745 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1); 746 } 747 } 748 749 /** 750 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD. 751 * @scmd: SCSI cmd to examine. 752 * 753 * Notes: 754 * This is *only* called when we are examining the status of commands 755 * queued during error recovery. the main difference here is that we 756 * don't allow for the possibility of retries here, and we are a lot 757 * more restrictive about what we consider acceptable. 758 */ 759 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd) 760 { 761 /* 762 * first check the host byte, to see if there is anything in there 763 * that would indicate what we need to do. 764 */ 765 if (host_byte(scmd->result) == DID_RESET) { 766 /* 767 * rats. we are already in the error handler, so we now 768 * get to try and figure out what to do next. if the sense 769 * is valid, we have a pretty good idea of what to do. 770 * if not, we mark it as FAILED. 771 */ 772 return scsi_check_sense(scmd); 773 } 774 if (host_byte(scmd->result) != DID_OK) 775 return FAILED; 776 777 /* 778 * now, check the status byte to see if this indicates 779 * anything special. 780 */ 781 switch (get_status_byte(scmd)) { 782 case SAM_STAT_GOOD: 783 scsi_handle_queue_ramp_up(scmd->device); 784 fallthrough; 785 case SAM_STAT_COMMAND_TERMINATED: 786 return SUCCESS; 787 case SAM_STAT_CHECK_CONDITION: 788 return scsi_check_sense(scmd); 789 case SAM_STAT_CONDITION_MET: 790 case SAM_STAT_INTERMEDIATE: 791 case SAM_STAT_INTERMEDIATE_CONDITION_MET: 792 /* 793 * who knows? FIXME(eric) 794 */ 795 return SUCCESS; 796 case SAM_STAT_RESERVATION_CONFLICT: 797 if (scmd->cmnd[0] == TEST_UNIT_READY) 798 /* it is a success, we probed the device and 799 * found it */ 800 return SUCCESS; 801 /* otherwise, we failed to send the command */ 802 return FAILED; 803 case SAM_STAT_TASK_SET_FULL: 804 scsi_handle_queue_full(scmd->device); 805 fallthrough; 806 case SAM_STAT_BUSY: 807 return NEEDS_RETRY; 808 default: 809 return FAILED; 810 } 811 return FAILED; 812 } 813 814 /** 815 * scsi_eh_done - Completion function for error handling. 816 * @scmd: Cmd that is done. 817 */ 818 void scsi_eh_done(struct scsi_cmnd *scmd) 819 { 820 struct completion *eh_action; 821 822 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, 823 "%s result: %x\n", __func__, scmd->result)); 824 825 eh_action = scmd->device->host->eh_action; 826 if (eh_action) 827 complete(eh_action); 828 } 829 830 /** 831 * scsi_try_host_reset - ask host adapter to reset itself 832 * @scmd: SCSI cmd to send host reset. 833 */ 834 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd) 835 { 836 unsigned long flags; 837 enum scsi_disposition rtn; 838 struct Scsi_Host *host = scmd->device->host; 839 struct scsi_host_template *hostt = host->hostt; 840 841 SCSI_LOG_ERROR_RECOVERY(3, 842 shost_printk(KERN_INFO, host, "Snd Host RST\n")); 843 844 if (!hostt->eh_host_reset_handler) 845 return FAILED; 846 847 rtn = hostt->eh_host_reset_handler(scmd); 848 849 if (rtn == SUCCESS) { 850 if (!hostt->skip_settle_delay) 851 ssleep(HOST_RESET_SETTLE_TIME); 852 spin_lock_irqsave(host->host_lock, flags); 853 scsi_report_bus_reset(host, scmd_channel(scmd)); 854 spin_unlock_irqrestore(host->host_lock, flags); 855 } 856 857 return rtn; 858 } 859 860 /** 861 * scsi_try_bus_reset - ask host to perform a bus reset 862 * @scmd: SCSI cmd to send bus reset. 863 */ 864 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd) 865 { 866 unsigned long flags; 867 enum scsi_disposition rtn; 868 struct Scsi_Host *host = scmd->device->host; 869 struct scsi_host_template *hostt = host->hostt; 870 871 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, 872 "%s: Snd Bus RST\n", __func__)); 873 874 if (!hostt->eh_bus_reset_handler) 875 return FAILED; 876 877 rtn = hostt->eh_bus_reset_handler(scmd); 878 879 if (rtn == SUCCESS) { 880 if (!hostt->skip_settle_delay) 881 ssleep(BUS_RESET_SETTLE_TIME); 882 spin_lock_irqsave(host->host_lock, flags); 883 scsi_report_bus_reset(host, scmd_channel(scmd)); 884 spin_unlock_irqrestore(host->host_lock, flags); 885 } 886 887 return rtn; 888 } 889 890 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data) 891 { 892 sdev->was_reset = 1; 893 sdev->expecting_cc_ua = 1; 894 } 895 896 /** 897 * scsi_try_target_reset - Ask host to perform a target reset 898 * @scmd: SCSI cmd used to send a target reset 899 * 900 * Notes: 901 * There is no timeout for this operation. if this operation is 902 * unreliable for a given host, then the host itself needs to put a 903 * timer on it, and set the host back to a consistent state prior to 904 * returning. 905 */ 906 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd) 907 { 908 unsigned long flags; 909 enum scsi_disposition rtn; 910 struct Scsi_Host *host = scmd->device->host; 911 struct scsi_host_template *hostt = host->hostt; 912 913 if (!hostt->eh_target_reset_handler) 914 return FAILED; 915 916 rtn = hostt->eh_target_reset_handler(scmd); 917 if (rtn == SUCCESS) { 918 spin_lock_irqsave(host->host_lock, flags); 919 __starget_for_each_device(scsi_target(scmd->device), NULL, 920 __scsi_report_device_reset); 921 spin_unlock_irqrestore(host->host_lock, flags); 922 } 923 924 return rtn; 925 } 926 927 /** 928 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev 929 * @scmd: SCSI cmd used to send BDR 930 * 931 * Notes: 932 * There is no timeout for this operation. if this operation is 933 * unreliable for a given host, then the host itself needs to put a 934 * timer on it, and set the host back to a consistent state prior to 935 * returning. 936 */ 937 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd) 938 { 939 enum scsi_disposition rtn; 940 struct scsi_host_template *hostt = scmd->device->host->hostt; 941 942 if (!hostt->eh_device_reset_handler) 943 return FAILED; 944 945 rtn = hostt->eh_device_reset_handler(scmd); 946 if (rtn == SUCCESS) 947 __scsi_report_device_reset(scmd->device, NULL); 948 return rtn; 949 } 950 951 /** 952 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command 953 * @hostt: SCSI driver host template 954 * @scmd: SCSI cmd used to send a target reset 955 * 956 * Return value: 957 * SUCCESS, FAILED, or FAST_IO_FAIL 958 * 959 * Notes: 960 * SUCCESS does not necessarily indicate that the command 961 * has been aborted; it only indicates that the LLDDs 962 * has cleared all references to that command. 963 * LLDDs should return FAILED only if an abort was required 964 * but could not be executed. LLDDs should return FAST_IO_FAIL 965 * if the device is temporarily unavailable (eg due to a 966 * link down on FibreChannel) 967 */ 968 static enum scsi_disposition 969 scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd) 970 { 971 if (!hostt->eh_abort_handler) 972 return FAILED; 973 974 return hostt->eh_abort_handler(scmd); 975 } 976 977 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd) 978 { 979 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS) 980 if (scsi_try_bus_device_reset(scmd) != SUCCESS) 981 if (scsi_try_target_reset(scmd) != SUCCESS) 982 if (scsi_try_bus_reset(scmd) != SUCCESS) 983 scsi_try_host_reset(scmd); 984 } 985 986 /** 987 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery 988 * @scmd: SCSI command structure to hijack 989 * @ses: structure to save restore information 990 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed 991 * @cmnd_size: size in bytes of @cmnd (must be <= MAX_COMMAND_SIZE) 992 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored) 993 * 994 * This function is used to save a scsi command information before re-execution 995 * as part of the error recovery process. If @sense_bytes is 0 the command 996 * sent must be one that does not transfer any data. If @sense_bytes != 0 997 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command 998 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer. 999 */ 1000 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses, 1001 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes) 1002 { 1003 struct scsi_device *sdev = scmd->device; 1004 1005 /* 1006 * We need saved copies of a number of fields - this is because 1007 * error handling may need to overwrite these with different values 1008 * to run different commands, and once error handling is complete, 1009 * we will need to restore these values prior to running the actual 1010 * command. 1011 */ 1012 ses->cmd_len = scmd->cmd_len; 1013 ses->data_direction = scmd->sc_data_direction; 1014 ses->sdb = scmd->sdb; 1015 ses->result = scmd->result; 1016 ses->resid_len = scmd->resid_len; 1017 ses->underflow = scmd->underflow; 1018 ses->prot_op = scmd->prot_op; 1019 ses->eh_eflags = scmd->eh_eflags; 1020 1021 scmd->prot_op = SCSI_PROT_NORMAL; 1022 scmd->eh_eflags = 0; 1023 memcpy(ses->cmnd, scmd->cmnd, sizeof(ses->cmnd)); 1024 memset(scmd->cmnd, 0, sizeof(scmd->cmnd)); 1025 memset(&scmd->sdb, 0, sizeof(scmd->sdb)); 1026 scmd->result = 0; 1027 scmd->resid_len = 0; 1028 1029 if (sense_bytes) { 1030 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE, 1031 sense_bytes); 1032 sg_init_one(&ses->sense_sgl, scmd->sense_buffer, 1033 scmd->sdb.length); 1034 scmd->sdb.table.sgl = &ses->sense_sgl; 1035 scmd->sc_data_direction = DMA_FROM_DEVICE; 1036 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1; 1037 scmd->cmnd[0] = REQUEST_SENSE; 1038 scmd->cmnd[4] = scmd->sdb.length; 1039 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]); 1040 } else { 1041 scmd->sc_data_direction = DMA_NONE; 1042 if (cmnd) { 1043 BUG_ON(cmnd_size > sizeof(scmd->cmnd)); 1044 memcpy(scmd->cmnd, cmnd, cmnd_size); 1045 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]); 1046 } 1047 } 1048 1049 scmd->underflow = 0; 1050 1051 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN) 1052 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) | 1053 (sdev->lun << 5 & 0xe0); 1054 1055 /* 1056 * Zero the sense buffer. The scsi spec mandates that any 1057 * untransferred sense data should be interpreted as being zero. 1058 */ 1059 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 1060 } 1061 EXPORT_SYMBOL(scsi_eh_prep_cmnd); 1062 1063 /** 1064 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery 1065 * @scmd: SCSI command structure to restore 1066 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd 1067 * 1068 * Undo any damage done by above scsi_eh_prep_cmnd(). 1069 */ 1070 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses) 1071 { 1072 /* 1073 * Restore original data 1074 */ 1075 scmd->cmd_len = ses->cmd_len; 1076 memcpy(scmd->cmnd, ses->cmnd, sizeof(ses->cmnd)); 1077 scmd->sc_data_direction = ses->data_direction; 1078 scmd->sdb = ses->sdb; 1079 scmd->result = ses->result; 1080 scmd->resid_len = ses->resid_len; 1081 scmd->underflow = ses->underflow; 1082 scmd->prot_op = ses->prot_op; 1083 scmd->eh_eflags = ses->eh_eflags; 1084 } 1085 EXPORT_SYMBOL(scsi_eh_restore_cmnd); 1086 1087 /** 1088 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery 1089 * @scmd: SCSI command structure to hijack 1090 * @cmnd: CDB to send 1091 * @cmnd_size: size in bytes of @cmnd 1092 * @timeout: timeout for this request 1093 * @sense_bytes: size of sense data to copy or 0 1094 * 1095 * This function is used to send a scsi command down to a target device 1096 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above. 1097 * 1098 * Return value: 1099 * SUCCESS or FAILED or NEEDS_RETRY 1100 */ 1101 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd, 1102 unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes) 1103 { 1104 struct scsi_device *sdev = scmd->device; 1105 struct Scsi_Host *shost = sdev->host; 1106 DECLARE_COMPLETION_ONSTACK(done); 1107 unsigned long timeleft = timeout, delay; 1108 struct scsi_eh_save ses; 1109 const unsigned long stall_for = msecs_to_jiffies(100); 1110 int rtn; 1111 1112 retry: 1113 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes); 1114 shost->eh_action = &done; 1115 1116 scsi_log_send(scmd); 1117 scmd->submitter = SUBMITTED_BY_SCSI_ERROR_HANDLER; 1118 1119 /* 1120 * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can 1121 * change the SCSI device state after we have examined it and before 1122 * .queuecommand() is called. 1123 */ 1124 mutex_lock(&sdev->state_mutex); 1125 while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) { 1126 mutex_unlock(&sdev->state_mutex); 1127 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev, 1128 "%s: state %d <> %d\n", __func__, sdev->sdev_state, 1129 SDEV_BLOCK)); 1130 delay = min(timeleft, stall_for); 1131 timeleft -= delay; 1132 msleep(jiffies_to_msecs(delay)); 1133 mutex_lock(&sdev->state_mutex); 1134 } 1135 if (sdev->sdev_state != SDEV_BLOCK) 1136 rtn = shost->hostt->queuecommand(shost, scmd); 1137 else 1138 rtn = FAILED; 1139 mutex_unlock(&sdev->state_mutex); 1140 1141 if (rtn) { 1142 if (timeleft > stall_for) { 1143 scsi_eh_restore_cmnd(scmd, &ses); 1144 1145 timeleft -= stall_for; 1146 msleep(jiffies_to_msecs(stall_for)); 1147 goto retry; 1148 } 1149 /* signal not to enter either branch of the if () below */ 1150 timeleft = 0; 1151 rtn = FAILED; 1152 } else { 1153 timeleft = wait_for_completion_timeout(&done, timeout); 1154 rtn = SUCCESS; 1155 } 1156 1157 shost->eh_action = NULL; 1158 1159 scsi_log_completion(scmd, rtn); 1160 1161 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, 1162 "%s timeleft: %ld\n", 1163 __func__, timeleft)); 1164 1165 /* 1166 * If there is time left scsi_eh_done got called, and we will examine 1167 * the actual status codes to see whether the command actually did 1168 * complete normally, else if we have a zero return and no time left, 1169 * the command must still be pending, so abort it and return FAILED. 1170 * If we never actually managed to issue the command, because 1171 * ->queuecommand() kept returning non zero, use the rtn = FAILED 1172 * value above (so don't execute either branch of the if) 1173 */ 1174 if (timeleft) { 1175 rtn = scsi_eh_completed_normally(scmd); 1176 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, 1177 "%s: scsi_eh_completed_normally %x\n", __func__, rtn)); 1178 1179 switch (rtn) { 1180 case SUCCESS: 1181 case NEEDS_RETRY: 1182 case FAILED: 1183 break; 1184 case ADD_TO_MLQUEUE: 1185 rtn = NEEDS_RETRY; 1186 break; 1187 default: 1188 rtn = FAILED; 1189 break; 1190 } 1191 } else if (rtn != FAILED) { 1192 scsi_abort_eh_cmnd(scmd); 1193 rtn = FAILED; 1194 } 1195 1196 scsi_eh_restore_cmnd(scmd, &ses); 1197 1198 return rtn; 1199 } 1200 1201 /** 1202 * scsi_request_sense - Request sense data from a particular target. 1203 * @scmd: SCSI cmd for request sense. 1204 * 1205 * Notes: 1206 * Some hosts automatically obtain this information, others require 1207 * that we obtain it on our own. This function will *not* return until 1208 * the command either times out, or it completes. 1209 */ 1210 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd) 1211 { 1212 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0); 1213 } 1214 1215 static enum scsi_disposition 1216 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn) 1217 { 1218 if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) { 1219 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd); 1220 if (sdrv->eh_action) 1221 rtn = sdrv->eh_action(scmd, rtn); 1222 } 1223 return rtn; 1224 } 1225 1226 /** 1227 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with. 1228 * @scmd: Original SCSI cmd that eh has finished. 1229 * @done_q: Queue for processed commands. 1230 * 1231 * Notes: 1232 * We don't want to use the normal command completion while we are are 1233 * still handling errors - it may cause other commands to be queued, 1234 * and that would disturb what we are doing. Thus we really want to 1235 * keep a list of pending commands for final completion, and once we 1236 * are ready to leave error handling we handle completion for real. 1237 */ 1238 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q) 1239 { 1240 list_move_tail(&scmd->eh_entry, done_q); 1241 } 1242 EXPORT_SYMBOL(scsi_eh_finish_cmd); 1243 1244 /** 1245 * scsi_eh_get_sense - Get device sense data. 1246 * @work_q: Queue of commands to process. 1247 * @done_q: Queue of processed commands. 1248 * 1249 * Description: 1250 * See if we need to request sense information. if so, then get it 1251 * now, so we have a better idea of what to do. 1252 * 1253 * Notes: 1254 * This has the unfortunate side effect that if a shost adapter does 1255 * not automatically request sense information, we end up shutting 1256 * it down before we request it. 1257 * 1258 * All drivers should request sense information internally these days, 1259 * so for now all I have to say is tough noogies if you end up in here. 1260 * 1261 * XXX: Long term this code should go away, but that needs an audit of 1262 * all LLDDs first. 1263 */ 1264 int scsi_eh_get_sense(struct list_head *work_q, 1265 struct list_head *done_q) 1266 { 1267 struct scsi_cmnd *scmd, *next; 1268 struct Scsi_Host *shost; 1269 enum scsi_disposition rtn; 1270 1271 /* 1272 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO, 1273 * should not get sense. 1274 */ 1275 list_for_each_entry_safe(scmd, next, work_q, eh_entry) { 1276 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) || 1277 SCSI_SENSE_VALID(scmd)) 1278 continue; 1279 1280 shost = scmd->device->host; 1281 if (scsi_host_eh_past_deadline(shost)) { 1282 SCSI_LOG_ERROR_RECOVERY(3, 1283 scmd_printk(KERN_INFO, scmd, 1284 "%s: skip request sense, past eh deadline\n", 1285 current->comm)); 1286 break; 1287 } 1288 if (!scsi_status_is_check_condition(scmd->result)) 1289 /* 1290 * don't request sense if there's no check condition 1291 * status because the error we're processing isn't one 1292 * that has a sense code (and some devices get 1293 * confused by sense requests out of the blue) 1294 */ 1295 continue; 1296 1297 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd, 1298 "%s: requesting sense\n", 1299 current->comm)); 1300 rtn = scsi_request_sense(scmd); 1301 if (rtn != SUCCESS) 1302 continue; 1303 1304 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, 1305 "sense requested, result %x\n", scmd->result)); 1306 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd)); 1307 1308 rtn = scsi_decide_disposition(scmd); 1309 1310 /* 1311 * if the result was normal, then just pass it along to the 1312 * upper level. 1313 */ 1314 if (rtn == SUCCESS) 1315 /* 1316 * We don't want this command reissued, just finished 1317 * with the sense data, so set retries to the max 1318 * allowed to ensure it won't get reissued. If the user 1319 * has requested infinite retries, we also want to 1320 * finish this command, so force completion by setting 1321 * retries and allowed to the same value. 1322 */ 1323 if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT) 1324 scmd->retries = scmd->allowed = 1; 1325 else 1326 scmd->retries = scmd->allowed; 1327 else if (rtn != NEEDS_RETRY) 1328 continue; 1329 1330 scsi_eh_finish_cmd(scmd, done_q); 1331 } 1332 1333 return list_empty(work_q); 1334 } 1335 EXPORT_SYMBOL_GPL(scsi_eh_get_sense); 1336 1337 /** 1338 * scsi_eh_tur - Send TUR to device. 1339 * @scmd: &scsi_cmnd to send TUR 1340 * 1341 * Return value: 1342 * 0 - Device is ready. 1 - Device NOT ready. 1343 */ 1344 static int scsi_eh_tur(struct scsi_cmnd *scmd) 1345 { 1346 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0}; 1347 int retry_cnt = 1; 1348 enum scsi_disposition rtn; 1349 1350 retry_tur: 1351 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, 1352 scmd->device->eh_timeout, 0); 1353 1354 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, 1355 "%s return: %x\n", __func__, rtn)); 1356 1357 switch (rtn) { 1358 case NEEDS_RETRY: 1359 if (retry_cnt--) 1360 goto retry_tur; 1361 fallthrough; 1362 case SUCCESS: 1363 return 0; 1364 default: 1365 return 1; 1366 } 1367 } 1368 1369 /** 1370 * scsi_eh_test_devices - check if devices are responding from error recovery. 1371 * @cmd_list: scsi commands in error recovery. 1372 * @work_q: queue for commands which still need more error recovery 1373 * @done_q: queue for commands which are finished 1374 * @try_stu: boolean on if a STU command should be tried in addition to TUR. 1375 * 1376 * Decription: 1377 * Tests if devices are in a working state. Commands to devices now in 1378 * a working state are sent to the done_q while commands to devices which 1379 * are still failing to respond are returned to the work_q for more 1380 * processing. 1381 **/ 1382 static int scsi_eh_test_devices(struct list_head *cmd_list, 1383 struct list_head *work_q, 1384 struct list_head *done_q, int try_stu) 1385 { 1386 struct scsi_cmnd *scmd, *next; 1387 struct scsi_device *sdev; 1388 int finish_cmds; 1389 1390 while (!list_empty(cmd_list)) { 1391 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry); 1392 sdev = scmd->device; 1393 1394 if (!try_stu) { 1395 if (scsi_host_eh_past_deadline(sdev->host)) { 1396 /* Push items back onto work_q */ 1397 list_splice_init(cmd_list, work_q); 1398 SCSI_LOG_ERROR_RECOVERY(3, 1399 sdev_printk(KERN_INFO, sdev, 1400 "%s: skip test device, past eh deadline", 1401 current->comm)); 1402 break; 1403 } 1404 } 1405 1406 finish_cmds = !scsi_device_online(scmd->device) || 1407 (try_stu && !scsi_eh_try_stu(scmd) && 1408 !scsi_eh_tur(scmd)) || 1409 !scsi_eh_tur(scmd); 1410 1411 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry) 1412 if (scmd->device == sdev) { 1413 if (finish_cmds && 1414 (try_stu || 1415 scsi_eh_action(scmd, SUCCESS) == SUCCESS)) 1416 scsi_eh_finish_cmd(scmd, done_q); 1417 else 1418 list_move_tail(&scmd->eh_entry, work_q); 1419 } 1420 } 1421 return list_empty(work_q); 1422 } 1423 1424 /** 1425 * scsi_eh_try_stu - Send START_UNIT to device. 1426 * @scmd: &scsi_cmnd to send START_UNIT 1427 * 1428 * Return value: 1429 * 0 - Device is ready. 1 - Device NOT ready. 1430 */ 1431 static int scsi_eh_try_stu(struct scsi_cmnd *scmd) 1432 { 1433 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0}; 1434 1435 if (scmd->device->allow_restart) { 1436 int i; 1437 enum scsi_disposition rtn = NEEDS_RETRY; 1438 1439 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++) 1440 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, 1441 scmd->device->eh_timeout, 0); 1442 1443 if (rtn == SUCCESS) 1444 return 0; 1445 } 1446 1447 return 1; 1448 } 1449 1450 /** 1451 * scsi_eh_stu - send START_UNIT if needed 1452 * @shost: &scsi host being recovered. 1453 * @work_q: &list_head for pending commands. 1454 * @done_q: &list_head for processed commands. 1455 * 1456 * Notes: 1457 * If commands are failing due to not ready, initializing command required, 1458 * try revalidating the device, which will end up sending a start unit. 1459 */ 1460 static int scsi_eh_stu(struct Scsi_Host *shost, 1461 struct list_head *work_q, 1462 struct list_head *done_q) 1463 { 1464 struct scsi_cmnd *scmd, *stu_scmd, *next; 1465 struct scsi_device *sdev; 1466 1467 shost_for_each_device(sdev, shost) { 1468 if (scsi_host_eh_past_deadline(shost)) { 1469 SCSI_LOG_ERROR_RECOVERY(3, 1470 sdev_printk(KERN_INFO, sdev, 1471 "%s: skip START_UNIT, past eh deadline\n", 1472 current->comm)); 1473 scsi_device_put(sdev); 1474 break; 1475 } 1476 stu_scmd = NULL; 1477 list_for_each_entry(scmd, work_q, eh_entry) 1478 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) && 1479 scsi_check_sense(scmd) == FAILED ) { 1480 stu_scmd = scmd; 1481 break; 1482 } 1483 1484 if (!stu_scmd) 1485 continue; 1486 1487 SCSI_LOG_ERROR_RECOVERY(3, 1488 sdev_printk(KERN_INFO, sdev, 1489 "%s: Sending START_UNIT\n", 1490 current->comm)); 1491 1492 if (!scsi_eh_try_stu(stu_scmd)) { 1493 if (!scsi_device_online(sdev) || 1494 !scsi_eh_tur(stu_scmd)) { 1495 list_for_each_entry_safe(scmd, next, 1496 work_q, eh_entry) { 1497 if (scmd->device == sdev && 1498 scsi_eh_action(scmd, SUCCESS) == SUCCESS) 1499 scsi_eh_finish_cmd(scmd, done_q); 1500 } 1501 } 1502 } else { 1503 SCSI_LOG_ERROR_RECOVERY(3, 1504 sdev_printk(KERN_INFO, sdev, 1505 "%s: START_UNIT failed\n", 1506 current->comm)); 1507 } 1508 } 1509 1510 return list_empty(work_q); 1511 } 1512 1513 1514 /** 1515 * scsi_eh_bus_device_reset - send bdr if needed 1516 * @shost: scsi host being recovered. 1517 * @work_q: &list_head for pending commands. 1518 * @done_q: &list_head for processed commands. 1519 * 1520 * Notes: 1521 * Try a bus device reset. Still, look to see whether we have multiple 1522 * devices that are jammed or not - if we have multiple devices, it 1523 * makes no sense to try bus_device_reset - we really would need to try 1524 * a bus_reset instead. 1525 */ 1526 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost, 1527 struct list_head *work_q, 1528 struct list_head *done_q) 1529 { 1530 struct scsi_cmnd *scmd, *bdr_scmd, *next; 1531 struct scsi_device *sdev; 1532 enum scsi_disposition rtn; 1533 1534 shost_for_each_device(sdev, shost) { 1535 if (scsi_host_eh_past_deadline(shost)) { 1536 SCSI_LOG_ERROR_RECOVERY(3, 1537 sdev_printk(KERN_INFO, sdev, 1538 "%s: skip BDR, past eh deadline\n", 1539 current->comm)); 1540 scsi_device_put(sdev); 1541 break; 1542 } 1543 bdr_scmd = NULL; 1544 list_for_each_entry(scmd, work_q, eh_entry) 1545 if (scmd->device == sdev) { 1546 bdr_scmd = scmd; 1547 break; 1548 } 1549 1550 if (!bdr_scmd) 1551 continue; 1552 1553 SCSI_LOG_ERROR_RECOVERY(3, 1554 sdev_printk(KERN_INFO, sdev, 1555 "%s: Sending BDR\n", current->comm)); 1556 rtn = scsi_try_bus_device_reset(bdr_scmd); 1557 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) { 1558 if (!scsi_device_online(sdev) || 1559 rtn == FAST_IO_FAIL || 1560 !scsi_eh_tur(bdr_scmd)) { 1561 list_for_each_entry_safe(scmd, next, 1562 work_q, eh_entry) { 1563 if (scmd->device == sdev && 1564 scsi_eh_action(scmd, rtn) != FAILED) 1565 scsi_eh_finish_cmd(scmd, 1566 done_q); 1567 } 1568 } 1569 } else { 1570 SCSI_LOG_ERROR_RECOVERY(3, 1571 sdev_printk(KERN_INFO, sdev, 1572 "%s: BDR failed\n", current->comm)); 1573 } 1574 } 1575 1576 return list_empty(work_q); 1577 } 1578 1579 /** 1580 * scsi_eh_target_reset - send target reset if needed 1581 * @shost: scsi host being recovered. 1582 * @work_q: &list_head for pending commands. 1583 * @done_q: &list_head for processed commands. 1584 * 1585 * Notes: 1586 * Try a target reset. 1587 */ 1588 static int scsi_eh_target_reset(struct Scsi_Host *shost, 1589 struct list_head *work_q, 1590 struct list_head *done_q) 1591 { 1592 LIST_HEAD(tmp_list); 1593 LIST_HEAD(check_list); 1594 1595 list_splice_init(work_q, &tmp_list); 1596 1597 while (!list_empty(&tmp_list)) { 1598 struct scsi_cmnd *next, *scmd; 1599 enum scsi_disposition rtn; 1600 unsigned int id; 1601 1602 if (scsi_host_eh_past_deadline(shost)) { 1603 /* push back on work queue for further processing */ 1604 list_splice_init(&check_list, work_q); 1605 list_splice_init(&tmp_list, work_q); 1606 SCSI_LOG_ERROR_RECOVERY(3, 1607 shost_printk(KERN_INFO, shost, 1608 "%s: Skip target reset, past eh deadline\n", 1609 current->comm)); 1610 return list_empty(work_q); 1611 } 1612 1613 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry); 1614 id = scmd_id(scmd); 1615 1616 SCSI_LOG_ERROR_RECOVERY(3, 1617 shost_printk(KERN_INFO, shost, 1618 "%s: Sending target reset to target %d\n", 1619 current->comm, id)); 1620 rtn = scsi_try_target_reset(scmd); 1621 if (rtn != SUCCESS && rtn != FAST_IO_FAIL) 1622 SCSI_LOG_ERROR_RECOVERY(3, 1623 shost_printk(KERN_INFO, shost, 1624 "%s: Target reset failed" 1625 " target: %d\n", 1626 current->comm, id)); 1627 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) { 1628 if (scmd_id(scmd) != id) 1629 continue; 1630 1631 if (rtn == SUCCESS) 1632 list_move_tail(&scmd->eh_entry, &check_list); 1633 else if (rtn == FAST_IO_FAIL) 1634 scsi_eh_finish_cmd(scmd, done_q); 1635 else 1636 /* push back on work queue for further processing */ 1637 list_move(&scmd->eh_entry, work_q); 1638 } 1639 } 1640 1641 return scsi_eh_test_devices(&check_list, work_q, done_q, 0); 1642 } 1643 1644 /** 1645 * scsi_eh_bus_reset - send a bus reset 1646 * @shost: &scsi host being recovered. 1647 * @work_q: &list_head for pending commands. 1648 * @done_q: &list_head for processed commands. 1649 */ 1650 static int scsi_eh_bus_reset(struct Scsi_Host *shost, 1651 struct list_head *work_q, 1652 struct list_head *done_q) 1653 { 1654 struct scsi_cmnd *scmd, *chan_scmd, *next; 1655 LIST_HEAD(check_list); 1656 unsigned int channel; 1657 enum scsi_disposition rtn; 1658 1659 /* 1660 * we really want to loop over the various channels, and do this on 1661 * a channel by channel basis. we should also check to see if any 1662 * of the failed commands are on soft_reset devices, and if so, skip 1663 * the reset. 1664 */ 1665 1666 for (channel = 0; channel <= shost->max_channel; channel++) { 1667 if (scsi_host_eh_past_deadline(shost)) { 1668 list_splice_init(&check_list, work_q); 1669 SCSI_LOG_ERROR_RECOVERY(3, 1670 shost_printk(KERN_INFO, shost, 1671 "%s: skip BRST, past eh deadline\n", 1672 current->comm)); 1673 return list_empty(work_q); 1674 } 1675 1676 chan_scmd = NULL; 1677 list_for_each_entry(scmd, work_q, eh_entry) { 1678 if (channel == scmd_channel(scmd)) { 1679 chan_scmd = scmd; 1680 break; 1681 /* 1682 * FIXME add back in some support for 1683 * soft_reset devices. 1684 */ 1685 } 1686 } 1687 1688 if (!chan_scmd) 1689 continue; 1690 SCSI_LOG_ERROR_RECOVERY(3, 1691 shost_printk(KERN_INFO, shost, 1692 "%s: Sending BRST chan: %d\n", 1693 current->comm, channel)); 1694 rtn = scsi_try_bus_reset(chan_scmd); 1695 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) { 1696 list_for_each_entry_safe(scmd, next, work_q, eh_entry) { 1697 if (channel == scmd_channel(scmd)) { 1698 if (rtn == FAST_IO_FAIL) 1699 scsi_eh_finish_cmd(scmd, 1700 done_q); 1701 else 1702 list_move_tail(&scmd->eh_entry, 1703 &check_list); 1704 } 1705 } 1706 } else { 1707 SCSI_LOG_ERROR_RECOVERY(3, 1708 shost_printk(KERN_INFO, shost, 1709 "%s: BRST failed chan: %d\n", 1710 current->comm, channel)); 1711 } 1712 } 1713 return scsi_eh_test_devices(&check_list, work_q, done_q, 0); 1714 } 1715 1716 /** 1717 * scsi_eh_host_reset - send a host reset 1718 * @shost: host to be reset. 1719 * @work_q: &list_head for pending commands. 1720 * @done_q: &list_head for processed commands. 1721 */ 1722 static int scsi_eh_host_reset(struct Scsi_Host *shost, 1723 struct list_head *work_q, 1724 struct list_head *done_q) 1725 { 1726 struct scsi_cmnd *scmd, *next; 1727 LIST_HEAD(check_list); 1728 enum scsi_disposition rtn; 1729 1730 if (!list_empty(work_q)) { 1731 scmd = list_entry(work_q->next, 1732 struct scsi_cmnd, eh_entry); 1733 1734 SCSI_LOG_ERROR_RECOVERY(3, 1735 shost_printk(KERN_INFO, shost, 1736 "%s: Sending HRST\n", 1737 current->comm)); 1738 1739 rtn = scsi_try_host_reset(scmd); 1740 if (rtn == SUCCESS) { 1741 list_splice_init(work_q, &check_list); 1742 } else if (rtn == FAST_IO_FAIL) { 1743 list_for_each_entry_safe(scmd, next, work_q, eh_entry) { 1744 scsi_eh_finish_cmd(scmd, done_q); 1745 } 1746 } else { 1747 SCSI_LOG_ERROR_RECOVERY(3, 1748 shost_printk(KERN_INFO, shost, 1749 "%s: HRST failed\n", 1750 current->comm)); 1751 } 1752 } 1753 return scsi_eh_test_devices(&check_list, work_q, done_q, 1); 1754 } 1755 1756 /** 1757 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover 1758 * @work_q: &list_head for pending commands. 1759 * @done_q: &list_head for processed commands. 1760 */ 1761 static void scsi_eh_offline_sdevs(struct list_head *work_q, 1762 struct list_head *done_q) 1763 { 1764 struct scsi_cmnd *scmd, *next; 1765 struct scsi_device *sdev; 1766 1767 list_for_each_entry_safe(scmd, next, work_q, eh_entry) { 1768 sdev_printk(KERN_INFO, scmd->device, "Device offlined - " 1769 "not ready after error recovery\n"); 1770 sdev = scmd->device; 1771 1772 mutex_lock(&sdev->state_mutex); 1773 scsi_device_set_state(sdev, SDEV_OFFLINE); 1774 mutex_unlock(&sdev->state_mutex); 1775 1776 scsi_eh_finish_cmd(scmd, done_q); 1777 } 1778 return; 1779 } 1780 1781 /** 1782 * scsi_noretry_cmd - determine if command should be failed fast 1783 * @scmd: SCSI cmd to examine. 1784 */ 1785 bool scsi_noretry_cmd(struct scsi_cmnd *scmd) 1786 { 1787 struct request *req = scsi_cmd_to_rq(scmd); 1788 1789 switch (host_byte(scmd->result)) { 1790 case DID_OK: 1791 break; 1792 case DID_TIME_OUT: 1793 goto check_type; 1794 case DID_BUS_BUSY: 1795 return !!(req->cmd_flags & REQ_FAILFAST_TRANSPORT); 1796 case DID_PARITY: 1797 return !!(req->cmd_flags & REQ_FAILFAST_DEV); 1798 case DID_ERROR: 1799 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT) 1800 return false; 1801 fallthrough; 1802 case DID_SOFT_ERROR: 1803 return !!(req->cmd_flags & REQ_FAILFAST_DRIVER); 1804 } 1805 1806 if (!scsi_status_is_check_condition(scmd->result)) 1807 return false; 1808 1809 check_type: 1810 /* 1811 * assume caller has checked sense and determined 1812 * the check condition was retryable. 1813 */ 1814 if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req)) 1815 return true; 1816 1817 return false; 1818 } 1819 1820 /** 1821 * scsi_decide_disposition - Disposition a cmd on return from LLD. 1822 * @scmd: SCSI cmd to examine. 1823 * 1824 * Notes: 1825 * This is *only* called when we are examining the status after sending 1826 * out the actual data command. any commands that are queued for error 1827 * recovery (e.g. test_unit_ready) do *not* come through here. 1828 * 1829 * When this routine returns failed, it means the error handler thread 1830 * is woken. In cases where the error code indicates an error that 1831 * doesn't require the error handler read (i.e. we don't need to 1832 * abort/reset), this function should return SUCCESS. 1833 */ 1834 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd) 1835 { 1836 enum scsi_disposition rtn; 1837 1838 /* 1839 * if the device is offline, then we clearly just pass the result back 1840 * up to the top level. 1841 */ 1842 if (!scsi_device_online(scmd->device)) { 1843 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd, 1844 "%s: device offline - report as SUCCESS\n", __func__)); 1845 return SUCCESS; 1846 } 1847 1848 /* 1849 * first check the host byte, to see if there is anything in there 1850 * that would indicate what we need to do. 1851 */ 1852 switch (host_byte(scmd->result)) { 1853 case DID_PASSTHROUGH: 1854 /* 1855 * no matter what, pass this through to the upper layer. 1856 * nuke this special code so that it looks like we are saying 1857 * did_ok. 1858 */ 1859 scmd->result &= 0xff00ffff; 1860 return SUCCESS; 1861 case DID_OK: 1862 /* 1863 * looks good. drop through, and check the next byte. 1864 */ 1865 break; 1866 case DID_ABORT: 1867 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) { 1868 set_host_byte(scmd, DID_TIME_OUT); 1869 return SUCCESS; 1870 } 1871 fallthrough; 1872 case DID_NO_CONNECT: 1873 case DID_BAD_TARGET: 1874 /* 1875 * note - this means that we just report the status back 1876 * to the top level driver, not that we actually think 1877 * that it indicates SUCCESS. 1878 */ 1879 return SUCCESS; 1880 case DID_SOFT_ERROR: 1881 /* 1882 * when the low level driver returns did_soft_error, 1883 * it is responsible for keeping an internal retry counter 1884 * in order to avoid endless loops (db) 1885 */ 1886 goto maybe_retry; 1887 case DID_IMM_RETRY: 1888 return NEEDS_RETRY; 1889 1890 case DID_REQUEUE: 1891 return ADD_TO_MLQUEUE; 1892 case DID_TRANSPORT_DISRUPTED: 1893 /* 1894 * LLD/transport was disrupted during processing of the IO. 1895 * The transport class is now blocked/blocking, 1896 * and the transport will decide what to do with the IO 1897 * based on its timers and recovery capablilities if 1898 * there are enough retries. 1899 */ 1900 goto maybe_retry; 1901 case DID_TRANSPORT_FAILFAST: 1902 /* 1903 * The transport decided to failfast the IO (most likely 1904 * the fast io fail tmo fired), so send IO directly upwards. 1905 */ 1906 return SUCCESS; 1907 case DID_TRANSPORT_MARGINAL: 1908 /* 1909 * caller has decided not to do retries on 1910 * abort success, so send IO directly upwards 1911 */ 1912 return SUCCESS; 1913 case DID_ERROR: 1914 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT) 1915 /* 1916 * execute reservation conflict processing code 1917 * lower down 1918 */ 1919 break; 1920 fallthrough; 1921 case DID_BUS_BUSY: 1922 case DID_PARITY: 1923 goto maybe_retry; 1924 case DID_TIME_OUT: 1925 /* 1926 * when we scan the bus, we get timeout messages for 1927 * these commands if there is no device available. 1928 * other hosts report did_no_connect for the same thing. 1929 */ 1930 if ((scmd->cmnd[0] == TEST_UNIT_READY || 1931 scmd->cmnd[0] == INQUIRY)) { 1932 return SUCCESS; 1933 } else { 1934 return FAILED; 1935 } 1936 case DID_RESET: 1937 return SUCCESS; 1938 default: 1939 return FAILED; 1940 } 1941 1942 /* 1943 * check the status byte to see if this indicates anything special. 1944 */ 1945 switch (get_status_byte(scmd)) { 1946 case SAM_STAT_TASK_SET_FULL: 1947 scsi_handle_queue_full(scmd->device); 1948 /* 1949 * the case of trying to send too many commands to a 1950 * tagged queueing device. 1951 */ 1952 fallthrough; 1953 case SAM_STAT_BUSY: 1954 /* 1955 * device can't talk to us at the moment. Should only 1956 * occur (SAM-3) when the task queue is empty, so will cause 1957 * the empty queue handling to trigger a stall in the 1958 * device. 1959 */ 1960 return ADD_TO_MLQUEUE; 1961 case SAM_STAT_GOOD: 1962 if (scmd->cmnd[0] == REPORT_LUNS) 1963 scmd->device->sdev_target->expecting_lun_change = 0; 1964 scsi_handle_queue_ramp_up(scmd->device); 1965 fallthrough; 1966 case SAM_STAT_COMMAND_TERMINATED: 1967 return SUCCESS; 1968 case SAM_STAT_TASK_ABORTED: 1969 goto maybe_retry; 1970 case SAM_STAT_CHECK_CONDITION: 1971 rtn = scsi_check_sense(scmd); 1972 if (rtn == NEEDS_RETRY) 1973 goto maybe_retry; 1974 /* if rtn == FAILED, we have no sense information; 1975 * returning FAILED will wake the error handler thread 1976 * to collect the sense and redo the decide 1977 * disposition */ 1978 return rtn; 1979 case SAM_STAT_CONDITION_MET: 1980 case SAM_STAT_INTERMEDIATE: 1981 case SAM_STAT_INTERMEDIATE_CONDITION_MET: 1982 case SAM_STAT_ACA_ACTIVE: 1983 /* 1984 * who knows? FIXME(eric) 1985 */ 1986 return SUCCESS; 1987 1988 case SAM_STAT_RESERVATION_CONFLICT: 1989 sdev_printk(KERN_INFO, scmd->device, 1990 "reservation conflict\n"); 1991 set_scsi_ml_byte(scmd, SCSIML_STAT_RESV_CONFLICT); 1992 return SUCCESS; /* causes immediate i/o error */ 1993 } 1994 return FAILED; 1995 1996 maybe_retry: 1997 1998 /* we requeue for retry because the error was retryable, and 1999 * the request was not marked fast fail. Note that above, 2000 * even if the request is marked fast fail, we still requeue 2001 * for queue congestion conditions (QUEUE_FULL or BUSY) */ 2002 if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) { 2003 return NEEDS_RETRY; 2004 } else { 2005 /* 2006 * no more retries - report this one back to upper level. 2007 */ 2008 return SUCCESS; 2009 } 2010 } 2011 2012 static void eh_lock_door_done(struct request *req, blk_status_t status) 2013 { 2014 blk_mq_free_request(req); 2015 } 2016 2017 /** 2018 * scsi_eh_lock_door - Prevent medium removal for the specified device 2019 * @sdev: SCSI device to prevent medium removal 2020 * 2021 * Locking: 2022 * We must be called from process context. 2023 * 2024 * Notes: 2025 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the 2026 * head of the devices request queue, and continue. 2027 */ 2028 static void scsi_eh_lock_door(struct scsi_device *sdev) 2029 { 2030 struct scsi_cmnd *scmd; 2031 struct request *req; 2032 2033 req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN, 0); 2034 if (IS_ERR(req)) 2035 return; 2036 scmd = blk_mq_rq_to_pdu(req); 2037 2038 scmd->cmnd[0] = ALLOW_MEDIUM_REMOVAL; 2039 scmd->cmnd[1] = 0; 2040 scmd->cmnd[2] = 0; 2041 scmd->cmnd[3] = 0; 2042 scmd->cmnd[4] = SCSI_REMOVAL_PREVENT; 2043 scmd->cmnd[5] = 0; 2044 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]); 2045 scmd->allowed = 5; 2046 2047 req->rq_flags |= RQF_QUIET; 2048 req->timeout = 10 * HZ; 2049 req->end_io = eh_lock_door_done; 2050 2051 blk_execute_rq_nowait(req, true); 2052 } 2053 2054 /** 2055 * scsi_restart_operations - restart io operations to the specified host. 2056 * @shost: Host we are restarting. 2057 * 2058 * Notes: 2059 * When we entered the error handler, we blocked all further i/o to 2060 * this device. we need to 'reverse' this process. 2061 */ 2062 static void scsi_restart_operations(struct Scsi_Host *shost) 2063 { 2064 struct scsi_device *sdev; 2065 unsigned long flags; 2066 2067 /* 2068 * If the door was locked, we need to insert a door lock request 2069 * onto the head of the SCSI request queue for the device. There 2070 * is no point trying to lock the door of an off-line device. 2071 */ 2072 shost_for_each_device(sdev, shost) { 2073 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) { 2074 scsi_eh_lock_door(sdev); 2075 sdev->was_reset = 0; 2076 } 2077 } 2078 2079 /* 2080 * next free up anything directly waiting upon the host. this 2081 * will be requests for character device operations, and also for 2082 * ioctls to queued block devices. 2083 */ 2084 SCSI_LOG_ERROR_RECOVERY(3, 2085 shost_printk(KERN_INFO, shost, "waking up host to restart\n")); 2086 2087 spin_lock_irqsave(shost->host_lock, flags); 2088 if (scsi_host_set_state(shost, SHOST_RUNNING)) 2089 if (scsi_host_set_state(shost, SHOST_CANCEL)) 2090 BUG_ON(scsi_host_set_state(shost, SHOST_DEL)); 2091 spin_unlock_irqrestore(shost->host_lock, flags); 2092 2093 wake_up(&shost->host_wait); 2094 2095 /* 2096 * finally we need to re-initiate requests that may be pending. we will 2097 * have had everything blocked while error handling is taking place, and 2098 * now that error recovery is done, we will need to ensure that these 2099 * requests are started. 2100 */ 2101 scsi_run_host_queues(shost); 2102 2103 /* 2104 * if eh is active and host_eh_scheduled is pending we need to re-run 2105 * recovery. we do this check after scsi_run_host_queues() to allow 2106 * everything pent up since the last eh run a chance to make forward 2107 * progress before we sync again. Either we'll immediately re-run 2108 * recovery or scsi_device_unbusy() will wake us again when these 2109 * pending commands complete. 2110 */ 2111 spin_lock_irqsave(shost->host_lock, flags); 2112 if (shost->host_eh_scheduled) 2113 if (scsi_host_set_state(shost, SHOST_RECOVERY)) 2114 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)); 2115 spin_unlock_irqrestore(shost->host_lock, flags); 2116 } 2117 2118 /** 2119 * scsi_eh_ready_devs - check device ready state and recover if not. 2120 * @shost: host to be recovered. 2121 * @work_q: &list_head for pending commands. 2122 * @done_q: &list_head for processed commands. 2123 */ 2124 void scsi_eh_ready_devs(struct Scsi_Host *shost, 2125 struct list_head *work_q, 2126 struct list_head *done_q) 2127 { 2128 if (!scsi_eh_stu(shost, work_q, done_q)) 2129 if (!scsi_eh_bus_device_reset(shost, work_q, done_q)) 2130 if (!scsi_eh_target_reset(shost, work_q, done_q)) 2131 if (!scsi_eh_bus_reset(shost, work_q, done_q)) 2132 if (!scsi_eh_host_reset(shost, work_q, done_q)) 2133 scsi_eh_offline_sdevs(work_q, 2134 done_q); 2135 } 2136 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs); 2137 2138 /** 2139 * scsi_eh_flush_done_q - finish processed commands or retry them. 2140 * @done_q: list_head of processed commands. 2141 */ 2142 void scsi_eh_flush_done_q(struct list_head *done_q) 2143 { 2144 struct scsi_cmnd *scmd, *next; 2145 2146 list_for_each_entry_safe(scmd, next, done_q, eh_entry) { 2147 list_del_init(&scmd->eh_entry); 2148 if (scsi_device_online(scmd->device) && 2149 !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd) && 2150 scsi_eh_should_retry_cmd(scmd)) { 2151 SCSI_LOG_ERROR_RECOVERY(3, 2152 scmd_printk(KERN_INFO, scmd, 2153 "%s: flush retry cmd\n", 2154 current->comm)); 2155 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY); 2156 } else { 2157 /* 2158 * If just we got sense for the device (called 2159 * scsi_eh_get_sense), scmd->result is already 2160 * set, do not set DID_TIME_OUT. 2161 */ 2162 if (!scmd->result) 2163 scmd->result |= (DID_TIME_OUT << 16); 2164 SCSI_LOG_ERROR_RECOVERY(3, 2165 scmd_printk(KERN_INFO, scmd, 2166 "%s: flush finish cmd\n", 2167 current->comm)); 2168 scsi_finish_command(scmd); 2169 } 2170 } 2171 } 2172 EXPORT_SYMBOL(scsi_eh_flush_done_q); 2173 2174 /** 2175 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed. 2176 * @shost: Host to unjam. 2177 * 2178 * Notes: 2179 * When we come in here, we *know* that all commands on the bus have 2180 * either completed, failed or timed out. we also know that no further 2181 * commands are being sent to the host, so things are relatively quiet 2182 * and we have freedom to fiddle with things as we wish. 2183 * 2184 * This is only the *default* implementation. it is possible for 2185 * individual drivers to supply their own version of this function, and 2186 * if the maintainer wishes to do this, it is strongly suggested that 2187 * this function be taken as a template and modified. this function 2188 * was designed to correctly handle problems for about 95% of the 2189 * different cases out there, and it should always provide at least a 2190 * reasonable amount of error recovery. 2191 * 2192 * Any command marked 'failed' or 'timeout' must eventually have 2193 * scsi_finish_cmd() called for it. we do all of the retry stuff 2194 * here, so when we restart the host after we return it should have an 2195 * empty queue. 2196 */ 2197 static void scsi_unjam_host(struct Scsi_Host *shost) 2198 { 2199 unsigned long flags; 2200 LIST_HEAD(eh_work_q); 2201 LIST_HEAD(eh_done_q); 2202 2203 spin_lock_irqsave(shost->host_lock, flags); 2204 list_splice_init(&shost->eh_cmd_q, &eh_work_q); 2205 spin_unlock_irqrestore(shost->host_lock, flags); 2206 2207 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q)); 2208 2209 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q)) 2210 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q); 2211 2212 spin_lock_irqsave(shost->host_lock, flags); 2213 if (shost->eh_deadline != -1) 2214 shost->last_reset = 0; 2215 spin_unlock_irqrestore(shost->host_lock, flags); 2216 scsi_eh_flush_done_q(&eh_done_q); 2217 } 2218 2219 /** 2220 * scsi_error_handler - SCSI error handler thread 2221 * @data: Host for which we are running. 2222 * 2223 * Notes: 2224 * This is the main error handling loop. This is run as a kernel thread 2225 * for every SCSI host and handles all error handling activity. 2226 */ 2227 int scsi_error_handler(void *data) 2228 { 2229 struct Scsi_Host *shost = data; 2230 2231 /* 2232 * We use TASK_INTERRUPTIBLE so that the thread is not 2233 * counted against the load average as a running process. 2234 * We never actually get interrupted because kthread_run 2235 * disables signal delivery for the created thread. 2236 */ 2237 while (true) { 2238 /* 2239 * The sequence in kthread_stop() sets the stop flag first 2240 * then wakes the process. To avoid missed wakeups, the task 2241 * should always be in a non running state before the stop 2242 * flag is checked 2243 */ 2244 set_current_state(TASK_INTERRUPTIBLE); 2245 if (kthread_should_stop()) 2246 break; 2247 2248 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) || 2249 shost->host_failed != scsi_host_busy(shost)) { 2250 SCSI_LOG_ERROR_RECOVERY(1, 2251 shost_printk(KERN_INFO, shost, 2252 "scsi_eh_%d: sleeping\n", 2253 shost->host_no)); 2254 schedule(); 2255 continue; 2256 } 2257 2258 __set_current_state(TASK_RUNNING); 2259 SCSI_LOG_ERROR_RECOVERY(1, 2260 shost_printk(KERN_INFO, shost, 2261 "scsi_eh_%d: waking up %d/%d/%d\n", 2262 shost->host_no, shost->host_eh_scheduled, 2263 shost->host_failed, 2264 scsi_host_busy(shost))); 2265 2266 /* 2267 * We have a host that is failing for some reason. Figure out 2268 * what we need to do to get it up and online again (if we can). 2269 * If we fail, we end up taking the thing offline. 2270 */ 2271 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) { 2272 SCSI_LOG_ERROR_RECOVERY(1, 2273 shost_printk(KERN_ERR, shost, 2274 "scsi_eh_%d: unable to autoresume\n", 2275 shost->host_no)); 2276 continue; 2277 } 2278 2279 if (shost->transportt->eh_strategy_handler) 2280 shost->transportt->eh_strategy_handler(shost); 2281 else 2282 scsi_unjam_host(shost); 2283 2284 /* All scmds have been handled */ 2285 shost->host_failed = 0; 2286 2287 /* 2288 * Note - if the above fails completely, the action is to take 2289 * individual devices offline and flush the queue of any 2290 * outstanding requests that may have been pending. When we 2291 * restart, we restart any I/O to any other devices on the bus 2292 * which are still online. 2293 */ 2294 scsi_restart_operations(shost); 2295 if (!shost->eh_noresume) 2296 scsi_autopm_put_host(shost); 2297 } 2298 __set_current_state(TASK_RUNNING); 2299 2300 SCSI_LOG_ERROR_RECOVERY(1, 2301 shost_printk(KERN_INFO, shost, 2302 "Error handler scsi_eh_%d exiting\n", 2303 shost->host_no)); 2304 shost->ehandler = NULL; 2305 return 0; 2306 } 2307 2308 /* 2309 * Function: scsi_report_bus_reset() 2310 * 2311 * Purpose: Utility function used by low-level drivers to report that 2312 * they have observed a bus reset on the bus being handled. 2313 * 2314 * Arguments: shost - Host in question 2315 * channel - channel on which reset was observed. 2316 * 2317 * Returns: Nothing 2318 * 2319 * Lock status: Host lock must be held. 2320 * 2321 * Notes: This only needs to be called if the reset is one which 2322 * originates from an unknown location. Resets originated 2323 * by the mid-level itself don't need to call this, but there 2324 * should be no harm. 2325 * 2326 * The main purpose of this is to make sure that a CHECK_CONDITION 2327 * is properly treated. 2328 */ 2329 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel) 2330 { 2331 struct scsi_device *sdev; 2332 2333 __shost_for_each_device(sdev, shost) { 2334 if (channel == sdev_channel(sdev)) 2335 __scsi_report_device_reset(sdev, NULL); 2336 } 2337 } 2338 EXPORT_SYMBOL(scsi_report_bus_reset); 2339 2340 /* 2341 * Function: scsi_report_device_reset() 2342 * 2343 * Purpose: Utility function used by low-level drivers to report that 2344 * they have observed a device reset on the device being handled. 2345 * 2346 * Arguments: shost - Host in question 2347 * channel - channel on which reset was observed 2348 * target - target on which reset was observed 2349 * 2350 * Returns: Nothing 2351 * 2352 * Lock status: Host lock must be held 2353 * 2354 * Notes: This only needs to be called if the reset is one which 2355 * originates from an unknown location. Resets originated 2356 * by the mid-level itself don't need to call this, but there 2357 * should be no harm. 2358 * 2359 * The main purpose of this is to make sure that a CHECK_CONDITION 2360 * is properly treated. 2361 */ 2362 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target) 2363 { 2364 struct scsi_device *sdev; 2365 2366 __shost_for_each_device(sdev, shost) { 2367 if (channel == sdev_channel(sdev) && 2368 target == sdev_id(sdev)) 2369 __scsi_report_device_reset(sdev, NULL); 2370 } 2371 } 2372 EXPORT_SYMBOL(scsi_report_device_reset); 2373 2374 /** 2375 * scsi_ioctl_reset: explicitly reset a host/bus/target/device 2376 * @dev: scsi_device to operate on 2377 * @arg: reset type (see sg.h) 2378 */ 2379 int 2380 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg) 2381 { 2382 struct scsi_cmnd *scmd; 2383 struct Scsi_Host *shost = dev->host; 2384 struct request *rq; 2385 unsigned long flags; 2386 int error = 0, val; 2387 enum scsi_disposition rtn; 2388 2389 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) 2390 return -EACCES; 2391 2392 error = get_user(val, arg); 2393 if (error) 2394 return error; 2395 2396 if (scsi_autopm_get_host(shost) < 0) 2397 return -EIO; 2398 2399 error = -EIO; 2400 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) + 2401 shost->hostt->cmd_size, GFP_KERNEL); 2402 if (!rq) 2403 goto out_put_autopm_host; 2404 blk_rq_init(NULL, rq); 2405 2406 scmd = (struct scsi_cmnd *)(rq + 1); 2407 scsi_init_command(dev, scmd); 2408 2409 scmd->submitter = SUBMITTED_BY_SCSI_RESET_IOCTL; 2410 memset(&scmd->sdb, 0, sizeof(scmd->sdb)); 2411 2412 scmd->cmd_len = 0; 2413 2414 scmd->sc_data_direction = DMA_BIDIRECTIONAL; 2415 2416 spin_lock_irqsave(shost->host_lock, flags); 2417 shost->tmf_in_progress = 1; 2418 spin_unlock_irqrestore(shost->host_lock, flags); 2419 2420 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) { 2421 case SG_SCSI_RESET_NOTHING: 2422 rtn = SUCCESS; 2423 break; 2424 case SG_SCSI_RESET_DEVICE: 2425 rtn = scsi_try_bus_device_reset(scmd); 2426 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) 2427 break; 2428 fallthrough; 2429 case SG_SCSI_RESET_TARGET: 2430 rtn = scsi_try_target_reset(scmd); 2431 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) 2432 break; 2433 fallthrough; 2434 case SG_SCSI_RESET_BUS: 2435 rtn = scsi_try_bus_reset(scmd); 2436 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) 2437 break; 2438 fallthrough; 2439 case SG_SCSI_RESET_HOST: 2440 rtn = scsi_try_host_reset(scmd); 2441 if (rtn == SUCCESS) 2442 break; 2443 fallthrough; 2444 default: 2445 rtn = FAILED; 2446 break; 2447 } 2448 2449 error = (rtn == SUCCESS) ? 0 : -EIO; 2450 2451 spin_lock_irqsave(shost->host_lock, flags); 2452 shost->tmf_in_progress = 0; 2453 spin_unlock_irqrestore(shost->host_lock, flags); 2454 2455 /* 2456 * be sure to wake up anyone who was sleeping or had their queue 2457 * suspended while we performed the TMF. 2458 */ 2459 SCSI_LOG_ERROR_RECOVERY(3, 2460 shost_printk(KERN_INFO, shost, 2461 "waking up host to restart after TMF\n")); 2462 2463 wake_up(&shost->host_wait); 2464 scsi_run_host_queues(shost); 2465 2466 kfree(rq); 2467 2468 out_put_autopm_host: 2469 scsi_autopm_put_host(shost); 2470 return error; 2471 } 2472 2473 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd, 2474 struct scsi_sense_hdr *sshdr) 2475 { 2476 return scsi_normalize_sense(cmd->sense_buffer, 2477 SCSI_SENSE_BUFFERSIZE, sshdr); 2478 } 2479 EXPORT_SYMBOL(scsi_command_normalize_sense); 2480 2481 /** 2482 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format) 2483 * @sense_buffer: byte array of sense data 2484 * @sb_len: number of valid bytes in sense_buffer 2485 * @info_out: pointer to 64 integer where 8 or 4 byte information 2486 * field will be placed if found. 2487 * 2488 * Return value: 2489 * true if information field found, false if not found. 2490 */ 2491 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len, 2492 u64 *info_out) 2493 { 2494 const u8 * ucp; 2495 2496 if (sb_len < 7) 2497 return false; 2498 switch (sense_buffer[0] & 0x7f) { 2499 case 0x70: 2500 case 0x71: 2501 if (sense_buffer[0] & 0x80) { 2502 *info_out = get_unaligned_be32(&sense_buffer[3]); 2503 return true; 2504 } 2505 return false; 2506 case 0x72: 2507 case 0x73: 2508 ucp = scsi_sense_desc_find(sense_buffer, sb_len, 2509 0 /* info desc */); 2510 if (ucp && (0xa == ucp[1])) { 2511 *info_out = get_unaligned_be64(&ucp[4]); 2512 return true; 2513 } 2514 return false; 2515 default: 2516 return false; 2517 } 2518 } 2519 EXPORT_SYMBOL(scsi_get_sense_info_fld); 2520