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