blob: 62b56de38ae81ba4a83c72d516cb2ef73c9016b0 [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
Damien Le Moal29087692017-04-24 16:51:12 +090049#include <asm/unaligned.h>
50
James Bottomley14216562012-07-25 23:55:55 +040051static void scsi_eh_done(struct scsi_cmnd *scmd);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * These should *probably* be handled by the host itself.
55 * Since it is allowed to sleep, it probably should.
56 */
57#define BUS_RESET_SETTLE_TIME (10)
58#define HOST_RESET_SETTLE_TIME (10)
59
David Jeffery3eef6252011-05-19 14:41:12 -040060static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +010061static int scsi_try_to_abort_cmd(struct scsi_host_template *,
62 struct scsi_cmnd *);
David Jeffery3eef6252011-05-19 14:41:12 -040063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* called with shost->host_lock held */
65void scsi_eh_wakeup(struct Scsi_Host *shost)
66{
Christoph Hellwig74665012014-01-22 15:29:29 +010067 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090068 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -050069 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020070 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
71 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73}
Tejun Heof8bbfc22006-05-19 21:07:05 +090074
75/**
76 * scsi_schedule_eh - schedule EH for SCSI host
77 * @shost: SCSI host to invoke error handling on.
78 *
79 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080080 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090081void scsi_schedule_eh(struct Scsi_Host *shost)
82{
83 unsigned long flags;
84
85 spin_lock_irqsave(shost->host_lock, flags);
86
87 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
88 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
89 shost->host_eh_scheduled++;
90 scsi_eh_wakeup(shost);
91 }
92
93 spin_unlock_irqrestore(shost->host_lock, flags);
94}
95EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Hannes Reineckeb4562022013-10-23 10:51:21 +020097static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
98{
Ren Mingxinbb3b6212013-11-11 13:44:56 +010099 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200100 return 0;
101
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100102 /*
103 * 32bit accesses are guaranteed to be atomic
104 * (on all supported architectures), so instead
105 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100106 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100107 * time_before call.
108 */
109 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100110 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200111 return 0;
112
113 return 1;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100117 * scmd_eh_abort_handler - Handle command aborts
118 * @work: command to be aborted.
119 */
120void
121scmd_eh_abort_handler(struct work_struct *work)
122{
123 struct scsi_cmnd *scmd =
124 container_of(work, struct scsi_cmnd, abort_work.work);
125 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100126 int rtn;
127
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100128 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100129 SCSI_LOG_ERROR_RECOVERY(3,
130 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100131 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100132 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100133 SCSI_LOG_ERROR_RECOVERY(3,
134 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100135 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100136 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
137 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200138 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100139 if (scsi_host_eh_past_deadline(sdev->host)) {
140 SCSI_LOG_ERROR_RECOVERY(3,
141 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100142 "eh timeout, not retrying "
143 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100144 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100145 (++scmd->retries <= scmd->allowed)) {
146 SCSI_LOG_ERROR_RECOVERY(3,
147 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100148 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100149 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100150 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100151 } else {
152 SCSI_LOG_ERROR_RECOVERY(3,
153 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100154 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100155 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100156 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100157 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100158 } else {
159 SCSI_LOG_ERROR_RECOVERY(3,
160 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100161 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200162 (rtn == FAST_IO_FAIL) ?
163 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100164 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100165 }
166
Hannes Reineckea0658632017-04-06 15:36:35 +0200167 scsi_eh_scmd_add(scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100168}
169
170/**
171 * scsi_abort_command - schedule a command abort
172 * @scmd: scmd to abort.
173 *
174 * We only need to abort commands after a command timeout
175 */
176static int
177scsi_abort_command(struct scsi_cmnd *scmd)
178{
179 struct scsi_device *sdev = scmd->device;
180 struct Scsi_Host *shost = sdev->host;
181 unsigned long flags;
182
183 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
184 /*
185 * Retry after abort failed, escalate to next level.
186 */
187 SCSI_LOG_ERROR_RECOVERY(3,
188 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100189 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200190 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100191 return FAILED;
192 }
193
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100194 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100195 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100196 shost->last_reset = jiffies;
197 spin_unlock_irqrestore(shost->host_lock, flags);
198
199 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
200 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100201 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100202 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
203 return SUCCESS;
204}
205
206/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200207 * scsi_eh_reset - call into ->eh_action to reset internal counters
208 * @scmd: scmd to run eh on.
209 *
210 * The scsi driver might be carrying internal state about the
211 * devices, so we need to call into the driver to reset the
212 * internal state once the error handler is started.
213 */
214static void scsi_eh_reset(struct scsi_cmnd *scmd)
215{
216 if (!blk_rq_is_passthrough(scmd->request)) {
217 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
218 if (sdrv->eh_reset)
219 sdrv->eh_reset(scmd);
220 }
221}
222
223/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * scsi_eh_scmd_add - add scsi cmd to error handling.
225 * @scmd: scmd to run eh on.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800226 */
Hannes Reineckea0658632017-04-06 15:36:35 +0200227void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct Scsi_Host *shost = scmd->device->host;
230 unsigned long flags;
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200231 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200233 WARN_ON_ONCE(!shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200236 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
237 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
238 WARN_ON_ONCE(ret);
239 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100240 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200241 shost->last_reset = jiffies;
242
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200243 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 shost->host_failed++;
246 scsi_eh_wakeup(shost);
247 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700252 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
254 * Notes:
255 * We do not need to lock this. There is the potential for a race
256 * only in that the normal completion handling might run, but if the
257 * normal completion function determines that the timer has already
258 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800259 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700260enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Bart Van Asschebed22132017-08-25 13:46:32 -0700262 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700263 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100264 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500265
Kei Tokunagabf816232010-04-01 20:41:40 +0900266 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 scsi_log_completion(scmd, TIMEOUT_ERROR);
268
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100269 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200270 host->last_reset = jiffies;
271
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100272 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100273 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500274
Hannes Reineckea33c0702014-06-13 14:01:45 +0200275 if (rtn == BLK_EH_NOT_HANDLED) {
Hannes Reineckea0658632017-04-06 15:36:35 +0200276 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200277 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200278 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200279 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200280 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700281
Christoph Hellwigfa990782008-11-05 15:03:15 +0100282 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/**
286 * scsi_block_when_processing_errors - Prevent cmds from being queued.
287 * @sdev: Device on which we are performing recovery.
288 *
289 * Description:
290 * We block until the host is out of error recovery, and then check to
291 * see whether the host or the device is offline.
292 *
293 * Return value:
294 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296int scsi_block_when_processing_errors(struct scsi_device *sdev)
297{
298 int online;
299
James Bottomley939647e2005-09-18 15:05:20 -0500300 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 online = scsi_device_online(sdev);
303
Hannes Reinecke91921e02014-06-25 16:39:59 +0200304 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
305 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return online;
308}
309EXPORT_SYMBOL(scsi_block_when_processing_errors);
310
311#ifdef CONFIG_SCSI_LOGGING
312/**
313 * scsi_eh_prt_fail_stats - Log info on failures.
314 * @shost: scsi host being recovered.
315 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
318 struct list_head *work_q)
319{
320 struct scsi_cmnd *scmd;
321 struct scsi_device *sdev;
322 int total_failures = 0;
323 int cmd_failed = 0;
324 int cmd_cancel = 0;
325 int devices_failed = 0;
326
327 shost_for_each_device(sdev, shost) {
328 list_for_each_entry(scmd, work_q, eh_entry) {
329 if (scmd->device == sdev) {
330 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200331 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100333 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ++cmd_failed;
335 }
336 }
337
338 if (cmd_cancel || cmd_failed) {
339 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200340 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500341 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700342 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500343 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 cmd_cancel = 0;
345 cmd_failed = 0;
346 ++devices_failed;
347 }
348 }
349
Hannes Reinecke91921e02014-06-25 16:39:59 +0200350 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
351 "Total of %d commands on %d"
352 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100353 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355#endif
356
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400357 /**
358 * scsi_report_lun_change - Set flag on all *other* devices on the same target
359 * to indicate that a UNIT ATTENTION is expected.
360 * @sdev: Device reporting the UNIT ATTENTION
361 */
362static void scsi_report_lun_change(struct scsi_device *sdev)
363{
364 sdev->sdev_target->expecting_lun_change = 1;
365}
366
367/**
368 * scsi_report_sense - Examine scsi sense information and log messages for
369 * certain conditions, also issue uevents for some of them.
370 * @sdev: Device reporting the sense code
371 * @sshdr: sshdr to be examined
372 */
373static void scsi_report_sense(struct scsi_device *sdev,
374 struct scsi_sense_hdr *sshdr)
375{
376 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
377
378 if (sshdr->sense_key == UNIT_ATTENTION) {
379 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
380 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
381 sdev_printk(KERN_WARNING, sdev,
382 "Inquiry data has changed");
383 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
384 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
385 scsi_report_lun_change(sdev);
386 sdev_printk(KERN_WARNING, sdev,
387 "Warning! Received an indication that the "
388 "LUN assignments on this target have "
389 "changed. The Linux SCSI layer does not "
390 "automatically remap LUN assignments.\n");
391 } else if (sshdr->asc == 0x3f)
392 sdev_printk(KERN_WARNING, sdev,
393 "Warning! Received an indication that the "
394 "operating parameters on this target have "
395 "changed. The Linux SCSI layer does not "
396 "automatically adjust these parameters.\n");
397
398 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
399 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
400 sdev_printk(KERN_WARNING, sdev,
401 "Warning! Received an indication that the "
402 "LUN reached a thin provisioning soft "
403 "threshold.\n");
404 }
405
Hannes Reineckecf3431b2017-10-17 09:11:24 +0200406 if (sshdr->asc == 0x29) {
407 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
408 sdev_printk(KERN_WARNING, sdev,
409 "Power-on or device reset occurred\n");
410 }
411
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400412 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
413 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
414 sdev_printk(KERN_WARNING, sdev,
415 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200416 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
417 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
418 sdev_printk(KERN_WARNING, sdev,
419 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400420 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
421 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
422 sdev_printk(KERN_WARNING, sdev,
423 "Capacity data has changed");
424 } else if (sshdr->asc == 0x2a)
425 sdev_printk(KERN_WARNING, sdev,
426 "Parameters changed");
427 }
428
429 if (evt_type != SDEV_EVT_MAXBITS) {
430 set_bit(evt_type, sdev->pending_events);
431 schedule_work(&sdev->event_work);
432 }
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/**
436 * scsi_check_sense - Examine scsi cmd sense
437 * @scmd: Cmd to have sense checked.
438 *
439 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200440 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * Notes:
443 * When a deferred error is detected the current command has
444 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800445 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200446int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700448 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct scsi_sense_hdr sshdr;
450
451 if (! scsi_command_normalize_sense(scmd, &sshdr))
452 return FAILED; /* no valid sense data */
453
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400454 scsi_report_sense(sdev, &sshdr);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (scsi_sense_is_deferred(&sshdr))
457 return NEEDS_RETRY;
458
Christoph Hellwigee14c672015-08-27 14:16:59 +0200459 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700460 int rc;
461
Christoph Hellwigee14c672015-08-27 14:16:59 +0200462 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700463 if (rc != SCSI_RETURN_NOT_HANDLED)
464 return rc;
465 /* handler does not care. Drop down to default handling */
466 }
467
Christoph Hellwige925cc42014-11-06 15:11:22 -0600468 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
469 /*
470 * nasty: for mid-layer issued TURs, we need to return the
471 * actual sense data without any recovery attempt. For eh
472 * issued ones, we need to try to recover and interpret
473 */
474 return SUCCESS;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * Previous logic looked for FILEMARK, EOM or ILI which are
478 * mainly associated with tapes and returned SUCCESS.
479 */
480 if (sshdr.response_code == 0x70) {
481 /* fixed format */
482 if (scmd->sense_buffer[2] & 0xe0)
483 return SUCCESS;
484 } else {
485 /*
486 * descriptor format: look for "stream commands sense data
487 * descriptor" (see SSC-3). Assume single sense data
488 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
489 */
490 if ((sshdr.additional_length > 3) &&
491 (scmd->sense_buffer[8] == 0x4) &&
492 (scmd->sense_buffer[11] & 0xe0))
493 return SUCCESS;
494 }
495
496 switch (sshdr.sense_key) {
497 case NO_SENSE:
498 return SUCCESS;
499 case RECOVERED_ERROR:
500 return /* soft_error */ SUCCESS;
501
502 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400503 if (sshdr.asc == 0x10) /* DIF */
504 return SUCCESS;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return NEEDS_RETRY;
507 case NOT_READY:
508 case UNIT_ATTENTION:
509 /*
510 * if we are expecting a cc/ua because of a bus reset that we
511 * performed, treat this just as a retry. otherwise this is
512 * information that we should pass up to the upper-level driver
513 * so that we can deal with it there.
514 */
515 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900516 /*
517 * Because some device does not queue unit
518 * attentions correctly, we carefully check
519 * additional sense code and qualifier so as
520 * not to squash media change unit attention.
521 */
522 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
523 scmd->device->expecting_cc_ua = 0;
524 return NEEDS_RETRY;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400528 * we might also expect a cc/ua if another LUN on the target
529 * reported a UA with an ASC/ASCQ of 3F 0E -
530 * REPORTED LUNS DATA HAS CHANGED.
531 */
532 if (scmd->device->sdev_target->expecting_lun_change &&
533 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
534 return NEEDS_RETRY;
535 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100536 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * should retry.
538 */
539 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
540 return NEEDS_RETRY;
541 /*
542 * if the device is not started, we need to wake
543 * the error handler to start the motor
544 */
545 if (scmd->device->allow_restart &&
546 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
547 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100548 /*
549 * Pass the UA upwards for a determination in the completion
550 * functions.
551 */
552 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100554 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200555 case DATA_PROTECT:
556 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
557 /* Thin provisioning hard threshold reached */
558 set_host_byte(scmd, DID_ALLOC_FAILURE);
559 return SUCCESS;
560 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700561 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 case COPY_ABORTED:
563 case VOLUME_OVERFLOW:
564 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100565 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200566 set_host_byte(scmd, DID_TARGET_FAILURE);
567 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800570 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
571 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
572 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200573 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200574 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NEEDS_RETRY;
577
578 case HARDWARE_ERROR:
579 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700580 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200582 set_host_byte(scmd, DID_TARGET_FAILURE);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700583 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500586 if (sshdr.asc == 0x20 || /* Invalid command operation code */
587 sshdr.asc == 0x21 || /* Logical block address out of range */
Hannes Reineckea8bbb2a2017-10-17 09:10:56 +0200588 sshdr.asc == 0x22 || /* Invalid function */
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500589 sshdr.asc == 0x24 || /* Invalid field in cdb */
Martin Wilckd0b7a902017-09-27 14:44:19 +0200590 sshdr.asc == 0x26 || /* Parameter value invalid */
591 sshdr.asc == 0x27) { /* Write protected */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200592 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500593 }
594 return SUCCESS;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 default:
597 return SUCCESS;
598 }
599}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200600EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Vasu Dev4a840672009-10-22 15:46:33 -0700602static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
603{
604 struct scsi_host_template *sht = sdev->host->hostt;
605 struct scsi_device *tmp_sdev;
606
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100607 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700608 sdev->queue_depth >= sdev->max_queue_depth)
609 return;
610
611 if (time_before(jiffies,
612 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
613 return;
614
615 if (time_before(jiffies,
616 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
617 return;
618
619 /*
620 * Walk all devices of a target and do
621 * ramp up on them.
622 */
623 shost_for_each_device(tmp_sdev, sdev->host) {
624 if (tmp_sdev->channel != sdev->channel ||
625 tmp_sdev->id != sdev->id ||
626 tmp_sdev->queue_depth == sdev->max_queue_depth)
627 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100628
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100629 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700630 sdev->last_queue_ramp_up = jiffies;
631 }
632}
633
Mike Christie42a6a912009-10-15 17:46:44 -0700634static void scsi_handle_queue_full(struct scsi_device *sdev)
635{
636 struct scsi_host_template *sht = sdev->host->hostt;
637 struct scsi_device *tmp_sdev;
638
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100639 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700640 return;
641
642 shost_for_each_device(tmp_sdev, sdev->host) {
643 if (tmp_sdev->channel != sdev->channel ||
644 tmp_sdev->id != sdev->id)
645 continue;
646 /*
647 * We do not know the number of commands that were at
648 * the device when we got the queue full so we start
649 * from the highest possible value and work our way down.
650 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100651 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700652 }
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
656 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
657 * @scmd: SCSI cmd to examine.
658 *
659 * Notes:
660 * This is *only* called when we are examining the status of commands
661 * queued during error recovery. the main difference here is that we
662 * don't allow for the possibility of retries here, and we are a lot
663 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
666{
667 /*
668 * first check the host byte, to see if there is anything in there
669 * that would indicate what we need to do.
670 */
671 if (host_byte(scmd->result) == DID_RESET) {
672 /*
673 * rats. we are already in the error handler, so we now
674 * get to try and figure out what to do next. if the sense
675 * is valid, we have a pretty good idea of what to do.
676 * if not, we mark it as FAILED.
677 */
678 return scsi_check_sense(scmd);
679 }
680 if (host_byte(scmd->result) != DID_OK)
681 return FAILED;
682
683 /*
684 * next, check the message byte.
685 */
686 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
687 return FAILED;
688
689 /*
690 * now, check the status byte to see if this indicates
691 * anything special.
692 */
693 switch (status_byte(scmd->result)) {
694 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700695 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700696 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 case COMMAND_TERMINATED:
698 return SUCCESS;
699 case CHECK_CONDITION:
700 return scsi_check_sense(scmd);
701 case CONDITION_GOOD:
702 case INTERMEDIATE_GOOD:
703 case INTERMEDIATE_C_GOOD:
704 /*
705 * who knows? FIXME(eric)
706 */
707 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500708 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500709 if (scmd->cmnd[0] == TEST_UNIT_READY)
710 /* it is a success, we probed the device and
711 * found it */
712 return SUCCESS;
713 /* otherwise, we failed to send the command */
714 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700716 scsi_handle_queue_full(scmd->device);
717 /* fall through */
718 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200719 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 default:
721 return FAILED;
722 }
723 return FAILED;
724}
725
726/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * scsi_eh_done - Completion function for error handling.
728 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static void scsi_eh_done(struct scsi_cmnd *scmd)
731{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100732 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600733
Hannes Reinecke91921e02014-06-25 16:39:59 +0200734 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100735 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600736
737 eh_action = scmd->device->host->eh_action;
738 if (eh_action)
739 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/**
Brian King292148f2007-01-30 17:51:17 -0600743 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200744 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800745 */
Brian King292148f2007-01-30 17:51:17 -0600746static int scsi_try_host_reset(struct scsi_cmnd *scmd)
747{
748 unsigned long flags;
749 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100750 struct Scsi_Host *host = scmd->device->host;
751 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600752
Hannes Reinecke91921e02014-06-25 16:39:59 +0200753 SCSI_LOG_ERROR_RECOVERY(3,
754 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600755
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100756 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600757 return FAILED;
758
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100759 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600760
761 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100762 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600763 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100764 spin_lock_irqsave(host->host_lock, flags);
765 scsi_report_bus_reset(host, scmd_channel(scmd));
766 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600767 }
768
769 return rtn;
770}
771
772/**
773 * scsi_try_bus_reset - ask host to perform a bus reset
774 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800775 */
Brian King292148f2007-01-30 17:51:17 -0600776static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
777{
778 unsigned long flags;
779 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100780 struct Scsi_Host *host = scmd->device->host;
781 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600782
Hannes Reinecke91921e02014-06-25 16:39:59 +0200783 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
784 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600785
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100786 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600787 return FAILED;
788
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100789 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600790
791 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100792 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600793 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100794 spin_lock_irqsave(host->host_lock, flags);
795 scsi_report_bus_reset(host, scmd_channel(scmd));
796 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600797 }
798
799 return rtn;
800}
801
Mike Christie30bd7df2008-02-29 18:25:19 -0600802static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
803{
804 sdev->was_reset = 1;
805 sdev->expecting_cc_ua = 1;
806}
807
808/**
809 * scsi_try_target_reset - Ask host to perform a target reset
810 * @scmd: SCSI cmd used to send a target reset
811 *
812 * Notes:
813 * There is no timeout for this operation. if this operation is
814 * unreliable for a given host, then the host itself needs to put a
815 * timer on it, and set the host back to a consistent state prior to
816 * returning.
817 */
818static int scsi_try_target_reset(struct scsi_cmnd *scmd)
819{
820 unsigned long flags;
821 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100822 struct Scsi_Host *host = scmd->device->host;
823 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600824
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100825 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600826 return FAILED;
827
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100828 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600829 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100830 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600831 __starget_for_each_device(scsi_target(scmd->device), NULL,
832 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100833 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600834 }
835
836 return rtn;
837}
838
Brian King292148f2007-01-30 17:51:17 -0600839/**
840 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
841 * @scmd: SCSI cmd used to send BDR
842 *
843 * Notes:
844 * There is no timeout for this operation. if this operation is
845 * unreliable for a given host, then the host itself needs to put a
846 * timer on it, and set the host back to a consistent state prior to
847 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800848 */
Brian King292148f2007-01-30 17:51:17 -0600849static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
850{
851 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100852 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600853
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100854 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600855 return FAILED;
856
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100857 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600858 if (rtn == SUCCESS)
859 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600860 return rtn;
861}
862
Hannes Reinecke883a0302014-10-24 14:27:04 +0200863/**
864 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800865 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200866 * @scmd: SCSI cmd used to send a target reset
867 *
868 * Return value:
869 * SUCCESS, FAILED, or FAST_IO_FAIL
870 *
871 * Notes:
872 * SUCCESS does not necessarily indicate that the command
873 * has been aborted; it only indicates that the LLDDs
874 * has cleared all references to that command.
875 * LLDDs should return FAILED only if an abort was required
876 * but could not be executed. LLDDs should return FAST_IO_FAIL
877 * if the device is temporarily unavailable (eg due to a
878 * link down on FibreChannel)
879 */
880static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
881 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600882{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100883 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600884 return FAILED;
885
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100886 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600887}
888
Brian King292148f2007-01-30 17:51:17 -0600889static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
890{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100891 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600892 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600893 if (scsi_try_target_reset(scmd) != SUCCESS)
894 if (scsi_try_bus_reset(scmd) != SUCCESS)
895 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600896}
897
898/**
Santosh Y3b729f72012-04-08 18:47:09 +0530899 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100900 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200901 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200902 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300903 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200904 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100905 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200906 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200907 * as part of the error recovery process. If @sense_bytes is 0 the command
908 * sent must be one that does not transfer any data. If @sense_bytes != 0
909 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
910 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800911 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200912void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
913 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
f59114b2005-04-17 15:00:23 -0500915 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Christoph Hellwig631c2282006-07-08 20:42:15 +0200917 /*
918 * We need saved copies of a number of fields - this is because
919 * error handling may need to overwrite these with different values
920 * to run different commands, and once error handling is complete,
921 * we will need to restore these values prior to running the actual
922 * command.
923 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200924 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300925 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200926 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200927 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200928 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200929 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400930 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400931 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200932 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200933
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400934 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700935 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300936 scmd->cmnd = ses->eh_cmnd;
937 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200938 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200939 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700940 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200941
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200942 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200943 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
944 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200945 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200946 scmd->sdb.length);
947 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200948 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400949 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200950 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200951 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200952 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200953 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200954 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200955 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300956 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200957 memcpy(scmd->cmnd, cmnd, cmnd_size);
958 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
959 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200960 }
961
962 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200963
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200964 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500966 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Christoph Hellwig631c2282006-07-08 20:42:15 +0200968 /*
969 * Zero the sense buffer. The scsi spec mandates that any
970 * untransferred sense data should be interpreted as being zero.
971 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900972 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200973}
974EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200975
Boaz Harroshe1c23462007-10-08 16:36:45 +0200976/**
Santosh Y3b729f72012-04-08 18:47:09 +0530977 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200978 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200979 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +0200980 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200981 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800982 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200983void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
984{
985 /*
986 * Restore original data
987 */
988 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300989 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200990 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200991 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200992 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200993 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -0400994 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400995 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200996 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200997}
998EXPORT_SYMBOL(scsi_eh_restore_cmnd);
999
1000/**
Santosh Y3b729f72012-04-08 18:47:09 +05301001 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001002 * @scmd: SCSI command structure to hijack
1003 * @cmnd: CDB to send
1004 * @cmnd_size: size in bytes of @cmnd
1005 * @timeout: timeout for this request
1006 * @sense_bytes: size of sense data to copy or 0
1007 *
1008 * This function is used to send a scsi command down to a target device
1009 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1010 *
1011 * Return value:
1012 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001013 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001014static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1015 int cmnd_size, int timeout, unsigned sense_bytes)
1016{
1017 struct scsi_device *sdev = scmd->device;
1018 struct Scsi_Host *shost = sdev->host;
1019 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001020 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001021 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001022 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001023 int rtn;
1024
Hannes Reineckefc736482013-04-25 08:10:00 +02001025retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001026 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001027 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001030 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001031 rtn = shost->hostt->queuecommand(shost, scmd);
1032 if (rtn) {
1033 if (timeleft > stall_for) {
1034 scsi_eh_restore_cmnd(scmd, &ses);
1035 timeleft -= stall_for;
1036 msleep(jiffies_to_msecs(stall_for));
1037 goto retry;
1038 }
1039 /* signal not to enter either branch of the if () below */
1040 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001041 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001042 } else {
1043 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001044 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
f59114b2005-04-17 15:00:23 -05001047 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Hannes Reineckefc736482013-04-25 08:10:00 +02001049 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Hannes Reinecke91921e02014-06-25 16:39:59 +02001051 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001052 "%s timeleft: %ld\n",
1053 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001056 * If there is time left scsi_eh_done got called, and we will examine
1057 * the actual status codes to see whether the command actually did
1058 * complete normally, else if we have a zero return and no time left,
1059 * the command must still be pending, so abort it and return FAILED.
1060 * If we never actually managed to issue the command, because
1061 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1062 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001064 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001066 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1067 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 switch (rtn) {
1070 case SUCCESS:
1071 case NEEDS_RETRY:
1072 case FAILED:
1073 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001074 case ADD_TO_MLQUEUE:
1075 rtn = NEEDS_RETRY;
1076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 default:
1078 rtn = FAILED;
1079 break;
1080 }
Alan Stern511833a2014-11-21 10:44:49 -05001081 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001082 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001083 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Boaz Harroshe1c23462007-10-08 16:36:45 +02001086 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return rtn;
1089}
1090
1091/**
1092 * scsi_request_sense - Request sense data from a particular target.
1093 * @scmd: SCSI cmd for request sense.
1094 *
1095 * Notes:
1096 * Some hosts automatically obtain this information, others require
1097 * that we obtain it on our own. This function will *not* return until
1098 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100static int scsi_request_sense(struct scsi_cmnd *scmd)
1101{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001102 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
James Bottomley24510792013-11-11 13:44:53 +01001105static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1106{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001107 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001108 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1109 if (sdrv->eh_action)
1110 rtn = sdrv->eh_action(scmd, rtn);
1111 }
1112 return rtn;
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/**
1116 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1117 * @scmd: Original SCSI cmd that eh has finished.
1118 * @done_q: Queue for processed commands.
1119 *
1120 * Notes:
1121 * We don't want to use the normal command completion while we are are
1122 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001123 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 * keep a list of pending commands for final completion, and once we
1125 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001126 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001127void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 list_move_tail(&scmd->eh_entry, done_q);
1130}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001131EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133/**
1134 * scsi_eh_get_sense - Get device sense data.
1135 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001136 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 *
1138 * Description:
1139 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001140 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 *
1142 * Notes:
1143 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001144 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * it down before we request it.
1146 *
1147 * All drivers should request sense information internally these days,
1148 * so for now all I have to say is tough noogies if you end up in here.
1149 *
1150 * XXX: Long term this code should go away, but that needs an audit of
1151 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001152 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001153int scsi_eh_get_sense(struct list_head *work_q,
1154 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001156 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001157 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int rtn;
1159
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001160 /*
1161 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1162 * should not get sense.
1163 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001164 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001165 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 SCSI_SENSE_VALID(scmd))
1167 continue;
1168
Hannes Reineckeb4562022013-10-23 10:51:21 +02001169 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001170 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001171 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001172 scmd_printk(KERN_INFO, scmd,
1173 "%s: skip request sense, past eh deadline\n",
1174 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001175 break;
1176 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001177 if (status_byte(scmd->result) != CHECK_CONDITION)
1178 /*
1179 * don't request sense if there's no check condition
1180 * status because the error we're processing isn't one
1181 * that has a sense code (and some devices get
1182 * confused by sense requests out of the blue)
1183 */
1184 continue;
1185
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001186 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1187 "%s: requesting sense\n",
1188 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 rtn = scsi_request_sense(scmd);
1190 if (rtn != SUCCESS)
1191 continue;
1192
Hannes Reinecke91921e02014-06-25 16:39:59 +02001193 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001194 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001195 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 rtn = scsi_decide_disposition(scmd);
1198
1199 /*
1200 * if the result was normal, then just pass it along to the
1201 * upper level.
1202 */
1203 if (rtn == SUCCESS)
1204 /* we don't want this command reissued, just
1205 * finished with the sense data, so set
1206 * retries to the max allowed to ensure it
1207 * won't get reissued */
1208 scmd->retries = scmd->allowed;
1209 else if (rtn != NEEDS_RETRY)
1210 continue;
1211
1212 scsi_eh_finish_cmd(scmd, done_q);
1213 }
1214
1215 return list_empty(work_q);
1216}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001217EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001221 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
1223 * Return value:
1224 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static int scsi_eh_tur(struct scsi_cmnd *scmd)
1227{
1228 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1229 int retry_cnt = 1, rtn;
1230
1231retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001232 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1233 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Hannes Reinecke91921e02014-06-25 16:39:59 +02001235 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001236 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001237
1238 switch (rtn) {
1239 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (retry_cnt--)
1241 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001242 /*FALLTHRU*/
1243 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001244 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001245 default:
1246 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/**
David Jeffery3eef6252011-05-19 14:41:12 -04001251 * scsi_eh_test_devices - check if devices are responding from error recovery.
1252 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001253 * @work_q: queue for commands which still need more error recovery
1254 * @done_q: queue for commands which are finished
1255 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001256 *
1257 * Decription:
1258 * Tests if devices are in a working state. Commands to devices now in
1259 * a working state are sent to the done_q while commands to devices which
1260 * are still failing to respond are returned to the work_q for more
1261 * processing.
1262 **/
1263static int scsi_eh_test_devices(struct list_head *cmd_list,
1264 struct list_head *work_q,
1265 struct list_head *done_q, int try_stu)
1266{
1267 struct scsi_cmnd *scmd, *next;
1268 struct scsi_device *sdev;
1269 int finish_cmds;
1270
1271 while (!list_empty(cmd_list)) {
1272 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1273 sdev = scmd->device;
1274
Hannes Reineckeb4562022013-10-23 10:51:21 +02001275 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001276 if (scsi_host_eh_past_deadline(sdev->host)) {
1277 /* Push items back onto work_q */
1278 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001279 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001280 sdev_printk(KERN_INFO, sdev,
1281 "%s: skip test device, past eh deadline",
1282 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001283 break;
1284 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001285 }
1286
David Jeffery3eef6252011-05-19 14:41:12 -04001287 finish_cmds = !scsi_device_online(scmd->device) ||
1288 (try_stu && !scsi_eh_try_stu(scmd) &&
1289 !scsi_eh_tur(scmd)) ||
1290 !scsi_eh_tur(scmd);
1291
1292 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1293 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001294 if (finish_cmds &&
1295 (try_stu ||
1296 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001297 scsi_eh_finish_cmd(scmd, done_q);
1298 else
1299 list_move_tail(&scmd->eh_entry, work_q);
1300 }
1301 }
1302 return list_empty(work_q);
1303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001307 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 *
1309 * Return value:
1310 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1313{
1314 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Christoph Hellwig631c2282006-07-08 20:42:15 +02001316 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001317 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Brian Kinged773e62007-03-29 15:25:52 -05001319 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001320 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001321
Christoph Hellwig631c2282006-07-08 20:42:15 +02001322 if (rtn == SUCCESS)
1323 return 0;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 1;
1327}
1328
1329 /**
1330 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001331 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001332 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001333 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 *
1335 * Notes:
1336 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001337 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339static int scsi_eh_stu(struct Scsi_Host *shost,
1340 struct list_head *work_q,
1341 struct list_head *done_q)
1342{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001343 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct scsi_device *sdev;
1345
1346 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001347 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001348 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001349 sdev_printk(KERN_INFO, sdev,
1350 "%s: skip START_UNIT, past eh deadline\n",
1351 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001352 break;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 stu_scmd = NULL;
1355 list_for_each_entry(scmd, work_q, eh_entry)
1356 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1357 scsi_check_sense(scmd) == FAILED ) {
1358 stu_scmd = scmd;
1359 break;
1360 }
1361
1362 if (!stu_scmd)
1363 continue;
1364
Hannes Reinecke91921e02014-06-25 16:39:59 +02001365 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001366 sdev_printk(KERN_INFO, sdev,
1367 "%s: Sending START_UNIT\n",
1368 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (!scsi_eh_try_stu(stu_scmd)) {
1371 if (!scsi_device_online(sdev) ||
1372 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001373 list_for_each_entry_safe(scmd, next,
1374 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001375 if (scmd->device == sdev &&
1376 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 scsi_eh_finish_cmd(scmd, done_q);
1378 }
1379 }
1380 } else {
1381 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001382 sdev_printk(KERN_INFO, sdev,
1383 "%s: START_UNIT failed\n",
1384 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 }
1387
1388 return list_empty(work_q);
1389}
1390
1391
1392/**
1393 * scsi_eh_bus_device_reset - send bdr if needed
1394 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001395 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001396 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 *
1398 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001399 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * devices that are jammed or not - if we have multiple devices, it
1401 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001402 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1405 struct list_head *work_q,
1406 struct list_head *done_q)
1407{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001408 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct scsi_device *sdev;
1410 int rtn;
1411
1412 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001413 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001414 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001415 sdev_printk(KERN_INFO, sdev,
1416 "%s: skip BDR, past eh deadline\n",
1417 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001418 break;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 bdr_scmd = NULL;
1421 list_for_each_entry(scmd, work_q, eh_entry)
1422 if (scmd->device == sdev) {
1423 bdr_scmd = scmd;
1424 break;
1425 }
1426
1427 if (!bdr_scmd)
1428 continue;
1429
Hannes Reinecke91921e02014-06-25 16:39:59 +02001430 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001431 sdev_printk(KERN_INFO, sdev,
1432 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001434 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001436 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001438 list_for_each_entry_safe(scmd, next,
1439 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001440 if (scmd->device == sdev &&
1441 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 scsi_eh_finish_cmd(scmd,
1443 done_q);
1444 }
1445 }
1446 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001447 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001448 sdev_printk(KERN_INFO, sdev,
1449 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 }
1452
1453 return list_empty(work_q);
1454}
1455
1456/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001457 * scsi_eh_target_reset - send target reset if needed
1458 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001459 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001460 * @done_q: &list_head for processed commands.
1461 *
1462 * Notes:
1463 * Try a target reset.
1464 */
1465static int scsi_eh_target_reset(struct Scsi_Host *shost,
1466 struct list_head *work_q,
1467 struct list_head *done_q)
1468{
James Bottomley98db5192010-10-25 15:53:41 -05001469 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001470 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001471
James Bottomley98db5192010-10-25 15:53:41 -05001472 list_splice_init(work_q, &tmp_list);
1473
1474 while (!list_empty(&tmp_list)) {
1475 struct scsi_cmnd *next, *scmd;
1476 int rtn;
1477 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001478
Hannes Reineckeb4562022013-10-23 10:51:21 +02001479 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001480 /* push back on work queue for further processing */
1481 list_splice_init(&check_list, work_q);
1482 list_splice_init(&tmp_list, work_q);
1483 SCSI_LOG_ERROR_RECOVERY(3,
1484 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001485 "%s: Skip target reset, past eh deadline\n",
1486 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001487 return list_empty(work_q);
1488 }
James Bottomley98db5192010-10-25 15:53:41 -05001489
1490 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1491 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001492
Hannes Reinecke91921e02014-06-25 16:39:59 +02001493 SCSI_LOG_ERROR_RECOVERY(3,
1494 shost_printk(KERN_INFO, shost,
1495 "%s: Sending target reset to target %d\n",
1496 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001497 rtn = scsi_try_target_reset(scmd);
1498 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001499 SCSI_LOG_ERROR_RECOVERY(3,
1500 shost_printk(KERN_INFO, shost,
1501 "%s: Target reset failed"
1502 " target: %d\n",
1503 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001504 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1505 if (scmd_id(scmd) != id)
1506 continue;
1507
David Jeffery3eef6252011-05-19 14:41:12 -04001508 if (rtn == SUCCESS)
1509 list_move_tail(&scmd->eh_entry, &check_list);
1510 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001511 scsi_eh_finish_cmd(scmd, done_q);
1512 else
1513 /* push back on work queue for further processing */
1514 list_move(&scmd->eh_entry, work_q);
1515 }
1516 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001517
David Jeffery3eef6252011-05-19 14:41:12 -04001518 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001519}
1520
1521/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001522 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001523 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001524 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001525 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1528 struct list_head *work_q,
1529 struct list_head *done_q)
1530{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001531 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001532 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 unsigned int channel;
1534 int rtn;
1535
1536 /*
1537 * we really want to loop over the various channels, and do this on
1538 * a channel by channel basis. we should also check to see if any
1539 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001540 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 */
1542
1543 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001544 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001545 list_splice_init(&check_list, work_q);
1546 SCSI_LOG_ERROR_RECOVERY(3,
1547 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001548 "%s: skip BRST, past eh deadline\n",
1549 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001550 return list_empty(work_q);
1551 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 chan_scmd = NULL;
1554 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001555 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 chan_scmd = scmd;
1557 break;
1558 /*
1559 * FIXME add back in some support for
1560 * soft_reset devices.
1561 */
1562 }
1563 }
1564
1565 if (!chan_scmd)
1566 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001567 SCSI_LOG_ERROR_RECOVERY(3,
1568 shost_printk(KERN_INFO, shost,
1569 "%s: Sending BRST chan: %d\n",
1570 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001572 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001573 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001574 if (channel == scmd_channel(scmd)) {
1575 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 scsi_eh_finish_cmd(scmd,
1577 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001578 else
1579 list_move_tail(&scmd->eh_entry,
1580 &check_list);
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001584 SCSI_LOG_ERROR_RECOVERY(3,
1585 shost_printk(KERN_INFO, shost,
1586 "%s: BRST failed chan: %d\n",
1587 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 }
David Jeffery3eef6252011-05-19 14:41:12 -04001590 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001594 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001595 * @shost: host to be reset.
1596 * @work_q: &list_head for pending commands.
1597 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001598 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001599static int scsi_eh_host_reset(struct Scsi_Host *shost,
1600 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 struct list_head *done_q)
1602{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001603 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001604 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (!list_empty(work_q)) {
1608 scmd = list_entry(work_q->next,
1609 struct scsi_cmnd, eh_entry);
1610
Hannes Reinecke91921e02014-06-25 16:39:59 +02001611 SCSI_LOG_ERROR_RECOVERY(3,
1612 shost_printk(KERN_INFO, shost,
1613 "%s: Sending HRST\n",
1614 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001617 if (rtn == SUCCESS) {
1618 list_splice_init(work_q, &check_list);
1619 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001620 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 scsi_eh_finish_cmd(scmd, done_q);
1622 }
1623 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001624 SCSI_LOG_ERROR_RECOVERY(3,
1625 shost_printk(KERN_INFO, shost,
1626 "%s: HRST failed\n",
1627 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 }
David Jeffery3eef6252011-05-19 14:41:12 -04001630 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633/**
1634 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001635 * @work_q: &list_head for pending commands.
1636 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638static void scsi_eh_offline_sdevs(struct list_head *work_q,
1639 struct list_head *done_q)
1640{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001641 struct scsi_cmnd *scmd, *next;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001642 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001644 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001645 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1646 "not ready after error recovery\n");
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001647 sdev = scmd->device;
1648
1649 mutex_lock(&sdev->state_mutex);
1650 scsi_device_set_state(sdev, SDEV_OFFLINE);
1651 mutex_unlock(&sdev->state_mutex);
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 scsi_eh_finish_cmd(scmd, done_q);
1654 }
1655 return;
1656}
1657
1658/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001659 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001660 * @scmd: SCSI cmd to examine.
1661 */
1662int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1663{
1664 switch (host_byte(scmd->result)) {
1665 case DID_OK:
1666 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001667 case DID_TIME_OUT:
1668 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001669 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001670 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001671 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001672 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001673 case DID_ERROR:
1674 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1675 status_byte(scmd->result) == RESERVATION_CONFLICT)
1676 return 0;
1677 /* fall through */
1678 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001679 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001680 }
1681
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001682 if (status_byte(scmd->result) != CHECK_CONDITION)
1683 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001684
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001685check_type:
1686 /*
1687 * assume caller has checked sense and determined
1688 * the check condition was retryable.
1689 */
1690 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001691 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001692 return 1;
1693 else
1694 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001695}
1696
1697/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1699 * @scmd: SCSI cmd to examine.
1700 *
1701 * Notes:
1702 * This is *only* called when we are examining the status after sending
1703 * out the actual data command. any commands that are queued for error
1704 * recovery (e.g. test_unit_ready) do *not* come through here.
1705 *
1706 * When this routine returns failed, it means the error handler thread
1707 * is woken. In cases where the error code indicates an error that
1708 * doesn't require the error handler read (i.e. we don't need to
1709 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711int scsi_decide_disposition(struct scsi_cmnd *scmd)
1712{
1713 int rtn;
1714
1715 /*
1716 * if the device is offline, then we clearly just pass the result back
1717 * up to the top level.
1718 */
1719 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001720 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1721 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return SUCCESS;
1723 }
1724
1725 /*
1726 * first check the host byte, to see if there is anything in there
1727 * that would indicate what we need to do.
1728 */
1729 switch (host_byte(scmd->result)) {
1730 case DID_PASSTHROUGH:
1731 /*
1732 * no matter what, pass this through to the upper layer.
1733 * nuke this special code so that it looks like we are saying
1734 * did_ok.
1735 */
1736 scmd->result &= 0xff00ffff;
1737 return SUCCESS;
1738 case DID_OK:
1739 /*
1740 * looks good. drop through, and check the next byte.
1741 */
1742 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001743 case DID_ABORT:
1744 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001745 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001746 return SUCCESS;
1747 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001748 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 case DID_NO_CONNECT:
1750 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * note - this means that we just report the status back
1753 * to the top level driver, not that we actually think
1754 * that it indicates SUCCESS.
1755 */
1756 return SUCCESS;
Petros Koutoupisad950282017-10-30 16:38:10 -05001757 case DID_SOFT_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
1759 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001760 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * in order to avoid endless loops (db)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto maybe_retry;
1764 case DID_IMM_RETRY:
1765 return NEEDS_RETRY;
1766
bf341912005-04-12 17:49:09 -05001767 case DID_REQUEUE:
1768 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001769 case DID_TRANSPORT_DISRUPTED:
1770 /*
1771 * LLD/transport was disrupted during processing of the IO.
1772 * The transport class is now blocked/blocking,
1773 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001774 * based on its timers and recovery capablilities if
1775 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001776 */
Mike Christie939c22882008-11-04 19:47:19 -06001777 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001778 case DID_TRANSPORT_FAILFAST:
1779 /*
1780 * The transport decided to failfast the IO (most likely
1781 * the fast io fail tmo fired), so send IO directly upwards.
1782 */
1783 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 case DID_ERROR:
1785 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1786 status_byte(scmd->result) == RESERVATION_CONFLICT)
1787 /*
1788 * execute reservation conflict processing code
1789 * lower down
1790 */
1791 break;
1792 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 case DID_BUS_BUSY:
1794 case DID_PARITY:
1795 goto maybe_retry;
1796 case DID_TIME_OUT:
1797 /*
1798 * when we scan the bus, we get timeout messages for
1799 * these commands if there is no device available.
1800 * other hosts report did_no_connect for the same thing.
1801 */
1802 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1803 scmd->cmnd[0] == INQUIRY)) {
1804 return SUCCESS;
1805 } else {
1806 return FAILED;
1807 }
1808 case DID_RESET:
1809 return SUCCESS;
1810 default:
1811 return FAILED;
1812 }
1813
1814 /*
1815 * next, check the message byte.
1816 */
1817 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1818 return FAILED;
1819
1820 /*
1821 * check the status byte to see if this indicates anything special.
1822 */
1823 switch (status_byte(scmd->result)) {
1824 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001825 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * the case of trying to send too many commands to a
1828 * tagged queueing device.
1829 */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001830 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 case BUSY:
1832 /*
1833 * device can't talk to us at the moment. Should only
1834 * occur (SAM-3) when the task queue is empty, so will cause
1835 * the empty queue handling to trigger a stall in the
1836 * device.
1837 */
1838 return ADD_TO_MLQUEUE;
1839 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001840 if (scmd->cmnd[0] == REPORT_LUNS)
1841 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001842 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001843 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001846 case TASK_ABORTED:
1847 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 case CHECK_CONDITION:
1849 rtn = scsi_check_sense(scmd);
1850 if (rtn == NEEDS_RETRY)
1851 goto maybe_retry;
1852 /* if rtn == FAILED, we have no sense information;
1853 * returning FAILED will wake the error handler thread
1854 * to collect the sense and redo the decide
1855 * disposition */
1856 return rtn;
1857 case CONDITION_GOOD:
1858 case INTERMEDIATE_GOOD:
1859 case INTERMEDIATE_C_GOOD:
1860 case ACA_ACTIVE:
1861 /*
1862 * who knows? FIXME(eric)
1863 */
1864 return SUCCESS;
1865
1866 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001867 sdev_printk(KERN_INFO, scmd->device,
1868 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001869 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return SUCCESS; /* causes immediate i/o error */
1871 default:
1872 return FAILED;
1873 }
1874 return FAILED;
1875
1876 maybe_retry:
1877
1878 /* we requeue for retry because the error was retryable, and
1879 * the request was not marked fast fail. Note that above,
1880 * even if the request is marked fast fail, we still requeue
1881 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001882 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001883 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return NEEDS_RETRY;
1885 } else {
1886 /*
1887 * no more retries - report this one back to upper level.
1888 */
1889 return SUCCESS;
1890 }
1891}
1892
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001893static void eh_lock_door_done(struct request *req, blk_status_t status)
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001894{
1895 __blk_put_request(req->q, req);
1896}
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * scsi_eh_lock_door - Prevent medium removal for the specified device
1900 * @sdev: SCSI device to prevent medium removal
1901 *
1902 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001903 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 *
1905 * Notes:
1906 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1907 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909static void scsi_eh_lock_door(struct scsi_device *sdev)
1910{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001911 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001912 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
James Bottomley91bc31f2009-05-17 09:30:48 -05001914 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001915 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001916 * request becomes available
1917 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001918 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001919 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001920 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001921 rq = scsi_req(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001923 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1924 rq->cmd[1] = 0;
1925 rq->cmd[2] = 0;
1926 rq->cmd[3] = 0;
1927 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1928 rq->cmd[5] = 0;
1929 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001930
Christoph Hellwige8064022016-10-20 15:12:13 +02001931 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001932 req->timeout = 10 * HZ;
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001933 rq->retries = 5;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001934
1935 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/**
1939 * scsi_restart_operations - restart io operations to the specified host.
1940 * @shost: Host we are restarting.
1941 *
1942 * Notes:
1943 * When we entered the error handler, we blocked all further i/o to
1944 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001945 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static void scsi_restart_operations(struct Scsi_Host *shost)
1947{
1948 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001949 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /*
1952 * If the door was locked, we need to insert a door lock request
1953 * onto the head of the SCSI request queue for the device. There
1954 * is no point trying to lock the door of an off-line device.
1955 */
1956 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001957 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001959 sdev->was_reset = 0;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963 /*
1964 * next free up anything directly waiting upon the host. this
1965 * will be requests for character device operations, and also for
1966 * ioctls to queued block devices.
1967 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02001968 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02001969 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
James Bottomley939647e2005-09-18 15:05:20 -05001971 spin_lock_irqsave(shost->host_lock, flags);
1972 if (scsi_host_set_state(shost, SHOST_RUNNING))
1973 if (scsi_host_set_state(shost, SHOST_CANCEL))
1974 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1975 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 wake_up(&shost->host_wait);
1978
1979 /*
1980 * finally we need to re-initiate requests that may be pending. we will
1981 * have had everything blocked while error handling is taking place, and
1982 * now that error recovery is done, we will need to ensure that these
1983 * requests are started.
1984 */
1985 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07001986
1987 /*
1988 * if eh is active and host_eh_scheduled is pending we need to re-run
1989 * recovery. we do this check after scsi_run_host_queues() to allow
1990 * everything pent up since the last eh run a chance to make forward
1991 * progress before we sync again. Either we'll immediately re-run
1992 * recovery or scsi_device_unbusy() will wake us again when these
1993 * pending commands complete.
1994 */
1995 spin_lock_irqsave(shost->host_lock, flags);
1996 if (shost->host_eh_scheduled)
1997 if (scsi_host_set_state(shost, SHOST_RECOVERY))
1998 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
1999 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002/**
2003 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002004 * @shost: host to be recovered.
2005 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002006 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002007 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002008void scsi_eh_ready_devs(struct Scsi_Host *shost,
2009 struct list_head *work_q,
2010 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 if (!scsi_eh_stu(shost, work_q, done_q))
2013 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002014 if (!scsi_eh_target_reset(shost, work_q, done_q))
2015 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002016 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002017 scsi_eh_offline_sdevs(work_q,
2018 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002020EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022/**
2023 * scsi_eh_flush_done_q - finish processed commands or retry them.
2024 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002025 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002026void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002028 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002030 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2031 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002033 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002034 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002035 SCSI_LOG_ERROR_RECOVERY(3,
2036 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002037 "%s: flush retry cmd\n",
2038 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2040 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002041 /*
2042 * If just we got sense for the device (called
2043 * scsi_eh_get_sense), scmd->result is already
2044 * set, do not set DRIVER_TIMEOUT.
2045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (!scmd->result)
2047 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002048 SCSI_LOG_ERROR_RECOVERY(3,
2049 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002050 "%s: flush finish cmd\n",
2051 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 scsi_finish_command(scmd);
2053 }
2054 }
2055}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002056EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058/**
2059 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2060 * @shost: Host to unjam.
2061 *
2062 * Notes:
2063 * When we come in here, we *know* that all commands on the bus have
2064 * either completed, failed or timed out. we also know that no further
2065 * commands are being sent to the host, so things are relatively quiet
2066 * and we have freedom to fiddle with things as we wish.
2067 *
2068 * This is only the *default* implementation. it is possible for
2069 * individual drivers to supply their own version of this function, and
2070 * if the maintainer wishes to do this, it is strongly suggested that
2071 * this function be taken as a template and modified. this function
2072 * was designed to correctly handle problems for about 95% of the
2073 * different cases out there, and it should always provide at least a
2074 * reasonable amount of error recovery.
2075 *
2076 * Any command marked 'failed' or 'timeout' must eventually have
2077 * scsi_finish_cmd() called for it. we do all of the retry stuff
2078 * here, so when we restart the host after we return it should have an
2079 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081static void scsi_unjam_host(struct Scsi_Host *shost)
2082{
2083 unsigned long flags;
2084 LIST_HEAD(eh_work_q);
2085 LIST_HEAD(eh_done_q);
2086
2087 spin_lock_irqsave(shost->host_lock, flags);
2088 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2089 spin_unlock_irqrestore(shost->host_lock, flags);
2090
2091 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2092
2093 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002094 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Hannes Reineckeb4562022013-10-23 10:51:21 +02002096 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002097 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002098 shost->last_reset = 0;
2099 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 scsi_eh_flush_done_q(&eh_done_q);
2101}
2102
2103/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002104 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 * @data: Host for which we are running.
2106 *
2107 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002108 * This is the main error handling loop. This is run as a kernel thread
2109 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111int scsi_error_handler(void *data)
2112{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002113 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002116 * We use TASK_INTERRUPTIBLE so that the thread is not
2117 * counted against the load average as a running process.
2118 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002119 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
Michal Hocko537b6042015-08-27 20:16:37 +02002121 while (true) {
2122 /*
2123 * The sequence in kthread_stop() sets the stop flag first
2124 * then wakes the process. To avoid missed wakeups, the task
2125 * should always be in a non running state before the stop
2126 * flag is checked
2127 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002128 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002129 if (kthread_should_stop())
2130 break;
2131
Tejun Heoee7863b2006-05-15 20:57:20 +09002132 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002133 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002134 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002135 shost_printk(KERN_INFO, shost,
2136 "scsi_eh_%d: sleeping\n",
2137 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002138 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002139 continue;
2140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
James Bottomley3ed7a472005-09-19 09:50:04 -05002142 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002143 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002144 shost_printk(KERN_INFO, shost,
2145 "scsi_eh_%d: waking up %d/%d/%d\n",
2146 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002147 shost->host_failed,
2148 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /*
2151 * We have a host that is failing for some reason. Figure out
2152 * what we need to do to get it up and online again (if we can).
2153 * If we fail, we end up taking the thing offline.
2154 */
Lin Mingae0751f2011-12-05 09:20:24 +08002155 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002156 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002157 shost_printk(KERN_ERR, shost,
2158 "scsi_eh_%d: unable to autoresume\n",
2159 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002160 continue;
2161 }
2162
Christoph Hellwig9227c332006-04-01 19:21:04 +02002163 if (shost->transportt->eh_strategy_handler)
2164 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 else
2166 scsi_unjam_host(shost);
2167
Wei Fang72d8c362016-06-07 14:53:56 +08002168 /* All scmds have been handled */
2169 shost->host_failed = 0;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /*
2172 * Note - if the above fails completely, the action is to take
2173 * individual devices offline and flush the queue of any
2174 * outstanding requests that may have been pending. When we
2175 * restart, we restart any I/O to any other devices on the bus
2176 * which are still online.
2177 */
2178 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002179 if (!shost->eh_noresume)
2180 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002182 __set_current_state(TASK_RUNNING);
2183
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002184 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002185 shost_printk(KERN_INFO, shost,
2186 "Error handler scsi_eh_%d exiting\n",
2187 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002188 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 0;
2190}
2191
2192/*
2193 * Function: scsi_report_bus_reset()
2194 *
2195 * Purpose: Utility function used by low-level drivers to report that
2196 * they have observed a bus reset on the bus being handled.
2197 *
2198 * Arguments: shost - Host in question
2199 * channel - channel on which reset was observed.
2200 *
2201 * Returns: Nothing
2202 *
2203 * Lock status: Host lock must be held.
2204 *
2205 * Notes: This only needs to be called if the reset is one which
2206 * originates from an unknown location. Resets originated
2207 * by the mid-level itself don't need to call this, but there
2208 * should be no harm.
2209 *
2210 * The main purpose of this is to make sure that a CHECK_CONDITION
2211 * is properly treated.
2212 */
2213void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2214{
2215 struct scsi_device *sdev;
2216
2217 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002218 if (channel == sdev_channel(sdev))
2219 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221}
2222EXPORT_SYMBOL(scsi_report_bus_reset);
2223
2224/*
2225 * Function: scsi_report_device_reset()
2226 *
2227 * Purpose: Utility function used by low-level drivers to report that
2228 * they have observed a device reset on the device being handled.
2229 *
2230 * Arguments: shost - Host in question
2231 * channel - channel on which reset was observed
2232 * target - target on which reset was observed
2233 *
2234 * Returns: Nothing
2235 *
2236 * Lock status: Host lock must be held
2237 *
2238 * Notes: This only needs to be called if the reset is one which
2239 * originates from an unknown location. Resets originated
2240 * by the mid-level itself don't need to call this, but there
2241 * should be no harm.
2242 *
2243 * The main purpose of this is to make sure that a CHECK_CONDITION
2244 * is properly treated.
2245 */
2246void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2247{
2248 struct scsi_device *sdev;
2249
2250 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002251 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002252 target == sdev_id(sdev))
2253 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255}
2256EXPORT_SYMBOL(scsi_report_device_reset);
2257
2258static void
2259scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2260{
2261}
2262
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002263/**
2264 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2265 * @dev: scsi_device to operate on
2266 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 */
2268int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002269scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Alan Sternbc4f2402010-06-17 10:41:42 -04002271 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002272 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002273 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002274 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002275 int error = 0, rtn, val;
2276
2277 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2278 return -EACCES;
2279
2280 error = get_user(val, arg);
2281 if (error)
2282 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Alan Sternbc4f2402010-06-17 10:41:42 -04002284 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002285 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002286
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002287 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002288 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2289 shost->hostt->cmd_size, GFP_KERNEL);
2290 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002291 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002292 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002293
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002294 scmd = (struct scsi_cmnd *)(rq + 1);
2295 scsi_init_command(dev, scmd);
2296 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002297 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002300 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 scmd->cmd_len = 0;
2303
2304 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
James Smartd7a1bb02006-03-08 14:50:12 -05002306 spin_lock_irqsave(shost->host_lock, flags);
2307 shost->tmf_in_progress = 1;
2308 spin_unlock_irqrestore(shost->host_lock, flags);
2309
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002310 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2311 case SG_SCSI_RESET_NOTHING:
2312 rtn = SUCCESS;
2313 break;
2314 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002316 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 break;
2318 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002319 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002320 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002321 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002322 break;
2323 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002324 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002326 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 break;
2328 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002329 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002331 if (rtn == SUCCESS)
2332 break;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002333 /* FALLTHROUGH */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07002334 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002339 error = (rtn == SUCCESS) ? 0 : -EIO;
2340
James Smartd7a1bb02006-03-08 14:50:12 -05002341 spin_lock_irqsave(shost->host_lock, flags);
2342 shost->tmf_in_progress = 0;
2343 spin_unlock_irqrestore(shost->host_lock, flags);
2344
2345 /*
2346 * be sure to wake up anyone who was sleeping or had their queue
2347 * suspended while we performed the TMF.
2348 */
2349 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002350 shost_printk(KERN_INFO, shost,
2351 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002352
2353 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002354 scsi_run_host_queues(shost);
2355
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002356 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002357 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002358
Christoph Hellwig04796332014-02-20 14:20:55 -08002359out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002360 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002361 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002363EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002365bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2366 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
2368 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002369 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371EXPORT_SYMBOL(scsi_command_normalize_sense);
2372
2373/**
Rob Landleyeb448202007-11-03 13:30:39 -05002374 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 * @sense_buffer: byte array of sense data
2376 * @sb_len: number of valid bytes in sense_buffer
2377 * @info_out: pointer to 64 integer where 8 or 4 byte information
2378 * field will be placed if found.
2379 *
2380 * Return value:
Damien Le Moal29087692017-04-24 16:51:12 +09002381 * true if information field found, false if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002382 */
Damien Le Moal29087692017-04-24 16:51:12 +09002383bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2384 u64 *info_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 const u8 * ucp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (sb_len < 7)
Damien Le Moal29087692017-04-24 16:51:12 +09002389 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 switch (sense_buffer[0] & 0x7f) {
2391 case 0x70:
2392 case 0x71:
2393 if (sense_buffer[0] & 0x80) {
Damien Le Moal29087692017-04-24 16:51:12 +09002394 *info_out = get_unaligned_be32(&sense_buffer[3]);
2395 return true;
2396 }
2397 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 case 0x72:
2399 case 0x73:
2400 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2401 0 /* info desc */);
2402 if (ucp && (0xa == ucp[1])) {
Damien Le Moal29087692017-04-24 16:51:12 +09002403 *info_out = get_unaligned_be64(&ucp[4]);
2404 return true;
2405 }
2406 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 default:
Damien Le Moal29087692017-04-24 16:51:12 +09002408 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410}
2411EXPORT_SYMBOL(scsi_get_sense_info_fld);