Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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