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