File Index
Files and directories
Sign in
FILE

scsi_error.c

File preview with syntax highlighting.

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