blob: b7a8fdfeb2f47babeb014a55c9357b8b4f87fb34 [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
Ming Leic84b0232018-06-24 22:03:26 +080069 if (scsi_host_busy(shost) == 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);
Christoph Hellwig66005932018-05-29 15:52:29 +0200286 enum blk_eh_timer_return rtn = BLK_EH_DONE;
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
Christoph Hellwig66005932018-05-29 15:52:29 +0200298 if (rtn == BLK_EH_DONE) {
Keith Busch065990b2018-07-23 08:37:51 -0600299 /*
300 * For blk-mq, we must set the request state to complete now
301 * before sending the request to the scsi error handler. This
302 * will prevent a use-after-free in the event the LLD manages
303 * to complete the request before the error handler finishes
304 * processing this timed out request.
305 *
306 * If the request was already completed, then the LLD beat the
307 * time out handler from transferring the request to the scsi
308 * error handler. In that case we can return immediately as no
309 * further action is required.
310 */
311 if (req->q->mq_ops && !blk_mq_mark_complete(req))
312 return rtn;
Hannes Reineckea0658632017-04-06 15:36:35 +0200313 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200314 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200315 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200316 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200317 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700318
Christoph Hellwigfa990782008-11-05 15:03:15 +0100319 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/**
323 * scsi_block_when_processing_errors - Prevent cmds from being queued.
324 * @sdev: Device on which we are performing recovery.
325 *
326 * Description:
327 * We block until the host is out of error recovery, and then check to
328 * see whether the host or the device is offline.
329 *
330 * Return value:
331 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333int scsi_block_when_processing_errors(struct scsi_device *sdev)
334{
335 int online;
336
James Bottomley939647e2005-09-18 15:05:20 -0500337 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 online = scsi_device_online(sdev);
340
Hannes Reinecke91921e02014-06-25 16:39:59 +0200341 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
342 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 return online;
345}
346EXPORT_SYMBOL(scsi_block_when_processing_errors);
347
348#ifdef CONFIG_SCSI_LOGGING
349/**
350 * scsi_eh_prt_fail_stats - Log info on failures.
351 * @shost: scsi host being recovered.
352 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
355 struct list_head *work_q)
356{
357 struct scsi_cmnd *scmd;
358 struct scsi_device *sdev;
359 int total_failures = 0;
360 int cmd_failed = 0;
361 int cmd_cancel = 0;
362 int devices_failed = 0;
363
364 shost_for_each_device(sdev, shost) {
365 list_for_each_entry(scmd, work_q, eh_entry) {
366 if (scmd->device == sdev) {
367 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200368 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100370 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ++cmd_failed;
372 }
373 }
374
375 if (cmd_cancel || cmd_failed) {
376 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200377 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500378 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700379 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500380 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 cmd_cancel = 0;
382 cmd_failed = 0;
383 ++devices_failed;
384 }
385 }
386
Hannes Reinecke91921e02014-06-25 16:39:59 +0200387 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
388 "Total of %d commands on %d"
389 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100390 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392#endif
393
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400394 /**
395 * scsi_report_lun_change - Set flag on all *other* devices on the same target
396 * to indicate that a UNIT ATTENTION is expected.
397 * @sdev: Device reporting the UNIT ATTENTION
398 */
399static void scsi_report_lun_change(struct scsi_device *sdev)
400{
401 sdev->sdev_target->expecting_lun_change = 1;
402}
403
404/**
405 * scsi_report_sense - Examine scsi sense information and log messages for
406 * certain conditions, also issue uevents for some of them.
407 * @sdev: Device reporting the sense code
408 * @sshdr: sshdr to be examined
409 */
410static void scsi_report_sense(struct scsi_device *sdev,
411 struct scsi_sense_hdr *sshdr)
412{
413 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
414
415 if (sshdr->sense_key == UNIT_ATTENTION) {
416 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
417 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
418 sdev_printk(KERN_WARNING, sdev,
419 "Inquiry data has changed");
420 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
421 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
422 scsi_report_lun_change(sdev);
423 sdev_printk(KERN_WARNING, sdev,
424 "Warning! Received an indication that the "
425 "LUN assignments on this target have "
426 "changed. The Linux SCSI layer does not "
427 "automatically remap LUN assignments.\n");
428 } else if (sshdr->asc == 0x3f)
429 sdev_printk(KERN_WARNING, sdev,
430 "Warning! Received an indication that the "
431 "operating parameters on this target have "
432 "changed. The Linux SCSI layer does not "
433 "automatically adjust these parameters.\n");
434
435 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
436 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
437 sdev_printk(KERN_WARNING, sdev,
438 "Warning! Received an indication that the "
439 "LUN reached a thin provisioning soft "
440 "threshold.\n");
441 }
442
Hannes Reineckecf3431b2017-10-17 09:11:24 +0200443 if (sshdr->asc == 0x29) {
444 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
445 sdev_printk(KERN_WARNING, sdev,
446 "Power-on or device reset occurred\n");
447 }
448
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400449 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
450 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
451 sdev_printk(KERN_WARNING, sdev,
452 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200453 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
454 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
455 sdev_printk(KERN_WARNING, sdev,
456 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400457 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
458 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
459 sdev_printk(KERN_WARNING, sdev,
460 "Capacity data has changed");
461 } else if (sshdr->asc == 0x2a)
462 sdev_printk(KERN_WARNING, sdev,
463 "Parameters changed");
464 }
465
466 if (evt_type != SDEV_EVT_MAXBITS) {
467 set_bit(evt_type, sdev->pending_events);
468 schedule_work(&sdev->event_work);
469 }
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472/**
473 * scsi_check_sense - Examine scsi cmd sense
474 * @scmd: Cmd to have sense checked.
475 *
476 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200477 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *
479 * Notes:
480 * When a deferred error is detected the current command has
481 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800482 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200483int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700485 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct scsi_sense_hdr sshdr;
487
488 if (! scsi_command_normalize_sense(scmd, &sshdr))
489 return FAILED; /* no valid sense data */
490
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400491 scsi_report_sense(sdev, &sshdr);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (scsi_sense_is_deferred(&sshdr))
494 return NEEDS_RETRY;
495
Christoph Hellwigee14c672015-08-27 14:16:59 +0200496 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700497 int rc;
498
Christoph Hellwigee14c672015-08-27 14:16:59 +0200499 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700500 if (rc != SCSI_RETURN_NOT_HANDLED)
501 return rc;
502 /* handler does not care. Drop down to default handling */
503 }
504
Christoph Hellwige925cc42014-11-06 15:11:22 -0600505 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
506 /*
507 * nasty: for mid-layer issued TURs, we need to return the
508 * actual sense data without any recovery attempt. For eh
509 * issued ones, we need to try to recover and interpret
510 */
511 return SUCCESS;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
514 * Previous logic looked for FILEMARK, EOM or ILI which are
515 * mainly associated with tapes and returned SUCCESS.
516 */
517 if (sshdr.response_code == 0x70) {
518 /* fixed format */
519 if (scmd->sense_buffer[2] & 0xe0)
520 return SUCCESS;
521 } else {
522 /*
523 * descriptor format: look for "stream commands sense data
524 * descriptor" (see SSC-3). Assume single sense data
525 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
526 */
527 if ((sshdr.additional_length > 3) &&
528 (scmd->sense_buffer[8] == 0x4) &&
529 (scmd->sense_buffer[11] & 0xe0))
530 return SUCCESS;
531 }
532
533 switch (sshdr.sense_key) {
534 case NO_SENSE:
535 return SUCCESS;
536 case RECOVERED_ERROR:
537 return /* soft_error */ SUCCESS;
538
539 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400540 if (sshdr.asc == 0x10) /* DIF */
541 return SUCCESS;
542
Martin Wilck29cfc2a2018-04-18 01:35:10 +0200543 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
544 return ADD_TO_MLQUEUE;
Martin Wilckc3606522018-04-18 01:35:11 +0200545 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
546 sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
547 return ADD_TO_MLQUEUE;
Martin Wilck29cfc2a2018-04-18 01:35:10 +0200548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return NEEDS_RETRY;
550 case NOT_READY:
551 case UNIT_ATTENTION:
552 /*
553 * if we are expecting a cc/ua because of a bus reset that we
554 * performed, treat this just as a retry. otherwise this is
555 * information that we should pass up to the upper-level driver
556 * so that we can deal with it there.
557 */
558 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900559 /*
560 * Because some device does not queue unit
561 * attentions correctly, we carefully check
562 * additional sense code and qualifier so as
563 * not to squash media change unit attention.
564 */
565 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
566 scmd->device->expecting_cc_ua = 0;
567 return NEEDS_RETRY;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400571 * we might also expect a cc/ua if another LUN on the target
572 * reported a UA with an ASC/ASCQ of 3F 0E -
573 * REPORTED LUNS DATA HAS CHANGED.
574 */
575 if (scmd->device->sdev_target->expecting_lun_change &&
576 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
577 return NEEDS_RETRY;
578 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100579 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * should retry.
581 */
582 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
583 return NEEDS_RETRY;
584 /*
585 * if the device is not started, we need to wake
586 * the error handler to start the motor
587 */
588 if (scmd->device->allow_restart &&
589 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
590 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100591 /*
592 * Pass the UA upwards for a determination in the completion
593 * functions.
594 */
595 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100597 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200598 case DATA_PROTECT:
599 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
600 /* Thin provisioning hard threshold reached */
601 set_host_byte(scmd, DID_ALLOC_FAILURE);
602 return SUCCESS;
603 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700604 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 case COPY_ABORTED:
606 case VOLUME_OVERFLOW:
607 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100608 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200609 set_host_byte(scmd, DID_TARGET_FAILURE);
610 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800613 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
614 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
615 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200616 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200617 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return NEEDS_RETRY;
620
621 case HARDWARE_ERROR:
622 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700623 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200625 set_host_byte(scmd, DID_TARGET_FAILURE);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700626 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500629 if (sshdr.asc == 0x20 || /* Invalid command operation code */
630 sshdr.asc == 0x21 || /* Logical block address out of range */
Hannes Reineckea8bbb2a2017-10-17 09:10:56 +0200631 sshdr.asc == 0x22 || /* Invalid function */
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500632 sshdr.asc == 0x24 || /* Invalid field in cdb */
Martin Wilckd0b7a902017-09-27 14:44:19 +0200633 sshdr.asc == 0x26 || /* Parameter value invalid */
634 sshdr.asc == 0x27) { /* Write protected */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200635 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500636 }
637 return SUCCESS;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 default:
640 return SUCCESS;
641 }
642}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200643EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Vasu Dev4a840672009-10-22 15:46:33 -0700645static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
646{
647 struct scsi_host_template *sht = sdev->host->hostt;
648 struct scsi_device *tmp_sdev;
649
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100650 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700651 sdev->queue_depth >= sdev->max_queue_depth)
652 return;
653
654 if (time_before(jiffies,
655 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
656 return;
657
658 if (time_before(jiffies,
659 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
660 return;
661
662 /*
663 * Walk all devices of a target and do
664 * ramp up on them.
665 */
666 shost_for_each_device(tmp_sdev, sdev->host) {
667 if (tmp_sdev->channel != sdev->channel ||
668 tmp_sdev->id != sdev->id ||
669 tmp_sdev->queue_depth == sdev->max_queue_depth)
670 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100671
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100672 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700673 sdev->last_queue_ramp_up = jiffies;
674 }
675}
676
Mike Christie42a6a912009-10-15 17:46:44 -0700677static void scsi_handle_queue_full(struct scsi_device *sdev)
678{
679 struct scsi_host_template *sht = sdev->host->hostt;
680 struct scsi_device *tmp_sdev;
681
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100682 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700683 return;
684
685 shost_for_each_device(tmp_sdev, sdev->host) {
686 if (tmp_sdev->channel != sdev->channel ||
687 tmp_sdev->id != sdev->id)
688 continue;
689 /*
690 * We do not know the number of commands that were at
691 * the device when we got the queue full so we start
692 * from the highest possible value and work our way down.
693 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100694 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700695 }
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/**
699 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
700 * @scmd: SCSI cmd to examine.
701 *
702 * Notes:
703 * This is *only* called when we are examining the status of commands
704 * queued during error recovery. the main difference here is that we
705 * don't allow for the possibility of retries here, and we are a lot
706 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
709{
710 /*
711 * first check the host byte, to see if there is anything in there
712 * that would indicate what we need to do.
713 */
714 if (host_byte(scmd->result) == DID_RESET) {
715 /*
716 * rats. we are already in the error handler, so we now
717 * get to try and figure out what to do next. if the sense
718 * is valid, we have a pretty good idea of what to do.
719 * if not, we mark it as FAILED.
720 */
721 return scsi_check_sense(scmd);
722 }
723 if (host_byte(scmd->result) != DID_OK)
724 return FAILED;
725
726 /*
727 * next, check the message byte.
728 */
729 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
730 return FAILED;
731
732 /*
733 * now, check the status byte to see if this indicates
734 * anything special.
735 */
736 switch (status_byte(scmd->result)) {
737 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700738 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700739 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 case COMMAND_TERMINATED:
741 return SUCCESS;
742 case CHECK_CONDITION:
743 return scsi_check_sense(scmd);
744 case CONDITION_GOOD:
745 case INTERMEDIATE_GOOD:
746 case INTERMEDIATE_C_GOOD:
747 /*
748 * who knows? FIXME(eric)
749 */
750 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500751 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500752 if (scmd->cmnd[0] == TEST_UNIT_READY)
753 /* it is a success, we probed the device and
754 * found it */
755 return SUCCESS;
756 /* otherwise, we failed to send the command */
757 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700759 scsi_handle_queue_full(scmd->device);
760 /* fall through */
761 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200762 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 default:
764 return FAILED;
765 }
766 return FAILED;
767}
768
769/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * scsi_eh_done - Completion function for error handling.
771 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static void scsi_eh_done(struct scsi_cmnd *scmd)
774{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100775 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600776
Hannes Reinecke91921e02014-06-25 16:39:59 +0200777 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100778 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600779
780 eh_action = scmd->device->host->eh_action;
781 if (eh_action)
782 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785/**
Brian King292148f2007-01-30 17:51:17 -0600786 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200787 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800788 */
Brian King292148f2007-01-30 17:51:17 -0600789static int scsi_try_host_reset(struct scsi_cmnd *scmd)
790{
791 unsigned long flags;
792 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100793 struct Scsi_Host *host = scmd->device->host;
794 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600795
Hannes Reinecke91921e02014-06-25 16:39:59 +0200796 SCSI_LOG_ERROR_RECOVERY(3,
797 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600798
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100799 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600800 return FAILED;
801
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100802 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600803
804 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100805 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600806 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100807 spin_lock_irqsave(host->host_lock, flags);
808 scsi_report_bus_reset(host, scmd_channel(scmd));
809 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600810 }
811
812 return rtn;
813}
814
815/**
816 * scsi_try_bus_reset - ask host to perform a bus reset
817 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800818 */
Brian King292148f2007-01-30 17:51:17 -0600819static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
820{
821 unsigned long flags;
822 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100823 struct Scsi_Host *host = scmd->device->host;
824 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600825
Hannes Reinecke91921e02014-06-25 16:39:59 +0200826 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
827 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600828
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100829 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600830 return FAILED;
831
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100832 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600833
834 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100835 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600836 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100837 spin_lock_irqsave(host->host_lock, flags);
838 scsi_report_bus_reset(host, scmd_channel(scmd));
839 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600840 }
841
842 return rtn;
843}
844
Mike Christie30bd7df2008-02-29 18:25:19 -0600845static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
846{
847 sdev->was_reset = 1;
848 sdev->expecting_cc_ua = 1;
849}
850
851/**
852 * scsi_try_target_reset - Ask host to perform a target reset
853 * @scmd: SCSI cmd used to send a target reset
854 *
855 * Notes:
856 * There is no timeout for this operation. if this operation is
857 * unreliable for a given host, then the host itself needs to put a
858 * timer on it, and set the host back to a consistent state prior to
859 * returning.
860 */
861static int scsi_try_target_reset(struct scsi_cmnd *scmd)
862{
863 unsigned long flags;
864 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100865 struct Scsi_Host *host = scmd->device->host;
866 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600867
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100868 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600869 return FAILED;
870
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100871 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600872 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100873 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600874 __starget_for_each_device(scsi_target(scmd->device), NULL,
875 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100876 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600877 }
878
879 return rtn;
880}
881
Brian King292148f2007-01-30 17:51:17 -0600882/**
883 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
884 * @scmd: SCSI cmd used to send BDR
885 *
886 * Notes:
887 * There is no timeout for this operation. if this operation is
888 * unreliable for a given host, then the host itself needs to put a
889 * timer on it, and set the host back to a consistent state prior to
890 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800891 */
Brian King292148f2007-01-30 17:51:17 -0600892static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
893{
894 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100895 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600896
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100897 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600898 return FAILED;
899
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100900 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600901 if (rtn == SUCCESS)
902 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600903 return rtn;
904}
905
Hannes Reinecke883a0302014-10-24 14:27:04 +0200906/**
907 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800908 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200909 * @scmd: SCSI cmd used to send a target reset
910 *
911 * Return value:
912 * SUCCESS, FAILED, or FAST_IO_FAIL
913 *
914 * Notes:
915 * SUCCESS does not necessarily indicate that the command
916 * has been aborted; it only indicates that the LLDDs
917 * has cleared all references to that command.
918 * LLDDs should return FAILED only if an abort was required
919 * but could not be executed. LLDDs should return FAST_IO_FAIL
920 * if the device is temporarily unavailable (eg due to a
921 * link down on FibreChannel)
922 */
923static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
924 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600925{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100926 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600927 return FAILED;
928
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100929 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600930}
931
Brian King292148f2007-01-30 17:51:17 -0600932static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
933{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100934 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600935 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600936 if (scsi_try_target_reset(scmd) != SUCCESS)
937 if (scsi_try_bus_reset(scmd) != SUCCESS)
938 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600939}
940
941/**
Santosh Y3b729f72012-04-08 18:47:09 +0530942 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100943 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200944 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200945 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300946 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200947 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100948 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200949 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200950 * as part of the error recovery process. If @sense_bytes is 0 the command
951 * sent must be one that does not transfer any data. If @sense_bytes != 0
952 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
953 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800954 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200955void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
956 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
f59114b2005-04-17 15:00:23 -0500958 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Christoph Hellwig631c2282006-07-08 20:42:15 +0200960 /*
961 * We need saved copies of a number of fields - this is because
962 * error handling may need to overwrite these with different values
963 * to run different commands, and once error handling is complete,
964 * we will need to restore these values prior to running the actual
965 * command.
966 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200967 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300968 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200969 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200970 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200971 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200972 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400973 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400974 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200975 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200976
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400977 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700978 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300979 scmd->cmnd = ses->eh_cmnd;
980 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200981 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200982 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700983 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200984
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200985 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200986 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
987 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200988 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200989 scmd->sdb.length);
990 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200991 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400992 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200993 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200994 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200995 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200996 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200997 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200998 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300999 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +02001000 memcpy(scmd->cmnd, cmnd, cmnd_size);
1001 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1002 }
Christoph Hellwig631c2282006-07-08 20:42:15 +02001003 }
1004
1005 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001006
Boaz Harrosh55db6c12007-10-08 16:35:19 +02001007 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -05001009 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Christoph Hellwig631c2282006-07-08 20:42:15 +02001011 /*
1012 * Zero the sense buffer. The scsi spec mandates that any
1013 * untransferred sense data should be interpreted as being zero.
1014 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001015 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001016}
1017EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +02001018
Boaz Harroshe1c23462007-10-08 16:36:45 +02001019/**
Santosh Y3b729f72012-04-08 18:47:09 +05301020 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001021 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001022 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +02001023 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001024 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001025 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001026void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1027{
1028 /*
1029 * Restore original data
1030 */
1031 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001032 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001033 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001034 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001035 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001036 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001037 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001038 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +02001039 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001040}
1041EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1042
1043/**
Santosh Y3b729f72012-04-08 18:47:09 +05301044 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001045 * @scmd: SCSI command structure to hijack
1046 * @cmnd: CDB to send
1047 * @cmnd_size: size in bytes of @cmnd
1048 * @timeout: timeout for this request
1049 * @sense_bytes: size of sense data to copy or 0
1050 *
1051 * This function is used to send a scsi command down to a target device
1052 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1053 *
1054 * Return value:
1055 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001056 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001057static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1058 int cmnd_size, int timeout, unsigned sense_bytes)
1059{
1060 struct scsi_device *sdev = scmd->device;
1061 struct Scsi_Host *shost = sdev->host;
1062 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001063 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001064 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001065 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001066 int rtn;
1067
Hannes Reineckefc736482013-04-25 08:10:00 +02001068retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001069 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001070 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001073 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001074 rtn = shost->hostt->queuecommand(shost, scmd);
1075 if (rtn) {
1076 if (timeleft > stall_for) {
1077 scsi_eh_restore_cmnd(scmd, &ses);
1078 timeleft -= stall_for;
1079 msleep(jiffies_to_msecs(stall_for));
1080 goto retry;
1081 }
1082 /* signal not to enter either branch of the if () below */
1083 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001084 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001085 } else {
1086 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001087 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
f59114b2005-04-17 15:00:23 -05001090 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Hannes Reineckefc736482013-04-25 08:10:00 +02001092 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Hannes Reinecke91921e02014-06-25 16:39:59 +02001094 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001095 "%s timeleft: %ld\n",
1096 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001099 * If there is time left scsi_eh_done got called, and we will examine
1100 * the actual status codes to see whether the command actually did
1101 * complete normally, else if we have a zero return and no time left,
1102 * the command must still be pending, so abort it and return FAILED.
1103 * If we never actually managed to issue the command, because
1104 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1105 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001107 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001109 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1110 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 switch (rtn) {
1113 case SUCCESS:
1114 case NEEDS_RETRY:
1115 case FAILED:
1116 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001117 case ADD_TO_MLQUEUE:
1118 rtn = NEEDS_RETRY;
1119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 default:
1121 rtn = FAILED;
1122 break;
1123 }
Alan Stern511833a2014-11-21 10:44:49 -05001124 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001125 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001126 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Boaz Harroshe1c23462007-10-08 16:36:45 +02001129 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return rtn;
1132}
1133
1134/**
1135 * scsi_request_sense - Request sense data from a particular target.
1136 * @scmd: SCSI cmd for request sense.
1137 *
1138 * Notes:
1139 * Some hosts automatically obtain this information, others require
1140 * that we obtain it on our own. This function will *not* return until
1141 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static int scsi_request_sense(struct scsi_cmnd *scmd)
1144{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001145 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
James Bottomley24510792013-11-11 13:44:53 +01001148static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1149{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001150 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001151 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1152 if (sdrv->eh_action)
1153 rtn = sdrv->eh_action(scmd, rtn);
1154 }
1155 return rtn;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/**
1159 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1160 * @scmd: Original SCSI cmd that eh has finished.
1161 * @done_q: Queue for processed commands.
1162 *
1163 * Notes:
1164 * We don't want to use the normal command completion while we are are
1165 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001166 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * keep a list of pending commands for final completion, and once we
1168 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001169 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001170void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 list_move_tail(&scmd->eh_entry, done_q);
1173}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001174EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176/**
1177 * scsi_eh_get_sense - Get device sense data.
1178 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001179 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 *
1181 * Description:
1182 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001183 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 *
1185 * Notes:
1186 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001187 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * it down before we request it.
1189 *
1190 * All drivers should request sense information internally these days,
1191 * so for now all I have to say is tough noogies if you end up in here.
1192 *
1193 * XXX: Long term this code should go away, but that needs an audit of
1194 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001195 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001196int scsi_eh_get_sense(struct list_head *work_q,
1197 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001199 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001200 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 int rtn;
1202
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001203 /*
1204 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1205 * should not get sense.
1206 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001207 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001208 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 SCSI_SENSE_VALID(scmd))
1210 continue;
1211
Hannes Reineckeb4562022013-10-23 10:51:21 +02001212 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001213 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001214 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001215 scmd_printk(KERN_INFO, scmd,
1216 "%s: skip request sense, past eh deadline\n",
1217 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001218 break;
1219 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001220 if (status_byte(scmd->result) != CHECK_CONDITION)
1221 /*
1222 * don't request sense if there's no check condition
1223 * status because the error we're processing isn't one
1224 * that has a sense code (and some devices get
1225 * confused by sense requests out of the blue)
1226 */
1227 continue;
1228
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001229 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1230 "%s: requesting sense\n",
1231 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 rtn = scsi_request_sense(scmd);
1233 if (rtn != SUCCESS)
1234 continue;
1235
Hannes Reinecke91921e02014-06-25 16:39:59 +02001236 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001237 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001238 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 rtn = scsi_decide_disposition(scmd);
1241
1242 /*
1243 * if the result was normal, then just pass it along to the
1244 * upper level.
1245 */
1246 if (rtn == SUCCESS)
1247 /* we don't want this command reissued, just
1248 * finished with the sense data, so set
1249 * retries to the max allowed to ensure it
1250 * won't get reissued */
1251 scmd->retries = scmd->allowed;
1252 else if (rtn != NEEDS_RETRY)
1253 continue;
1254
1255 scsi_eh_finish_cmd(scmd, done_q);
1256 }
1257
1258 return list_empty(work_q);
1259}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001260EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001264 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 *
1266 * Return value:
1267 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269static int scsi_eh_tur(struct scsi_cmnd *scmd)
1270{
1271 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1272 int retry_cnt = 1, rtn;
1273
1274retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001275 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1276 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Hannes Reinecke91921e02014-06-25 16:39:59 +02001278 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001279 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001280
1281 switch (rtn) {
1282 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (retry_cnt--)
1284 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001285 /*FALLTHRU*/
1286 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001287 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001288 default:
1289 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293/**
David Jeffery3eef6252011-05-19 14:41:12 -04001294 * scsi_eh_test_devices - check if devices are responding from error recovery.
1295 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001296 * @work_q: queue for commands which still need more error recovery
1297 * @done_q: queue for commands which are finished
1298 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001299 *
1300 * Decription:
1301 * Tests if devices are in a working state. Commands to devices now in
1302 * a working state are sent to the done_q while commands to devices which
1303 * are still failing to respond are returned to the work_q for more
1304 * processing.
1305 **/
1306static int scsi_eh_test_devices(struct list_head *cmd_list,
1307 struct list_head *work_q,
1308 struct list_head *done_q, int try_stu)
1309{
1310 struct scsi_cmnd *scmd, *next;
1311 struct scsi_device *sdev;
1312 int finish_cmds;
1313
1314 while (!list_empty(cmd_list)) {
1315 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1316 sdev = scmd->device;
1317
Hannes Reineckeb4562022013-10-23 10:51:21 +02001318 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001319 if (scsi_host_eh_past_deadline(sdev->host)) {
1320 /* Push items back onto work_q */
1321 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001322 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001323 sdev_printk(KERN_INFO, sdev,
1324 "%s: skip test device, past eh deadline",
1325 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001326 break;
1327 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001328 }
1329
David Jeffery3eef6252011-05-19 14:41:12 -04001330 finish_cmds = !scsi_device_online(scmd->device) ||
1331 (try_stu && !scsi_eh_try_stu(scmd) &&
1332 !scsi_eh_tur(scmd)) ||
1333 !scsi_eh_tur(scmd);
1334
1335 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1336 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001337 if (finish_cmds &&
1338 (try_stu ||
1339 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001340 scsi_eh_finish_cmd(scmd, done_q);
1341 else
1342 list_move_tail(&scmd->eh_entry, work_q);
1343 }
1344 }
1345 return list_empty(work_q);
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001350 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
1352 * Return value:
1353 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1356{
1357 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Christoph Hellwig631c2282006-07-08 20:42:15 +02001359 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001360 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Brian Kinged773e62007-03-29 15:25:52 -05001362 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001363 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001364
Christoph Hellwig631c2282006-07-08 20:42:15 +02001365 if (rtn == SUCCESS)
1366 return 0;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 1;
1370}
1371
1372 /**
1373 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001374 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001375 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001376 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 *
1378 * Notes:
1379 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001380 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382static int scsi_eh_stu(struct Scsi_Host *shost,
1383 struct list_head *work_q,
1384 struct list_head *done_q)
1385{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001386 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct scsi_device *sdev;
1388
1389 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001390 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001391 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001392 sdev_printk(KERN_INFO, sdev,
1393 "%s: skip START_UNIT, past eh deadline\n",
1394 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001395 break;
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 stu_scmd = NULL;
1398 list_for_each_entry(scmd, work_q, eh_entry)
1399 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1400 scsi_check_sense(scmd) == FAILED ) {
1401 stu_scmd = scmd;
1402 break;
1403 }
1404
1405 if (!stu_scmd)
1406 continue;
1407
Hannes Reinecke91921e02014-06-25 16:39:59 +02001408 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001409 sdev_printk(KERN_INFO, sdev,
1410 "%s: Sending START_UNIT\n",
1411 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 if (!scsi_eh_try_stu(stu_scmd)) {
1414 if (!scsi_device_online(sdev) ||
1415 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001416 list_for_each_entry_safe(scmd, next,
1417 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001418 if (scmd->device == sdev &&
1419 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 scsi_eh_finish_cmd(scmd, done_q);
1421 }
1422 }
1423 } else {
1424 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001425 sdev_printk(KERN_INFO, sdev,
1426 "%s: START_UNIT failed\n",
1427 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
1430
1431 return list_empty(work_q);
1432}
1433
1434
1435/**
1436 * scsi_eh_bus_device_reset - send bdr if needed
1437 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001438 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001439 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *
1441 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001442 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * devices that are jammed or not - if we have multiple devices, it
1444 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001445 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1448 struct list_head *work_q,
1449 struct list_head *done_q)
1450{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001451 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct scsi_device *sdev;
1453 int rtn;
1454
1455 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001456 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001457 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001458 sdev_printk(KERN_INFO, sdev,
1459 "%s: skip BDR, past eh deadline\n",
1460 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001461 break;
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 bdr_scmd = NULL;
1464 list_for_each_entry(scmd, work_q, eh_entry)
1465 if (scmd->device == sdev) {
1466 bdr_scmd = scmd;
1467 break;
1468 }
1469
1470 if (!bdr_scmd)
1471 continue;
1472
Hannes Reinecke91921e02014-06-25 16:39:59 +02001473 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001474 sdev_printk(KERN_INFO, sdev,
1475 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001477 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001479 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001481 list_for_each_entry_safe(scmd, next,
1482 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001483 if (scmd->device == sdev &&
1484 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 scsi_eh_finish_cmd(scmd,
1486 done_q);
1487 }
1488 }
1489 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001490 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001491 sdev_printk(KERN_INFO, sdev,
1492 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 }
1495
1496 return list_empty(work_q);
1497}
1498
1499/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001500 * scsi_eh_target_reset - send target reset if needed
1501 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001502 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001503 * @done_q: &list_head for processed commands.
1504 *
1505 * Notes:
1506 * Try a target reset.
1507 */
1508static int scsi_eh_target_reset(struct Scsi_Host *shost,
1509 struct list_head *work_q,
1510 struct list_head *done_q)
1511{
James Bottomley98db5192010-10-25 15:53:41 -05001512 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001513 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001514
James Bottomley98db5192010-10-25 15:53:41 -05001515 list_splice_init(work_q, &tmp_list);
1516
1517 while (!list_empty(&tmp_list)) {
1518 struct scsi_cmnd *next, *scmd;
1519 int rtn;
1520 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001521
Hannes Reineckeb4562022013-10-23 10:51:21 +02001522 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001523 /* push back on work queue for further processing */
1524 list_splice_init(&check_list, work_q);
1525 list_splice_init(&tmp_list, work_q);
1526 SCSI_LOG_ERROR_RECOVERY(3,
1527 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001528 "%s: Skip target reset, past eh deadline\n",
1529 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001530 return list_empty(work_q);
1531 }
James Bottomley98db5192010-10-25 15:53:41 -05001532
1533 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1534 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001535
Hannes Reinecke91921e02014-06-25 16:39:59 +02001536 SCSI_LOG_ERROR_RECOVERY(3,
1537 shost_printk(KERN_INFO, shost,
1538 "%s: Sending target reset to target %d\n",
1539 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001540 rtn = scsi_try_target_reset(scmd);
1541 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001542 SCSI_LOG_ERROR_RECOVERY(3,
1543 shost_printk(KERN_INFO, shost,
1544 "%s: Target reset failed"
1545 " target: %d\n",
1546 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001547 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1548 if (scmd_id(scmd) != id)
1549 continue;
1550
David Jeffery3eef6252011-05-19 14:41:12 -04001551 if (rtn == SUCCESS)
1552 list_move_tail(&scmd->eh_entry, &check_list);
1553 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001554 scsi_eh_finish_cmd(scmd, done_q);
1555 else
1556 /* push back on work queue for further processing */
1557 list_move(&scmd->eh_entry, work_q);
1558 }
1559 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001560
David Jeffery3eef6252011-05-19 14:41:12 -04001561 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001562}
1563
1564/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001565 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001566 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001567 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001568 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1571 struct list_head *work_q,
1572 struct list_head *done_q)
1573{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001574 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001575 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 unsigned int channel;
1577 int rtn;
1578
1579 /*
1580 * we really want to loop over the various channels, and do this on
1581 * a channel by channel basis. we should also check to see if any
1582 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001583 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
1585
1586 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001587 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001588 list_splice_init(&check_list, work_q);
1589 SCSI_LOG_ERROR_RECOVERY(3,
1590 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001591 "%s: skip BRST, past eh deadline\n",
1592 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001593 return list_empty(work_q);
1594 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 chan_scmd = NULL;
1597 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001598 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 chan_scmd = scmd;
1600 break;
1601 /*
1602 * FIXME add back in some support for
1603 * soft_reset devices.
1604 */
1605 }
1606 }
1607
1608 if (!chan_scmd)
1609 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001610 SCSI_LOG_ERROR_RECOVERY(3,
1611 shost_printk(KERN_INFO, shost,
1612 "%s: Sending BRST chan: %d\n",
1613 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001615 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001616 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001617 if (channel == scmd_channel(scmd)) {
1618 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 scsi_eh_finish_cmd(scmd,
1620 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001621 else
1622 list_move_tail(&scmd->eh_entry,
1623 &check_list);
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001627 SCSI_LOG_ERROR_RECOVERY(3,
1628 shost_printk(KERN_INFO, shost,
1629 "%s: BRST failed chan: %d\n",
1630 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 }
David Jeffery3eef6252011-05-19 14:41:12 -04001633 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001637 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001638 * @shost: host to be reset.
1639 * @work_q: &list_head for pending commands.
1640 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001641 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001642static int scsi_eh_host_reset(struct Scsi_Host *shost,
1643 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct list_head *done_q)
1645{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001646 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001647 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (!list_empty(work_q)) {
1651 scmd = list_entry(work_q->next,
1652 struct scsi_cmnd, eh_entry);
1653
Hannes Reinecke91921e02014-06-25 16:39:59 +02001654 SCSI_LOG_ERROR_RECOVERY(3,
1655 shost_printk(KERN_INFO, shost,
1656 "%s: Sending HRST\n",
1657 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001660 if (rtn == SUCCESS) {
1661 list_splice_init(work_q, &check_list);
1662 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001663 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 scsi_eh_finish_cmd(scmd, done_q);
1665 }
1666 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001667 SCSI_LOG_ERROR_RECOVERY(3,
1668 shost_printk(KERN_INFO, shost,
1669 "%s: HRST failed\n",
1670 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672 }
David Jeffery3eef6252011-05-19 14:41:12 -04001673 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
1676/**
1677 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001678 * @work_q: &list_head for pending commands.
1679 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001680 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681static void scsi_eh_offline_sdevs(struct list_head *work_q,
1682 struct list_head *done_q)
1683{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001684 struct scsi_cmnd *scmd, *next;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001685 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001687 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001688 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1689 "not ready after error recovery\n");
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001690 sdev = scmd->device;
1691
1692 mutex_lock(&sdev->state_mutex);
1693 scsi_device_set_state(sdev, SDEV_OFFLINE);
1694 mutex_unlock(&sdev->state_mutex);
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 scsi_eh_finish_cmd(scmd, done_q);
1697 }
1698 return;
1699}
1700
1701/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001702 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001703 * @scmd: SCSI cmd to examine.
1704 */
1705int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1706{
1707 switch (host_byte(scmd->result)) {
1708 case DID_OK:
1709 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001710 case DID_TIME_OUT:
1711 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001712 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001713 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001714 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001715 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001716 case DID_ERROR:
1717 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1718 status_byte(scmd->result) == RESERVATION_CONFLICT)
1719 return 0;
1720 /* fall through */
1721 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001722 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001723 }
1724
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001725 if (status_byte(scmd->result) != CHECK_CONDITION)
1726 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001727
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001728check_type:
1729 /*
1730 * assume caller has checked sense and determined
1731 * the check condition was retryable.
1732 */
1733 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001734 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001735 return 1;
1736 else
1737 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001738}
1739
1740/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1742 * @scmd: SCSI cmd to examine.
1743 *
1744 * Notes:
1745 * This is *only* called when we are examining the status after sending
1746 * out the actual data command. any commands that are queued for error
1747 * recovery (e.g. test_unit_ready) do *not* come through here.
1748 *
1749 * When this routine returns failed, it means the error handler thread
1750 * is woken. In cases where the error code indicates an error that
1751 * doesn't require the error handler read (i.e. we don't need to
1752 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754int scsi_decide_disposition(struct scsi_cmnd *scmd)
1755{
1756 int rtn;
1757
1758 /*
1759 * if the device is offline, then we clearly just pass the result back
1760 * up to the top level.
1761 */
1762 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001763 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1764 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return SUCCESS;
1766 }
1767
1768 /*
1769 * first check the host byte, to see if there is anything in there
1770 * that would indicate what we need to do.
1771 */
1772 switch (host_byte(scmd->result)) {
1773 case DID_PASSTHROUGH:
1774 /*
1775 * no matter what, pass this through to the upper layer.
1776 * nuke this special code so that it looks like we are saying
1777 * did_ok.
1778 */
1779 scmd->result &= 0xff00ffff;
1780 return SUCCESS;
1781 case DID_OK:
1782 /*
1783 * looks good. drop through, and check the next byte.
1784 */
1785 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001786 case DID_ABORT:
1787 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001788 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001789 return SUCCESS;
1790 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001791 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 case DID_NO_CONNECT:
1793 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /*
1795 * note - this means that we just report the status back
1796 * to the top level driver, not that we actually think
1797 * that it indicates SUCCESS.
1798 */
1799 return SUCCESS;
Petros Koutoupisad950282017-10-30 16:38:10 -05001800 case DID_SOFT_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /*
1802 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001803 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * in order to avoid endless loops (db)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto maybe_retry;
1807 case DID_IMM_RETRY:
1808 return NEEDS_RETRY;
1809
bf341912005-04-12 17:49:09 -05001810 case DID_REQUEUE:
1811 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001812 case DID_TRANSPORT_DISRUPTED:
1813 /*
1814 * LLD/transport was disrupted during processing of the IO.
1815 * The transport class is now blocked/blocking,
1816 * and the transport will decide what to do with the IO
Mike Christie939c2282008-11-04 19:47:19 -06001817 * based on its timers and recovery capablilities if
1818 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001819 */
Mike Christie939c2282008-11-04 19:47:19 -06001820 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001821 case DID_TRANSPORT_FAILFAST:
1822 /*
1823 * The transport decided to failfast the IO (most likely
1824 * the fast io fail tmo fired), so send IO directly upwards.
1825 */
1826 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 case DID_ERROR:
1828 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1829 status_byte(scmd->result) == RESERVATION_CONFLICT)
1830 /*
1831 * execute reservation conflict processing code
1832 * lower down
1833 */
1834 break;
1835 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 case DID_BUS_BUSY:
1837 case DID_PARITY:
1838 goto maybe_retry;
1839 case DID_TIME_OUT:
1840 /*
1841 * when we scan the bus, we get timeout messages for
1842 * these commands if there is no device available.
1843 * other hosts report did_no_connect for the same thing.
1844 */
1845 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1846 scmd->cmnd[0] == INQUIRY)) {
1847 return SUCCESS;
1848 } else {
1849 return FAILED;
1850 }
1851 case DID_RESET:
1852 return SUCCESS;
1853 default:
1854 return FAILED;
1855 }
1856
1857 /*
1858 * next, check the message byte.
1859 */
1860 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1861 return FAILED;
1862
1863 /*
1864 * check the status byte to see if this indicates anything special.
1865 */
1866 switch (status_byte(scmd->result)) {
1867 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001868 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
1870 * the case of trying to send too many commands to a
1871 * tagged queueing device.
1872 */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001873 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 case BUSY:
1875 /*
1876 * device can't talk to us at the moment. Should only
1877 * occur (SAM-3) when the task queue is empty, so will cause
1878 * the empty queue handling to trigger a stall in the
1879 * device.
1880 */
1881 return ADD_TO_MLQUEUE;
1882 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001883 if (scmd->cmnd[0] == REPORT_LUNS)
1884 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001885 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001886 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001889 case TASK_ABORTED:
1890 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 case CHECK_CONDITION:
1892 rtn = scsi_check_sense(scmd);
1893 if (rtn == NEEDS_RETRY)
1894 goto maybe_retry;
1895 /* if rtn == FAILED, we have no sense information;
1896 * returning FAILED will wake the error handler thread
1897 * to collect the sense and redo the decide
1898 * disposition */
1899 return rtn;
1900 case CONDITION_GOOD:
1901 case INTERMEDIATE_GOOD:
1902 case INTERMEDIATE_C_GOOD:
1903 case ACA_ACTIVE:
1904 /*
1905 * who knows? FIXME(eric)
1906 */
1907 return SUCCESS;
1908
1909 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001910 sdev_printk(KERN_INFO, scmd->device,
1911 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001912 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return SUCCESS; /* causes immediate i/o error */
1914 default:
1915 return FAILED;
1916 }
1917 return FAILED;
1918
Jianchao Wang8ef7fe42018-02-26 15:59:16 +08001919maybe_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* we requeue for retry because the error was retryable, and
1922 * the request was not marked fast fail. Note that above,
1923 * even if the request is marked fast fail, we still requeue
1924 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001925 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001926 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return NEEDS_RETRY;
1928 } else {
1929 /*
1930 * no more retries - report this one back to upper level.
1931 */
1932 return SUCCESS;
1933 }
1934}
1935
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001936static void eh_lock_door_done(struct request *req, blk_status_t status)
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001937{
1938 __blk_put_request(req->q, req);
1939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 * scsi_eh_lock_door - Prevent medium removal for the specified device
1943 * @sdev: SCSI device to prevent medium removal
1944 *
1945 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001946 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 *
1948 * Notes:
1949 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1950 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952static void scsi_eh_lock_door(struct scsi_device *sdev)
1953{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001954 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001955 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Christoph Hellwigff005a02018-05-09 09:54:05 +02001957 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, 0);
Joe Lawrencea492f072014-08-28 08:15:21 -06001958 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001959 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001960 rq = scsi_req(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001962 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1963 rq->cmd[1] = 0;
1964 rq->cmd[2] = 0;
1965 rq->cmd[3] = 0;
1966 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1967 rq->cmd[5] = 0;
1968 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001969
Christoph Hellwige8064022016-10-20 15:12:13 +02001970 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001971 req->timeout = 10 * HZ;
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001972 rq->retries = 5;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001973
1974 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/**
1978 * scsi_restart_operations - restart io operations to the specified host.
1979 * @shost: Host we are restarting.
1980 *
1981 * Notes:
1982 * When we entered the error handler, we blocked all further i/o to
1983 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985static void scsi_restart_operations(struct Scsi_Host *shost)
1986{
1987 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001988 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
1991 * If the door was locked, we need to insert a door lock request
1992 * onto the head of the SCSI request queue for the device. There
1993 * is no point trying to lock the door of an off-line device.
1994 */
1995 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001996 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001998 sdev->was_reset = 0;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
2002 /*
2003 * next free up anything directly waiting upon the host. this
2004 * will be requests for character device operations, and also for
2005 * ioctls to queued block devices.
2006 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02002007 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002008 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
James Bottomley939647e2005-09-18 15:05:20 -05002010 spin_lock_irqsave(shost->host_lock, flags);
2011 if (scsi_host_set_state(shost, SHOST_RUNNING))
2012 if (scsi_host_set_state(shost, SHOST_CANCEL))
2013 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2014 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 wake_up(&shost->host_wait);
2017
2018 /*
2019 * finally we need to re-initiate requests that may be pending. we will
2020 * have had everything blocked while error handling is taking place, and
2021 * now that error recovery is done, we will need to ensure that these
2022 * requests are started.
2023 */
2024 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002025
2026 /*
2027 * if eh is active and host_eh_scheduled is pending we need to re-run
2028 * recovery. we do this check after scsi_run_host_queues() to allow
2029 * everything pent up since the last eh run a chance to make forward
2030 * progress before we sync again. Either we'll immediately re-run
2031 * recovery or scsi_device_unbusy() will wake us again when these
2032 * pending commands complete.
2033 */
2034 spin_lock_irqsave(shost->host_lock, flags);
2035 if (shost->host_eh_scheduled)
2036 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2037 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2038 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041/**
2042 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002043 * @shost: host to be recovered.
2044 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002045 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002046 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002047void scsi_eh_ready_devs(struct Scsi_Host *shost,
2048 struct list_head *work_q,
2049 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 if (!scsi_eh_stu(shost, work_q, done_q))
2052 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002053 if (!scsi_eh_target_reset(shost, work_q, done_q))
2054 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002055 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002056 scsi_eh_offline_sdevs(work_q,
2057 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002059EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061/**
2062 * scsi_eh_flush_done_q - finish processed commands or retry them.
2063 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002064 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002065void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002067 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002069 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2070 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002072 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002073 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002074 SCSI_LOG_ERROR_RECOVERY(3,
2075 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002076 "%s: flush retry cmd\n",
2077 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2079 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002080 /*
2081 * If just we got sense for the device (called
2082 * scsi_eh_get_sense), scmd->result is already
2083 * set, do not set DRIVER_TIMEOUT.
2084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (!scmd->result)
2086 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002087 SCSI_LOG_ERROR_RECOVERY(3,
2088 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002089 "%s: flush finish cmd\n",
2090 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 scsi_finish_command(scmd);
2092 }
2093 }
2094}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002095EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097/**
2098 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2099 * @shost: Host to unjam.
2100 *
2101 * Notes:
2102 * When we come in here, we *know* that all commands on the bus have
2103 * either completed, failed or timed out. we also know that no further
2104 * commands are being sent to the host, so things are relatively quiet
2105 * and we have freedom to fiddle with things as we wish.
2106 *
2107 * This is only the *default* implementation. it is possible for
2108 * individual drivers to supply their own version of this function, and
2109 * if the maintainer wishes to do this, it is strongly suggested that
2110 * this function be taken as a template and modified. this function
2111 * was designed to correctly handle problems for about 95% of the
2112 * different cases out there, and it should always provide at least a
2113 * reasonable amount of error recovery.
2114 *
2115 * Any command marked 'failed' or 'timeout' must eventually have
2116 * scsi_finish_cmd() called for it. we do all of the retry stuff
2117 * here, so when we restart the host after we return it should have an
2118 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120static void scsi_unjam_host(struct Scsi_Host *shost)
2121{
2122 unsigned long flags;
2123 LIST_HEAD(eh_work_q);
2124 LIST_HEAD(eh_done_q);
2125
2126 spin_lock_irqsave(shost->host_lock, flags);
2127 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2128 spin_unlock_irqrestore(shost->host_lock, flags);
2129
2130 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2131
2132 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002133 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Hannes Reineckeb4562022013-10-23 10:51:21 +02002135 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002136 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002137 shost->last_reset = 0;
2138 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 scsi_eh_flush_done_q(&eh_done_q);
2140}
2141
2142/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002143 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 * @data: Host for which we are running.
2145 *
2146 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002147 * This is the main error handling loop. This is run as a kernel thread
2148 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150int scsi_error_handler(void *data)
2151{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002152 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002155 * We use TASK_INTERRUPTIBLE so that the thread is not
2156 * counted against the load average as a running process.
2157 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002158 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 */
Michal Hocko537b6042015-08-27 20:16:37 +02002160 while (true) {
2161 /*
2162 * The sequence in kthread_stop() sets the stop flag first
2163 * then wakes the process. To avoid missed wakeups, the task
2164 * should always be in a non running state before the stop
2165 * flag is checked
2166 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002167 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002168 if (kthread_should_stop())
2169 break;
2170
Tejun Heoee7863b2006-05-15 20:57:20 +09002171 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Ming Leic84b0232018-06-24 22:03:26 +08002172 shost->host_failed != scsi_host_busy(shost)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002173 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002174 shost_printk(KERN_INFO, shost,
2175 "scsi_eh_%d: sleeping\n",
2176 shost->host_no));
James Bottomley3ed7a4702005-09-19 09:50:04 -05002177 schedule();
James Bottomley3ed7a4702005-09-19 09:50:04 -05002178 continue;
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
James Bottomley3ed7a4702005-09-19 09:50:04 -05002181 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002182 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002183 shost_printk(KERN_INFO, shost,
2184 "scsi_eh_%d: waking up %d/%d/%d\n",
2185 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002186 shost->host_failed,
Ming Leic84b0232018-06-24 22:03:26 +08002187 scsi_host_busy(shost)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
2190 * We have a host that is failing for some reason. Figure out
2191 * what we need to do to get it up and online again (if we can).
2192 * If we fail, we end up taking the thing offline.
2193 */
Lin Mingae0751f2011-12-05 09:20:24 +08002194 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002195 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002196 shost_printk(KERN_ERR, shost,
2197 "scsi_eh_%d: unable to autoresume\n",
2198 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002199 continue;
2200 }
2201
Christoph Hellwig9227c332006-04-01 19:21:04 +02002202 if (shost->transportt->eh_strategy_handler)
2203 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 else
2205 scsi_unjam_host(shost);
2206
Wei Fang72d8c362016-06-07 14:53:56 +08002207 /* All scmds have been handled */
2208 shost->host_failed = 0;
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /*
2211 * Note - if the above fails completely, the action is to take
2212 * individual devices offline and flush the queue of any
2213 * outstanding requests that may have been pending. When we
2214 * restart, we restart any I/O to any other devices on the bus
2215 * which are still online.
2216 */
2217 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002218 if (!shost->eh_noresume)
2219 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002221 __set_current_state(TASK_RUNNING);
2222
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002223 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002224 shost_printk(KERN_INFO, shost,
2225 "Error handler scsi_eh_%d exiting\n",
2226 shost->host_no));
James Bottomley3ed7a4702005-09-19 09:50:04 -05002227 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return 0;
2229}
2230
2231/*
2232 * Function: scsi_report_bus_reset()
2233 *
2234 * Purpose: Utility function used by low-level drivers to report that
2235 * they have observed a bus reset on the bus being handled.
2236 *
2237 * Arguments: shost - Host in question
2238 * channel - channel on which reset was observed.
2239 *
2240 * Returns: Nothing
2241 *
2242 * Lock status: Host lock must be held.
2243 *
2244 * Notes: This only needs to be called if the reset is one which
2245 * originates from an unknown location. Resets originated
2246 * by the mid-level itself don't need to call this, but there
2247 * should be no harm.
2248 *
2249 * The main purpose of this is to make sure that a CHECK_CONDITION
2250 * is properly treated.
2251 */
2252void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2253{
2254 struct scsi_device *sdev;
2255
2256 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002257 if (channel == sdev_channel(sdev))
2258 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260}
2261EXPORT_SYMBOL(scsi_report_bus_reset);
2262
2263/*
2264 * Function: scsi_report_device_reset()
2265 *
2266 * Purpose: Utility function used by low-level drivers to report that
2267 * they have observed a device reset on the device being handled.
2268 *
2269 * Arguments: shost - Host in question
2270 * channel - channel on which reset was observed
2271 * target - target on which reset was observed
2272 *
2273 * Returns: Nothing
2274 *
2275 * Lock status: Host lock must be held
2276 *
2277 * Notes: This only needs to be called if the reset is one which
2278 * originates from an unknown location. Resets originated
2279 * by the mid-level itself don't need to call this, but there
2280 * should be no harm.
2281 *
2282 * The main purpose of this is to make sure that a CHECK_CONDITION
2283 * is properly treated.
2284 */
2285void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2286{
2287 struct scsi_device *sdev;
2288
2289 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002290 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002291 target == sdev_id(sdev))
2292 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294}
2295EXPORT_SYMBOL(scsi_report_device_reset);
2296
2297static void
2298scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2299{
2300}
2301
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002302/**
2303 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2304 * @dev: scsi_device to operate on
2305 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
2307int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002308scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Alan Sternbc4f2402010-06-17 10:41:42 -04002310 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002311 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002312 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002313 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002314 int error = 0, rtn, val;
2315
2316 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2317 return -EACCES;
2318
2319 error = get_user(val, arg);
2320 if (error)
2321 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Alan Sternbc4f2402010-06-17 10:41:42 -04002323 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002324 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002325
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002326 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002327 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2328 shost->hostt->cmd_size, GFP_KERNEL);
2329 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002330 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002331 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002332
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002333 scmd = (struct scsi_cmnd *)(rq + 1);
2334 scsi_init_command(dev, scmd);
2335 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002336 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002339 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 scmd->cmd_len = 0;
2342
2343 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
James Smartd7a1bb02006-03-08 14:50:12 -05002345 spin_lock_irqsave(shost->host_lock, flags);
2346 shost->tmf_in_progress = 1;
2347 spin_unlock_irqrestore(shost->host_lock, flags);
2348
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002349 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2350 case SG_SCSI_RESET_NOTHING:
2351 rtn = SUCCESS;
2352 break;
2353 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 rtn = scsi_try_bus_device_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_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002359 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002360 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002361 break;
2362 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002363 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002365 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 break;
2367 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002368 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002370 if (rtn == SUCCESS)
2371 break;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002372 /* FALLTHROUGH */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07002373 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002378 error = (rtn == SUCCESS) ? 0 : -EIO;
2379
James Smartd7a1bb02006-03-08 14:50:12 -05002380 spin_lock_irqsave(shost->host_lock, flags);
2381 shost->tmf_in_progress = 0;
2382 spin_unlock_irqrestore(shost->host_lock, flags);
2383
2384 /*
2385 * be sure to wake up anyone who was sleeping or had their queue
2386 * suspended while we performed the TMF.
2387 */
2388 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002389 shost_printk(KERN_INFO, shost,
2390 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002391
2392 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002393 scsi_run_host_queues(shost);
2394
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002395 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002396 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002397
Christoph Hellwig04796332014-02-20 14:20:55 -08002398out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002399 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002400 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002402EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002404bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2405 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
2407 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002408 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410EXPORT_SYMBOL(scsi_command_normalize_sense);
2411
2412/**
Rob Landleyeb448202007-11-03 13:30:39 -05002413 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * @sense_buffer: byte array of sense data
2415 * @sb_len: number of valid bytes in sense_buffer
2416 * @info_out: pointer to 64 integer where 8 or 4 byte information
2417 * field will be placed if found.
2418 *
2419 * Return value:
Damien Le Moal29087692017-04-24 16:51:12 +09002420 * true if information field found, false if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002421 */
Damien Le Moal29087692017-04-24 16:51:12 +09002422bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2423 u64 *info_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 const u8 * ucp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 if (sb_len < 7)
Damien Le Moal29087692017-04-24 16:51:12 +09002428 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 switch (sense_buffer[0] & 0x7f) {
2430 case 0x70:
2431 case 0x71:
2432 if (sense_buffer[0] & 0x80) {
Damien Le Moal29087692017-04-24 16:51:12 +09002433 *info_out = get_unaligned_be32(&sense_buffer[3]);
2434 return true;
2435 }
2436 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 case 0x72:
2438 case 0x73:
2439 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2440 0 /* info desc */);
2441 if (ucp && (0xa == ucp[1])) {
Damien Le Moal29087692017-04-24 16:51:12 +09002442 *info_out = get_unaligned_be64(&ucp[4]);
2443 return true;
2444 }
2445 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 default:
Damien Le Moal29087692017-04-24 16:51:12 +09002447 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449}
2450EXPORT_SYMBOL(scsi_get_sense_info_fld);