blob: cff7d9de79f2b70e1379202bc109dbed5883b6af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 *
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
Jesper Juhl0bf8c862011-03-21 20:47:31 +01006 * Leonard Zubkoff and David Miller at Linux Expo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * ideas originating from all over the place.
8 *
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
11 *
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
Jesper Juhl0bf8c862011-03-21 20:47:31 +010013 * minor cleanups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 */
16
17#include <linux/module.h>
18#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/timer.h>
21#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070023#include <linux/freezer.h>
Christoph Hellwigc5478de2005-09-06 14:04:26 +020024#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
26#include <linux/blkdev.h>
27#include <linux/delay.h>
Hannes Reineckefc736482013-04-25 08:10:00 +020028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020031#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -050034#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <scsi/scsi_eh.h>
Sagi Grimberg7708c162015-07-08 17:58:52 +030036#include <scsi/scsi_common.h>
James Smartc829c392006-03-13 08:28:57 -050037#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <scsi/scsi_host.h>
39#include <scsi/scsi_ioctl.h>
Christoph Hellwigee14c672015-08-27 14:16:59 +020040#include <scsi/scsi_dh.h>
Christoph Hellwig176aa9d2014-10-11 12:06:47 +020041#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "scsi_priv.h"
44#include "scsi_logging.h"
Adrian Bunk79ee8302007-08-10 14:50:42 -070045#include "scsi_transport_api.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Kei Tokunagabf816232010-04-01 20:41:40 +090047#include <trace/events/scsi.h>
48
James Bottomley14216562012-07-25 23:55:55 +040049static void scsi_eh_done(struct scsi_cmnd *scmd);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * These should *probably* be handled by the host itself.
53 * Since it is allowed to sleep, it probably should.
54 */
55#define BUS_RESET_SETTLE_TIME (10)
56#define HOST_RESET_SETTLE_TIME (10)
57
David Jeffery3eef6252011-05-19 14:41:12 -040058static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +010059static int scsi_try_to_abort_cmd(struct scsi_host_template *,
60 struct scsi_cmnd *);
David Jeffery3eef6252011-05-19 14:41:12 -040061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* called with shost->host_lock held */
63void scsi_eh_wakeup(struct Scsi_Host *shost)
64{
Christoph Hellwig74665012014-01-22 15:29:29 +010065 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090066 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -050067 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020068 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
69 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71}
Tejun Heof8bbfc22006-05-19 21:07:05 +090072
73/**
74 * scsi_schedule_eh - schedule EH for SCSI host
75 * @shost: SCSI host to invoke error handling on.
76 *
77 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080078 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090079void scsi_schedule_eh(struct Scsi_Host *shost)
80{
81 unsigned long flags;
82
83 spin_lock_irqsave(shost->host_lock, flags);
84
85 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
86 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
87 shost->host_eh_scheduled++;
88 scsi_eh_wakeup(shost);
89 }
90
91 spin_unlock_irqrestore(shost->host_lock, flags);
92}
93EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Hannes Reineckeb4562022013-10-23 10:51:21 +020095static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
96{
Ren Mingxinbb3b6212013-11-11 13:44:56 +010097 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +020098 return 0;
99
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100100 /*
101 * 32bit accesses are guaranteed to be atomic
102 * (on all supported architectures), so instead
103 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100104 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100105 * time_before call.
106 */
107 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100108 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200109 return 0;
110
111 return 1;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100115 * scmd_eh_abort_handler - Handle command aborts
116 * @work: command to be aborted.
117 */
118void
119scmd_eh_abort_handler(struct work_struct *work)
120{
121 struct scsi_cmnd *scmd =
122 container_of(work, struct scsi_cmnd, abort_work.work);
123 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100124 int rtn;
125
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100126 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100127 SCSI_LOG_ERROR_RECOVERY(3,
128 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100129 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100130 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100131 SCSI_LOG_ERROR_RECOVERY(3,
132 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100133 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100134 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
135 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200136 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100137 if (scsi_host_eh_past_deadline(sdev->host)) {
138 SCSI_LOG_ERROR_RECOVERY(3,
139 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100140 "eh timeout, not retrying "
141 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100142 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100143 (++scmd->retries <= scmd->allowed)) {
144 SCSI_LOG_ERROR_RECOVERY(3,
145 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100146 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100147 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100148 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100149 } else {
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100152 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100153 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100154 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100155 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100156 } else {
157 SCSI_LOG_ERROR_RECOVERY(3,
158 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100159 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200160 (rtn == FAST_IO_FAIL) ?
161 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100162 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100163 }
164
165 if (!scsi_eh_scmd_add(scmd, 0)) {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100168 "terminate aborted command\n"));
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200169 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100170 scsi_finish_command(scmd);
171 }
172}
173
174/**
175 * scsi_abort_command - schedule a command abort
176 * @scmd: scmd to abort.
177 *
178 * We only need to abort commands after a command timeout
179 */
180static int
181scsi_abort_command(struct scsi_cmnd *scmd)
182{
183 struct scsi_device *sdev = scmd->device;
184 struct Scsi_Host *shost = sdev->host;
185 unsigned long flags;
186
187 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
188 /*
189 * Retry after abort failed, escalate to next level.
190 */
Hannes Reinecke7daf4802014-03-31 16:37:34 +0200191 scmd->eh_eflags &= ~SCSI_EH_ABORT_SCHEDULED;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100192 SCSI_LOG_ERROR_RECOVERY(3,
193 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100194 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200195 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100196 return FAILED;
197 }
198
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100199 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100200 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100201 shost->last_reset = jiffies;
202 spin_unlock_irqrestore(shost->host_lock, flags);
203
204 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
205 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100206 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100207 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
208 return SUCCESS;
209}
210
211/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200212 * scsi_eh_reset - call into ->eh_action to reset internal counters
213 * @scmd: scmd to run eh on.
214 *
215 * The scsi driver might be carrying internal state about the
216 * devices, so we need to call into the driver to reset the
217 * internal state once the error handler is started.
218 */
219static void scsi_eh_reset(struct scsi_cmnd *scmd)
220{
221 if (!blk_rq_is_passthrough(scmd->request)) {
222 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
223 if (sdrv->eh_reset)
224 sdrv->eh_reset(scmd);
225 }
226}
227
228/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * scsi_eh_scmd_add - add scsi cmd to error handling.
230 * @scmd: scmd to run eh on.
231 * @eh_flag: optional SCSI_EH flag.
232 *
233 * Return value:
234 * 0 on failure.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
237{
238 struct Scsi_Host *shost = scmd->device->host;
239 unsigned long flags;
James Bottomley939647e2005-09-18 15:05:20 -0500240 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
James Bottomley3ed7a472005-09-19 09:50:04 -0500242 if (!shost->ehandler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244
245 spin_lock_irqsave(shost->host_lock, flags);
James Bottomley939647e2005-09-18 15:05:20 -0500246 if (scsi_host_set_state(shost, SHOST_RECOVERY))
247 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
248 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100250 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200251 shost->last_reset = jiffies;
252
James Bottomley939647e2005-09-18 15:05:20 -0500253 ret = 1;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100254 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
255 eh_flag &= ~SCSI_EH_CANCEL_CMD;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200256 scmd->eh_eflags |= eh_flag;
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200257 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 shost->host_failed++;
260 scsi_eh_wakeup(shost);
James Bottomley939647e2005-09-18 15:05:20 -0500261 out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomley939647e2005-09-18 15:05:20 -0500263 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700268 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
270 * Notes:
271 * We do not need to lock this. There is the potential for a race
272 * only in that the normal completion handling might run, but if the
273 * normal completion function determines that the timer has already
274 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800275 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700276enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700278 struct scsi_cmnd *scmd = req->special;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700279 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100280 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500281
Kei Tokunagabf816232010-04-01 20:41:40 +0900282 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 scsi_log_completion(scmd, TIMEOUT_ERROR);
284
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100285 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200286 host->last_reset = jiffies;
287
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100288 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100289 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500290
Hannes Reineckea33c0702014-06-13 14:01:45 +0200291 if (rtn == BLK_EH_NOT_HANDLED) {
292 if (!host->hostt->no_async_abort &&
293 scsi_abort_command(scmd) == SUCCESS)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100294 return BLK_EH_NOT_HANDLED;
295
Hannes Reineckea33c0702014-06-13 14:01:45 +0200296 set_host_byte(scmd, DID_TIME_OUT);
297 if (!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))
298 rtn = BLK_EH_HANDLED;
299 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700300
Christoph Hellwigfa990782008-11-05 15:03:15 +0100301 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/**
305 * scsi_block_when_processing_errors - Prevent cmds from being queued.
306 * @sdev: Device on which we are performing recovery.
307 *
308 * Description:
309 * We block until the host is out of error recovery, and then check to
310 * see whether the host or the device is offline.
311 *
312 * Return value:
313 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315int scsi_block_when_processing_errors(struct scsi_device *sdev)
316{
317 int online;
318
James Bottomley939647e2005-09-18 15:05:20 -0500319 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 online = scsi_device_online(sdev);
322
Hannes Reinecke91921e02014-06-25 16:39:59 +0200323 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
324 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 return online;
327}
328EXPORT_SYMBOL(scsi_block_when_processing_errors);
329
330#ifdef CONFIG_SCSI_LOGGING
331/**
332 * scsi_eh_prt_fail_stats - Log info on failures.
333 * @shost: scsi host being recovered.
334 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
337 struct list_head *work_q)
338{
339 struct scsi_cmnd *scmd;
340 struct scsi_device *sdev;
341 int total_failures = 0;
342 int cmd_failed = 0;
343 int cmd_cancel = 0;
344 int devices_failed = 0;
345
346 shost_for_each_device(sdev, shost) {
347 list_for_each_entry(scmd, work_q, eh_entry) {
348 if (scmd->device == sdev) {
349 ++total_failures;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200350 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100352 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ++cmd_failed;
354 }
355 }
356
357 if (cmd_cancel || cmd_failed) {
358 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200359 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500360 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700361 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500362 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 cmd_cancel = 0;
364 cmd_failed = 0;
365 ++devices_failed;
366 }
367 }
368
Hannes Reinecke91921e02014-06-25 16:39:59 +0200369 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
370 "Total of %d commands on %d"
371 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100372 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374#endif
375
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400376 /**
377 * scsi_report_lun_change - Set flag on all *other* devices on the same target
378 * to indicate that a UNIT ATTENTION is expected.
379 * @sdev: Device reporting the UNIT ATTENTION
380 */
381static void scsi_report_lun_change(struct scsi_device *sdev)
382{
383 sdev->sdev_target->expecting_lun_change = 1;
384}
385
386/**
387 * scsi_report_sense - Examine scsi sense information and log messages for
388 * certain conditions, also issue uevents for some of them.
389 * @sdev: Device reporting the sense code
390 * @sshdr: sshdr to be examined
391 */
392static void scsi_report_sense(struct scsi_device *sdev,
393 struct scsi_sense_hdr *sshdr)
394{
395 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
396
397 if (sshdr->sense_key == UNIT_ATTENTION) {
398 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
399 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
400 sdev_printk(KERN_WARNING, sdev,
401 "Inquiry data has changed");
402 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
403 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
404 scsi_report_lun_change(sdev);
405 sdev_printk(KERN_WARNING, sdev,
406 "Warning! Received an indication that the "
407 "LUN assignments on this target have "
408 "changed. The Linux SCSI layer does not "
409 "automatically remap LUN assignments.\n");
410 } else if (sshdr->asc == 0x3f)
411 sdev_printk(KERN_WARNING, sdev,
412 "Warning! Received an indication that the "
413 "operating parameters on this target have "
414 "changed. The Linux SCSI layer does not "
415 "automatically adjust these parameters.\n");
416
417 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
418 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
419 sdev_printk(KERN_WARNING, sdev,
420 "Warning! Received an indication that the "
421 "LUN reached a thin provisioning soft "
422 "threshold.\n");
423 }
424
425 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
426 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
427 sdev_printk(KERN_WARNING, sdev,
428 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200429 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
430 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
431 sdev_printk(KERN_WARNING, sdev,
432 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400433 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
434 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
435 sdev_printk(KERN_WARNING, sdev,
436 "Capacity data has changed");
437 } else if (sshdr->asc == 0x2a)
438 sdev_printk(KERN_WARNING, sdev,
439 "Parameters changed");
440 }
441
442 if (evt_type != SDEV_EVT_MAXBITS) {
443 set_bit(evt_type, sdev->pending_events);
444 schedule_work(&sdev->event_work);
445 }
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/**
449 * scsi_check_sense - Examine scsi cmd sense
450 * @scmd: Cmd to have sense checked.
451 *
452 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200453 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *
455 * Notes:
456 * When a deferred error is detected the current command has
457 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800458 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200459int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700461 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct scsi_sense_hdr sshdr;
463
464 if (! scsi_command_normalize_sense(scmd, &sshdr))
465 return FAILED; /* no valid sense data */
466
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400467 scsi_report_sense(sdev, &sshdr);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (scsi_sense_is_deferred(&sshdr))
470 return NEEDS_RETRY;
471
Christoph Hellwigee14c672015-08-27 14:16:59 +0200472 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700473 int rc;
474
Christoph Hellwigee14c672015-08-27 14:16:59 +0200475 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700476 if (rc != SCSI_RETURN_NOT_HANDLED)
477 return rc;
478 /* handler does not care. Drop down to default handling */
479 }
480
Christoph Hellwige925cc42014-11-06 15:11:22 -0600481 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
482 /*
483 * nasty: for mid-layer issued TURs, we need to return the
484 * actual sense data without any recovery attempt. For eh
485 * issued ones, we need to try to recover and interpret
486 */
487 return SUCCESS;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Previous logic looked for FILEMARK, EOM or ILI which are
491 * mainly associated with tapes and returned SUCCESS.
492 */
493 if (sshdr.response_code == 0x70) {
494 /* fixed format */
495 if (scmd->sense_buffer[2] & 0xe0)
496 return SUCCESS;
497 } else {
498 /*
499 * descriptor format: look for "stream commands sense data
500 * descriptor" (see SSC-3). Assume single sense data
501 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
502 */
503 if ((sshdr.additional_length > 3) &&
504 (scmd->sense_buffer[8] == 0x4) &&
505 (scmd->sense_buffer[11] & 0xe0))
506 return SUCCESS;
507 }
508
509 switch (sshdr.sense_key) {
510 case NO_SENSE:
511 return SUCCESS;
512 case RECOVERED_ERROR:
513 return /* soft_error */ SUCCESS;
514
515 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400516 if (sshdr.asc == 0x10) /* DIF */
517 return SUCCESS;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return NEEDS_RETRY;
520 case NOT_READY:
521 case UNIT_ATTENTION:
522 /*
523 * if we are expecting a cc/ua because of a bus reset that we
524 * performed, treat this just as a retry. otherwise this is
525 * information that we should pass up to the upper-level driver
526 * so that we can deal with it there.
527 */
528 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900529 /*
530 * Because some device does not queue unit
531 * attentions correctly, we carefully check
532 * additional sense code and qualifier so as
533 * not to squash media change unit attention.
534 */
535 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
536 scmd->device->expecting_cc_ua = 0;
537 return NEEDS_RETRY;
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400541 * we might also expect a cc/ua if another LUN on the target
542 * reported a UA with an ASC/ASCQ of 3F 0E -
543 * REPORTED LUNS DATA HAS CHANGED.
544 */
545 if (scmd->device->sdev_target->expecting_lun_change &&
546 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
547 return NEEDS_RETRY;
548 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100549 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * should retry.
551 */
552 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
553 return NEEDS_RETRY;
554 /*
555 * if the device is not started, we need to wake
556 * the error handler to start the motor
557 */
558 if (scmd->device->allow_restart &&
559 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
560 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100561 /*
562 * Pass the UA upwards for a determination in the completion
563 * functions.
564 */
565 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100567 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200568 case DATA_PROTECT:
569 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
570 /* Thin provisioning hard threshold reached */
571 set_host_byte(scmd, DID_ALLOC_FAILURE);
572 return SUCCESS;
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 case COPY_ABORTED:
575 case VOLUME_OVERFLOW:
576 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100577 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200578 set_host_byte(scmd, DID_TARGET_FAILURE);
579 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800582 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
583 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
584 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200585 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200586 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return NEEDS_RETRY;
589
590 case HARDWARE_ERROR:
591 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700592 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200594 set_host_byte(scmd, DID_TARGET_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500597 if (sshdr.asc == 0x20 || /* Invalid command operation code */
598 sshdr.asc == 0x21 || /* Logical block address out of range */
599 sshdr.asc == 0x24 || /* Invalid field in cdb */
600 sshdr.asc == 0x26) { /* Parameter value invalid */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200601 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500602 }
603 return SUCCESS;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 default:
606 return SUCCESS;
607 }
608}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200609EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Vasu Dev4a840672009-10-22 15:46:33 -0700611static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
612{
613 struct scsi_host_template *sht = sdev->host->hostt;
614 struct scsi_device *tmp_sdev;
615
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100616 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700617 sdev->queue_depth >= sdev->max_queue_depth)
618 return;
619
620 if (time_before(jiffies,
621 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
622 return;
623
624 if (time_before(jiffies,
625 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
626 return;
627
628 /*
629 * Walk all devices of a target and do
630 * ramp up on them.
631 */
632 shost_for_each_device(tmp_sdev, sdev->host) {
633 if (tmp_sdev->channel != sdev->channel ||
634 tmp_sdev->id != sdev->id ||
635 tmp_sdev->queue_depth == sdev->max_queue_depth)
636 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100637
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100638 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700639 sdev->last_queue_ramp_up = jiffies;
640 }
641}
642
Mike Christie42a6a912009-10-15 17:46:44 -0700643static void scsi_handle_queue_full(struct scsi_device *sdev)
644{
645 struct scsi_host_template *sht = sdev->host->hostt;
646 struct scsi_device *tmp_sdev;
647
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100648 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700649 return;
650
651 shost_for_each_device(tmp_sdev, sdev->host) {
652 if (tmp_sdev->channel != sdev->channel ||
653 tmp_sdev->id != sdev->id)
654 continue;
655 /*
656 * We do not know the number of commands that were at
657 * the device when we got the queue full so we start
658 * from the highest possible value and work our way down.
659 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100660 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700661 }
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/**
665 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
666 * @scmd: SCSI cmd to examine.
667 *
668 * Notes:
669 * This is *only* called when we are examining the status of commands
670 * queued during error recovery. the main difference here is that we
671 * don't allow for the possibility of retries here, and we are a lot
672 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
675{
676 /*
677 * first check the host byte, to see if there is anything in there
678 * that would indicate what we need to do.
679 */
680 if (host_byte(scmd->result) == DID_RESET) {
681 /*
682 * rats. we are already in the error handler, so we now
683 * get to try and figure out what to do next. if the sense
684 * is valid, we have a pretty good idea of what to do.
685 * if not, we mark it as FAILED.
686 */
687 return scsi_check_sense(scmd);
688 }
689 if (host_byte(scmd->result) != DID_OK)
690 return FAILED;
691
692 /*
693 * next, check the message byte.
694 */
695 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
696 return FAILED;
697
698 /*
699 * now, check the status byte to see if this indicates
700 * anything special.
701 */
702 switch (status_byte(scmd->result)) {
703 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700704 scsi_handle_queue_ramp_up(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case COMMAND_TERMINATED:
706 return SUCCESS;
707 case CHECK_CONDITION:
708 return scsi_check_sense(scmd);
709 case CONDITION_GOOD:
710 case INTERMEDIATE_GOOD:
711 case INTERMEDIATE_C_GOOD:
712 /*
713 * who knows? FIXME(eric)
714 */
715 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500716 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500717 if (scmd->cmnd[0] == TEST_UNIT_READY)
718 /* it is a success, we probed the device and
719 * found it */
720 return SUCCESS;
721 /* otherwise, we failed to send the command */
722 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700724 scsi_handle_queue_full(scmd->device);
725 /* fall through */
726 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200727 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 default:
729 return FAILED;
730 }
731 return FAILED;
732}
733
734/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * scsi_eh_done - Completion function for error handling.
736 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static void scsi_eh_done(struct scsi_cmnd *scmd)
739{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100740 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600741
Hannes Reinecke91921e02014-06-25 16:39:59 +0200742 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100743 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600744
745 eh_action = scmd->device->host->eh_action;
746 if (eh_action)
747 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750/**
Brian King292148f2007-01-30 17:51:17 -0600751 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200752 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800753 */
Brian King292148f2007-01-30 17:51:17 -0600754static int scsi_try_host_reset(struct scsi_cmnd *scmd)
755{
756 unsigned long flags;
757 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100758 struct Scsi_Host *host = scmd->device->host;
759 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600760
Hannes Reinecke91921e02014-06-25 16:39:59 +0200761 SCSI_LOG_ERROR_RECOVERY(3,
762 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600763
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100764 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600765 return FAILED;
766
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100767 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600768
769 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100770 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600771 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100772 spin_lock_irqsave(host->host_lock, flags);
773 scsi_report_bus_reset(host, scmd_channel(scmd));
774 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600775 }
776
777 return rtn;
778}
779
780/**
781 * scsi_try_bus_reset - ask host to perform a bus reset
782 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800783 */
Brian King292148f2007-01-30 17:51:17 -0600784static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
785{
786 unsigned long flags;
787 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100788 struct Scsi_Host *host = scmd->device->host;
789 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600790
Hannes Reinecke91921e02014-06-25 16:39:59 +0200791 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
792 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600793
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100794 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600795 return FAILED;
796
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100797 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600798
799 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100800 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600801 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100802 spin_lock_irqsave(host->host_lock, flags);
803 scsi_report_bus_reset(host, scmd_channel(scmd));
804 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600805 }
806
807 return rtn;
808}
809
Mike Christie30bd7df2008-02-29 18:25:19 -0600810static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
811{
812 sdev->was_reset = 1;
813 sdev->expecting_cc_ua = 1;
814}
815
816/**
817 * scsi_try_target_reset - Ask host to perform a target reset
818 * @scmd: SCSI cmd used to send a target reset
819 *
820 * Notes:
821 * There is no timeout for this operation. if this operation is
822 * unreliable for a given host, then the host itself needs to put a
823 * timer on it, and set the host back to a consistent state prior to
824 * returning.
825 */
826static int scsi_try_target_reset(struct scsi_cmnd *scmd)
827{
828 unsigned long flags;
829 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100830 struct Scsi_Host *host = scmd->device->host;
831 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600832
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100833 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600834 return FAILED;
835
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100836 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600837 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100838 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600839 __starget_for_each_device(scsi_target(scmd->device), NULL,
840 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100841 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600842 }
843
844 return rtn;
845}
846
Brian King292148f2007-01-30 17:51:17 -0600847/**
848 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
849 * @scmd: SCSI cmd used to send BDR
850 *
851 * Notes:
852 * There is no timeout for this operation. if this operation is
853 * unreliable for a given host, then the host itself needs to put a
854 * timer on it, and set the host back to a consistent state prior to
855 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800856 */
Brian King292148f2007-01-30 17:51:17 -0600857static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
858{
859 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100860 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600861
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100862 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600863 return FAILED;
864
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100865 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600866 if (rtn == SUCCESS)
867 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600868 return rtn;
869}
870
Hannes Reinecke883a0302014-10-24 14:27:04 +0200871/**
872 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800873 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200874 * @scmd: SCSI cmd used to send a target reset
875 *
876 * Return value:
877 * SUCCESS, FAILED, or FAST_IO_FAIL
878 *
879 * Notes:
880 * SUCCESS does not necessarily indicate that the command
881 * has been aborted; it only indicates that the LLDDs
882 * has cleared all references to that command.
883 * LLDDs should return FAILED only if an abort was required
884 * but could not be executed. LLDDs should return FAST_IO_FAIL
885 * if the device is temporarily unavailable (eg due to a
886 * link down on FibreChannel)
887 */
888static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
889 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600890{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100891 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600892 return FAILED;
893
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100894 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600895}
896
Brian King292148f2007-01-30 17:51:17 -0600897static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
898{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100899 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600900 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600901 if (scsi_try_target_reset(scmd) != SUCCESS)
902 if (scsi_try_bus_reset(scmd) != SUCCESS)
903 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600904}
905
906/**
Santosh Y3b729f72012-04-08 18:47:09 +0530907 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100908 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200909 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200910 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300911 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200912 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100913 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200914 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200915 * as part of the error recovery process. If @sense_bytes is 0 the command
916 * sent must be one that does not transfer any data. If @sense_bytes != 0
917 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
918 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800919 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200920void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
921 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
f59114b2005-04-17 15:00:23 -0500923 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Christoph Hellwig631c2282006-07-08 20:42:15 +0200925 /*
926 * We need saved copies of a number of fields - this is because
927 * error handling may need to overwrite these with different values
928 * to run different commands, and once error handling is complete,
929 * we will need to restore these values prior to running the actual
930 * command.
931 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200932 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300933 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200934 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200935 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200936 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200937 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400938 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400939 ses->prot_op = scmd->prot_op;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200940
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400941 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700942 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300943 scmd->cmnd = ses->eh_cmnd;
944 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200945 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200946 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700947 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200948
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200949 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200950 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
951 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200952 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200953 scmd->sdb.length);
954 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200955 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400956 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200957 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200958 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200959 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200960 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200961 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200962 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300963 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200964 memcpy(scmd->cmnd, cmnd, cmnd_size);
965 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
966 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200967 }
968
969 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200970
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200971 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500973 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Christoph Hellwig631c2282006-07-08 20:42:15 +0200975 /*
976 * Zero the sense buffer. The scsi spec mandates that any
977 * untransferred sense data should be interpreted as being zero.
978 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900979 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200980}
981EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200982
Boaz Harroshe1c23462007-10-08 16:36:45 +0200983/**
Santosh Y3b729f72012-04-08 18:47:09 +0530984 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200985 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200986 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +0200987 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200988 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800989 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200990void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
991{
992 /*
993 * Restore original data
994 */
995 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300996 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200997 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200998 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200999 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001000 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001001 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001002 scmd->prot_op = ses->prot_op;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001003}
1004EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1005
1006/**
Santosh Y3b729f72012-04-08 18:47:09 +05301007 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001008 * @scmd: SCSI command structure to hijack
1009 * @cmnd: CDB to send
1010 * @cmnd_size: size in bytes of @cmnd
1011 * @timeout: timeout for this request
1012 * @sense_bytes: size of sense data to copy or 0
1013 *
1014 * This function is used to send a scsi command down to a target device
1015 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1016 *
1017 * Return value:
1018 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001019 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001020static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1021 int cmnd_size, int timeout, unsigned sense_bytes)
1022{
1023 struct scsi_device *sdev = scmd->device;
1024 struct Scsi_Host *shost = sdev->host;
1025 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001026 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001027 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001028 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001029 int rtn;
1030
Hannes Reineckefc736482013-04-25 08:10:00 +02001031retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001032 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001033 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001036 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001037 rtn = shost->hostt->queuecommand(shost, scmd);
1038 if (rtn) {
1039 if (timeleft > stall_for) {
1040 scsi_eh_restore_cmnd(scmd, &ses);
1041 timeleft -= stall_for;
1042 msleep(jiffies_to_msecs(stall_for));
1043 goto retry;
1044 }
1045 /* signal not to enter either branch of the if () below */
1046 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001047 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001048 } else {
1049 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001050 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
f59114b2005-04-17 15:00:23 -05001053 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Hannes Reineckefc736482013-04-25 08:10:00 +02001055 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Hannes Reinecke91921e02014-06-25 16:39:59 +02001057 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001058 "%s timeleft: %ld\n",
1059 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001062 * If there is time left scsi_eh_done got called, and we will examine
1063 * the actual status codes to see whether the command actually did
1064 * complete normally, else if we have a zero return and no time left,
1065 * the command must still be pending, so abort it and return FAILED.
1066 * If we never actually managed to issue the command, because
1067 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1068 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001070 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001072 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1073 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 switch (rtn) {
1076 case SUCCESS:
1077 case NEEDS_RETRY:
1078 case FAILED:
1079 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001080 case ADD_TO_MLQUEUE:
1081 rtn = NEEDS_RETRY;
1082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 default:
1084 rtn = FAILED;
1085 break;
1086 }
Alan Stern511833a2014-11-21 10:44:49 -05001087 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001088 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001089 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
Boaz Harroshe1c23462007-10-08 16:36:45 +02001092 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return rtn;
1095}
1096
1097/**
1098 * scsi_request_sense - Request sense data from a particular target.
1099 * @scmd: SCSI cmd for request sense.
1100 *
1101 * Notes:
1102 * Some hosts automatically obtain this information, others require
1103 * that we obtain it on our own. This function will *not* return until
1104 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static int scsi_request_sense(struct scsi_cmnd *scmd)
1107{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001108 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
James Bottomley24510792013-11-11 13:44:53 +01001111static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1112{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001113 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001114 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1115 if (sdrv->eh_action)
1116 rtn = sdrv->eh_action(scmd, rtn);
1117 }
1118 return rtn;
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/**
1122 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1123 * @scmd: Original SCSI cmd that eh has finished.
1124 * @done_q: Queue for processed commands.
1125 *
1126 * Notes:
1127 * We don't want to use the normal command completion while we are are
1128 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001129 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * keep a list of pending commands for final completion, and once we
1131 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001132 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001133void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001135 scmd->eh_eflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 list_move_tail(&scmd->eh_entry, done_q);
1137}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001138EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140/**
1141 * scsi_eh_get_sense - Get device sense data.
1142 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001143 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 *
1145 * Description:
1146 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001147 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 *
1149 * Notes:
1150 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001151 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * it down before we request it.
1153 *
1154 * All drivers should request sense information internally these days,
1155 * so for now all I have to say is tough noogies if you end up in here.
1156 *
1157 * XXX: Long term this code should go away, but that needs an audit of
1158 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001159 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001160int scsi_eh_get_sense(struct list_head *work_q,
1161 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001163 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001164 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int rtn;
1166
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001167 /*
1168 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1169 * should not get sense.
1170 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001171 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001172 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001173 (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 SCSI_SENSE_VALID(scmd))
1175 continue;
1176
Hannes Reineckeb4562022013-10-23 10:51:21 +02001177 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001178 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001179 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001180 scmd_printk(KERN_INFO, scmd,
1181 "%s: skip request sense, past eh deadline\n",
1182 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001183 break;
1184 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001185 if (status_byte(scmd->result) != CHECK_CONDITION)
1186 /*
1187 * don't request sense if there's no check condition
1188 * status because the error we're processing isn't one
1189 * that has a sense code (and some devices get
1190 * confused by sense requests out of the blue)
1191 */
1192 continue;
1193
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001194 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1195 "%s: requesting sense\n",
1196 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 rtn = scsi_request_sense(scmd);
1198 if (rtn != SUCCESS)
1199 continue;
1200
Hannes Reinecke91921e02014-06-25 16:39:59 +02001201 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001202 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001203 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 rtn = scsi_decide_disposition(scmd);
1206
1207 /*
1208 * if the result was normal, then just pass it along to the
1209 * upper level.
1210 */
1211 if (rtn == SUCCESS)
1212 /* we don't want this command reissued, just
1213 * finished with the sense data, so set
1214 * retries to the max allowed to ensure it
1215 * won't get reissued */
1216 scmd->retries = scmd->allowed;
1217 else if (rtn != NEEDS_RETRY)
1218 continue;
1219
1220 scsi_eh_finish_cmd(scmd, done_q);
1221 }
1222
1223 return list_empty(work_q);
1224}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001225EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001229 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 *
1231 * Return value:
1232 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234static int scsi_eh_tur(struct scsi_cmnd *scmd)
1235{
1236 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1237 int retry_cnt = 1, rtn;
1238
1239retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001240 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1241 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Hannes Reinecke91921e02014-06-25 16:39:59 +02001243 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001244 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001245
1246 switch (rtn) {
1247 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (retry_cnt--)
1249 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001250 /*FALLTHRU*/
1251 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001252 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001253 default:
1254 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258/**
David Jeffery3eef6252011-05-19 14:41:12 -04001259 * scsi_eh_test_devices - check if devices are responding from error recovery.
1260 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001261 * @work_q: queue for commands which still need more error recovery
1262 * @done_q: queue for commands which are finished
1263 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001264 *
1265 * Decription:
1266 * Tests if devices are in a working state. Commands to devices now in
1267 * a working state are sent to the done_q while commands to devices which
1268 * are still failing to respond are returned to the work_q for more
1269 * processing.
1270 **/
1271static int scsi_eh_test_devices(struct list_head *cmd_list,
1272 struct list_head *work_q,
1273 struct list_head *done_q, int try_stu)
1274{
1275 struct scsi_cmnd *scmd, *next;
1276 struct scsi_device *sdev;
1277 int finish_cmds;
1278
1279 while (!list_empty(cmd_list)) {
1280 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1281 sdev = scmd->device;
1282
Hannes Reineckeb4562022013-10-23 10:51:21 +02001283 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001284 if (scsi_host_eh_past_deadline(sdev->host)) {
1285 /* Push items back onto work_q */
1286 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001287 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001288 sdev_printk(KERN_INFO, sdev,
1289 "%s: skip test device, past eh deadline",
1290 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001291 break;
1292 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001293 }
1294
David Jeffery3eef6252011-05-19 14:41:12 -04001295 finish_cmds = !scsi_device_online(scmd->device) ||
1296 (try_stu && !scsi_eh_try_stu(scmd) &&
1297 !scsi_eh_tur(scmd)) ||
1298 !scsi_eh_tur(scmd);
1299
1300 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1301 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001302 if (finish_cmds &&
1303 (try_stu ||
1304 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001305 scsi_eh_finish_cmd(scmd, done_q);
1306 else
1307 list_move_tail(&scmd->eh_entry, work_q);
1308 }
1309 }
1310 return list_empty(work_q);
1311}
1312
1313
1314/**
Rob Landleyeb448202007-11-03 13:30:39 -05001315 * scsi_eh_abort_cmds - abort pending commands.
1316 * @work_q: &list_head for pending commands.
1317 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 *
1319 * Decription:
1320 * Try and see whether or not it makes sense to try and abort the
Rob Landleyeb448202007-11-03 13:30:39 -05001321 * running command. This only works out to be the case if we have one
1322 * command that has timed out. If the command simply failed, it makes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 * no sense to try and abort the command, since as far as the shost
1324 * adapter is concerned, it isn't running.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326static int scsi_eh_abort_cmds(struct list_head *work_q,
1327 struct list_head *done_q)
1328{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001329 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001330 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int rtn;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001332 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001334 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001335 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 continue;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001337 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001338 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001339 list_splice_init(&check_list, work_q);
1340 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001341 scmd_printk(KERN_INFO, scmd,
1342 "%s: skip aborting cmd, past eh deadline\n",
1343 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001344 return list_empty(work_q);
1345 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001346 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001347 scmd_printk(KERN_INFO, scmd,
1348 "%s: aborting cmd\n", current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001349 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
Hannes Reinecke6fd046f2013-10-23 10:51:22 +02001350 if (rtn == FAILED) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001351 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001352 scmd_printk(KERN_INFO, scmd,
1353 "%s: aborting cmd failed\n",
1354 current->comm));
Hannes Reinecke6fd046f2013-10-23 10:51:22 +02001355 list_splice_init(&check_list, work_q);
1356 return list_empty(work_q);
1357 }
1358 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
1359 if (rtn == FAST_IO_FAIL)
1360 scsi_eh_finish_cmd(scmd, done_q);
1361 else
1362 list_move_tail(&scmd->eh_entry, &check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
David Jeffery3eef6252011-05-19 14:41:12 -04001365 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001370 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 *
1372 * Return value:
1373 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1376{
1377 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Christoph Hellwig631c2282006-07-08 20:42:15 +02001379 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001380 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Brian Kinged773e62007-03-29 15:25:52 -05001382 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001383 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001384
Christoph Hellwig631c2282006-07-08 20:42:15 +02001385 if (rtn == SUCCESS)
1386 return 0;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 1;
1390}
1391
1392 /**
1393 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001394 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001395 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001396 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 *
1398 * Notes:
1399 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001400 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static int scsi_eh_stu(struct Scsi_Host *shost,
1403 struct list_head *work_q,
1404 struct list_head *done_q)
1405{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001406 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct scsi_device *sdev;
1408
1409 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001410 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001411 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001412 sdev_printk(KERN_INFO, sdev,
1413 "%s: skip START_UNIT, past eh deadline\n",
1414 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001415 break;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 stu_scmd = NULL;
1418 list_for_each_entry(scmd, work_q, eh_entry)
1419 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1420 scsi_check_sense(scmd) == FAILED ) {
1421 stu_scmd = scmd;
1422 break;
1423 }
1424
1425 if (!stu_scmd)
1426 continue;
1427
Hannes Reinecke91921e02014-06-25 16:39:59 +02001428 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001429 sdev_printk(KERN_INFO, sdev,
1430 "%s: Sending START_UNIT\n",
1431 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (!scsi_eh_try_stu(stu_scmd)) {
1434 if (!scsi_device_online(sdev) ||
1435 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001436 list_for_each_entry_safe(scmd, next,
1437 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001438 if (scmd->device == sdev &&
1439 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 scsi_eh_finish_cmd(scmd, done_q);
1441 }
1442 }
1443 } else {
1444 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001445 sdev_printk(KERN_INFO, sdev,
1446 "%s: START_UNIT failed\n",
1447 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 }
1450
1451 return list_empty(work_q);
1452}
1453
1454
1455/**
1456 * scsi_eh_bus_device_reset - send bdr if needed
1457 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001458 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001459 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
1461 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001462 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * devices that are jammed or not - if we have multiple devices, it
1464 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001465 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1468 struct list_head *work_q,
1469 struct list_head *done_q)
1470{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001471 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 struct scsi_device *sdev;
1473 int rtn;
1474
1475 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001476 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001477 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001478 sdev_printk(KERN_INFO, sdev,
1479 "%s: skip BDR, past eh deadline\n",
1480 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001481 break;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 bdr_scmd = NULL;
1484 list_for_each_entry(scmd, work_q, eh_entry)
1485 if (scmd->device == sdev) {
1486 bdr_scmd = scmd;
1487 break;
1488 }
1489
1490 if (!bdr_scmd)
1491 continue;
1492
Hannes Reinecke91921e02014-06-25 16:39:59 +02001493 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001494 sdev_printk(KERN_INFO, sdev,
1495 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001497 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001499 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001501 list_for_each_entry_safe(scmd, next,
1502 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001503 if (scmd->device == sdev &&
1504 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 scsi_eh_finish_cmd(scmd,
1506 done_q);
1507 }
1508 }
1509 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001510 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001511 sdev_printk(KERN_INFO, sdev,
1512 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 }
1515
1516 return list_empty(work_q);
1517}
1518
1519/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001520 * scsi_eh_target_reset - send target reset if needed
1521 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001522 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001523 * @done_q: &list_head for processed commands.
1524 *
1525 * Notes:
1526 * Try a target reset.
1527 */
1528static int scsi_eh_target_reset(struct Scsi_Host *shost,
1529 struct list_head *work_q,
1530 struct list_head *done_q)
1531{
James Bottomley98db5192010-10-25 15:53:41 -05001532 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001533 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001534
James Bottomley98db5192010-10-25 15:53:41 -05001535 list_splice_init(work_q, &tmp_list);
1536
1537 while (!list_empty(&tmp_list)) {
1538 struct scsi_cmnd *next, *scmd;
1539 int rtn;
1540 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001541
Hannes Reineckeb4562022013-10-23 10:51:21 +02001542 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001543 /* push back on work queue for further processing */
1544 list_splice_init(&check_list, work_q);
1545 list_splice_init(&tmp_list, work_q);
1546 SCSI_LOG_ERROR_RECOVERY(3,
1547 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001548 "%s: Skip target reset, past eh deadline\n",
1549 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001550 return list_empty(work_q);
1551 }
James Bottomley98db5192010-10-25 15:53:41 -05001552
1553 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1554 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001555
Hannes Reinecke91921e02014-06-25 16:39:59 +02001556 SCSI_LOG_ERROR_RECOVERY(3,
1557 shost_printk(KERN_INFO, shost,
1558 "%s: Sending target reset to target %d\n",
1559 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001560 rtn = scsi_try_target_reset(scmd);
1561 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001562 SCSI_LOG_ERROR_RECOVERY(3,
1563 shost_printk(KERN_INFO, shost,
1564 "%s: Target reset failed"
1565 " target: %d\n",
1566 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001567 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1568 if (scmd_id(scmd) != id)
1569 continue;
1570
David Jeffery3eef6252011-05-19 14:41:12 -04001571 if (rtn == SUCCESS)
1572 list_move_tail(&scmd->eh_entry, &check_list);
1573 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001574 scsi_eh_finish_cmd(scmd, done_q);
1575 else
1576 /* push back on work queue for further processing */
1577 list_move(&scmd->eh_entry, work_q);
1578 }
1579 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001580
David Jeffery3eef6252011-05-19 14:41:12 -04001581 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001582}
1583
1584/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001585 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001586 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001587 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001588 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1591 struct list_head *work_q,
1592 struct list_head *done_q)
1593{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001594 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001595 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 unsigned int channel;
1597 int rtn;
1598
1599 /*
1600 * we really want to loop over the various channels, and do this on
1601 * a channel by channel basis. we should also check to see if any
1602 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001603 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 */
1605
1606 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001607 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001608 list_splice_init(&check_list, work_q);
1609 SCSI_LOG_ERROR_RECOVERY(3,
1610 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001611 "%s: skip BRST, past eh deadline\n",
1612 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001613 return list_empty(work_q);
1614 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 chan_scmd = NULL;
1617 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001618 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 chan_scmd = scmd;
1620 break;
1621 /*
1622 * FIXME add back in some support for
1623 * soft_reset devices.
1624 */
1625 }
1626 }
1627
1628 if (!chan_scmd)
1629 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001630 SCSI_LOG_ERROR_RECOVERY(3,
1631 shost_printk(KERN_INFO, shost,
1632 "%s: Sending BRST chan: %d\n",
1633 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001635 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001636 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001637 if (channel == scmd_channel(scmd)) {
1638 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 scsi_eh_finish_cmd(scmd,
1640 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001641 else
1642 list_move_tail(&scmd->eh_entry,
1643 &check_list);
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001647 SCSI_LOG_ERROR_RECOVERY(3,
1648 shost_printk(KERN_INFO, shost,
1649 "%s: BRST failed chan: %d\n",
1650 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652 }
David Jeffery3eef6252011-05-19 14:41:12 -04001653 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001657 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001658 * @shost: host to be reset.
1659 * @work_q: &list_head for pending commands.
1660 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001661 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001662static int scsi_eh_host_reset(struct Scsi_Host *shost,
1663 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 struct list_head *done_q)
1665{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001666 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001667 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 if (!list_empty(work_q)) {
1671 scmd = list_entry(work_q->next,
1672 struct scsi_cmnd, eh_entry);
1673
Hannes Reinecke91921e02014-06-25 16:39:59 +02001674 SCSI_LOG_ERROR_RECOVERY(3,
1675 shost_printk(KERN_INFO, shost,
1676 "%s: Sending HRST\n",
1677 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001680 if (rtn == SUCCESS) {
1681 list_splice_init(work_q, &check_list);
1682 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001683 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 scsi_eh_finish_cmd(scmd, done_q);
1685 }
1686 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001687 SCSI_LOG_ERROR_RECOVERY(3,
1688 shost_printk(KERN_INFO, shost,
1689 "%s: HRST failed\n",
1690 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692 }
David Jeffery3eef6252011-05-19 14:41:12 -04001693 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696/**
1697 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001698 * @work_q: &list_head for pending commands.
1699 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701static void scsi_eh_offline_sdevs(struct list_head *work_q,
1702 struct list_head *done_q)
1703{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001704 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001706 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001707 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1708 "not ready after error recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001710 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /*
1712 * FIXME: Handle lost cmds.
1713 */
1714 }
1715 scsi_eh_finish_cmd(scmd, done_q);
1716 }
1717 return;
1718}
1719
1720/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001721 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001722 * @scmd: SCSI cmd to examine.
1723 */
1724int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1725{
1726 switch (host_byte(scmd->result)) {
1727 case DID_OK:
1728 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001729 case DID_TIME_OUT:
1730 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001731 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001732 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001733 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001734 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001735 case DID_ERROR:
1736 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1737 status_byte(scmd->result) == RESERVATION_CONFLICT)
1738 return 0;
1739 /* fall through */
1740 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001741 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001742 }
1743
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001744 if (status_byte(scmd->result) != CHECK_CONDITION)
1745 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001746
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001747check_type:
1748 /*
1749 * assume caller has checked sense and determined
1750 * the check condition was retryable.
1751 */
1752 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001753 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001754 return 1;
1755 else
1756 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001757}
1758
1759/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1761 * @scmd: SCSI cmd to examine.
1762 *
1763 * Notes:
1764 * This is *only* called when we are examining the status after sending
1765 * out the actual data command. any commands that are queued for error
1766 * recovery (e.g. test_unit_ready) do *not* come through here.
1767 *
1768 * When this routine returns failed, it means the error handler thread
1769 * is woken. In cases where the error code indicates an error that
1770 * doesn't require the error handler read (i.e. we don't need to
1771 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773int scsi_decide_disposition(struct scsi_cmnd *scmd)
1774{
1775 int rtn;
1776
1777 /*
1778 * if the device is offline, then we clearly just pass the result back
1779 * up to the top level.
1780 */
1781 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001782 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1783 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return SUCCESS;
1785 }
1786
1787 /*
1788 * first check the host byte, to see if there is anything in there
1789 * that would indicate what we need to do.
1790 */
1791 switch (host_byte(scmd->result)) {
1792 case DID_PASSTHROUGH:
1793 /*
1794 * no matter what, pass this through to the upper layer.
1795 * nuke this special code so that it looks like we are saying
1796 * did_ok.
1797 */
1798 scmd->result &= 0xff00ffff;
1799 return SUCCESS;
1800 case DID_OK:
1801 /*
1802 * looks good. drop through, and check the next byte.
1803 */
1804 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001805 case DID_ABORT:
1806 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001807 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001808 return SUCCESS;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 case DID_NO_CONNECT:
1811 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * note - this means that we just report the status back
1814 * to the top level driver, not that we actually think
1815 * that it indicates SUCCESS.
1816 */
1817 return SUCCESS;
1818 /*
1819 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001820 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * in order to avoid endless loops (db)
1822 *
1823 * actually this is a bug in this function here. we should
1824 * be mindful of the maximum number of retries specified
1825 * and not get stuck in a loop.
1826 */
1827 case DID_SOFT_ERROR:
1828 goto maybe_retry;
1829 case DID_IMM_RETRY:
1830 return NEEDS_RETRY;
1831
bf341912005-04-12 17:49:09 -05001832 case DID_REQUEUE:
1833 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001834 case DID_TRANSPORT_DISRUPTED:
1835 /*
1836 * LLD/transport was disrupted during processing of the IO.
1837 * The transport class is now blocked/blocking,
1838 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001839 * based on its timers and recovery capablilities if
1840 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001841 */
Mike Christie939c22882008-11-04 19:47:19 -06001842 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001843 case DID_TRANSPORT_FAILFAST:
1844 /*
1845 * The transport decided to failfast the IO (most likely
1846 * the fast io fail tmo fired), so send IO directly upwards.
1847 */
1848 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 case DID_ERROR:
1850 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1851 status_byte(scmd->result) == RESERVATION_CONFLICT)
1852 /*
1853 * execute reservation conflict processing code
1854 * lower down
1855 */
1856 break;
1857 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 case DID_BUS_BUSY:
1859 case DID_PARITY:
1860 goto maybe_retry;
1861 case DID_TIME_OUT:
1862 /*
1863 * when we scan the bus, we get timeout messages for
1864 * these commands if there is no device available.
1865 * other hosts report did_no_connect for the same thing.
1866 */
1867 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1868 scmd->cmnd[0] == INQUIRY)) {
1869 return SUCCESS;
1870 } else {
1871 return FAILED;
1872 }
1873 case DID_RESET:
1874 return SUCCESS;
1875 default:
1876 return FAILED;
1877 }
1878
1879 /*
1880 * next, check the message byte.
1881 */
1882 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1883 return FAILED;
1884
1885 /*
1886 * check the status byte to see if this indicates anything special.
1887 */
1888 switch (status_byte(scmd->result)) {
1889 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001890 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /*
1892 * the case of trying to send too many commands to a
1893 * tagged queueing device.
1894 */
1895 case BUSY:
1896 /*
1897 * device can't talk to us at the moment. Should only
1898 * occur (SAM-3) when the task queue is empty, so will cause
1899 * the empty queue handling to trigger a stall in the
1900 * device.
1901 */
1902 return ADD_TO_MLQUEUE;
1903 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001904 if (scmd->cmnd[0] == REPORT_LUNS)
1905 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001906 scsi_handle_queue_ramp_up(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001909 case TASK_ABORTED:
1910 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 case CHECK_CONDITION:
1912 rtn = scsi_check_sense(scmd);
1913 if (rtn == NEEDS_RETRY)
1914 goto maybe_retry;
1915 /* if rtn == FAILED, we have no sense information;
1916 * returning FAILED will wake the error handler thread
1917 * to collect the sense and redo the decide
1918 * disposition */
1919 return rtn;
1920 case CONDITION_GOOD:
1921 case INTERMEDIATE_GOOD:
1922 case INTERMEDIATE_C_GOOD:
1923 case ACA_ACTIVE:
1924 /*
1925 * who knows? FIXME(eric)
1926 */
1927 return SUCCESS;
1928
1929 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001930 sdev_printk(KERN_INFO, scmd->device,
1931 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001932 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return SUCCESS; /* causes immediate i/o error */
1934 default:
1935 return FAILED;
1936 }
1937 return FAILED;
1938
1939 maybe_retry:
1940
1941 /* we requeue for retry because the error was retryable, and
1942 * the request was not marked fast fail. Note that above,
1943 * even if the request is marked fast fail, we still requeue
1944 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001945 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001946 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return NEEDS_RETRY;
1948 } else {
1949 /*
1950 * no more retries - report this one back to upper level.
1951 */
1952 return SUCCESS;
1953 }
1954}
1955
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001956static void eh_lock_door_done(struct request *req, int uptodate)
1957{
1958 __blk_put_request(req->q, req);
1959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 * scsi_eh_lock_door - Prevent medium removal for the specified device
1963 * @sdev: SCSI device to prevent medium removal
1964 *
1965 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001966 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 *
1968 * Notes:
1969 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1970 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static void scsi_eh_lock_door(struct scsi_device *sdev)
1973{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001974 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001975 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
James Bottomley91bc31f2009-05-17 09:30:48 -05001977 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001978 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001979 * request becomes available
1980 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001981 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001982 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001983 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001984 rq = scsi_req(req);
1985 scsi_req_init(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001987 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1988 rq->cmd[1] = 0;
1989 rq->cmd[2] = 0;
1990 rq->cmd[3] = 0;
1991 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1992 rq->cmd[5] = 0;
1993 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001994
Christoph Hellwige8064022016-10-20 15:12:13 +02001995 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001996 req->timeout = 10 * HZ;
1997 req->retries = 5;
1998
1999 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002/**
2003 * scsi_restart_operations - restart io operations to the specified host.
2004 * @shost: Host we are restarting.
2005 *
2006 * Notes:
2007 * When we entered the error handler, we blocked all further i/o to
2008 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010static void scsi_restart_operations(struct Scsi_Host *shost)
2011{
2012 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05002013 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 /*
2016 * If the door was locked, we need to insert a door lock request
2017 * onto the head of the SCSI request queue for the device. There
2018 * is no point trying to lock the door of an off-line device.
2019 */
2020 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01002021 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01002023 sdev->was_reset = 0;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026
2027 /*
2028 * next free up anything directly waiting upon the host. this
2029 * will be requests for character device operations, and also for
2030 * ioctls to queued block devices.
2031 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02002032 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002033 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
James Bottomley939647e2005-09-18 15:05:20 -05002035 spin_lock_irqsave(shost->host_lock, flags);
2036 if (scsi_host_set_state(shost, SHOST_RUNNING))
2037 if (scsi_host_set_state(shost, SHOST_CANCEL))
2038 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2039 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 wake_up(&shost->host_wait);
2042
2043 /*
2044 * finally we need to re-initiate requests that may be pending. we will
2045 * have had everything blocked while error handling is taking place, and
2046 * now that error recovery is done, we will need to ensure that these
2047 * requests are started.
2048 */
2049 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002050
2051 /*
2052 * if eh is active and host_eh_scheduled is pending we need to re-run
2053 * recovery. we do this check after scsi_run_host_queues() to allow
2054 * everything pent up since the last eh run a chance to make forward
2055 * progress before we sync again. Either we'll immediately re-run
2056 * recovery or scsi_device_unbusy() will wake us again when these
2057 * pending commands complete.
2058 */
2059 spin_lock_irqsave(shost->host_lock, flags);
2060 if (shost->host_eh_scheduled)
2061 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2062 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2063 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066/**
2067 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002068 * @shost: host to be recovered.
2069 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002070 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002071 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002072void scsi_eh_ready_devs(struct Scsi_Host *shost,
2073 struct list_head *work_q,
2074 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 if (!scsi_eh_stu(shost, work_q, done_q))
2077 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002078 if (!scsi_eh_target_reset(shost, work_q, done_q))
2079 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002080 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002081 scsi_eh_offline_sdevs(work_q,
2082 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002084EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086/**
2087 * scsi_eh_flush_done_q - finish processed commands or retry them.
2088 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002089 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002090void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002092 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002094 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2095 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002097 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002098 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002099 SCSI_LOG_ERROR_RECOVERY(3,
2100 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002101 "%s: flush retry cmd\n",
2102 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2104 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002105 /*
2106 * If just we got sense for the device (called
2107 * scsi_eh_get_sense), scmd->result is already
2108 * set, do not set DRIVER_TIMEOUT.
2109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (!scmd->result)
2111 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002112 SCSI_LOG_ERROR_RECOVERY(3,
2113 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002114 "%s: flush finish cmd\n",
2115 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 scsi_finish_command(scmd);
2117 }
2118 }
2119}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002120EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122/**
2123 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2124 * @shost: Host to unjam.
2125 *
2126 * Notes:
2127 * When we come in here, we *know* that all commands on the bus have
2128 * either completed, failed or timed out. we also know that no further
2129 * commands are being sent to the host, so things are relatively quiet
2130 * and we have freedom to fiddle with things as we wish.
2131 *
2132 * This is only the *default* implementation. it is possible for
2133 * individual drivers to supply their own version of this function, and
2134 * if the maintainer wishes to do this, it is strongly suggested that
2135 * this function be taken as a template and modified. this function
2136 * was designed to correctly handle problems for about 95% of the
2137 * different cases out there, and it should always provide at least a
2138 * reasonable amount of error recovery.
2139 *
2140 * Any command marked 'failed' or 'timeout' must eventually have
2141 * scsi_finish_cmd() called for it. we do all of the retry stuff
2142 * here, so when we restart the host after we return it should have an
2143 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145static void scsi_unjam_host(struct Scsi_Host *shost)
2146{
2147 unsigned long flags;
2148 LIST_HEAD(eh_work_q);
2149 LIST_HEAD(eh_done_q);
2150
2151 spin_lock_irqsave(shost->host_lock, flags);
2152 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2153 spin_unlock_irqrestore(shost->host_lock, flags);
2154
2155 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2156
2157 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2158 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
2159 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2160
Hannes Reineckeb4562022013-10-23 10:51:21 +02002161 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002162 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002163 shost->last_reset = 0;
2164 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 scsi_eh_flush_done_q(&eh_done_q);
2166}
2167
2168/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002169 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * @data: Host for which we are running.
2171 *
2172 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002173 * This is the main error handling loop. This is run as a kernel thread
2174 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176int scsi_error_handler(void *data)
2177{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002178 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002181 * We use TASK_INTERRUPTIBLE so that the thread is not
2182 * counted against the load average as a running process.
2183 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002184 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 */
Michal Hocko537b6042015-08-27 20:16:37 +02002186 while (true) {
2187 /*
2188 * The sequence in kthread_stop() sets the stop flag first
2189 * then wakes the process. To avoid missed wakeups, the task
2190 * should always be in a non running state before the stop
2191 * flag is checked
2192 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002193 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002194 if (kthread_should_stop())
2195 break;
2196
Tejun Heoee7863b2006-05-15 20:57:20 +09002197 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002198 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002199 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002200 shost_printk(KERN_INFO, shost,
2201 "scsi_eh_%d: sleeping\n",
2202 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002203 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002204 continue;
2205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
James Bottomley3ed7a472005-09-19 09:50:04 -05002207 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002208 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002209 shost_printk(KERN_INFO, shost,
2210 "scsi_eh_%d: waking up %d/%d/%d\n",
2211 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002212 shost->host_failed,
2213 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /*
2216 * We have a host that is failing for some reason. Figure out
2217 * what we need to do to get it up and online again (if we can).
2218 * If we fail, we end up taking the thing offline.
2219 */
Lin Mingae0751f2011-12-05 09:20:24 +08002220 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002221 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002222 shost_printk(KERN_ERR, shost,
2223 "scsi_eh_%d: unable to autoresume\n",
2224 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002225 continue;
2226 }
2227
Christoph Hellwig9227c332006-04-01 19:21:04 +02002228 if (shost->transportt->eh_strategy_handler)
2229 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 else
2231 scsi_unjam_host(shost);
2232
Wei Fang72d8c362016-06-07 14:53:56 +08002233 /* All scmds have been handled */
2234 shost->host_failed = 0;
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /*
2237 * Note - if the above fails completely, the action is to take
2238 * individual devices offline and flush the queue of any
2239 * outstanding requests that may have been pending. When we
2240 * restart, we restart any I/O to any other devices on the bus
2241 * which are still online.
2242 */
2243 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002244 if (!shost->eh_noresume)
2245 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002247 __set_current_state(TASK_RUNNING);
2248
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002249 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002250 shost_printk(KERN_INFO, shost,
2251 "Error handler scsi_eh_%d exiting\n",
2252 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002253 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return 0;
2255}
2256
2257/*
2258 * Function: scsi_report_bus_reset()
2259 *
2260 * Purpose: Utility function used by low-level drivers to report that
2261 * they have observed a bus reset on the bus being handled.
2262 *
2263 * Arguments: shost - Host in question
2264 * channel - channel on which reset was observed.
2265 *
2266 * Returns: Nothing
2267 *
2268 * Lock status: Host lock must be held.
2269 *
2270 * Notes: This only needs to be called if the reset is one which
2271 * originates from an unknown location. Resets originated
2272 * by the mid-level itself don't need to call this, but there
2273 * should be no harm.
2274 *
2275 * The main purpose of this is to make sure that a CHECK_CONDITION
2276 * is properly treated.
2277 */
2278void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2279{
2280 struct scsi_device *sdev;
2281
2282 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002283 if (channel == sdev_channel(sdev))
2284 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286}
2287EXPORT_SYMBOL(scsi_report_bus_reset);
2288
2289/*
2290 * Function: scsi_report_device_reset()
2291 *
2292 * Purpose: Utility function used by low-level drivers to report that
2293 * they have observed a device reset on the device being handled.
2294 *
2295 * Arguments: shost - Host in question
2296 * channel - channel on which reset was observed
2297 * target - target on which reset was observed
2298 *
2299 * Returns: Nothing
2300 *
2301 * Lock status: Host lock must be held
2302 *
2303 * Notes: This only needs to be called if the reset is one which
2304 * originates from an unknown location. Resets originated
2305 * by the mid-level itself don't need to call this, but there
2306 * should be no harm.
2307 *
2308 * The main purpose of this is to make sure that a CHECK_CONDITION
2309 * is properly treated.
2310 */
2311void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2312{
2313 struct scsi_device *sdev;
2314
2315 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002316 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002317 target == sdev_id(sdev))
2318 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320}
2321EXPORT_SYMBOL(scsi_report_device_reset);
2322
2323static void
2324scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2325{
2326}
2327
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002328/**
2329 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2330 * @dev: scsi_device to operate on
2331 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 */
2333int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002334scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Alan Sternbc4f2402010-06-17 10:41:42 -04002336 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002337 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002338 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002339 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002340 int error = 0, rtn, val;
2341
2342 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2343 return -EACCES;
2344
2345 error = get_user(val, arg);
2346 if (error)
2347 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Alan Sternbc4f2402010-06-17 10:41:42 -04002349 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002350 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002351
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002352 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002353 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2354 shost->hostt->cmd_size, GFP_KERNEL);
2355 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002356 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002357 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002358
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002359 scmd = (struct scsi_cmnd *)(rq + 1);
2360 scsi_init_command(dev, scmd);
2361 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002362 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002365 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 scmd->cmd_len = 0;
2368
2369 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
James Smartd7a1bb02006-03-08 14:50:12 -05002371 spin_lock_irqsave(shost->host_lock, flags);
2372 shost->tmf_in_progress = 1;
2373 spin_unlock_irqrestore(shost->host_lock, flags);
2374
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002375 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2376 case SG_SCSI_RESET_NOTHING:
2377 rtn = SUCCESS;
2378 break;
2379 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002381 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 break;
2383 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002384 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002385 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002386 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002387 break;
2388 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002389 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002391 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
2393 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002394 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002396 if (rtn == SUCCESS)
2397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 default:
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002399 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002404 error = (rtn == SUCCESS) ? 0 : -EIO;
2405
James Smartd7a1bb02006-03-08 14:50:12 -05002406 spin_lock_irqsave(shost->host_lock, flags);
2407 shost->tmf_in_progress = 0;
2408 spin_unlock_irqrestore(shost->host_lock, flags);
2409
2410 /*
2411 * be sure to wake up anyone who was sleeping or had their queue
2412 * suspended while we performed the TMF.
2413 */
2414 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002415 shost_printk(KERN_INFO, shost,
2416 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002417
2418 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002419 scsi_run_host_queues(shost);
2420
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002421 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002422 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002423
Christoph Hellwig04796332014-02-20 14:20:55 -08002424out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002425 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002426 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002428EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002430bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2431 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002434 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436EXPORT_SYMBOL(scsi_command_normalize_sense);
2437
2438/**
Rob Landleyeb448202007-11-03 13:30:39 -05002439 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 * @sense_buffer: byte array of sense data
2441 * @sb_len: number of valid bytes in sense_buffer
2442 * @info_out: pointer to 64 integer where 8 or 4 byte information
2443 * field will be placed if found.
2444 *
2445 * Return value:
2446 * 1 if information field found, 0 if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2449 u64 * info_out)
2450{
2451 int j;
2452 const u8 * ucp;
2453 u64 ull;
2454
2455 if (sb_len < 7)
2456 return 0;
2457 switch (sense_buffer[0] & 0x7f) {
2458 case 0x70:
2459 case 0x71:
2460 if (sense_buffer[0] & 0x80) {
2461 *info_out = (sense_buffer[3] << 24) +
2462 (sense_buffer[4] << 16) +
2463 (sense_buffer[5] << 8) + sense_buffer[6];
2464 return 1;
2465 } else
2466 return 0;
2467 case 0x72:
2468 case 0x73:
2469 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2470 0 /* info desc */);
2471 if (ucp && (0xa == ucp[1])) {
2472 ull = 0;
2473 for (j = 0; j < 8; ++j) {
2474 if (j > 0)
2475 ull <<= 8;
2476 ull |= ucp[4 + j];
2477 }
2478 *info_out = ull;
2479 return 1;
2480 } else
2481 return 0;
2482 default:
2483 return 0;
2484 }
2485}
2486EXPORT_SYMBOL(scsi_get_sense_info_fld);