blob: 53e334356f31bc3eab742ad65da3f1953c12e319 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 *
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
Jesper Juhl0bf8c862011-03-21 20:47:31 +01006 * Leonard Zubkoff and David Miller at Linux Expo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * ideas originating from all over the place.
8 *
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
11 *
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
Jesper Juhl0bf8c862011-03-21 20:47:31 +010013 * minor cleanups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 */
16
17#include <linux/module.h>
18#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/timer.h>
21#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070023#include <linux/freezer.h>
Christoph Hellwigc5478de2005-09-06 14:04:26 +020024#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
26#include <linux/blkdev.h>
27#include <linux/delay.h>
Hannes Reineckefc736482013-04-25 08:10:00 +020028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020031#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -050034#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <scsi/scsi_eh.h>
Sagi Grimberg7708c162015-07-08 17:58:52 +030036#include <scsi/scsi_common.h>
James Smartc829c392006-03-13 08:28:57 -050037#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <scsi/scsi_host.h>
39#include <scsi/scsi_ioctl.h>
Christoph Hellwigee14c672015-08-27 14:16:59 +020040#include <scsi/scsi_dh.h>
Christoph Hellwig176aa9d2014-10-11 12:06:47 +020041#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "scsi_priv.h"
44#include "scsi_logging.h"
Adrian Bunk79ee8302007-08-10 14:50:42 -070045#include "scsi_transport_api.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Kei Tokunagabf816232010-04-01 20:41:40 +090047#include <trace/events/scsi.h>
48
James Bottomley14216562012-07-25 23:55:55 +040049static void scsi_eh_done(struct scsi_cmnd *scmd);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * These should *probably* be handled by the host itself.
53 * Since it is allowed to sleep, it probably should.
54 */
55#define BUS_RESET_SETTLE_TIME (10)
56#define HOST_RESET_SETTLE_TIME (10)
57
David Jeffery3eef6252011-05-19 14:41:12 -040058static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +010059static int scsi_try_to_abort_cmd(struct scsi_host_template *,
60 struct scsi_cmnd *);
David Jeffery3eef6252011-05-19 14:41:12 -040061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* called with shost->host_lock held */
63void scsi_eh_wakeup(struct Scsi_Host *shost)
64{
Christoph Hellwig74665012014-01-22 15:29:29 +010065 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090066 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -050067 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020068 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
69 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71}
Tejun Heof8bbfc22006-05-19 21:07:05 +090072
73/**
74 * scsi_schedule_eh - schedule EH for SCSI host
75 * @shost: SCSI host to invoke error handling on.
76 *
77 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080078 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090079void scsi_schedule_eh(struct Scsi_Host *shost)
80{
81 unsigned long flags;
82
83 spin_lock_irqsave(shost->host_lock, flags);
84
85 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
86 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
87 shost->host_eh_scheduled++;
88 scsi_eh_wakeup(shost);
89 }
90
91 spin_unlock_irqrestore(shost->host_lock, flags);
92}
93EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Hannes Reineckeb4562022013-10-23 10:51:21 +020095static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
96{
Ren Mingxinbb3b6212013-11-11 13:44:56 +010097 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +020098 return 0;
99
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100100 /*
101 * 32bit accesses are guaranteed to be atomic
102 * (on all supported architectures), so instead
103 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100104 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100105 * time_before call.
106 */
107 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100108 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200109 return 0;
110
111 return 1;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100115 * scmd_eh_abort_handler - Handle command aborts
116 * @work: command to be aborted.
117 */
118void
119scmd_eh_abort_handler(struct work_struct *work)
120{
121 struct scsi_cmnd *scmd =
122 container_of(work, struct scsi_cmnd, abort_work.work);
123 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100124 int rtn;
125
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100126 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100127 SCSI_LOG_ERROR_RECOVERY(3,
128 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100129 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100130 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100131 SCSI_LOG_ERROR_RECOVERY(3,
132 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100133 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100134 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
135 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200136 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100137 if (scsi_host_eh_past_deadline(sdev->host)) {
138 SCSI_LOG_ERROR_RECOVERY(3,
139 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100140 "eh timeout, not retrying "
141 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100142 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100143 (++scmd->retries <= scmd->allowed)) {
144 SCSI_LOG_ERROR_RECOVERY(3,
145 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100146 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100147 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100148 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100149 } else {
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100152 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100153 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100154 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100155 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100156 } else {
157 SCSI_LOG_ERROR_RECOVERY(3,
158 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100159 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200160 (rtn == FAST_IO_FAIL) ?
161 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100162 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100163 }
164
Hannes Reineckea0658632017-04-06 15:36:35 +0200165 scsi_eh_scmd_add(scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100166}
167
168/**
169 * scsi_abort_command - schedule a command abort
170 * @scmd: scmd to abort.
171 *
172 * We only need to abort commands after a command timeout
173 */
174static int
175scsi_abort_command(struct scsi_cmnd *scmd)
176{
177 struct scsi_device *sdev = scmd->device;
178 struct Scsi_Host *shost = sdev->host;
179 unsigned long flags;
180
181 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
182 /*
183 * Retry after abort failed, escalate to next level.
184 */
185 SCSI_LOG_ERROR_RECOVERY(3,
186 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100187 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200188 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100189 return FAILED;
190 }
191
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100192 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100193 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100194 shost->last_reset = jiffies;
195 spin_unlock_irqrestore(shost->host_lock, flags);
196
197 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
198 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100199 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100200 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
201 return SUCCESS;
202}
203
204/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200205 * scsi_eh_reset - call into ->eh_action to reset internal counters
206 * @scmd: scmd to run eh on.
207 *
208 * The scsi driver might be carrying internal state about the
209 * devices, so we need to call into the driver to reset the
210 * internal state once the error handler is started.
211 */
212static void scsi_eh_reset(struct scsi_cmnd *scmd)
213{
214 if (!blk_rq_is_passthrough(scmd->request)) {
215 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
216 if (sdrv->eh_reset)
217 sdrv->eh_reset(scmd);
218 }
219}
220
221/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * scsi_eh_scmd_add - add scsi cmd to error handling.
223 * @scmd: scmd to run eh on.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800224 */
Hannes Reineckea0658632017-04-06 15:36:35 +0200225void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct Scsi_Host *shost = scmd->device->host;
228 unsigned long flags;
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200229 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200231 WARN_ON_ONCE(!shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200234 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
235 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
236 WARN_ON_ONCE(ret);
237 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100238 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200239 shost->last_reset = jiffies;
240
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200241 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 shost->host_failed++;
244 scsi_eh_wakeup(shost);
245 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700250 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
252 * Notes:
253 * We do not need to lock this. There is the potential for a race
254 * only in that the normal completion handling might run, but if the
255 * normal completion function determines that the timer has already
256 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800257 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700258enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700260 struct scsi_cmnd *scmd = req->special;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700261 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100262 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500263
Kei Tokunagabf816232010-04-01 20:41:40 +0900264 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 scsi_log_completion(scmd, TIMEOUT_ERROR);
266
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100267 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200268 host->last_reset = jiffies;
269
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100270 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100271 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500272
Hannes Reineckea33c0702014-06-13 14:01:45 +0200273 if (rtn == BLK_EH_NOT_HANDLED) {
Hannes Reineckea0658632017-04-06 15:36:35 +0200274 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200275 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200276 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200277 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200278 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700279
Christoph Hellwigfa990782008-11-05 15:03:15 +0100280 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/**
284 * scsi_block_when_processing_errors - Prevent cmds from being queued.
285 * @sdev: Device on which we are performing recovery.
286 *
287 * Description:
288 * We block until the host is out of error recovery, and then check to
289 * see whether the host or the device is offline.
290 *
291 * Return value:
292 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294int scsi_block_when_processing_errors(struct scsi_device *sdev)
295{
296 int online;
297
James Bottomley939647e2005-09-18 15:05:20 -0500298 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 online = scsi_device_online(sdev);
301
Hannes Reinecke91921e02014-06-25 16:39:59 +0200302 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
303 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 return online;
306}
307EXPORT_SYMBOL(scsi_block_when_processing_errors);
308
309#ifdef CONFIG_SCSI_LOGGING
310/**
311 * scsi_eh_prt_fail_stats - Log info on failures.
312 * @shost: scsi host being recovered.
313 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
316 struct list_head *work_q)
317{
318 struct scsi_cmnd *scmd;
319 struct scsi_device *sdev;
320 int total_failures = 0;
321 int cmd_failed = 0;
322 int cmd_cancel = 0;
323 int devices_failed = 0;
324
325 shost_for_each_device(sdev, shost) {
326 list_for_each_entry(scmd, work_q, eh_entry) {
327 if (scmd->device == sdev) {
328 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200329 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100331 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ++cmd_failed;
333 }
334 }
335
336 if (cmd_cancel || cmd_failed) {
337 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200338 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500339 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700340 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500341 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 cmd_cancel = 0;
343 cmd_failed = 0;
344 ++devices_failed;
345 }
346 }
347
Hannes Reinecke91921e02014-06-25 16:39:59 +0200348 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
349 "Total of %d commands on %d"
350 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100351 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353#endif
354
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400355 /**
356 * scsi_report_lun_change - Set flag on all *other* devices on the same target
357 * to indicate that a UNIT ATTENTION is expected.
358 * @sdev: Device reporting the UNIT ATTENTION
359 */
360static void scsi_report_lun_change(struct scsi_device *sdev)
361{
362 sdev->sdev_target->expecting_lun_change = 1;
363}
364
365/**
366 * scsi_report_sense - Examine scsi sense information and log messages for
367 * certain conditions, also issue uevents for some of them.
368 * @sdev: Device reporting the sense code
369 * @sshdr: sshdr to be examined
370 */
371static void scsi_report_sense(struct scsi_device *sdev,
372 struct scsi_sense_hdr *sshdr)
373{
374 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
375
376 if (sshdr->sense_key == UNIT_ATTENTION) {
377 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
378 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
379 sdev_printk(KERN_WARNING, sdev,
380 "Inquiry data has changed");
381 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
382 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
383 scsi_report_lun_change(sdev);
384 sdev_printk(KERN_WARNING, sdev,
385 "Warning! Received an indication that the "
386 "LUN assignments on this target have "
387 "changed. The Linux SCSI layer does not "
388 "automatically remap LUN assignments.\n");
389 } else if (sshdr->asc == 0x3f)
390 sdev_printk(KERN_WARNING, sdev,
391 "Warning! Received an indication that the "
392 "operating parameters on this target have "
393 "changed. The Linux SCSI layer does not "
394 "automatically adjust these parameters.\n");
395
396 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
397 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
398 sdev_printk(KERN_WARNING, sdev,
399 "Warning! Received an indication that the "
400 "LUN reached a thin provisioning soft "
401 "threshold.\n");
402 }
403
404 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
405 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
406 sdev_printk(KERN_WARNING, sdev,
407 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200408 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
409 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
410 sdev_printk(KERN_WARNING, sdev,
411 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400412 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
413 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
414 sdev_printk(KERN_WARNING, sdev,
415 "Capacity data has changed");
416 } else if (sshdr->asc == 0x2a)
417 sdev_printk(KERN_WARNING, sdev,
418 "Parameters changed");
419 }
420
421 if (evt_type != SDEV_EVT_MAXBITS) {
422 set_bit(evt_type, sdev->pending_events);
423 schedule_work(&sdev->event_work);
424 }
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * scsi_check_sense - Examine scsi cmd sense
429 * @scmd: Cmd to have sense checked.
430 *
431 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200432 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 *
434 * Notes:
435 * When a deferred error is detected the current command has
436 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800437 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200438int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700440 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct scsi_sense_hdr sshdr;
442
443 if (! scsi_command_normalize_sense(scmd, &sshdr))
444 return FAILED; /* no valid sense data */
445
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400446 scsi_report_sense(sdev, &sshdr);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (scsi_sense_is_deferred(&sshdr))
449 return NEEDS_RETRY;
450
Christoph Hellwigee14c672015-08-27 14:16:59 +0200451 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700452 int rc;
453
Christoph Hellwigee14c672015-08-27 14:16:59 +0200454 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700455 if (rc != SCSI_RETURN_NOT_HANDLED)
456 return rc;
457 /* handler does not care. Drop down to default handling */
458 }
459
Christoph Hellwige925cc42014-11-06 15:11:22 -0600460 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
461 /*
462 * nasty: for mid-layer issued TURs, we need to return the
463 * actual sense data without any recovery attempt. For eh
464 * issued ones, we need to try to recover and interpret
465 */
466 return SUCCESS;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Previous logic looked for FILEMARK, EOM or ILI which are
470 * mainly associated with tapes and returned SUCCESS.
471 */
472 if (sshdr.response_code == 0x70) {
473 /* fixed format */
474 if (scmd->sense_buffer[2] & 0xe0)
475 return SUCCESS;
476 } else {
477 /*
478 * descriptor format: look for "stream commands sense data
479 * descriptor" (see SSC-3). Assume single sense data
480 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
481 */
482 if ((sshdr.additional_length > 3) &&
483 (scmd->sense_buffer[8] == 0x4) &&
484 (scmd->sense_buffer[11] & 0xe0))
485 return SUCCESS;
486 }
487
488 switch (sshdr.sense_key) {
489 case NO_SENSE:
490 return SUCCESS;
491 case RECOVERED_ERROR:
492 return /* soft_error */ SUCCESS;
493
494 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400495 if (sshdr.asc == 0x10) /* DIF */
496 return SUCCESS;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return NEEDS_RETRY;
499 case NOT_READY:
500 case UNIT_ATTENTION:
501 /*
502 * if we are expecting a cc/ua because of a bus reset that we
503 * performed, treat this just as a retry. otherwise this is
504 * information that we should pass up to the upper-level driver
505 * so that we can deal with it there.
506 */
507 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900508 /*
509 * Because some device does not queue unit
510 * attentions correctly, we carefully check
511 * additional sense code and qualifier so as
512 * not to squash media change unit attention.
513 */
514 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
515 scmd->device->expecting_cc_ua = 0;
516 return NEEDS_RETRY;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400520 * we might also expect a cc/ua if another LUN on the target
521 * reported a UA with an ASC/ASCQ of 3F 0E -
522 * REPORTED LUNS DATA HAS CHANGED.
523 */
524 if (scmd->device->sdev_target->expecting_lun_change &&
525 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
526 return NEEDS_RETRY;
527 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100528 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * should retry.
530 */
531 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
532 return NEEDS_RETRY;
533 /*
534 * if the device is not started, we need to wake
535 * the error handler to start the motor
536 */
537 if (scmd->device->allow_restart &&
538 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
539 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100540 /*
541 * Pass the UA upwards for a determination in the completion
542 * functions.
543 */
544 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100546 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200547 case DATA_PROTECT:
548 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
549 /* Thin provisioning hard threshold reached */
550 set_host_byte(scmd, DID_ALLOC_FAILURE);
551 return SUCCESS;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 case COPY_ABORTED:
554 case VOLUME_OVERFLOW:
555 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100556 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200557 set_host_byte(scmd, DID_TARGET_FAILURE);
558 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800561 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
562 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
563 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200564 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200565 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return NEEDS_RETRY;
568
569 case HARDWARE_ERROR:
570 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700571 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200573 set_host_byte(scmd, DID_TARGET_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500576 if (sshdr.asc == 0x20 || /* Invalid command operation code */
577 sshdr.asc == 0x21 || /* Logical block address out of range */
578 sshdr.asc == 0x24 || /* Invalid field in cdb */
579 sshdr.asc == 0x26) { /* Parameter value invalid */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200580 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500581 }
582 return SUCCESS;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 default:
585 return SUCCESS;
586 }
587}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200588EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Vasu Dev4a840672009-10-22 15:46:33 -0700590static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
591{
592 struct scsi_host_template *sht = sdev->host->hostt;
593 struct scsi_device *tmp_sdev;
594
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100595 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700596 sdev->queue_depth >= sdev->max_queue_depth)
597 return;
598
599 if (time_before(jiffies,
600 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
601 return;
602
603 if (time_before(jiffies,
604 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
605 return;
606
607 /*
608 * Walk all devices of a target and do
609 * ramp up on them.
610 */
611 shost_for_each_device(tmp_sdev, sdev->host) {
612 if (tmp_sdev->channel != sdev->channel ||
613 tmp_sdev->id != sdev->id ||
614 tmp_sdev->queue_depth == sdev->max_queue_depth)
615 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100616
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100617 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700618 sdev->last_queue_ramp_up = jiffies;
619 }
620}
621
Mike Christie42a6a912009-10-15 17:46:44 -0700622static void scsi_handle_queue_full(struct scsi_device *sdev)
623{
624 struct scsi_host_template *sht = sdev->host->hostt;
625 struct scsi_device *tmp_sdev;
626
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100627 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700628 return;
629
630 shost_for_each_device(tmp_sdev, sdev->host) {
631 if (tmp_sdev->channel != sdev->channel ||
632 tmp_sdev->id != sdev->id)
633 continue;
634 /*
635 * We do not know the number of commands that were at
636 * the device when we got the queue full so we start
637 * from the highest possible value and work our way down.
638 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100639 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700640 }
641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/**
644 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
645 * @scmd: SCSI cmd to examine.
646 *
647 * Notes:
648 * This is *only* called when we are examining the status of commands
649 * queued during error recovery. the main difference here is that we
650 * don't allow for the possibility of retries here, and we are a lot
651 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
654{
655 /*
656 * first check the host byte, to see if there is anything in there
657 * that would indicate what we need to do.
658 */
659 if (host_byte(scmd->result) == DID_RESET) {
660 /*
661 * rats. we are already in the error handler, so we now
662 * get to try and figure out what to do next. if the sense
663 * is valid, we have a pretty good idea of what to do.
664 * if not, we mark it as FAILED.
665 */
666 return scsi_check_sense(scmd);
667 }
668 if (host_byte(scmd->result) != DID_OK)
669 return FAILED;
670
671 /*
672 * next, check the message byte.
673 */
674 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
675 return FAILED;
676
677 /*
678 * now, check the status byte to see if this indicates
679 * anything special.
680 */
681 switch (status_byte(scmd->result)) {
682 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700683 scsi_handle_queue_ramp_up(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 case COMMAND_TERMINATED:
685 return SUCCESS;
686 case CHECK_CONDITION:
687 return scsi_check_sense(scmd);
688 case CONDITION_GOOD:
689 case INTERMEDIATE_GOOD:
690 case INTERMEDIATE_C_GOOD:
691 /*
692 * who knows? FIXME(eric)
693 */
694 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500695 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500696 if (scmd->cmnd[0] == TEST_UNIT_READY)
697 /* it is a success, we probed the device and
698 * found it */
699 return SUCCESS;
700 /* otherwise, we failed to send the command */
701 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700703 scsi_handle_queue_full(scmd->device);
704 /* fall through */
705 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200706 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 default:
708 return FAILED;
709 }
710 return FAILED;
711}
712
713/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * scsi_eh_done - Completion function for error handling.
715 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static void scsi_eh_done(struct scsi_cmnd *scmd)
718{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100719 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600720
Hannes Reinecke91921e02014-06-25 16:39:59 +0200721 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100722 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600723
724 eh_action = scmd->device->host->eh_action;
725 if (eh_action)
726 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/**
Brian King292148f2007-01-30 17:51:17 -0600730 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200731 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800732 */
Brian King292148f2007-01-30 17:51:17 -0600733static int scsi_try_host_reset(struct scsi_cmnd *scmd)
734{
735 unsigned long flags;
736 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100737 struct Scsi_Host *host = scmd->device->host;
738 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600739
Hannes Reinecke91921e02014-06-25 16:39:59 +0200740 SCSI_LOG_ERROR_RECOVERY(3,
741 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600742
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100743 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600744 return FAILED;
745
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100746 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600747
748 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100749 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600750 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100751 spin_lock_irqsave(host->host_lock, flags);
752 scsi_report_bus_reset(host, scmd_channel(scmd));
753 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600754 }
755
756 return rtn;
757}
758
759/**
760 * scsi_try_bus_reset - ask host to perform a bus reset
761 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800762 */
Brian King292148f2007-01-30 17:51:17 -0600763static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
764{
765 unsigned long flags;
766 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100767 struct Scsi_Host *host = scmd->device->host;
768 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600769
Hannes Reinecke91921e02014-06-25 16:39:59 +0200770 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
771 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600772
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100773 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600774 return FAILED;
775
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100776 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600777
778 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100779 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600780 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100781 spin_lock_irqsave(host->host_lock, flags);
782 scsi_report_bus_reset(host, scmd_channel(scmd));
783 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600784 }
785
786 return rtn;
787}
788
Mike Christie30bd7df2008-02-29 18:25:19 -0600789static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
790{
791 sdev->was_reset = 1;
792 sdev->expecting_cc_ua = 1;
793}
794
795/**
796 * scsi_try_target_reset - Ask host to perform a target reset
797 * @scmd: SCSI cmd used to send a target reset
798 *
799 * Notes:
800 * There is no timeout for this operation. if this operation is
801 * unreliable for a given host, then the host itself needs to put a
802 * timer on it, and set the host back to a consistent state prior to
803 * returning.
804 */
805static int scsi_try_target_reset(struct scsi_cmnd *scmd)
806{
807 unsigned long flags;
808 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100809 struct Scsi_Host *host = scmd->device->host;
810 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600811
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100812 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600813 return FAILED;
814
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100815 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600816 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100817 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600818 __starget_for_each_device(scsi_target(scmd->device), NULL,
819 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100820 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600821 }
822
823 return rtn;
824}
825
Brian King292148f2007-01-30 17:51:17 -0600826/**
827 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
828 * @scmd: SCSI cmd used to send BDR
829 *
830 * Notes:
831 * There is no timeout for this operation. if this operation is
832 * unreliable for a given host, then the host itself needs to put a
833 * timer on it, and set the host back to a consistent state prior to
834 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800835 */
Brian King292148f2007-01-30 17:51:17 -0600836static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
837{
838 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100839 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600840
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100841 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600842 return FAILED;
843
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100844 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600845 if (rtn == SUCCESS)
846 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600847 return rtn;
848}
849
Hannes Reinecke883a0302014-10-24 14:27:04 +0200850/**
851 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800852 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200853 * @scmd: SCSI cmd used to send a target reset
854 *
855 * Return value:
856 * SUCCESS, FAILED, or FAST_IO_FAIL
857 *
858 * Notes:
859 * SUCCESS does not necessarily indicate that the command
860 * has been aborted; it only indicates that the LLDDs
861 * has cleared all references to that command.
862 * LLDDs should return FAILED only if an abort was required
863 * but could not be executed. LLDDs should return FAST_IO_FAIL
864 * if the device is temporarily unavailable (eg due to a
865 * link down on FibreChannel)
866 */
867static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
868 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600869{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100870 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600871 return FAILED;
872
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100873 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600874}
875
Brian King292148f2007-01-30 17:51:17 -0600876static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
877{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100878 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600879 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600880 if (scsi_try_target_reset(scmd) != SUCCESS)
881 if (scsi_try_bus_reset(scmd) != SUCCESS)
882 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600883}
884
885/**
Santosh Y3b729f72012-04-08 18:47:09 +0530886 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100887 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200888 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200889 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300890 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200891 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100892 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200893 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200894 * as part of the error recovery process. If @sense_bytes is 0 the command
895 * sent must be one that does not transfer any data. If @sense_bytes != 0
896 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
897 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800898 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200899void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
900 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
f59114b2005-04-17 15:00:23 -0500902 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Christoph Hellwig631c2282006-07-08 20:42:15 +0200904 /*
905 * We need saved copies of a number of fields - this is because
906 * error handling may need to overwrite these with different values
907 * to run different commands, and once error handling is complete,
908 * we will need to restore these values prior to running the actual
909 * command.
910 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200911 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300912 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200913 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200914 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200915 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200916 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400917 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400918 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200919 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200920
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400921 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700922 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300923 scmd->cmnd = ses->eh_cmnd;
924 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200925 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200926 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700927 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200928
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200929 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200930 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
931 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200932 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200933 scmd->sdb.length);
934 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200935 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400936 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200937 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200938 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200939 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200940 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200941 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200942 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300943 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200944 memcpy(scmd->cmnd, cmnd, cmnd_size);
945 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
946 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200947 }
948
949 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200950
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200951 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500953 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Christoph Hellwig631c2282006-07-08 20:42:15 +0200955 /*
956 * Zero the sense buffer. The scsi spec mandates that any
957 * untransferred sense data should be interpreted as being zero.
958 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900959 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200960}
961EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200962
Boaz Harroshe1c23462007-10-08 16:36:45 +0200963/**
Santosh Y3b729f72012-04-08 18:47:09 +0530964 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200965 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200966 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +0200967 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200968 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800969 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200970void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
971{
972 /*
973 * Restore original data
974 */
975 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300976 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200977 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200978 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200979 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200980 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -0400981 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400982 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200983 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200984}
985EXPORT_SYMBOL(scsi_eh_restore_cmnd);
986
987/**
Santosh Y3b729f72012-04-08 18:47:09 +0530988 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200989 * @scmd: SCSI command structure to hijack
990 * @cmnd: CDB to send
991 * @cmnd_size: size in bytes of @cmnd
992 * @timeout: timeout for this request
993 * @sense_bytes: size of sense data to copy or 0
994 *
995 * This function is used to send a scsi command down to a target device
996 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
997 *
998 * Return value:
999 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001000 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001001static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1002 int cmnd_size, int timeout, unsigned sense_bytes)
1003{
1004 struct scsi_device *sdev = scmd->device;
1005 struct Scsi_Host *shost = sdev->host;
1006 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001007 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001008 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001009 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001010 int rtn;
1011
Hannes Reineckefc736482013-04-25 08:10:00 +02001012retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001013 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001014 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001017 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001018 rtn = shost->hostt->queuecommand(shost, scmd);
1019 if (rtn) {
1020 if (timeleft > stall_for) {
1021 scsi_eh_restore_cmnd(scmd, &ses);
1022 timeleft -= stall_for;
1023 msleep(jiffies_to_msecs(stall_for));
1024 goto retry;
1025 }
1026 /* signal not to enter either branch of the if () below */
1027 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001028 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001029 } else {
1030 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001031 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
f59114b2005-04-17 15:00:23 -05001034 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Hannes Reineckefc736482013-04-25 08:10:00 +02001036 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Hannes Reinecke91921e02014-06-25 16:39:59 +02001038 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001039 "%s timeleft: %ld\n",
1040 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001043 * If there is time left scsi_eh_done got called, and we will examine
1044 * the actual status codes to see whether the command actually did
1045 * complete normally, else if we have a zero return and no time left,
1046 * the command must still be pending, so abort it and return FAILED.
1047 * If we never actually managed to issue the command, because
1048 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1049 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001051 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001053 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1054 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 switch (rtn) {
1057 case SUCCESS:
1058 case NEEDS_RETRY:
1059 case FAILED:
1060 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001061 case ADD_TO_MLQUEUE:
1062 rtn = NEEDS_RETRY;
1063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 default:
1065 rtn = FAILED;
1066 break;
1067 }
Alan Stern511833a2014-11-21 10:44:49 -05001068 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001069 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001070 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
Boaz Harroshe1c23462007-10-08 16:36:45 +02001073 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return rtn;
1076}
1077
1078/**
1079 * scsi_request_sense - Request sense data from a particular target.
1080 * @scmd: SCSI cmd for request sense.
1081 *
1082 * Notes:
1083 * Some hosts automatically obtain this information, others require
1084 * that we obtain it on our own. This function will *not* return until
1085 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087static int scsi_request_sense(struct scsi_cmnd *scmd)
1088{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001089 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
James Bottomley24510792013-11-11 13:44:53 +01001092static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1093{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001094 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001095 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1096 if (sdrv->eh_action)
1097 rtn = sdrv->eh_action(scmd, rtn);
1098 }
1099 return rtn;
1100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/**
1103 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1104 * @scmd: Original SCSI cmd that eh has finished.
1105 * @done_q: Queue for processed commands.
1106 *
1107 * Notes:
1108 * We don't want to use the normal command completion while we are are
1109 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001110 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * keep a list of pending commands for final completion, and once we
1112 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001113 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001114void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 list_move_tail(&scmd->eh_entry, done_q);
1117}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001118EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120/**
1121 * scsi_eh_get_sense - Get device sense data.
1122 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001123 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *
1125 * Description:
1126 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001127 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
1129 * Notes:
1130 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001131 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * it down before we request it.
1133 *
1134 * All drivers should request sense information internally these days,
1135 * so for now all I have to say is tough noogies if you end up in here.
1136 *
1137 * XXX: Long term this code should go away, but that needs an audit of
1138 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001139 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001140int scsi_eh_get_sense(struct list_head *work_q,
1141 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001143 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001144 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int rtn;
1146
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001147 /*
1148 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1149 * should not get sense.
1150 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001151 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001152 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 SCSI_SENSE_VALID(scmd))
1154 continue;
1155
Hannes Reineckeb4562022013-10-23 10:51:21 +02001156 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001157 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001158 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001159 scmd_printk(KERN_INFO, scmd,
1160 "%s: skip request sense, past eh deadline\n",
1161 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001162 break;
1163 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001164 if (status_byte(scmd->result) != CHECK_CONDITION)
1165 /*
1166 * don't request sense if there's no check condition
1167 * status because the error we're processing isn't one
1168 * that has a sense code (and some devices get
1169 * confused by sense requests out of the blue)
1170 */
1171 continue;
1172
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001173 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1174 "%s: requesting sense\n",
1175 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 rtn = scsi_request_sense(scmd);
1177 if (rtn != SUCCESS)
1178 continue;
1179
Hannes Reinecke91921e02014-06-25 16:39:59 +02001180 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001181 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001182 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 rtn = scsi_decide_disposition(scmd);
1185
1186 /*
1187 * if the result was normal, then just pass it along to the
1188 * upper level.
1189 */
1190 if (rtn == SUCCESS)
1191 /* we don't want this command reissued, just
1192 * finished with the sense data, so set
1193 * retries to the max allowed to ensure it
1194 * won't get reissued */
1195 scmd->retries = scmd->allowed;
1196 else if (rtn != NEEDS_RETRY)
1197 continue;
1198
1199 scsi_eh_finish_cmd(scmd, done_q);
1200 }
1201
1202 return list_empty(work_q);
1203}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001204EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001208 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *
1210 * Return value:
1211 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213static int scsi_eh_tur(struct scsi_cmnd *scmd)
1214{
1215 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1216 int retry_cnt = 1, rtn;
1217
1218retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001219 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1220 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Hannes Reinecke91921e02014-06-25 16:39:59 +02001222 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001223 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001224
1225 switch (rtn) {
1226 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (retry_cnt--)
1228 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001229 /*FALLTHRU*/
1230 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001231 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001232 default:
1233 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237/**
David Jeffery3eef6252011-05-19 14:41:12 -04001238 * scsi_eh_test_devices - check if devices are responding from error recovery.
1239 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001240 * @work_q: queue for commands which still need more error recovery
1241 * @done_q: queue for commands which are finished
1242 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001243 *
1244 * Decription:
1245 * Tests if devices are in a working state. Commands to devices now in
1246 * a working state are sent to the done_q while commands to devices which
1247 * are still failing to respond are returned to the work_q for more
1248 * processing.
1249 **/
1250static int scsi_eh_test_devices(struct list_head *cmd_list,
1251 struct list_head *work_q,
1252 struct list_head *done_q, int try_stu)
1253{
1254 struct scsi_cmnd *scmd, *next;
1255 struct scsi_device *sdev;
1256 int finish_cmds;
1257
1258 while (!list_empty(cmd_list)) {
1259 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1260 sdev = scmd->device;
1261
Hannes Reineckeb4562022013-10-23 10:51:21 +02001262 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001263 if (scsi_host_eh_past_deadline(sdev->host)) {
1264 /* Push items back onto work_q */
1265 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001266 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001267 sdev_printk(KERN_INFO, sdev,
1268 "%s: skip test device, past eh deadline",
1269 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001270 break;
1271 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001272 }
1273
David Jeffery3eef6252011-05-19 14:41:12 -04001274 finish_cmds = !scsi_device_online(scmd->device) ||
1275 (try_stu && !scsi_eh_try_stu(scmd) &&
1276 !scsi_eh_tur(scmd)) ||
1277 !scsi_eh_tur(scmd);
1278
1279 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1280 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001281 if (finish_cmds &&
1282 (try_stu ||
1283 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001284 scsi_eh_finish_cmd(scmd, done_q);
1285 else
1286 list_move_tail(&scmd->eh_entry, work_q);
1287 }
1288 }
1289 return list_empty(work_q);
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001294 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *
1296 * Return value:
1297 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1300{
1301 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Christoph Hellwig631c2282006-07-08 20:42:15 +02001303 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001304 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Brian Kinged773e62007-03-29 15:25:52 -05001306 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001307 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001308
Christoph Hellwig631c2282006-07-08 20:42:15 +02001309 if (rtn == SUCCESS)
1310 return 0;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 1;
1314}
1315
1316 /**
1317 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001318 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001319 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001320 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 *
1322 * Notes:
1323 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001324 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326static int scsi_eh_stu(struct Scsi_Host *shost,
1327 struct list_head *work_q,
1328 struct list_head *done_q)
1329{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001330 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 struct scsi_device *sdev;
1332
1333 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001334 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001335 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001336 sdev_printk(KERN_INFO, sdev,
1337 "%s: skip START_UNIT, past eh deadline\n",
1338 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001339 break;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 stu_scmd = NULL;
1342 list_for_each_entry(scmd, work_q, eh_entry)
1343 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1344 scsi_check_sense(scmd) == FAILED ) {
1345 stu_scmd = scmd;
1346 break;
1347 }
1348
1349 if (!stu_scmd)
1350 continue;
1351
Hannes Reinecke91921e02014-06-25 16:39:59 +02001352 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001353 sdev_printk(KERN_INFO, sdev,
1354 "%s: Sending START_UNIT\n",
1355 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 if (!scsi_eh_try_stu(stu_scmd)) {
1358 if (!scsi_device_online(sdev) ||
1359 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001360 list_for_each_entry_safe(scmd, next,
1361 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001362 if (scmd->device == sdev &&
1363 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 scsi_eh_finish_cmd(scmd, done_q);
1365 }
1366 }
1367 } else {
1368 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001369 sdev_printk(KERN_INFO, sdev,
1370 "%s: START_UNIT failed\n",
1371 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 }
1374
1375 return list_empty(work_q);
1376}
1377
1378
1379/**
1380 * scsi_eh_bus_device_reset - send bdr if needed
1381 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001382 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001383 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 *
1385 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001386 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * devices that are jammed or not - if we have multiple devices, it
1388 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001389 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1392 struct list_head *work_q,
1393 struct list_head *done_q)
1394{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001395 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct scsi_device *sdev;
1397 int rtn;
1398
1399 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001400 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001401 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001402 sdev_printk(KERN_INFO, sdev,
1403 "%s: skip BDR, past eh deadline\n",
1404 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001405 break;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 bdr_scmd = NULL;
1408 list_for_each_entry(scmd, work_q, eh_entry)
1409 if (scmd->device == sdev) {
1410 bdr_scmd = scmd;
1411 break;
1412 }
1413
1414 if (!bdr_scmd)
1415 continue;
1416
Hannes Reinecke91921e02014-06-25 16:39:59 +02001417 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001418 sdev_printk(KERN_INFO, sdev,
1419 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001421 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001423 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001425 list_for_each_entry_safe(scmd, next,
1426 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001427 if (scmd->device == sdev &&
1428 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 scsi_eh_finish_cmd(scmd,
1430 done_q);
1431 }
1432 }
1433 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001434 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001435 sdev_printk(KERN_INFO, sdev,
1436 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 }
1439
1440 return list_empty(work_q);
1441}
1442
1443/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001444 * scsi_eh_target_reset - send target reset if needed
1445 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001446 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001447 * @done_q: &list_head for processed commands.
1448 *
1449 * Notes:
1450 * Try a target reset.
1451 */
1452static int scsi_eh_target_reset(struct Scsi_Host *shost,
1453 struct list_head *work_q,
1454 struct list_head *done_q)
1455{
James Bottomley98db5192010-10-25 15:53:41 -05001456 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001457 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001458
James Bottomley98db5192010-10-25 15:53:41 -05001459 list_splice_init(work_q, &tmp_list);
1460
1461 while (!list_empty(&tmp_list)) {
1462 struct scsi_cmnd *next, *scmd;
1463 int rtn;
1464 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001465
Hannes Reineckeb4562022013-10-23 10:51:21 +02001466 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001467 /* push back on work queue for further processing */
1468 list_splice_init(&check_list, work_q);
1469 list_splice_init(&tmp_list, work_q);
1470 SCSI_LOG_ERROR_RECOVERY(3,
1471 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001472 "%s: Skip target reset, past eh deadline\n",
1473 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001474 return list_empty(work_q);
1475 }
James Bottomley98db5192010-10-25 15:53:41 -05001476
1477 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1478 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001479
Hannes Reinecke91921e02014-06-25 16:39:59 +02001480 SCSI_LOG_ERROR_RECOVERY(3,
1481 shost_printk(KERN_INFO, shost,
1482 "%s: Sending target reset to target %d\n",
1483 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001484 rtn = scsi_try_target_reset(scmd);
1485 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001486 SCSI_LOG_ERROR_RECOVERY(3,
1487 shost_printk(KERN_INFO, shost,
1488 "%s: Target reset failed"
1489 " target: %d\n",
1490 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001491 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1492 if (scmd_id(scmd) != id)
1493 continue;
1494
David Jeffery3eef6252011-05-19 14:41:12 -04001495 if (rtn == SUCCESS)
1496 list_move_tail(&scmd->eh_entry, &check_list);
1497 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001498 scsi_eh_finish_cmd(scmd, done_q);
1499 else
1500 /* push back on work queue for further processing */
1501 list_move(&scmd->eh_entry, work_q);
1502 }
1503 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001504
David Jeffery3eef6252011-05-19 14:41:12 -04001505 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001506}
1507
1508/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001509 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001510 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001511 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001512 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001513 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1515 struct list_head *work_q,
1516 struct list_head *done_q)
1517{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001518 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001519 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 unsigned int channel;
1521 int rtn;
1522
1523 /*
1524 * we really want to loop over the various channels, and do this on
1525 * a channel by channel basis. we should also check to see if any
1526 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001527 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
1529
1530 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001531 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001532 list_splice_init(&check_list, work_q);
1533 SCSI_LOG_ERROR_RECOVERY(3,
1534 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001535 "%s: skip BRST, past eh deadline\n",
1536 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001537 return list_empty(work_q);
1538 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 chan_scmd = NULL;
1541 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001542 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 chan_scmd = scmd;
1544 break;
1545 /*
1546 * FIXME add back in some support for
1547 * soft_reset devices.
1548 */
1549 }
1550 }
1551
1552 if (!chan_scmd)
1553 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001554 SCSI_LOG_ERROR_RECOVERY(3,
1555 shost_printk(KERN_INFO, shost,
1556 "%s: Sending BRST chan: %d\n",
1557 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001559 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001560 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001561 if (channel == scmd_channel(scmd)) {
1562 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 scsi_eh_finish_cmd(scmd,
1564 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001565 else
1566 list_move_tail(&scmd->eh_entry,
1567 &check_list);
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001571 SCSI_LOG_ERROR_RECOVERY(3,
1572 shost_printk(KERN_INFO, shost,
1573 "%s: BRST failed chan: %d\n",
1574 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 }
David Jeffery3eef6252011-05-19 14:41:12 -04001577 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
1580/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001581 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001582 * @shost: host to be reset.
1583 * @work_q: &list_head for pending commands.
1584 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001585 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001586static int scsi_eh_host_reset(struct Scsi_Host *shost,
1587 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 struct list_head *done_q)
1589{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001590 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001591 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (!list_empty(work_q)) {
1595 scmd = list_entry(work_q->next,
1596 struct scsi_cmnd, eh_entry);
1597
Hannes Reinecke91921e02014-06-25 16:39:59 +02001598 SCSI_LOG_ERROR_RECOVERY(3,
1599 shost_printk(KERN_INFO, shost,
1600 "%s: Sending HRST\n",
1601 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001604 if (rtn == SUCCESS) {
1605 list_splice_init(work_q, &check_list);
1606 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001607 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 scsi_eh_finish_cmd(scmd, done_q);
1609 }
1610 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001611 SCSI_LOG_ERROR_RECOVERY(3,
1612 shost_printk(KERN_INFO, shost,
1613 "%s: HRST failed\n",
1614 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 }
David Jeffery3eef6252011-05-19 14:41:12 -04001617 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620/**
1621 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001622 * @work_q: &list_head for pending commands.
1623 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625static void scsi_eh_offline_sdevs(struct list_head *work_q,
1626 struct list_head *done_q)
1627{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001628 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001630 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001631 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1632 "not ready after error recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 scsi_eh_finish_cmd(scmd, done_q);
1635 }
1636 return;
1637}
1638
1639/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001640 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001641 * @scmd: SCSI cmd to examine.
1642 */
1643int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1644{
1645 switch (host_byte(scmd->result)) {
1646 case DID_OK:
1647 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001648 case DID_TIME_OUT:
1649 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001650 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001651 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001652 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001653 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001654 case DID_ERROR:
1655 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1656 status_byte(scmd->result) == RESERVATION_CONFLICT)
1657 return 0;
1658 /* fall through */
1659 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001660 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001661 }
1662
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001663 if (status_byte(scmd->result) != CHECK_CONDITION)
1664 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001665
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001666check_type:
1667 /*
1668 * assume caller has checked sense and determined
1669 * the check condition was retryable.
1670 */
1671 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001672 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001673 return 1;
1674 else
1675 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001676}
1677
1678/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1680 * @scmd: SCSI cmd to examine.
1681 *
1682 * Notes:
1683 * This is *only* called when we are examining the status after sending
1684 * out the actual data command. any commands that are queued for error
1685 * recovery (e.g. test_unit_ready) do *not* come through here.
1686 *
1687 * When this routine returns failed, it means the error handler thread
1688 * is woken. In cases where the error code indicates an error that
1689 * doesn't require the error handler read (i.e. we don't need to
1690 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692int scsi_decide_disposition(struct scsi_cmnd *scmd)
1693{
1694 int rtn;
1695
1696 /*
1697 * if the device is offline, then we clearly just pass the result back
1698 * up to the top level.
1699 */
1700 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001701 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1702 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return SUCCESS;
1704 }
1705
1706 /*
1707 * first check the host byte, to see if there is anything in there
1708 * that would indicate what we need to do.
1709 */
1710 switch (host_byte(scmd->result)) {
1711 case DID_PASSTHROUGH:
1712 /*
1713 * no matter what, pass this through to the upper layer.
1714 * nuke this special code so that it looks like we are saying
1715 * did_ok.
1716 */
1717 scmd->result &= 0xff00ffff;
1718 return SUCCESS;
1719 case DID_OK:
1720 /*
1721 * looks good. drop through, and check the next byte.
1722 */
1723 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001724 case DID_ABORT:
1725 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001726 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001727 return SUCCESS;
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 case DID_NO_CONNECT:
1730 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * note - this means that we just report the status back
1733 * to the top level driver, not that we actually think
1734 * that it indicates SUCCESS.
1735 */
1736 return SUCCESS;
1737 /*
1738 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001739 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * in order to avoid endless loops (db)
1741 *
1742 * actually this is a bug in this function here. we should
1743 * be mindful of the maximum number of retries specified
1744 * and not get stuck in a loop.
1745 */
1746 case DID_SOFT_ERROR:
1747 goto maybe_retry;
1748 case DID_IMM_RETRY:
1749 return NEEDS_RETRY;
1750
bf341912005-04-12 17:49:09 -05001751 case DID_REQUEUE:
1752 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001753 case DID_TRANSPORT_DISRUPTED:
1754 /*
1755 * LLD/transport was disrupted during processing of the IO.
1756 * The transport class is now blocked/blocking,
1757 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001758 * based on its timers and recovery capablilities if
1759 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001760 */
Mike Christie939c22882008-11-04 19:47:19 -06001761 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001762 case DID_TRANSPORT_FAILFAST:
1763 /*
1764 * The transport decided to failfast the IO (most likely
1765 * the fast io fail tmo fired), so send IO directly upwards.
1766 */
1767 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 case DID_ERROR:
1769 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1770 status_byte(scmd->result) == RESERVATION_CONFLICT)
1771 /*
1772 * execute reservation conflict processing code
1773 * lower down
1774 */
1775 break;
1776 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 case DID_BUS_BUSY:
1778 case DID_PARITY:
1779 goto maybe_retry;
1780 case DID_TIME_OUT:
1781 /*
1782 * when we scan the bus, we get timeout messages for
1783 * these commands if there is no device available.
1784 * other hosts report did_no_connect for the same thing.
1785 */
1786 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1787 scmd->cmnd[0] == INQUIRY)) {
1788 return SUCCESS;
1789 } else {
1790 return FAILED;
1791 }
1792 case DID_RESET:
1793 return SUCCESS;
1794 default:
1795 return FAILED;
1796 }
1797
1798 /*
1799 * next, check the message byte.
1800 */
1801 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1802 return FAILED;
1803
1804 /*
1805 * check the status byte to see if this indicates anything special.
1806 */
1807 switch (status_byte(scmd->result)) {
1808 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001809 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
1811 * the case of trying to send too many commands to a
1812 * tagged queueing device.
1813 */
1814 case BUSY:
1815 /*
1816 * device can't talk to us at the moment. Should only
1817 * occur (SAM-3) when the task queue is empty, so will cause
1818 * the empty queue handling to trigger a stall in the
1819 * device.
1820 */
1821 return ADD_TO_MLQUEUE;
1822 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001823 if (scmd->cmnd[0] == REPORT_LUNS)
1824 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001825 scsi_handle_queue_ramp_up(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001828 case TASK_ABORTED:
1829 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 case CHECK_CONDITION:
1831 rtn = scsi_check_sense(scmd);
1832 if (rtn == NEEDS_RETRY)
1833 goto maybe_retry;
1834 /* if rtn == FAILED, we have no sense information;
1835 * returning FAILED will wake the error handler thread
1836 * to collect the sense and redo the decide
1837 * disposition */
1838 return rtn;
1839 case CONDITION_GOOD:
1840 case INTERMEDIATE_GOOD:
1841 case INTERMEDIATE_C_GOOD:
1842 case ACA_ACTIVE:
1843 /*
1844 * who knows? FIXME(eric)
1845 */
1846 return SUCCESS;
1847
1848 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001849 sdev_printk(KERN_INFO, scmd->device,
1850 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001851 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return SUCCESS; /* causes immediate i/o error */
1853 default:
1854 return FAILED;
1855 }
1856 return FAILED;
1857
1858 maybe_retry:
1859
1860 /* we requeue for retry because the error was retryable, and
1861 * the request was not marked fast fail. Note that above,
1862 * even if the request is marked fast fail, we still requeue
1863 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001864 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001865 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return NEEDS_RETRY;
1867 } else {
1868 /*
1869 * no more retries - report this one back to upper level.
1870 */
1871 return SUCCESS;
1872 }
1873}
1874
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001875static void eh_lock_door_done(struct request *req, int uptodate)
1876{
1877 __blk_put_request(req->q, req);
1878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * scsi_eh_lock_door - Prevent medium removal for the specified device
1882 * @sdev: SCSI device to prevent medium removal
1883 *
1884 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001885 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 *
1887 * Notes:
1888 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1889 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891static void scsi_eh_lock_door(struct scsi_device *sdev)
1892{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001893 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001894 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
James Bottomley91bc31f2009-05-17 09:30:48 -05001896 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001897 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001898 * request becomes available
1899 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001900 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001901 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001902 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001903 rq = scsi_req(req);
1904 scsi_req_init(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001906 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1907 rq->cmd[1] = 0;
1908 rq->cmd[2] = 0;
1909 rq->cmd[3] = 0;
1910 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1911 rq->cmd[5] = 0;
1912 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001913
Christoph Hellwige8064022016-10-20 15:12:13 +02001914 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001915 req->timeout = 10 * HZ;
1916 req->retries = 5;
1917
1918 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/**
1922 * scsi_restart_operations - restart io operations to the specified host.
1923 * @shost: Host we are restarting.
1924 *
1925 * Notes:
1926 * When we entered the error handler, we blocked all further i/o to
1927 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929static void scsi_restart_operations(struct Scsi_Host *shost)
1930{
1931 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001932 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /*
1935 * If the door was locked, we need to insert a door lock request
1936 * onto the head of the SCSI request queue for the device. There
1937 * is no point trying to lock the door of an off-line device.
1938 */
1939 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001940 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001942 sdev->was_reset = 0;
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
1946 /*
1947 * next free up anything directly waiting upon the host. this
1948 * will be requests for character device operations, and also for
1949 * ioctls to queued block devices.
1950 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02001951 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02001952 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
James Bottomley939647e2005-09-18 15:05:20 -05001954 spin_lock_irqsave(shost->host_lock, flags);
1955 if (scsi_host_set_state(shost, SHOST_RUNNING))
1956 if (scsi_host_set_state(shost, SHOST_CANCEL))
1957 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1958 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 wake_up(&shost->host_wait);
1961
1962 /*
1963 * finally we need to re-initiate requests that may be pending. we will
1964 * have had everything blocked while error handling is taking place, and
1965 * now that error recovery is done, we will need to ensure that these
1966 * requests are started.
1967 */
1968 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07001969
1970 /*
1971 * if eh is active and host_eh_scheduled is pending we need to re-run
1972 * recovery. we do this check after scsi_run_host_queues() to allow
1973 * everything pent up since the last eh run a chance to make forward
1974 * progress before we sync again. Either we'll immediately re-run
1975 * recovery or scsi_device_unbusy() will wake us again when these
1976 * pending commands complete.
1977 */
1978 spin_lock_irqsave(shost->host_lock, flags);
1979 if (shost->host_eh_scheduled)
1980 if (scsi_host_set_state(shost, SHOST_RECOVERY))
1981 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
1982 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985/**
1986 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001987 * @shost: host to be recovered.
1988 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001989 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001990 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001991void scsi_eh_ready_devs(struct Scsi_Host *shost,
1992 struct list_head *work_q,
1993 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 if (!scsi_eh_stu(shost, work_q, done_q))
1996 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06001997 if (!scsi_eh_target_reset(shost, work_q, done_q))
1998 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02001999 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002000 scsi_eh_offline_sdevs(work_q,
2001 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002003EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005/**
2006 * scsi_eh_flush_done_q - finish processed commands or retry them.
2007 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002008 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002009void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002011 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002013 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2014 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002016 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002017 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002018 SCSI_LOG_ERROR_RECOVERY(3,
2019 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002020 "%s: flush retry cmd\n",
2021 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2023 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002024 /*
2025 * If just we got sense for the device (called
2026 * scsi_eh_get_sense), scmd->result is already
2027 * set, do not set DRIVER_TIMEOUT.
2028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (!scmd->result)
2030 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002031 SCSI_LOG_ERROR_RECOVERY(3,
2032 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002033 "%s: flush finish cmd\n",
2034 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 scsi_finish_command(scmd);
2036 }
2037 }
2038}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002039EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041/**
2042 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2043 * @shost: Host to unjam.
2044 *
2045 * Notes:
2046 * When we come in here, we *know* that all commands on the bus have
2047 * either completed, failed or timed out. we also know that no further
2048 * commands are being sent to the host, so things are relatively quiet
2049 * and we have freedom to fiddle with things as we wish.
2050 *
2051 * This is only the *default* implementation. it is possible for
2052 * individual drivers to supply their own version of this function, and
2053 * if the maintainer wishes to do this, it is strongly suggested that
2054 * this function be taken as a template and modified. this function
2055 * was designed to correctly handle problems for about 95% of the
2056 * different cases out there, and it should always provide at least a
2057 * reasonable amount of error recovery.
2058 *
2059 * Any command marked 'failed' or 'timeout' must eventually have
2060 * scsi_finish_cmd() called for it. we do all of the retry stuff
2061 * here, so when we restart the host after we return it should have an
2062 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002063 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static void scsi_unjam_host(struct Scsi_Host *shost)
2065{
2066 unsigned long flags;
2067 LIST_HEAD(eh_work_q);
2068 LIST_HEAD(eh_done_q);
2069
2070 spin_lock_irqsave(shost->host_lock, flags);
2071 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2072 spin_unlock_irqrestore(shost->host_lock, flags);
2073
2074 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2075
2076 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002077 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Hannes Reineckeb4562022013-10-23 10:51:21 +02002079 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002080 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002081 shost->last_reset = 0;
2082 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 scsi_eh_flush_done_q(&eh_done_q);
2084}
2085
2086/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002087 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * @data: Host for which we are running.
2089 *
2090 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002091 * This is the main error handling loop. This is run as a kernel thread
2092 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002093 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094int scsi_error_handler(void *data)
2095{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002096 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002099 * We use TASK_INTERRUPTIBLE so that the thread is not
2100 * counted against the load average as a running process.
2101 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002102 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 */
Michal Hocko537b6042015-08-27 20:16:37 +02002104 while (true) {
2105 /*
2106 * The sequence in kthread_stop() sets the stop flag first
2107 * then wakes the process. To avoid missed wakeups, the task
2108 * should always be in a non running state before the stop
2109 * flag is checked
2110 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002111 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002112 if (kthread_should_stop())
2113 break;
2114
Tejun Heoee7863b2006-05-15 20:57:20 +09002115 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002116 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002117 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002118 shost_printk(KERN_INFO, shost,
2119 "scsi_eh_%d: sleeping\n",
2120 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002121 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002122 continue;
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
James Bottomley3ed7a472005-09-19 09:50:04 -05002125 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002126 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002127 shost_printk(KERN_INFO, shost,
2128 "scsi_eh_%d: waking up %d/%d/%d\n",
2129 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002130 shost->host_failed,
2131 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /*
2134 * We have a host that is failing for some reason. Figure out
2135 * what we need to do to get it up and online again (if we can).
2136 * If we fail, we end up taking the thing offline.
2137 */
Lin Mingae0751f2011-12-05 09:20:24 +08002138 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002139 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002140 shost_printk(KERN_ERR, shost,
2141 "scsi_eh_%d: unable to autoresume\n",
2142 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002143 continue;
2144 }
2145
Christoph Hellwig9227c332006-04-01 19:21:04 +02002146 if (shost->transportt->eh_strategy_handler)
2147 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 else
2149 scsi_unjam_host(shost);
2150
Wei Fang72d8c362016-06-07 14:53:56 +08002151 /* All scmds have been handled */
2152 shost->host_failed = 0;
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
2155 * Note - if the above fails completely, the action is to take
2156 * individual devices offline and flush the queue of any
2157 * outstanding requests that may have been pending. When we
2158 * restart, we restart any I/O to any other devices on the bus
2159 * which are still online.
2160 */
2161 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002162 if (!shost->eh_noresume)
2163 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002165 __set_current_state(TASK_RUNNING);
2166
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002167 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002168 shost_printk(KERN_INFO, shost,
2169 "Error handler scsi_eh_%d exiting\n",
2170 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002171 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return 0;
2173}
2174
2175/*
2176 * Function: scsi_report_bus_reset()
2177 *
2178 * Purpose: Utility function used by low-level drivers to report that
2179 * they have observed a bus reset on the bus being handled.
2180 *
2181 * Arguments: shost - Host in question
2182 * channel - channel on which reset was observed.
2183 *
2184 * Returns: Nothing
2185 *
2186 * Lock status: Host lock must be held.
2187 *
2188 * Notes: This only needs to be called if the reset is one which
2189 * originates from an unknown location. Resets originated
2190 * by the mid-level itself don't need to call this, but there
2191 * should be no harm.
2192 *
2193 * The main purpose of this is to make sure that a CHECK_CONDITION
2194 * is properly treated.
2195 */
2196void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2197{
2198 struct scsi_device *sdev;
2199
2200 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002201 if (channel == sdev_channel(sdev))
2202 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204}
2205EXPORT_SYMBOL(scsi_report_bus_reset);
2206
2207/*
2208 * Function: scsi_report_device_reset()
2209 *
2210 * Purpose: Utility function used by low-level drivers to report that
2211 * they have observed a device reset on the device being handled.
2212 *
2213 * Arguments: shost - Host in question
2214 * channel - channel on which reset was observed
2215 * target - target on which reset was observed
2216 *
2217 * Returns: Nothing
2218 *
2219 * Lock status: Host lock must be held
2220 *
2221 * Notes: This only needs to be called if the reset is one which
2222 * originates from an unknown location. Resets originated
2223 * by the mid-level itself don't need to call this, but there
2224 * should be no harm.
2225 *
2226 * The main purpose of this is to make sure that a CHECK_CONDITION
2227 * is properly treated.
2228 */
2229void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2230{
2231 struct scsi_device *sdev;
2232
2233 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002234 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002235 target == sdev_id(sdev))
2236 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238}
2239EXPORT_SYMBOL(scsi_report_device_reset);
2240
2241static void
2242scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2243{
2244}
2245
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002246/**
2247 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2248 * @dev: scsi_device to operate on
2249 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 */
2251int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002252scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Alan Sternbc4f2402010-06-17 10:41:42 -04002254 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002255 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002256 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002257 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002258 int error = 0, rtn, val;
2259
2260 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2261 return -EACCES;
2262
2263 error = get_user(val, arg);
2264 if (error)
2265 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Alan Sternbc4f2402010-06-17 10:41:42 -04002267 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002268 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002269
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002270 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002271 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2272 shost->hostt->cmd_size, GFP_KERNEL);
2273 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002274 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002275 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002276
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002277 scmd = (struct scsi_cmnd *)(rq + 1);
2278 scsi_init_command(dev, scmd);
2279 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002280 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002283 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 scmd->cmd_len = 0;
2286
2287 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
James Smartd7a1bb02006-03-08 14:50:12 -05002289 spin_lock_irqsave(shost->host_lock, flags);
2290 shost->tmf_in_progress = 1;
2291 spin_unlock_irqrestore(shost->host_lock, flags);
2292
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002293 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2294 case SG_SCSI_RESET_NOTHING:
2295 rtn = SUCCESS;
2296 break;
2297 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002299 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 break;
2301 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002302 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002303 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002304 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002305 break;
2306 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002307 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002309 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 break;
2311 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002312 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002314 if (rtn == SUCCESS)
2315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 default:
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002317 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002322 error = (rtn == SUCCESS) ? 0 : -EIO;
2323
James Smartd7a1bb02006-03-08 14:50:12 -05002324 spin_lock_irqsave(shost->host_lock, flags);
2325 shost->tmf_in_progress = 0;
2326 spin_unlock_irqrestore(shost->host_lock, flags);
2327
2328 /*
2329 * be sure to wake up anyone who was sleeping or had their queue
2330 * suspended while we performed the TMF.
2331 */
2332 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002333 shost_printk(KERN_INFO, shost,
2334 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002335
2336 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002337 scsi_run_host_queues(shost);
2338
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002339 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002340 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002341
Christoph Hellwig04796332014-02-20 14:20:55 -08002342out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002343 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002344 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002346EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002348bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2349 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002352 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354EXPORT_SYMBOL(scsi_command_normalize_sense);
2355
2356/**
Rob Landleyeb448202007-11-03 13:30:39 -05002357 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 * @sense_buffer: byte array of sense data
2359 * @sb_len: number of valid bytes in sense_buffer
2360 * @info_out: pointer to 64 integer where 8 or 4 byte information
2361 * field will be placed if found.
2362 *
2363 * Return value:
2364 * 1 if information field found, 0 if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2367 u64 * info_out)
2368{
2369 int j;
2370 const u8 * ucp;
2371 u64 ull;
2372
2373 if (sb_len < 7)
2374 return 0;
2375 switch (sense_buffer[0] & 0x7f) {
2376 case 0x70:
2377 case 0x71:
2378 if (sense_buffer[0] & 0x80) {
2379 *info_out = (sense_buffer[3] << 24) +
2380 (sense_buffer[4] << 16) +
2381 (sense_buffer[5] << 8) + sense_buffer[6];
2382 return 1;
2383 } else
2384 return 0;
2385 case 0x72:
2386 case 0x73:
2387 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2388 0 /* info desc */);
2389 if (ucp && (0xa == ucp[1])) {
2390 ull = 0;
2391 for (j = 0; j < 8; ++j) {
2392 if (j > 0)
2393 ull <<= 8;
2394 ull |= ucp[4 + j];
2395 }
2396 *info_out = ull;
2397 return 1;
2398 } else
2399 return 0;
2400 default:
2401 return 0;
2402 }
2403}
2404EXPORT_SYMBOL(scsi_get_sense_info_fld);