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