blob: 94d2047e0096aee29eb3238aa69982a1306ef2dc [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>
Martin Wilck29cfc2a2018-04-18 01:35:10 +020041#include <scsi/scsi_devinfo.h>
Christoph Hellwig176aa9d2014-10-11 12:06:47 +020042#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include "scsi_priv.h"
45#include "scsi_logging.h"
Adrian Bunk79ee8302007-08-10 14:50:42 -070046#include "scsi_transport_api.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Kei Tokunagabf816232010-04-01 20:41:40 +090048#include <trace/events/scsi.h>
49
Damien Le Moal29087692017-04-24 16:51:12 +090050#include <asm/unaligned.h>
51
James Bottomley14216562012-07-25 23:55:55 +040052static void scsi_eh_done(struct scsi_cmnd *scmd);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * These should *probably* be handled by the host itself.
56 * Since it is allowed to sleep, it probably should.
57 */
58#define BUS_RESET_SETTLE_TIME (10)
59#define HOST_RESET_SETTLE_TIME (10)
60
David Jeffery3eef6252011-05-19 14:41:12 -040061static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +010062static int scsi_try_to_abort_cmd(struct scsi_host_template *,
63 struct scsi_cmnd *);
David Jeffery3eef6252011-05-19 14:41:12 -040064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065void scsi_eh_wakeup(struct Scsi_Host *shost)
66{
Bart Van Asschef0317e82017-12-04 10:06:24 -080067 lockdep_assert_held(shost->host_lock);
68
Christoph Hellwig74665012014-01-22 15:29:29 +010069 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090070 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a4702005-09-19 09:50:04 -050071 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020072 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
73 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75}
Tejun Heof8bbfc22006-05-19 21:07:05 +090076
77/**
78 * scsi_schedule_eh - schedule EH for SCSI host
79 * @shost: SCSI host to invoke error handling on.
80 *
81 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080082 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090083void scsi_schedule_eh(struct Scsi_Host *shost)
84{
85 unsigned long flags;
86
87 spin_lock_irqsave(shost->host_lock, flags);
88
89 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
90 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
91 shost->host_eh_scheduled++;
92 scsi_eh_wakeup(shost);
93 }
94
95 spin_unlock_irqrestore(shost->host_lock, flags);
96}
97EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Hannes Reineckeb4562022013-10-23 10:51:21 +020099static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
100{
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100101 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200102 return 0;
103
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100104 /*
105 * 32bit accesses are guaranteed to be atomic
106 * (on all supported architectures), so instead
107 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100108 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100109 * time_before call.
110 */
111 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100112 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200113 return 0;
114
115 return 1;
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100119 * scmd_eh_abort_handler - Handle command aborts
120 * @work: command to be aborted.
Bart Van Assche923f46f2018-02-12 10:38:05 -0800121 *
122 * Note: this function must be called only for a command that has timed out.
123 * Because the block layer marks a request as complete before it calls
124 * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
125 * timed out do not have any effect. Hence it is safe to call
126 * scsi_finish_command() from this function.
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100127 */
128void
129scmd_eh_abort_handler(struct work_struct *work)
130{
131 struct scsi_cmnd *scmd =
132 container_of(work, struct scsi_cmnd, abort_work.work);
133 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100134 int rtn;
135
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100136 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100137 SCSI_LOG_ERROR_RECOVERY(3,
138 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100139 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100140 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100141 SCSI_LOG_ERROR_RECOVERY(3,
142 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100143 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100144 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
145 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200146 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100147 if (scsi_host_eh_past_deadline(sdev->host)) {
148 SCSI_LOG_ERROR_RECOVERY(3,
149 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100150 "eh timeout, not retrying "
151 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100152 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100153 (++scmd->retries <= scmd->allowed)) {
154 SCSI_LOG_ERROR_RECOVERY(3,
155 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100156 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100157 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100158 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100159 } else {
160 SCSI_LOG_ERROR_RECOVERY(3,
161 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100162 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100163 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100164 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100165 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100166 } else {
167 SCSI_LOG_ERROR_RECOVERY(3,
168 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100169 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200170 (rtn == FAST_IO_FAIL) ?
171 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100172 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100173 }
174
Hannes Reineckea0658632017-04-06 15:36:35 +0200175 scsi_eh_scmd_add(scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100176}
177
178/**
179 * scsi_abort_command - schedule a command abort
180 * @scmd: scmd to abort.
181 *
182 * We only need to abort commands after a command timeout
183 */
184static int
185scsi_abort_command(struct scsi_cmnd *scmd)
186{
187 struct scsi_device *sdev = scmd->device;
188 struct Scsi_Host *shost = sdev->host;
189 unsigned long flags;
190
191 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
192 /*
193 * Retry after abort failed, escalate to next level.
194 */
195 SCSI_LOG_ERROR_RECOVERY(3,
196 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100197 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200198 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100199 return FAILED;
200 }
201
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100202 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100203 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100204 shost->last_reset = jiffies;
205 spin_unlock_irqrestore(shost->host_lock, flags);
206
207 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
208 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100209 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100210 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
211 return SUCCESS;
212}
213
214/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200215 * scsi_eh_reset - call into ->eh_action to reset internal counters
216 * @scmd: scmd to run eh on.
217 *
218 * The scsi driver might be carrying internal state about the
219 * devices, so we need to call into the driver to reset the
220 * internal state once the error handler is started.
221 */
222static void scsi_eh_reset(struct scsi_cmnd *scmd)
223{
224 if (!blk_rq_is_passthrough(scmd->request)) {
225 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
226 if (sdrv->eh_reset)
227 sdrv->eh_reset(scmd);
228 }
229}
230
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800231static void scsi_eh_inc_host_failed(struct rcu_head *head)
232{
Bart Van Assche3be88282018-02-22 11:30:20 -0800233 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
234 struct Scsi_Host *shost = scmd->device->host;
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800235 unsigned long flags;
236
237 spin_lock_irqsave(shost->host_lock, flags);
238 shost->host_failed++;
239 scsi_eh_wakeup(shost);
240 spin_unlock_irqrestore(shost->host_lock, flags);
241}
242
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200243/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * scsi_eh_scmd_add - add scsi cmd to error handling.
245 * @scmd: scmd to run eh on.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800246 */
Hannes Reineckea0658632017-04-06 15:36:35 +0200247void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct Scsi_Host *shost = scmd->device->host;
250 unsigned long flags;
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200251 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200253 WARN_ON_ONCE(!shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200256 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
257 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
258 WARN_ON_ONCE(ret);
259 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100260 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200261 shost->last_reset = jiffies;
262
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200263 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spin_unlock_irqrestore(shost->host_lock, flags);
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800266 /*
267 * Ensure that all tasks observe the host state change before the
268 * host_failed change.
269 */
Bart Van Assche3be88282018-02-22 11:30:20 -0800270 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700275 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
277 * Notes:
278 * We do not need to lock this. There is the potential for a race
279 * only in that the normal completion handling might run, but if the
280 * normal completion function determines that the timer has already
281 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800282 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700283enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Bart Van Asschebed22132017-08-25 13:46:32 -0700285 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700286 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100287 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500288
Kei Tokunagabf816232010-04-01 20:41:40 +0900289 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 scsi_log_completion(scmd, TIMEOUT_ERROR);
291
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100292 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200293 host->last_reset = jiffies;
294
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100295 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100296 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500297
Hannes Reineckea33c0702014-06-13 14:01:45 +0200298 if (rtn == BLK_EH_NOT_HANDLED) {
Hannes Reineckea0658632017-04-06 15:36:35 +0200299 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200300 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200301 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200302 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200303 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700304
Christoph Hellwigfa990782008-11-05 15:03:15 +0100305 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/**
309 * scsi_block_when_processing_errors - Prevent cmds from being queued.
310 * @sdev: Device on which we are performing recovery.
311 *
312 * Description:
313 * We block until the host is out of error recovery, and then check to
314 * see whether the host or the device is offline.
315 *
316 * Return value:
317 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319int scsi_block_when_processing_errors(struct scsi_device *sdev)
320{
321 int online;
322
James Bottomley939647e2005-09-18 15:05:20 -0500323 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 online = scsi_device_online(sdev);
326
Hannes Reinecke91921e02014-06-25 16:39:59 +0200327 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
328 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 return online;
331}
332EXPORT_SYMBOL(scsi_block_when_processing_errors);
333
334#ifdef CONFIG_SCSI_LOGGING
335/**
336 * scsi_eh_prt_fail_stats - Log info on failures.
337 * @shost: scsi host being recovered.
338 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
341 struct list_head *work_q)
342{
343 struct scsi_cmnd *scmd;
344 struct scsi_device *sdev;
345 int total_failures = 0;
346 int cmd_failed = 0;
347 int cmd_cancel = 0;
348 int devices_failed = 0;
349
350 shost_for_each_device(sdev, shost) {
351 list_for_each_entry(scmd, work_q, eh_entry) {
352 if (scmd->device == sdev) {
353 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200354 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100356 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ++cmd_failed;
358 }
359 }
360
361 if (cmd_cancel || cmd_failed) {
362 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200363 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500364 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700365 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500366 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 cmd_cancel = 0;
368 cmd_failed = 0;
369 ++devices_failed;
370 }
371 }
372
Hannes Reinecke91921e02014-06-25 16:39:59 +0200373 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
374 "Total of %d commands on %d"
375 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100376 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378#endif
379
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400380 /**
381 * scsi_report_lun_change - Set flag on all *other* devices on the same target
382 * to indicate that a UNIT ATTENTION is expected.
383 * @sdev: Device reporting the UNIT ATTENTION
384 */
385static void scsi_report_lun_change(struct scsi_device *sdev)
386{
387 sdev->sdev_target->expecting_lun_change = 1;
388}
389
390/**
391 * scsi_report_sense - Examine scsi sense information and log messages for
392 * certain conditions, also issue uevents for some of them.
393 * @sdev: Device reporting the sense code
394 * @sshdr: sshdr to be examined
395 */
396static void scsi_report_sense(struct scsi_device *sdev,
397 struct scsi_sense_hdr *sshdr)
398{
399 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
400
401 if (sshdr->sense_key == UNIT_ATTENTION) {
402 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
403 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
404 sdev_printk(KERN_WARNING, sdev,
405 "Inquiry data has changed");
406 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
407 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
408 scsi_report_lun_change(sdev);
409 sdev_printk(KERN_WARNING, sdev,
410 "Warning! Received an indication that the "
411 "LUN assignments on this target have "
412 "changed. The Linux SCSI layer does not "
413 "automatically remap LUN assignments.\n");
414 } else if (sshdr->asc == 0x3f)
415 sdev_printk(KERN_WARNING, sdev,
416 "Warning! Received an indication that the "
417 "operating parameters on this target have "
418 "changed. The Linux SCSI layer does not "
419 "automatically adjust these parameters.\n");
420
421 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
422 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
423 sdev_printk(KERN_WARNING, sdev,
424 "Warning! Received an indication that the "
425 "LUN reached a thin provisioning soft "
426 "threshold.\n");
427 }
428
Hannes Reineckecf3431b2017-10-17 09:11:24 +0200429 if (sshdr->asc == 0x29) {
430 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
431 sdev_printk(KERN_WARNING, sdev,
432 "Power-on or device reset occurred\n");
433 }
434
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400435 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
436 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
437 sdev_printk(KERN_WARNING, sdev,
438 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200439 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
440 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
441 sdev_printk(KERN_WARNING, sdev,
442 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400443 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
444 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
445 sdev_printk(KERN_WARNING, sdev,
446 "Capacity data has changed");
447 } else if (sshdr->asc == 0x2a)
448 sdev_printk(KERN_WARNING, sdev,
449 "Parameters changed");
450 }
451
452 if (evt_type != SDEV_EVT_MAXBITS) {
453 set_bit(evt_type, sdev->pending_events);
454 schedule_work(&sdev->event_work);
455 }
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/**
459 * scsi_check_sense - Examine scsi cmd sense
460 * @scmd: Cmd to have sense checked.
461 *
462 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200463 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
465 * Notes:
466 * When a deferred error is detected the current command has
467 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800468 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200469int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700471 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct scsi_sense_hdr sshdr;
473
474 if (! scsi_command_normalize_sense(scmd, &sshdr))
475 return FAILED; /* no valid sense data */
476
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400477 scsi_report_sense(sdev, &sshdr);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (scsi_sense_is_deferred(&sshdr))
480 return NEEDS_RETRY;
481
Christoph Hellwigee14c672015-08-27 14:16:59 +0200482 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700483 int rc;
484
Christoph Hellwigee14c672015-08-27 14:16:59 +0200485 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700486 if (rc != SCSI_RETURN_NOT_HANDLED)
487 return rc;
488 /* handler does not care. Drop down to default handling */
489 }
490
Christoph Hellwige925cc42014-11-06 15:11:22 -0600491 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
492 /*
493 * nasty: for mid-layer issued TURs, we need to return the
494 * actual sense data without any recovery attempt. For eh
495 * issued ones, we need to try to recover and interpret
496 */
497 return SUCCESS;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * Previous logic looked for FILEMARK, EOM or ILI which are
501 * mainly associated with tapes and returned SUCCESS.
502 */
503 if (sshdr.response_code == 0x70) {
504 /* fixed format */
505 if (scmd->sense_buffer[2] & 0xe0)
506 return SUCCESS;
507 } else {
508 /*
509 * descriptor format: look for "stream commands sense data
510 * descriptor" (see SSC-3). Assume single sense data
511 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
512 */
513 if ((sshdr.additional_length > 3) &&
514 (scmd->sense_buffer[8] == 0x4) &&
515 (scmd->sense_buffer[11] & 0xe0))
516 return SUCCESS;
517 }
518
519 switch (sshdr.sense_key) {
520 case NO_SENSE:
521 return SUCCESS;
522 case RECOVERED_ERROR:
523 return /* soft_error */ SUCCESS;
524
525 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400526 if (sshdr.asc == 0x10) /* DIF */
527 return SUCCESS;
528
Martin Wilck29cfc2a2018-04-18 01:35:10 +0200529 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
530 return ADD_TO_MLQUEUE;
Martin Wilckc3606522018-04-18 01:35:11 +0200531 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
532 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
533 return ADD_TO_MLQUEUE;
Martin Wilck29cfc2a2018-04-18 01:35:10 +0200534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return NEEDS_RETRY;
536 case NOT_READY:
537 case UNIT_ATTENTION:
538 /*
539 * if we are expecting a cc/ua because of a bus reset that we
540 * performed, treat this just as a retry. otherwise this is
541 * information that we should pass up to the upper-level driver
542 * so that we can deal with it there.
543 */
544 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900545 /*
546 * Because some device does not queue unit
547 * attentions correctly, we carefully check
548 * additional sense code and qualifier so as
549 * not to squash media change unit attention.
550 */
551 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
552 scmd->device->expecting_cc_ua = 0;
553 return NEEDS_RETRY;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400557 * we might also expect a cc/ua if another LUN on the target
558 * reported a UA with an ASC/ASCQ of 3F 0E -
559 * REPORTED LUNS DATA HAS CHANGED.
560 */
561 if (scmd->device->sdev_target->expecting_lun_change &&
562 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
563 return NEEDS_RETRY;
564 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100565 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * should retry.
567 */
568 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
569 return NEEDS_RETRY;
570 /*
571 * if the device is not started, we need to wake
572 * the error handler to start the motor
573 */
574 if (scmd->device->allow_restart &&
575 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
576 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100577 /*
578 * Pass the UA upwards for a determination in the completion
579 * functions.
580 */
581 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100583 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200584 case DATA_PROTECT:
585 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
586 /* Thin provisioning hard threshold reached */
587 set_host_byte(scmd, DID_ALLOC_FAILURE);
588 return SUCCESS;
589 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700590 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 case COPY_ABORTED:
592 case VOLUME_OVERFLOW:
593 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100594 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200595 set_host_byte(scmd, DID_TARGET_FAILURE);
596 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800599 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
600 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
601 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200602 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200603 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return NEEDS_RETRY;
606
607 case HARDWARE_ERROR:
608 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700609 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200611 set_host_byte(scmd, DID_TARGET_FAILURE);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700612 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500615 if (sshdr.asc == 0x20 || /* Invalid command operation code */
616 sshdr.asc == 0x21 || /* Logical block address out of range */
Hannes Reineckea8bbb2a2017-10-17 09:10:56 +0200617 sshdr.asc == 0x22 || /* Invalid function */
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500618 sshdr.asc == 0x24 || /* Invalid field in cdb */
Martin Wilckd0b7a902017-09-27 14:44:19 +0200619 sshdr.asc == 0x26 || /* Parameter value invalid */
620 sshdr.asc == 0x27) { /* Write protected */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200621 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500622 }
623 return SUCCESS;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 default:
626 return SUCCESS;
627 }
628}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200629EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Vasu Dev4a840672009-10-22 15:46:33 -0700631static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
632{
633 struct scsi_host_template *sht = sdev->host->hostt;
634 struct scsi_device *tmp_sdev;
635
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100636 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700637 sdev->queue_depth >= sdev->max_queue_depth)
638 return;
639
640 if (time_before(jiffies,
641 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
642 return;
643
644 if (time_before(jiffies,
645 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
646 return;
647
648 /*
649 * Walk all devices of a target and do
650 * ramp up on them.
651 */
652 shost_for_each_device(tmp_sdev, sdev->host) {
653 if (tmp_sdev->channel != sdev->channel ||
654 tmp_sdev->id != sdev->id ||
655 tmp_sdev->queue_depth == sdev->max_queue_depth)
656 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100657
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100658 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700659 sdev->last_queue_ramp_up = jiffies;
660 }
661}
662
Mike Christie42a6a912009-10-15 17:46:44 -0700663static void scsi_handle_queue_full(struct scsi_device *sdev)
664{
665 struct scsi_host_template *sht = sdev->host->hostt;
666 struct scsi_device *tmp_sdev;
667
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100668 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700669 return;
670
671 shost_for_each_device(tmp_sdev, sdev->host) {
672 if (tmp_sdev->channel != sdev->channel ||
673 tmp_sdev->id != sdev->id)
674 continue;
675 /*
676 * We do not know the number of commands that were at
677 * the device when we got the queue full so we start
678 * from the highest possible value and work our way down.
679 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100680 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700681 }
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/**
685 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
686 * @scmd: SCSI cmd to examine.
687 *
688 * Notes:
689 * This is *only* called when we are examining the status of commands
690 * queued during error recovery. the main difference here is that we
691 * don't allow for the possibility of retries here, and we are a lot
692 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
695{
696 /*
697 * first check the host byte, to see if there is anything in there
698 * that would indicate what we need to do.
699 */
700 if (host_byte(scmd->result) == DID_RESET) {
701 /*
702 * rats. we are already in the error handler, so we now
703 * get to try and figure out what to do next. if the sense
704 * is valid, we have a pretty good idea of what to do.
705 * if not, we mark it as FAILED.
706 */
707 return scsi_check_sense(scmd);
708 }
709 if (host_byte(scmd->result) != DID_OK)
710 return FAILED;
711
712 /*
713 * next, check the message byte.
714 */
715 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
716 return FAILED;
717
718 /*
719 * now, check the status byte to see if this indicates
720 * anything special.
721 */
722 switch (status_byte(scmd->result)) {
723 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700724 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700725 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 case COMMAND_TERMINATED:
727 return SUCCESS;
728 case CHECK_CONDITION:
729 return scsi_check_sense(scmd);
730 case CONDITION_GOOD:
731 case INTERMEDIATE_GOOD:
732 case INTERMEDIATE_C_GOOD:
733 /*
734 * who knows? FIXME(eric)
735 */
736 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500737 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500738 if (scmd->cmnd[0] == TEST_UNIT_READY)
739 /* it is a success, we probed the device and
740 * found it */
741 return SUCCESS;
742 /* otherwise, we failed to send the command */
743 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700745 scsi_handle_queue_full(scmd->device);
746 /* fall through */
747 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200748 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 default:
750 return FAILED;
751 }
752 return FAILED;
753}
754
755/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * scsi_eh_done - Completion function for error handling.
757 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800758 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static void scsi_eh_done(struct scsi_cmnd *scmd)
760{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100761 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600762
Hannes Reinecke91921e02014-06-25 16:39:59 +0200763 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100764 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600765
766 eh_action = scmd->device->host->eh_action;
767 if (eh_action)
768 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/**
Brian King292148f2007-01-30 17:51:17 -0600772 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200773 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800774 */
Brian King292148f2007-01-30 17:51:17 -0600775static int scsi_try_host_reset(struct scsi_cmnd *scmd)
776{
777 unsigned long flags;
778 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100779 struct Scsi_Host *host = scmd->device->host;
780 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600781
Hannes Reinecke91921e02014-06-25 16:39:59 +0200782 SCSI_LOG_ERROR_RECOVERY(3,
783 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600784
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100785 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600786 return FAILED;
787
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100788 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600789
790 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100791 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600792 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100793 spin_lock_irqsave(host->host_lock, flags);
794 scsi_report_bus_reset(host, scmd_channel(scmd));
795 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600796 }
797
798 return rtn;
799}
800
801/**
802 * scsi_try_bus_reset - ask host to perform a bus reset
803 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800804 */
Brian King292148f2007-01-30 17:51:17 -0600805static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
806{
807 unsigned long flags;
808 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100809 struct Scsi_Host *host = scmd->device->host;
810 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600811
Hannes Reinecke91921e02014-06-25 16:39:59 +0200812 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
813 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600814
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100815 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600816 return FAILED;
817
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100818 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600819
820 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100821 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600822 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100823 spin_lock_irqsave(host->host_lock, flags);
824 scsi_report_bus_reset(host, scmd_channel(scmd));
825 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600826 }
827
828 return rtn;
829}
830
Mike Christie30bd7df2008-02-29 18:25:19 -0600831static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
832{
833 sdev->was_reset = 1;
834 sdev->expecting_cc_ua = 1;
835}
836
837/**
838 * scsi_try_target_reset - Ask host to perform a target reset
839 * @scmd: SCSI cmd used to send a target reset
840 *
841 * Notes:
842 * There is no timeout for this operation. if this operation is
843 * unreliable for a given host, then the host itself needs to put a
844 * timer on it, and set the host back to a consistent state prior to
845 * returning.
846 */
847static int scsi_try_target_reset(struct scsi_cmnd *scmd)
848{
849 unsigned long flags;
850 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100851 struct Scsi_Host *host = scmd->device->host;
852 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600853
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100854 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600855 return FAILED;
856
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100857 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600858 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100859 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600860 __starget_for_each_device(scsi_target(scmd->device), NULL,
861 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100862 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600863 }
864
865 return rtn;
866}
867
Brian King292148f2007-01-30 17:51:17 -0600868/**
869 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
870 * @scmd: SCSI cmd used to send BDR
871 *
872 * Notes:
873 * There is no timeout for this operation. if this operation is
874 * unreliable for a given host, then the host itself needs to put a
875 * timer on it, and set the host back to a consistent state prior to
876 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800877 */
Brian King292148f2007-01-30 17:51:17 -0600878static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
879{
880 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100881 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600882
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100883 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600884 return FAILED;
885
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100886 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600887 if (rtn == SUCCESS)
888 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600889 return rtn;
890}
891
Hannes Reinecke883a0302014-10-24 14:27:04 +0200892/**
893 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800894 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200895 * @scmd: SCSI cmd used to send a target reset
896 *
897 * Return value:
898 * SUCCESS, FAILED, or FAST_IO_FAIL
899 *
900 * Notes:
901 * SUCCESS does not necessarily indicate that the command
902 * has been aborted; it only indicates that the LLDDs
903 * has cleared all references to that command.
904 * LLDDs should return FAILED only if an abort was required
905 * but could not be executed. LLDDs should return FAST_IO_FAIL
906 * if the device is temporarily unavailable (eg due to a
907 * link down on FibreChannel)
908 */
909static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
910 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600911{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100912 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600913 return FAILED;
914
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100915 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600916}
917
Brian King292148f2007-01-30 17:51:17 -0600918static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
919{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100920 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600921 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600922 if (scsi_try_target_reset(scmd) != SUCCESS)
923 if (scsi_try_bus_reset(scmd) != SUCCESS)
924 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600925}
926
927/**
Santosh Y3b729f72012-04-08 18:47:09 +0530928 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100929 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200930 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200931 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300932 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200933 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100934 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200935 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200936 * as part of the error recovery process. If @sense_bytes is 0 the command
937 * sent must be one that does not transfer any data. If @sense_bytes != 0
938 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
939 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800940 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200941void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
942 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
f59114b2005-04-17 15:00:23 -0500944 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Christoph Hellwig631c2282006-07-08 20:42:15 +0200946 /*
947 * We need saved copies of a number of fields - this is because
948 * error handling may need to overwrite these with different values
949 * to run different commands, and once error handling is complete,
950 * we will need to restore these values prior to running the actual
951 * command.
952 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200953 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300954 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200955 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200956 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200957 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200958 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400959 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400960 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200961 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200962
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400963 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700964 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300965 scmd->cmnd = ses->eh_cmnd;
966 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200967 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200968 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700969 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200970
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200971 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200972 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
973 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200974 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200975 scmd->sdb.length);
976 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200977 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400978 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200979 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200980 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200981 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200982 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200983 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200984 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300985 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200986 memcpy(scmd->cmnd, cmnd, cmnd_size);
987 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
988 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200989 }
990
991 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200992
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200993 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500995 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Christoph Hellwig631c2282006-07-08 20:42:15 +0200997 /*
998 * Zero the sense buffer. The scsi spec mandates that any
999 * untransferred sense data should be interpreted as being zero.
1000 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001001 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001002}
1003EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +02001004
Boaz Harroshe1c23462007-10-08 16:36:45 +02001005/**
Santosh Y3b729f72012-04-08 18:47:09 +05301006 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001007 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001008 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +02001009 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001010 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001011 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001012void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1013{
1014 /*
1015 * Restore original data
1016 */
1017 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001018 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001019 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001020 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001021 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001022 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001023 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001024 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +02001025 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001026}
1027EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1028
1029/**
Santosh Y3b729f72012-04-08 18:47:09 +05301030 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001031 * @scmd: SCSI command structure to hijack
1032 * @cmnd: CDB to send
1033 * @cmnd_size: size in bytes of @cmnd
1034 * @timeout: timeout for this request
1035 * @sense_bytes: size of sense data to copy or 0
1036 *
1037 * This function is used to send a scsi command down to a target device
1038 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1039 *
1040 * Return value:
1041 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001042 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001043static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1044 int cmnd_size, int timeout, unsigned sense_bytes)
1045{
1046 struct scsi_device *sdev = scmd->device;
1047 struct Scsi_Host *shost = sdev->host;
1048 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001049 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001050 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001051 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001052 int rtn;
1053
Hannes Reineckefc736482013-04-25 08:10:00 +02001054retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001055 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001056 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001059 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001060 rtn = shost->hostt->queuecommand(shost, scmd);
1061 if (rtn) {
1062 if (timeleft > stall_for) {
1063 scsi_eh_restore_cmnd(scmd, &ses);
1064 timeleft -= stall_for;
1065 msleep(jiffies_to_msecs(stall_for));
1066 goto retry;
1067 }
1068 /* signal not to enter either branch of the if () below */
1069 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001070 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001071 } else {
1072 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001073 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
f59114b2005-04-17 15:00:23 -05001076 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Hannes Reineckefc736482013-04-25 08:10:00 +02001078 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Hannes Reinecke91921e02014-06-25 16:39:59 +02001080 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001081 "%s timeleft: %ld\n",
1082 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001085 * If there is time left scsi_eh_done got called, and we will examine
1086 * the actual status codes to see whether the command actually did
1087 * complete normally, else if we have a zero return and no time left,
1088 * the command must still be pending, so abort it and return FAILED.
1089 * If we never actually managed to issue the command, because
1090 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1091 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001093 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001095 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1096 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 switch (rtn) {
1099 case SUCCESS:
1100 case NEEDS_RETRY:
1101 case FAILED:
1102 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001103 case ADD_TO_MLQUEUE:
1104 rtn = NEEDS_RETRY;
1105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 default:
1107 rtn = FAILED;
1108 break;
1109 }
Alan Stern511833a2014-11-21 10:44:49 -05001110 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001111 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001112 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114
Boaz Harroshe1c23462007-10-08 16:36:45 +02001115 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return rtn;
1118}
1119
1120/**
1121 * scsi_request_sense - Request sense data from a particular target.
1122 * @scmd: SCSI cmd for request sense.
1123 *
1124 * Notes:
1125 * Some hosts automatically obtain this information, others require
1126 * that we obtain it on our own. This function will *not* return until
1127 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129static int scsi_request_sense(struct scsi_cmnd *scmd)
1130{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001131 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
James Bottomley24510792013-11-11 13:44:53 +01001134static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1135{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001136 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001137 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1138 if (sdrv->eh_action)
1139 rtn = sdrv->eh_action(scmd, rtn);
1140 }
1141 return rtn;
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/**
1145 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1146 * @scmd: Original SCSI cmd that eh has finished.
1147 * @done_q: Queue for processed commands.
1148 *
1149 * Notes:
1150 * We don't want to use the normal command completion while we are are
1151 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001152 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * keep a list of pending commands for final completion, and once we
1154 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001155 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001156void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 list_move_tail(&scmd->eh_entry, done_q);
1159}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001160EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162/**
1163 * scsi_eh_get_sense - Get device sense data.
1164 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001165 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 *
1167 * Description:
1168 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001169 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 *
1171 * Notes:
1172 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001173 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * it down before we request it.
1175 *
1176 * All drivers should request sense information internally these days,
1177 * so for now all I have to say is tough noogies if you end up in here.
1178 *
1179 * XXX: Long term this code should go away, but that needs an audit of
1180 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001181 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001182int scsi_eh_get_sense(struct list_head *work_q,
1183 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001185 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001186 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int rtn;
1188
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001189 /*
1190 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1191 * should not get sense.
1192 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001193 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001194 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 SCSI_SENSE_VALID(scmd))
1196 continue;
1197
Hannes Reineckeb4562022013-10-23 10:51:21 +02001198 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001199 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001200 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001201 scmd_printk(KERN_INFO, scmd,
1202 "%s: skip request sense, past eh deadline\n",
1203 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001204 break;
1205 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001206 if (status_byte(scmd->result) != CHECK_CONDITION)
1207 /*
1208 * don't request sense if there's no check condition
1209 * status because the error we're processing isn't one
1210 * that has a sense code (and some devices get
1211 * confused by sense requests out of the blue)
1212 */
1213 continue;
1214
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001215 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1216 "%s: requesting sense\n",
1217 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 rtn = scsi_request_sense(scmd);
1219 if (rtn != SUCCESS)
1220 continue;
1221
Hannes Reinecke91921e02014-06-25 16:39:59 +02001222 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001223 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001224 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 rtn = scsi_decide_disposition(scmd);
1227
1228 /*
1229 * if the result was normal, then just pass it along to the
1230 * upper level.
1231 */
1232 if (rtn == SUCCESS)
1233 /* we don't want this command reissued, just
1234 * finished with the sense data, so set
1235 * retries to the max allowed to ensure it
1236 * won't get reissued */
1237 scmd->retries = scmd->allowed;
1238 else if (rtn != NEEDS_RETRY)
1239 continue;
1240
1241 scsi_eh_finish_cmd(scmd, done_q);
1242 }
1243
1244 return list_empty(work_q);
1245}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001246EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001250 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 *
1252 * Return value:
1253 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255static int scsi_eh_tur(struct scsi_cmnd *scmd)
1256{
1257 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1258 int retry_cnt = 1, rtn;
1259
1260retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001261 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1262 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Hannes Reinecke91921e02014-06-25 16:39:59 +02001264 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001265 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001266
1267 switch (rtn) {
1268 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (retry_cnt--)
1270 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001271 /*FALLTHRU*/
1272 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001273 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001274 default:
1275 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279/**
David Jeffery3eef6252011-05-19 14:41:12 -04001280 * scsi_eh_test_devices - check if devices are responding from error recovery.
1281 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001282 * @work_q: queue for commands which still need more error recovery
1283 * @done_q: queue for commands which are finished
1284 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001285 *
1286 * Decription:
1287 * Tests if devices are in a working state. Commands to devices now in
1288 * a working state are sent to the done_q while commands to devices which
1289 * are still failing to respond are returned to the work_q for more
1290 * processing.
1291 **/
1292static int scsi_eh_test_devices(struct list_head *cmd_list,
1293 struct list_head *work_q,
1294 struct list_head *done_q, int try_stu)
1295{
1296 struct scsi_cmnd *scmd, *next;
1297 struct scsi_device *sdev;
1298 int finish_cmds;
1299
1300 while (!list_empty(cmd_list)) {
1301 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1302 sdev = scmd->device;
1303
Hannes Reineckeb4562022013-10-23 10:51:21 +02001304 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001305 if (scsi_host_eh_past_deadline(sdev->host)) {
1306 /* Push items back onto work_q */
1307 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001308 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001309 sdev_printk(KERN_INFO, sdev,
1310 "%s: skip test device, past eh deadline",
1311 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001312 break;
1313 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001314 }
1315
David Jeffery3eef6252011-05-19 14:41:12 -04001316 finish_cmds = !scsi_device_online(scmd->device) ||
1317 (try_stu && !scsi_eh_try_stu(scmd) &&
1318 !scsi_eh_tur(scmd)) ||
1319 !scsi_eh_tur(scmd);
1320
1321 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1322 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001323 if (finish_cmds &&
1324 (try_stu ||
1325 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001326 scsi_eh_finish_cmd(scmd, done_q);
1327 else
1328 list_move_tail(&scmd->eh_entry, work_q);
1329 }
1330 }
1331 return list_empty(work_q);
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001336 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
1338 * Return value:
1339 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1342{
1343 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Christoph Hellwig631c2282006-07-08 20:42:15 +02001345 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001346 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Brian Kinged773e62007-03-29 15:25:52 -05001348 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001349 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001350
Christoph Hellwig631c2282006-07-08 20:42:15 +02001351 if (rtn == SUCCESS)
1352 return 0;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 1;
1356}
1357
1358 /**
1359 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001360 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001361 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001362 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
1364 * Notes:
1365 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001366 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368static int scsi_eh_stu(struct Scsi_Host *shost,
1369 struct list_head *work_q,
1370 struct list_head *done_q)
1371{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001372 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct scsi_device *sdev;
1374
1375 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001376 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001377 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001378 sdev_printk(KERN_INFO, sdev,
1379 "%s: skip START_UNIT, past eh deadline\n",
1380 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001381 break;
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 stu_scmd = NULL;
1384 list_for_each_entry(scmd, work_q, eh_entry)
1385 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1386 scsi_check_sense(scmd) == FAILED ) {
1387 stu_scmd = scmd;
1388 break;
1389 }
1390
1391 if (!stu_scmd)
1392 continue;
1393
Hannes Reinecke91921e02014-06-25 16:39:59 +02001394 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001395 sdev_printk(KERN_INFO, sdev,
1396 "%s: Sending START_UNIT\n",
1397 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (!scsi_eh_try_stu(stu_scmd)) {
1400 if (!scsi_device_online(sdev) ||
1401 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001402 list_for_each_entry_safe(scmd, next,
1403 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001404 if (scmd->device == sdev &&
1405 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 scsi_eh_finish_cmd(scmd, done_q);
1407 }
1408 }
1409 } else {
1410 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001411 sdev_printk(KERN_INFO, sdev,
1412 "%s: START_UNIT failed\n",
1413 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 }
1416
1417 return list_empty(work_q);
1418}
1419
1420
1421/**
1422 * scsi_eh_bus_device_reset - send bdr if needed
1423 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001424 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001425 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 *
1427 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001428 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 * devices that are jammed or not - if we have multiple devices, it
1430 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001431 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1434 struct list_head *work_q,
1435 struct list_head *done_q)
1436{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001437 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct scsi_device *sdev;
1439 int rtn;
1440
1441 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001442 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001443 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001444 sdev_printk(KERN_INFO, sdev,
1445 "%s: skip BDR, past eh deadline\n",
1446 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001447 break;
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 bdr_scmd = NULL;
1450 list_for_each_entry(scmd, work_q, eh_entry)
1451 if (scmd->device == sdev) {
1452 bdr_scmd = scmd;
1453 break;
1454 }
1455
1456 if (!bdr_scmd)
1457 continue;
1458
Hannes Reinecke91921e02014-06-25 16:39:59 +02001459 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001460 sdev_printk(KERN_INFO, sdev,
1461 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001463 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001465 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001467 list_for_each_entry_safe(scmd, next,
1468 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001469 if (scmd->device == sdev &&
1470 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 scsi_eh_finish_cmd(scmd,
1472 done_q);
1473 }
1474 }
1475 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001476 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001477 sdev_printk(KERN_INFO, sdev,
1478 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 }
1481
1482 return list_empty(work_q);
1483}
1484
1485/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001486 * scsi_eh_target_reset - send target reset if needed
1487 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001488 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001489 * @done_q: &list_head for processed commands.
1490 *
1491 * Notes:
1492 * Try a target reset.
1493 */
1494static int scsi_eh_target_reset(struct Scsi_Host *shost,
1495 struct list_head *work_q,
1496 struct list_head *done_q)
1497{
James Bottomley98db5192010-10-25 15:53:41 -05001498 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001499 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001500
James Bottomley98db5192010-10-25 15:53:41 -05001501 list_splice_init(work_q, &tmp_list);
1502
1503 while (!list_empty(&tmp_list)) {
1504 struct scsi_cmnd *next, *scmd;
1505 int rtn;
1506 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001507
Hannes Reineckeb4562022013-10-23 10:51:21 +02001508 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001509 /* push back on work queue for further processing */
1510 list_splice_init(&check_list, work_q);
1511 list_splice_init(&tmp_list, work_q);
1512 SCSI_LOG_ERROR_RECOVERY(3,
1513 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001514 "%s: Skip target reset, past eh deadline\n",
1515 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001516 return list_empty(work_q);
1517 }
James Bottomley98db5192010-10-25 15:53:41 -05001518
1519 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1520 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001521
Hannes Reinecke91921e02014-06-25 16:39:59 +02001522 SCSI_LOG_ERROR_RECOVERY(3,
1523 shost_printk(KERN_INFO, shost,
1524 "%s: Sending target reset to target %d\n",
1525 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001526 rtn = scsi_try_target_reset(scmd);
1527 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001528 SCSI_LOG_ERROR_RECOVERY(3,
1529 shost_printk(KERN_INFO, shost,
1530 "%s: Target reset failed"
1531 " target: %d\n",
1532 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001533 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1534 if (scmd_id(scmd) != id)
1535 continue;
1536
David Jeffery3eef6252011-05-19 14:41:12 -04001537 if (rtn == SUCCESS)
1538 list_move_tail(&scmd->eh_entry, &check_list);
1539 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001540 scsi_eh_finish_cmd(scmd, done_q);
1541 else
1542 /* push back on work queue for further processing */
1543 list_move(&scmd->eh_entry, work_q);
1544 }
1545 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001546
David Jeffery3eef6252011-05-19 14:41:12 -04001547 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001548}
1549
1550/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001551 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001552 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001553 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001554 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1557 struct list_head *work_q,
1558 struct list_head *done_q)
1559{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001560 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001561 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 unsigned int channel;
1563 int rtn;
1564
1565 /*
1566 * we really want to loop over the various channels, and do this on
1567 * a channel by channel basis. we should also check to see if any
1568 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001569 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
1571
1572 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001573 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001574 list_splice_init(&check_list, work_q);
1575 SCSI_LOG_ERROR_RECOVERY(3,
1576 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001577 "%s: skip BRST, past eh deadline\n",
1578 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001579 return list_empty(work_q);
1580 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 chan_scmd = NULL;
1583 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001584 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 chan_scmd = scmd;
1586 break;
1587 /*
1588 * FIXME add back in some support for
1589 * soft_reset devices.
1590 */
1591 }
1592 }
1593
1594 if (!chan_scmd)
1595 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001596 SCSI_LOG_ERROR_RECOVERY(3,
1597 shost_printk(KERN_INFO, shost,
1598 "%s: Sending BRST chan: %d\n",
1599 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001601 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001602 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001603 if (channel == scmd_channel(scmd)) {
1604 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 scsi_eh_finish_cmd(scmd,
1606 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001607 else
1608 list_move_tail(&scmd->eh_entry,
1609 &check_list);
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001613 SCSI_LOG_ERROR_RECOVERY(3,
1614 shost_printk(KERN_INFO, shost,
1615 "%s: BRST failed chan: %d\n",
1616 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 }
David Jeffery3eef6252011-05-19 14:41:12 -04001619 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001623 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001624 * @shost: host to be reset.
1625 * @work_q: &list_head for pending commands.
1626 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001627 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001628static int scsi_eh_host_reset(struct Scsi_Host *shost,
1629 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct list_head *done_q)
1631{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001632 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001633 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (!list_empty(work_q)) {
1637 scmd = list_entry(work_q->next,
1638 struct scsi_cmnd, eh_entry);
1639
Hannes Reinecke91921e02014-06-25 16:39:59 +02001640 SCSI_LOG_ERROR_RECOVERY(3,
1641 shost_printk(KERN_INFO, shost,
1642 "%s: Sending HRST\n",
1643 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001646 if (rtn == SUCCESS) {
1647 list_splice_init(work_q, &check_list);
1648 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001649 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 scsi_eh_finish_cmd(scmd, done_q);
1651 }
1652 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001653 SCSI_LOG_ERROR_RECOVERY(3,
1654 shost_printk(KERN_INFO, shost,
1655 "%s: HRST failed\n",
1656 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658 }
David Jeffery3eef6252011-05-19 14:41:12 -04001659 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662/**
1663 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001664 * @work_q: &list_head for pending commands.
1665 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667static void scsi_eh_offline_sdevs(struct list_head *work_q,
1668 struct list_head *done_q)
1669{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001670 struct scsi_cmnd *scmd, *next;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001671 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001673 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001674 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1675 "not ready after error recovery\n");
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001676 sdev = scmd->device;
1677
1678 mutex_lock(&sdev->state_mutex);
1679 scsi_device_set_state(sdev, SDEV_OFFLINE);
1680 mutex_unlock(&sdev->state_mutex);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 scsi_eh_finish_cmd(scmd, done_q);
1683 }
1684 return;
1685}
1686
1687/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001688 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001689 * @scmd: SCSI cmd to examine.
1690 */
1691int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1692{
1693 switch (host_byte(scmd->result)) {
1694 case DID_OK:
1695 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001696 case DID_TIME_OUT:
1697 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001698 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001699 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001700 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001701 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001702 case DID_ERROR:
1703 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1704 status_byte(scmd->result) == RESERVATION_CONFLICT)
1705 return 0;
1706 /* fall through */
1707 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001708 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001709 }
1710
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001711 if (status_byte(scmd->result) != CHECK_CONDITION)
1712 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001713
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001714check_type:
1715 /*
1716 * assume caller has checked sense and determined
1717 * the check condition was retryable.
1718 */
1719 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001720 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001721 return 1;
1722 else
1723 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001724}
1725
1726/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1728 * @scmd: SCSI cmd to examine.
1729 *
1730 * Notes:
1731 * This is *only* called when we are examining the status after sending
1732 * out the actual data command. any commands that are queued for error
1733 * recovery (e.g. test_unit_ready) do *not* come through here.
1734 *
1735 * When this routine returns failed, it means the error handler thread
1736 * is woken. In cases where the error code indicates an error that
1737 * doesn't require the error handler read (i.e. we don't need to
1738 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740int scsi_decide_disposition(struct scsi_cmnd *scmd)
1741{
1742 int rtn;
1743
1744 /*
1745 * if the device is offline, then we clearly just pass the result back
1746 * up to the top level.
1747 */
1748 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001749 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1750 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return SUCCESS;
1752 }
1753
1754 /*
1755 * first check the host byte, to see if there is anything in there
1756 * that would indicate what we need to do.
1757 */
1758 switch (host_byte(scmd->result)) {
1759 case DID_PASSTHROUGH:
1760 /*
1761 * no matter what, pass this through to the upper layer.
1762 * nuke this special code so that it looks like we are saying
1763 * did_ok.
1764 */
1765 scmd->result &= 0xff00ffff;
1766 return SUCCESS;
1767 case DID_OK:
1768 /*
1769 * looks good. drop through, and check the next byte.
1770 */
1771 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001772 case DID_ABORT:
1773 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001774 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001775 return SUCCESS;
1776 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001777 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 case DID_NO_CONNECT:
1779 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
1781 * note - this means that we just report the status back
1782 * to the top level driver, not that we actually think
1783 * that it indicates SUCCESS.
1784 */
1785 return SUCCESS;
Petros Koutoupisad950282017-10-30 16:38:10 -05001786 case DID_SOFT_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001789 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * in order to avoid endless loops (db)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto maybe_retry;
1793 case DID_IMM_RETRY:
1794 return NEEDS_RETRY;
1795
bf341912005-04-12 17:49:09 -05001796 case DID_REQUEUE:
1797 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001798 case DID_TRANSPORT_DISRUPTED:
1799 /*
1800 * LLD/transport was disrupted during processing of the IO.
1801 * The transport class is now blocked/blocking,
1802 * and the transport will decide what to do with the IO
Mike Christie939c2282008-11-04 19:47:19 -06001803 * based on its timers and recovery capablilities if
1804 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001805 */
Mike Christie939c2282008-11-04 19:47:19 -06001806 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001807 case DID_TRANSPORT_FAILFAST:
1808 /*
1809 * The transport decided to failfast the IO (most likely
1810 * the fast io fail tmo fired), so send IO directly upwards.
1811 */
1812 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 case DID_ERROR:
1814 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1815 status_byte(scmd->result) == RESERVATION_CONFLICT)
1816 /*
1817 * execute reservation conflict processing code
1818 * lower down
1819 */
1820 break;
1821 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 case DID_BUS_BUSY:
1823 case DID_PARITY:
1824 goto maybe_retry;
1825 case DID_TIME_OUT:
1826 /*
1827 * when we scan the bus, we get timeout messages for
1828 * these commands if there is no device available.
1829 * other hosts report did_no_connect for the same thing.
1830 */
1831 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1832 scmd->cmnd[0] == INQUIRY)) {
1833 return SUCCESS;
1834 } else {
1835 return FAILED;
1836 }
1837 case DID_RESET:
1838 return SUCCESS;
1839 default:
1840 return FAILED;
1841 }
1842
1843 /*
1844 * next, check the message byte.
1845 */
1846 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1847 return FAILED;
1848
1849 /*
1850 * check the status byte to see if this indicates anything special.
1851 */
1852 switch (status_byte(scmd->result)) {
1853 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001854 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * the case of trying to send too many commands to a
1857 * tagged queueing device.
1858 */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001859 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 case BUSY:
1861 /*
1862 * device can't talk to us at the moment. Should only
1863 * occur (SAM-3) when the task queue is empty, so will cause
1864 * the empty queue handling to trigger a stall in the
1865 * device.
1866 */
1867 return ADD_TO_MLQUEUE;
1868 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001869 if (scmd->cmnd[0] == REPORT_LUNS)
1870 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001871 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001872 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001875 case TASK_ABORTED:
1876 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case CHECK_CONDITION:
1878 rtn = scsi_check_sense(scmd);
1879 if (rtn == NEEDS_RETRY)
1880 goto maybe_retry;
1881 /* if rtn == FAILED, we have no sense information;
1882 * returning FAILED will wake the error handler thread
1883 * to collect the sense and redo the decide
1884 * disposition */
1885 return rtn;
1886 case CONDITION_GOOD:
1887 case INTERMEDIATE_GOOD:
1888 case INTERMEDIATE_C_GOOD:
1889 case ACA_ACTIVE:
1890 /*
1891 * who knows? FIXME(eric)
1892 */
1893 return SUCCESS;
1894
1895 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001896 sdev_printk(KERN_INFO, scmd->device,
1897 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001898 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return SUCCESS; /* causes immediate i/o error */
1900 default:
1901 return FAILED;
1902 }
1903 return FAILED;
1904
Jianchao Wang8ef7fe42018-02-26 15:59:16 +08001905maybe_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* we requeue for retry because the error was retryable, and
1908 * the request was not marked fast fail. Note that above,
1909 * even if the request is marked fast fail, we still requeue
1910 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001911 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001912 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return NEEDS_RETRY;
1914 } else {
1915 /*
1916 * no more retries - report this one back to upper level.
1917 */
1918 return SUCCESS;
1919 }
1920}
1921
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001922static void eh_lock_door_done(struct request *req, blk_status_t status)
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001923{
1924 __blk_put_request(req->q, req);
1925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 * scsi_eh_lock_door - Prevent medium removal for the specified device
1929 * @sdev: SCSI device to prevent medium removal
1930 *
1931 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001932 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 *
1934 * Notes:
1935 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1936 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001937 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938static void scsi_eh_lock_door(struct scsi_device *sdev)
1939{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001940 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001941 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
James Bottomley91bc31f2009-05-17 09:30:48 -05001943 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001944 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001945 * request becomes available
1946 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001947 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001948 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001949 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001950 rq = scsi_req(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001952 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1953 rq->cmd[1] = 0;
1954 rq->cmd[2] = 0;
1955 rq->cmd[3] = 0;
1956 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1957 rq->cmd[5] = 0;
1958 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001959
Christoph Hellwige8064022016-10-20 15:12:13 +02001960 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001961 req->timeout = 10 * HZ;
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001962 rq->retries = 5;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001963
1964 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967/**
1968 * scsi_restart_operations - restart io operations to the specified host.
1969 * @shost: Host we are restarting.
1970 *
1971 * Notes:
1972 * When we entered the error handler, we blocked all further i/o to
1973 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975static void scsi_restart_operations(struct Scsi_Host *shost)
1976{
1977 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001978 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
1981 * If the door was locked, we need to insert a door lock request
1982 * onto the head of the SCSI request queue for the device. There
1983 * is no point trying to lock the door of an off-line device.
1984 */
1985 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001986 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001988 sdev->was_reset = 0;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
1992 /*
1993 * next free up anything directly waiting upon the host. this
1994 * will be requests for character device operations, and also for
1995 * ioctls to queued block devices.
1996 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02001997 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02001998 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
James Bottomley939647e2005-09-18 15:05:20 -05002000 spin_lock_irqsave(shost->host_lock, flags);
2001 if (scsi_host_set_state(shost, SHOST_RUNNING))
2002 if (scsi_host_set_state(shost, SHOST_CANCEL))
2003 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2004 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 wake_up(&shost->host_wait);
2007
2008 /*
2009 * finally we need to re-initiate requests that may be pending. we will
2010 * have had everything blocked while error handling is taking place, and
2011 * now that error recovery is done, we will need to ensure that these
2012 * requests are started.
2013 */
2014 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002015
2016 /*
2017 * if eh is active and host_eh_scheduled is pending we need to re-run
2018 * recovery. we do this check after scsi_run_host_queues() to allow
2019 * everything pent up since the last eh run a chance to make forward
2020 * progress before we sync again. Either we'll immediately re-run
2021 * recovery or scsi_device_unbusy() will wake us again when these
2022 * pending commands complete.
2023 */
2024 spin_lock_irqsave(shost->host_lock, flags);
2025 if (shost->host_eh_scheduled)
2026 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2027 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2028 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
2031/**
2032 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002033 * @shost: host to be recovered.
2034 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002035 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002036 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002037void scsi_eh_ready_devs(struct Scsi_Host *shost,
2038 struct list_head *work_q,
2039 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 if (!scsi_eh_stu(shost, work_q, done_q))
2042 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002043 if (!scsi_eh_target_reset(shost, work_q, done_q))
2044 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002045 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002046 scsi_eh_offline_sdevs(work_q,
2047 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002049EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051/**
2052 * scsi_eh_flush_done_q - finish processed commands or retry them.
2053 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002054 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002055void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002057 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002059 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2060 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002062 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002063 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002064 SCSI_LOG_ERROR_RECOVERY(3,
2065 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002066 "%s: flush retry cmd\n",
2067 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2069 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002070 /*
2071 * If just we got sense for the device (called
2072 * scsi_eh_get_sense), scmd->result is already
2073 * set, do not set DRIVER_TIMEOUT.
2074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (!scmd->result)
2076 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002077 SCSI_LOG_ERROR_RECOVERY(3,
2078 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002079 "%s: flush finish cmd\n",
2080 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 scsi_finish_command(scmd);
2082 }
2083 }
2084}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002085EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087/**
2088 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2089 * @shost: Host to unjam.
2090 *
2091 * Notes:
2092 * When we come in here, we *know* that all commands on the bus have
2093 * either completed, failed or timed out. we also know that no further
2094 * commands are being sent to the host, so things are relatively quiet
2095 * and we have freedom to fiddle with things as we wish.
2096 *
2097 * This is only the *default* implementation. it is possible for
2098 * individual drivers to supply their own version of this function, and
2099 * if the maintainer wishes to do this, it is strongly suggested that
2100 * this function be taken as a template and modified. this function
2101 * was designed to correctly handle problems for about 95% of the
2102 * different cases out there, and it should always provide at least a
2103 * reasonable amount of error recovery.
2104 *
2105 * Any command marked 'failed' or 'timeout' must eventually have
2106 * scsi_finish_cmd() called for it. we do all of the retry stuff
2107 * here, so when we restart the host after we return it should have an
2108 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110static void scsi_unjam_host(struct Scsi_Host *shost)
2111{
2112 unsigned long flags;
2113 LIST_HEAD(eh_work_q);
2114 LIST_HEAD(eh_done_q);
2115
2116 spin_lock_irqsave(shost->host_lock, flags);
2117 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2118 spin_unlock_irqrestore(shost->host_lock, flags);
2119
2120 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2121
2122 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002123 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Hannes Reineckeb4562022013-10-23 10:51:21 +02002125 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002126 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002127 shost->last_reset = 0;
2128 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 scsi_eh_flush_done_q(&eh_done_q);
2130}
2131
2132/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002133 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 * @data: Host for which we are running.
2135 *
2136 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002137 * This is the main error handling loop. This is run as a kernel thread
2138 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140int scsi_error_handler(void *data)
2141{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002142 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002145 * We use TASK_INTERRUPTIBLE so that the thread is not
2146 * counted against the load average as a running process.
2147 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002148 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
Michal Hocko537b6042015-08-27 20:16:37 +02002150 while (true) {
2151 /*
2152 * The sequence in kthread_stop() sets the stop flag first
2153 * then wakes the process. To avoid missed wakeups, the task
2154 * should always be in a non running state before the stop
2155 * flag is checked
2156 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002157 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002158 if (kthread_should_stop())
2159 break;
2160
Tejun Heoee7863b2006-05-15 20:57:20 +09002161 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002162 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002163 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002164 shost_printk(KERN_INFO, shost,
2165 "scsi_eh_%d: sleeping\n",
2166 shost->host_no));
James Bottomley3ed7a4702005-09-19 09:50:04 -05002167 schedule();
James Bottomley3ed7a4702005-09-19 09:50:04 -05002168 continue;
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
James Bottomley3ed7a4702005-09-19 09:50:04 -05002171 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002172 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002173 shost_printk(KERN_INFO, shost,
2174 "scsi_eh_%d: waking up %d/%d/%d\n",
2175 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002176 shost->host_failed,
2177 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * We have a host that is failing for some reason. Figure out
2181 * what we need to do to get it up and online again (if we can).
2182 * If we fail, we end up taking the thing offline.
2183 */
Lin Mingae0751f2011-12-05 09:20:24 +08002184 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002185 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002186 shost_printk(KERN_ERR, shost,
2187 "scsi_eh_%d: unable to autoresume\n",
2188 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002189 continue;
2190 }
2191
Christoph Hellwig9227c332006-04-01 19:21:04 +02002192 if (shost->transportt->eh_strategy_handler)
2193 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 else
2195 scsi_unjam_host(shost);
2196
Wei Fang72d8c362016-06-07 14:53:56 +08002197 /* All scmds have been handled */
2198 shost->host_failed = 0;
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /*
2201 * Note - if the above fails completely, the action is to take
2202 * individual devices offline and flush the queue of any
2203 * outstanding requests that may have been pending. When we
2204 * restart, we restart any I/O to any other devices on the bus
2205 * which are still online.
2206 */
2207 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002208 if (!shost->eh_noresume)
2209 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002211 __set_current_state(TASK_RUNNING);
2212
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002213 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002214 shost_printk(KERN_INFO, shost,
2215 "Error handler scsi_eh_%d exiting\n",
2216 shost->host_no));
James Bottomley3ed7a4702005-09-19 09:50:04 -05002217 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 0;
2219}
2220
2221/*
2222 * Function: scsi_report_bus_reset()
2223 *
2224 * Purpose: Utility function used by low-level drivers to report that
2225 * they have observed a bus reset on the bus being handled.
2226 *
2227 * Arguments: shost - Host in question
2228 * channel - channel on which reset was observed.
2229 *
2230 * Returns: Nothing
2231 *
2232 * Lock status: Host lock must be held.
2233 *
2234 * Notes: This only needs to be called if the reset is one which
2235 * originates from an unknown location. Resets originated
2236 * by the mid-level itself don't need to call this, but there
2237 * should be no harm.
2238 *
2239 * The main purpose of this is to make sure that a CHECK_CONDITION
2240 * is properly treated.
2241 */
2242void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2243{
2244 struct scsi_device *sdev;
2245
2246 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002247 if (channel == sdev_channel(sdev))
2248 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250}
2251EXPORT_SYMBOL(scsi_report_bus_reset);
2252
2253/*
2254 * Function: scsi_report_device_reset()
2255 *
2256 * Purpose: Utility function used by low-level drivers to report that
2257 * they have observed a device reset on the device being handled.
2258 *
2259 * Arguments: shost - Host in question
2260 * channel - channel on which reset was observed
2261 * target - target on which reset was observed
2262 *
2263 * Returns: Nothing
2264 *
2265 * Lock status: Host lock must be held
2266 *
2267 * Notes: This only needs to be called if the reset is one which
2268 * originates from an unknown location. Resets originated
2269 * by the mid-level itself don't need to call this, but there
2270 * should be no harm.
2271 *
2272 * The main purpose of this is to make sure that a CHECK_CONDITION
2273 * is properly treated.
2274 */
2275void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2276{
2277 struct scsi_device *sdev;
2278
2279 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002280 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002281 target == sdev_id(sdev))
2282 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284}
2285EXPORT_SYMBOL(scsi_report_device_reset);
2286
2287static void
2288scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2289{
2290}
2291
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002292/**
2293 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2294 * @dev: scsi_device to operate on
2295 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 */
2297int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002298scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Alan Sternbc4f2402010-06-17 10:41:42 -04002300 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002301 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002302 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002303 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002304 int error = 0, rtn, val;
2305
2306 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2307 return -EACCES;
2308
2309 error = get_user(val, arg);
2310 if (error)
2311 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Alan Sternbc4f2402010-06-17 10:41:42 -04002313 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002314 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002315
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002316 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002317 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2318 shost->hostt->cmd_size, GFP_KERNEL);
2319 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002320 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002321 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002322
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002323 scmd = (struct scsi_cmnd *)(rq + 1);
2324 scsi_init_command(dev, scmd);
2325 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002326 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002329 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 scmd->cmd_len = 0;
2332
2333 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
James Smartd7a1bb02006-03-08 14:50:12 -05002335 spin_lock_irqsave(shost->host_lock, flags);
2336 shost->tmf_in_progress = 1;
2337 spin_unlock_irqrestore(shost->host_lock, flags);
2338
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002339 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2340 case SG_SCSI_RESET_NOTHING:
2341 rtn = SUCCESS;
2342 break;
2343 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002345 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 break;
2347 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002348 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002349 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002350 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002351 break;
2352 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002353 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002355 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 break;
2357 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002358 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002360 if (rtn == SUCCESS)
2361 break;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002362 /* FALLTHROUGH */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07002363 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002368 error = (rtn == SUCCESS) ? 0 : -EIO;
2369
James Smartd7a1bb02006-03-08 14:50:12 -05002370 spin_lock_irqsave(shost->host_lock, flags);
2371 shost->tmf_in_progress = 0;
2372 spin_unlock_irqrestore(shost->host_lock, flags);
2373
2374 /*
2375 * be sure to wake up anyone who was sleeping or had their queue
2376 * suspended while we performed the TMF.
2377 */
2378 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002379 shost_printk(KERN_INFO, shost,
2380 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002381
2382 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002383 scsi_run_host_queues(shost);
2384
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002385 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002386 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002387
Christoph Hellwig04796332014-02-20 14:20:55 -08002388out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002389 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002390 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002392EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002394bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2395 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002398 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400EXPORT_SYMBOL(scsi_command_normalize_sense);
2401
2402/**
Rob Landleyeb448202007-11-03 13:30:39 -05002403 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * @sense_buffer: byte array of sense data
2405 * @sb_len: number of valid bytes in sense_buffer
2406 * @info_out: pointer to 64 integer where 8 or 4 byte information
2407 * field will be placed if found.
2408 *
2409 * Return value:
Damien Le Moal29087692017-04-24 16:51:12 +09002410 * true if information field found, false if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002411 */
Damien Le Moal29087692017-04-24 16:51:12 +09002412bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2413 u64 *info_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 const u8 * ucp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 if (sb_len < 7)
Damien Le Moal29087692017-04-24 16:51:12 +09002418 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 switch (sense_buffer[0] & 0x7f) {
2420 case 0x70:
2421 case 0x71:
2422 if (sense_buffer[0] & 0x80) {
Damien Le Moal29087692017-04-24 16:51:12 +09002423 *info_out = get_unaligned_be32(&sense_buffer[3]);
2424 return true;
2425 }
2426 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 case 0x72:
2428 case 0x73:
2429 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2430 0 /* info desc */);
2431 if (ucp && (0xa == ucp[1])) {
Damien Le Moal29087692017-04-24 16:51:12 +09002432 *info_out = get_unaligned_be64(&ucp[4]);
2433 return true;
2434 }
2435 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 default:
Damien Le Moal29087692017-04-24 16:51:12 +09002437 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439}
2440EXPORT_SYMBOL(scsi_get_sense_info_fld);