blob: 946039117bf4f2c655db2bb1129ee75df1f1df21 [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 -070064void scsi_eh_wakeup(struct Scsi_Host *shost)
65{
Bart Van Asschef0317e82017-12-04 10:06:24 -080066 lockdep_assert_held(shost->host_lock);
67
Christoph Hellwig74665012014-01-22 15:29:29 +010068 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090069 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -050070 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020071 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
72 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74}
Tejun Heof8bbfc22006-05-19 21:07:05 +090075
76/**
77 * scsi_schedule_eh - schedule EH for SCSI host
78 * @shost: SCSI host to invoke error handling on.
79 *
80 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080081 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090082void scsi_schedule_eh(struct Scsi_Host *shost)
83{
84 unsigned long flags;
85
86 spin_lock_irqsave(shost->host_lock, flags);
87
88 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
89 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
90 shost->host_eh_scheduled++;
91 scsi_eh_wakeup(shost);
92 }
93
94 spin_unlock_irqrestore(shost->host_lock, flags);
95}
96EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Hannes Reineckeb4562022013-10-23 10:51:21 +020098static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
99{
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100100 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200101 return 0;
102
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100103 /*
104 * 32bit accesses are guaranteed to be atomic
105 * (on all supported architectures), so instead
106 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100107 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100108 * time_before call.
109 */
110 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100111 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200112 return 0;
113
114 return 1;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100118 * scmd_eh_abort_handler - Handle command aborts
119 * @work: command to be aborted.
Bart Van Assche923f46f2018-02-12 10:38:05 -0800120 *
121 * Note: this function must be called only for a command that has timed out.
122 * Because the block layer marks a request as complete before it calls
123 * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
124 * timed out do not have any effect. Hence it is safe to call
125 * scsi_finish_command() from this function.
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100126 */
127void
128scmd_eh_abort_handler(struct work_struct *work)
129{
130 struct scsi_cmnd *scmd =
131 container_of(work, struct scsi_cmnd, abort_work.work);
132 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100133 int rtn;
134
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100135 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100136 SCSI_LOG_ERROR_RECOVERY(3,
137 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100138 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100139 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100140 SCSI_LOG_ERROR_RECOVERY(3,
141 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100142 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100143 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
144 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200145 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100146 if (scsi_host_eh_past_deadline(sdev->host)) {
147 SCSI_LOG_ERROR_RECOVERY(3,
148 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100149 "eh timeout, not retrying "
150 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100151 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100152 (++scmd->retries <= scmd->allowed)) {
153 SCSI_LOG_ERROR_RECOVERY(3,
154 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100155 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100156 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100157 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100158 } else {
159 SCSI_LOG_ERROR_RECOVERY(3,
160 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100161 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100162 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100163 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100164 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100165 } else {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100168 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200169 (rtn == FAST_IO_FAIL) ?
170 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100171 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100172 }
173
Hannes Reineckea0658632017-04-06 15:36:35 +0200174 scsi_eh_scmd_add(scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100175}
176
177/**
178 * scsi_abort_command - schedule a command abort
179 * @scmd: scmd to abort.
180 *
181 * We only need to abort commands after a command timeout
182 */
183static int
184scsi_abort_command(struct scsi_cmnd *scmd)
185{
186 struct scsi_device *sdev = scmd->device;
187 struct Scsi_Host *shost = sdev->host;
188 unsigned long flags;
189
190 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
191 /*
192 * Retry after abort failed, escalate to next level.
193 */
194 SCSI_LOG_ERROR_RECOVERY(3,
195 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100196 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200197 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100198 return FAILED;
199 }
200
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100201 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100202 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100203 shost->last_reset = jiffies;
204 spin_unlock_irqrestore(shost->host_lock, flags);
205
206 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
207 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100208 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100209 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
210 return SUCCESS;
211}
212
213/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200214 * scsi_eh_reset - call into ->eh_action to reset internal counters
215 * @scmd: scmd to run eh on.
216 *
217 * The scsi driver might be carrying internal state about the
218 * devices, so we need to call into the driver to reset the
219 * internal state once the error handler is started.
220 */
221static void scsi_eh_reset(struct scsi_cmnd *scmd)
222{
223 if (!blk_rq_is_passthrough(scmd->request)) {
224 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
225 if (sdrv->eh_reset)
226 sdrv->eh_reset(scmd);
227 }
228}
229
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800230static void scsi_eh_inc_host_failed(struct rcu_head *head)
231{
Bart Van Assche3be88282018-02-22 11:30:20 -0800232 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
233 struct Scsi_Host *shost = scmd->device->host;
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800234 unsigned long flags;
235
236 spin_lock_irqsave(shost->host_lock, flags);
237 shost->host_failed++;
238 scsi_eh_wakeup(shost);
239 spin_unlock_irqrestore(shost->host_lock, flags);
240}
241
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200242/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * scsi_eh_scmd_add - add scsi cmd to error handling.
244 * @scmd: scmd to run eh on.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800245 */
Hannes Reineckea0658632017-04-06 15:36:35 +0200246void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct Scsi_Host *shost = scmd->device->host;
249 unsigned long flags;
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200250 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200252 WARN_ON_ONCE(!shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200255 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
256 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
257 WARN_ON_ONCE(ret);
258 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100259 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200260 shost->last_reset = jiffies;
261
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200262 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_unlock_irqrestore(shost->host_lock, flags);
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800265 /*
266 * Ensure that all tasks observe the host state change before the
267 * host_failed change.
268 */
Bart Van Assche3be88282018-02-22 11:30:20 -0800269 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700274 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
276 * Notes:
277 * We do not need to lock this. There is the potential for a race
278 * only in that the normal completion handling might run, but if the
279 * normal completion function determines that the timer has already
280 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800281 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700282enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Bart Van Asschebed22132017-08-25 13:46:32 -0700284 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700285 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100286 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500287
Kei Tokunagabf816232010-04-01 20:41:40 +0900288 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 scsi_log_completion(scmd, TIMEOUT_ERROR);
290
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100291 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200292 host->last_reset = jiffies;
293
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100294 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100295 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500296
Hannes Reineckea33c0702014-06-13 14:01:45 +0200297 if (rtn == BLK_EH_NOT_HANDLED) {
Hannes Reineckea0658632017-04-06 15:36:35 +0200298 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200299 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200300 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200301 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200302 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700303
Christoph Hellwigfa990782008-11-05 15:03:15 +0100304 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/**
308 * scsi_block_when_processing_errors - Prevent cmds from being queued.
309 * @sdev: Device on which we are performing recovery.
310 *
311 * Description:
312 * We block until the host is out of error recovery, and then check to
313 * see whether the host or the device is offline.
314 *
315 * Return value:
316 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800317 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318int scsi_block_when_processing_errors(struct scsi_device *sdev)
319{
320 int online;
321
James Bottomley939647e2005-09-18 15:05:20 -0500322 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 online = scsi_device_online(sdev);
325
Hannes Reinecke91921e02014-06-25 16:39:59 +0200326 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
327 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 return online;
330}
331EXPORT_SYMBOL(scsi_block_when_processing_errors);
332
333#ifdef CONFIG_SCSI_LOGGING
334/**
335 * scsi_eh_prt_fail_stats - Log info on failures.
336 * @shost: scsi host being recovered.
337 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
340 struct list_head *work_q)
341{
342 struct scsi_cmnd *scmd;
343 struct scsi_device *sdev;
344 int total_failures = 0;
345 int cmd_failed = 0;
346 int cmd_cancel = 0;
347 int devices_failed = 0;
348
349 shost_for_each_device(sdev, shost) {
350 list_for_each_entry(scmd, work_q, eh_entry) {
351 if (scmd->device == sdev) {
352 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200353 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100355 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ++cmd_failed;
357 }
358 }
359
360 if (cmd_cancel || cmd_failed) {
361 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200362 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500363 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700364 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500365 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 cmd_cancel = 0;
367 cmd_failed = 0;
368 ++devices_failed;
369 }
370 }
371
Hannes Reinecke91921e02014-06-25 16:39:59 +0200372 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
373 "Total of %d commands on %d"
374 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100375 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377#endif
378
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400379 /**
380 * scsi_report_lun_change - Set flag on all *other* devices on the same target
381 * to indicate that a UNIT ATTENTION is expected.
382 * @sdev: Device reporting the UNIT ATTENTION
383 */
384static void scsi_report_lun_change(struct scsi_device *sdev)
385{
386 sdev->sdev_target->expecting_lun_change = 1;
387}
388
389/**
390 * scsi_report_sense - Examine scsi sense information and log messages for
391 * certain conditions, also issue uevents for some of them.
392 * @sdev: Device reporting the sense code
393 * @sshdr: sshdr to be examined
394 */
395static void scsi_report_sense(struct scsi_device *sdev,
396 struct scsi_sense_hdr *sshdr)
397{
398 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
399
400 if (sshdr->sense_key == UNIT_ATTENTION) {
401 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
402 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
403 sdev_printk(KERN_WARNING, sdev,
404 "Inquiry data has changed");
405 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
406 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
407 scsi_report_lun_change(sdev);
408 sdev_printk(KERN_WARNING, sdev,
409 "Warning! Received an indication that the "
410 "LUN assignments on this target have "
411 "changed. The Linux SCSI layer does not "
412 "automatically remap LUN assignments.\n");
413 } else if (sshdr->asc == 0x3f)
414 sdev_printk(KERN_WARNING, sdev,
415 "Warning! Received an indication that the "
416 "operating parameters on this target have "
417 "changed. The Linux SCSI layer does not "
418 "automatically adjust these parameters.\n");
419
420 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
421 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
422 sdev_printk(KERN_WARNING, sdev,
423 "Warning! Received an indication that the "
424 "LUN reached a thin provisioning soft "
425 "threshold.\n");
426 }
427
Hannes Reineckecf3431b2017-10-17 09:11:24 +0200428 if (sshdr->asc == 0x29) {
429 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
430 sdev_printk(KERN_WARNING, sdev,
431 "Power-on or device reset occurred\n");
432 }
433
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400434 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
435 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
436 sdev_printk(KERN_WARNING, sdev,
437 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200438 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
439 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
440 sdev_printk(KERN_WARNING, sdev,
441 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400442 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
443 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
444 sdev_printk(KERN_WARNING, sdev,
445 "Capacity data has changed");
446 } else if (sshdr->asc == 0x2a)
447 sdev_printk(KERN_WARNING, sdev,
448 "Parameters changed");
449 }
450
451 if (evt_type != SDEV_EVT_MAXBITS) {
452 set_bit(evt_type, sdev->pending_events);
453 schedule_work(&sdev->event_work);
454 }
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/**
458 * scsi_check_sense - Examine scsi cmd sense
459 * @scmd: Cmd to have sense checked.
460 *
461 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200462 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
464 * Notes:
465 * When a deferred error is detected the current command has
466 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800467 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200468int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700470 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct scsi_sense_hdr sshdr;
472
473 if (! scsi_command_normalize_sense(scmd, &sshdr))
474 return FAILED; /* no valid sense data */
475
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400476 scsi_report_sense(sdev, &sshdr);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (scsi_sense_is_deferred(&sshdr))
479 return NEEDS_RETRY;
480
Christoph Hellwigee14c672015-08-27 14:16:59 +0200481 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700482 int rc;
483
Christoph Hellwigee14c672015-08-27 14:16:59 +0200484 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700485 if (rc != SCSI_RETURN_NOT_HANDLED)
486 return rc;
487 /* handler does not care. Drop down to default handling */
488 }
489
Christoph Hellwige925cc42014-11-06 15:11:22 -0600490 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
491 /*
492 * nasty: for mid-layer issued TURs, we need to return the
493 * actual sense data without any recovery attempt. For eh
494 * issued ones, we need to try to recover and interpret
495 */
496 return SUCCESS;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 /*
499 * Previous logic looked for FILEMARK, EOM or ILI which are
500 * mainly associated with tapes and returned SUCCESS.
501 */
502 if (sshdr.response_code == 0x70) {
503 /* fixed format */
504 if (scmd->sense_buffer[2] & 0xe0)
505 return SUCCESS;
506 } else {
507 /*
508 * descriptor format: look for "stream commands sense data
509 * descriptor" (see SSC-3). Assume single sense data
510 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
511 */
512 if ((sshdr.additional_length > 3) &&
513 (scmd->sense_buffer[8] == 0x4) &&
514 (scmd->sense_buffer[11] & 0xe0))
515 return SUCCESS;
516 }
517
518 switch (sshdr.sense_key) {
519 case NO_SENSE:
520 return SUCCESS;
521 case RECOVERED_ERROR:
522 return /* soft_error */ SUCCESS;
523
524 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400525 if (sshdr.asc == 0x10) /* DIF */
526 return SUCCESS;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return NEEDS_RETRY;
529 case NOT_READY:
530 case UNIT_ATTENTION:
531 /*
532 * if we are expecting a cc/ua because of a bus reset that we
533 * performed, treat this just as a retry. otherwise this is
534 * information that we should pass up to the upper-level driver
535 * so that we can deal with it there.
536 */
537 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900538 /*
539 * Because some device does not queue unit
540 * attentions correctly, we carefully check
541 * additional sense code and qualifier so as
542 * not to squash media change unit attention.
543 */
544 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
545 scmd->device->expecting_cc_ua = 0;
546 return NEEDS_RETRY;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400550 * we might also expect a cc/ua if another LUN on the target
551 * reported a UA with an ASC/ASCQ of 3F 0E -
552 * REPORTED LUNS DATA HAS CHANGED.
553 */
554 if (scmd->device->sdev_target->expecting_lun_change &&
555 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
556 return NEEDS_RETRY;
557 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100558 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * should retry.
560 */
561 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
562 return NEEDS_RETRY;
563 /*
564 * if the device is not started, we need to wake
565 * the error handler to start the motor
566 */
567 if (scmd->device->allow_restart &&
568 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
569 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100570 /*
571 * Pass the UA upwards for a determination in the completion
572 * functions.
573 */
574 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100576 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200577 case DATA_PROTECT:
578 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
579 /* Thin provisioning hard threshold reached */
580 set_host_byte(scmd, DID_ALLOC_FAILURE);
581 return SUCCESS;
582 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700583 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 case COPY_ABORTED:
585 case VOLUME_OVERFLOW:
586 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100587 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200588 set_host_byte(scmd, DID_TARGET_FAILURE);
589 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800592 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
593 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
594 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200595 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200596 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return NEEDS_RETRY;
599
600 case HARDWARE_ERROR:
601 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700602 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200604 set_host_byte(scmd, DID_TARGET_FAILURE);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700605 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500608 if (sshdr.asc == 0x20 || /* Invalid command operation code */
609 sshdr.asc == 0x21 || /* Logical block address out of range */
Hannes Reineckea8bbb2a2017-10-17 09:10:56 +0200610 sshdr.asc == 0x22 || /* Invalid function */
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500611 sshdr.asc == 0x24 || /* Invalid field in cdb */
Martin Wilckd0b7a902017-09-27 14:44:19 +0200612 sshdr.asc == 0x26 || /* Parameter value invalid */
613 sshdr.asc == 0x27) { /* Write protected */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200614 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500615 }
616 return SUCCESS;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 default:
619 return SUCCESS;
620 }
621}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200622EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Vasu Dev4a840672009-10-22 15:46:33 -0700624static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
625{
626 struct scsi_host_template *sht = sdev->host->hostt;
627 struct scsi_device *tmp_sdev;
628
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100629 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700630 sdev->queue_depth >= sdev->max_queue_depth)
631 return;
632
633 if (time_before(jiffies,
634 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
635 return;
636
637 if (time_before(jiffies,
638 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
639 return;
640
641 /*
642 * Walk all devices of a target and do
643 * ramp up on them.
644 */
645 shost_for_each_device(tmp_sdev, sdev->host) {
646 if (tmp_sdev->channel != sdev->channel ||
647 tmp_sdev->id != sdev->id ||
648 tmp_sdev->queue_depth == sdev->max_queue_depth)
649 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100650
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100651 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700652 sdev->last_queue_ramp_up = jiffies;
653 }
654}
655
Mike Christie42a6a912009-10-15 17:46:44 -0700656static void scsi_handle_queue_full(struct scsi_device *sdev)
657{
658 struct scsi_host_template *sht = sdev->host->hostt;
659 struct scsi_device *tmp_sdev;
660
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100661 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700662 return;
663
664 shost_for_each_device(tmp_sdev, sdev->host) {
665 if (tmp_sdev->channel != sdev->channel ||
666 tmp_sdev->id != sdev->id)
667 continue;
668 /*
669 * We do not know the number of commands that were at
670 * the device when we got the queue full so we start
671 * from the highest possible value and work our way down.
672 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100673 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700674 }
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/**
678 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
679 * @scmd: SCSI cmd to examine.
680 *
681 * Notes:
682 * This is *only* called when we are examining the status of commands
683 * queued during error recovery. the main difference here is that we
684 * don't allow for the possibility of retries here, and we are a lot
685 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800686 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
688{
689 /*
690 * first check the host byte, to see if there is anything in there
691 * that would indicate what we need to do.
692 */
693 if (host_byte(scmd->result) == DID_RESET) {
694 /*
695 * rats. we are already in the error handler, so we now
696 * get to try and figure out what to do next. if the sense
697 * is valid, we have a pretty good idea of what to do.
698 * if not, we mark it as FAILED.
699 */
700 return scsi_check_sense(scmd);
701 }
702 if (host_byte(scmd->result) != DID_OK)
703 return FAILED;
704
705 /*
706 * next, check the message byte.
707 */
708 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
709 return FAILED;
710
711 /*
712 * now, check the status byte to see if this indicates
713 * anything special.
714 */
715 switch (status_byte(scmd->result)) {
716 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700717 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700718 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case COMMAND_TERMINATED:
720 return SUCCESS;
721 case CHECK_CONDITION:
722 return scsi_check_sense(scmd);
723 case CONDITION_GOOD:
724 case INTERMEDIATE_GOOD:
725 case INTERMEDIATE_C_GOOD:
726 /*
727 * who knows? FIXME(eric)
728 */
729 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500730 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500731 if (scmd->cmnd[0] == TEST_UNIT_READY)
732 /* it is a success, we probed the device and
733 * found it */
734 return SUCCESS;
735 /* otherwise, we failed to send the command */
736 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700738 scsi_handle_queue_full(scmd->device);
739 /* fall through */
740 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200741 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 default:
743 return FAILED;
744 }
745 return FAILED;
746}
747
748/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * scsi_eh_done - Completion function for error handling.
750 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static void scsi_eh_done(struct scsi_cmnd *scmd)
753{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100754 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600755
Hannes Reinecke91921e02014-06-25 16:39:59 +0200756 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100757 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600758
759 eh_action = scmd->device->host->eh_action;
760 if (eh_action)
761 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/**
Brian King292148f2007-01-30 17:51:17 -0600765 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200766 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800767 */
Brian King292148f2007-01-30 17:51:17 -0600768static int scsi_try_host_reset(struct scsi_cmnd *scmd)
769{
770 unsigned long flags;
771 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100772 struct Scsi_Host *host = scmd->device->host;
773 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600774
Hannes Reinecke91921e02014-06-25 16:39:59 +0200775 SCSI_LOG_ERROR_RECOVERY(3,
776 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600777
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100778 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600779 return FAILED;
780
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100781 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600782
783 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100784 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600785 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100786 spin_lock_irqsave(host->host_lock, flags);
787 scsi_report_bus_reset(host, scmd_channel(scmd));
788 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600789 }
790
791 return rtn;
792}
793
794/**
795 * scsi_try_bus_reset - ask host to perform a bus reset
796 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800797 */
Brian King292148f2007-01-30 17:51:17 -0600798static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
799{
800 unsigned long flags;
801 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100802 struct Scsi_Host *host = scmd->device->host;
803 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600804
Hannes Reinecke91921e02014-06-25 16:39:59 +0200805 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
806 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600807
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100808 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600809 return FAILED;
810
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100811 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600812
813 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100814 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600815 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100816 spin_lock_irqsave(host->host_lock, flags);
817 scsi_report_bus_reset(host, scmd_channel(scmd));
818 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600819 }
820
821 return rtn;
822}
823
Mike Christie30bd7df2008-02-29 18:25:19 -0600824static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
825{
826 sdev->was_reset = 1;
827 sdev->expecting_cc_ua = 1;
828}
829
830/**
831 * scsi_try_target_reset - Ask host to perform a target reset
832 * @scmd: SCSI cmd used to send a target reset
833 *
834 * Notes:
835 * There is no timeout for this operation. if this operation is
836 * unreliable for a given host, then the host itself needs to put a
837 * timer on it, and set the host back to a consistent state prior to
838 * returning.
839 */
840static int scsi_try_target_reset(struct scsi_cmnd *scmd)
841{
842 unsigned long flags;
843 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100844 struct Scsi_Host *host = scmd->device->host;
845 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600846
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100847 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600848 return FAILED;
849
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100850 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600851 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100852 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600853 __starget_for_each_device(scsi_target(scmd->device), NULL,
854 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100855 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600856 }
857
858 return rtn;
859}
860
Brian King292148f2007-01-30 17:51:17 -0600861/**
862 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
863 * @scmd: SCSI cmd used to send BDR
864 *
865 * Notes:
866 * There is no timeout for this operation. if this operation is
867 * unreliable for a given host, then the host itself needs to put a
868 * timer on it, and set the host back to a consistent state prior to
869 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800870 */
Brian King292148f2007-01-30 17:51:17 -0600871static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
872{
873 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100874 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600875
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100876 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600877 return FAILED;
878
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100879 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600880 if (rtn == SUCCESS)
881 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600882 return rtn;
883}
884
Hannes Reinecke883a0302014-10-24 14:27:04 +0200885/**
886 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800887 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200888 * @scmd: SCSI cmd used to send a target reset
889 *
890 * Return value:
891 * SUCCESS, FAILED, or FAST_IO_FAIL
892 *
893 * Notes:
894 * SUCCESS does not necessarily indicate that the command
895 * has been aborted; it only indicates that the LLDDs
896 * has cleared all references to that command.
897 * LLDDs should return FAILED only if an abort was required
898 * but could not be executed. LLDDs should return FAST_IO_FAIL
899 * if the device is temporarily unavailable (eg due to a
900 * link down on FibreChannel)
901 */
902static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
903 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600904{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100905 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600906 return FAILED;
907
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100908 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600909}
910
Brian King292148f2007-01-30 17:51:17 -0600911static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
912{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100913 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600914 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600915 if (scsi_try_target_reset(scmd) != SUCCESS)
916 if (scsi_try_bus_reset(scmd) != SUCCESS)
917 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600918}
919
920/**
Santosh Y3b729f72012-04-08 18:47:09 +0530921 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100922 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200923 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200924 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300925 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200926 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100927 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200928 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200929 * as part of the error recovery process. If @sense_bytes is 0 the command
930 * sent must be one that does not transfer any data. If @sense_bytes != 0
931 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
932 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800933 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200934void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
935 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
f59114b2005-04-17 15:00:23 -0500937 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Christoph Hellwig631c2282006-07-08 20:42:15 +0200939 /*
940 * We need saved copies of a number of fields - this is because
941 * error handling may need to overwrite these with different values
942 * to run different commands, and once error handling is complete,
943 * we will need to restore these values prior to running the actual
944 * command.
945 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200946 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300947 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200948 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200949 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200950 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200951 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400952 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400953 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200954 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200955
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400956 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700957 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300958 scmd->cmnd = ses->eh_cmnd;
959 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200960 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200961 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700962 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200963
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200964 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200965 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
966 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200967 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200968 scmd->sdb.length);
969 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200970 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400971 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200972 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200973 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200974 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200975 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200976 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200977 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300978 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200979 memcpy(scmd->cmnd, cmnd, cmnd_size);
980 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
981 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200982 }
983
984 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200985
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200986 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500988 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Christoph Hellwig631c2282006-07-08 20:42:15 +0200990 /*
991 * Zero the sense buffer. The scsi spec mandates that any
992 * untransferred sense data should be interpreted as being zero.
993 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900994 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200995}
996EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200997
Boaz Harroshe1c23462007-10-08 16:36:45 +0200998/**
Santosh Y3b729f72012-04-08 18:47:09 +0530999 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001000 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001001 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +02001002 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001003 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001004 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001005void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1006{
1007 /*
1008 * Restore original data
1009 */
1010 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001011 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001012 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001013 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001014 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001015 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001016 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001017 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +02001018 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001019}
1020EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1021
1022/**
Santosh Y3b729f72012-04-08 18:47:09 +05301023 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001024 * @scmd: SCSI command structure to hijack
1025 * @cmnd: CDB to send
1026 * @cmnd_size: size in bytes of @cmnd
1027 * @timeout: timeout for this request
1028 * @sense_bytes: size of sense data to copy or 0
1029 *
1030 * This function is used to send a scsi command down to a target device
1031 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1032 *
1033 * Return value:
1034 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001035 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001036static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1037 int cmnd_size, int timeout, unsigned sense_bytes)
1038{
1039 struct scsi_device *sdev = scmd->device;
1040 struct Scsi_Host *shost = sdev->host;
1041 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001042 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001043 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001044 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001045 int rtn;
1046
Hannes Reineckefc736482013-04-25 08:10:00 +02001047retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001048 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001049 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001052 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001053 rtn = shost->hostt->queuecommand(shost, scmd);
1054 if (rtn) {
1055 if (timeleft > stall_for) {
1056 scsi_eh_restore_cmnd(scmd, &ses);
1057 timeleft -= stall_for;
1058 msleep(jiffies_to_msecs(stall_for));
1059 goto retry;
1060 }
1061 /* signal not to enter either branch of the if () below */
1062 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001063 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001064 } else {
1065 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001066 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
f59114b2005-04-17 15:00:23 -05001069 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Hannes Reineckefc736482013-04-25 08:10:00 +02001071 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Hannes Reinecke91921e02014-06-25 16:39:59 +02001073 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001074 "%s timeleft: %ld\n",
1075 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001078 * If there is time left scsi_eh_done got called, and we will examine
1079 * the actual status codes to see whether the command actually did
1080 * complete normally, else if we have a zero return and no time left,
1081 * the command must still be pending, so abort it and return FAILED.
1082 * If we never actually managed to issue the command, because
1083 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1084 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001086 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001088 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1089 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 switch (rtn) {
1092 case SUCCESS:
1093 case NEEDS_RETRY:
1094 case FAILED:
1095 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001096 case ADD_TO_MLQUEUE:
1097 rtn = NEEDS_RETRY;
1098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 default:
1100 rtn = FAILED;
1101 break;
1102 }
Alan Stern511833a2014-11-21 10:44:49 -05001103 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001104 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001105 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
Boaz Harroshe1c23462007-10-08 16:36:45 +02001108 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return rtn;
1111}
1112
1113/**
1114 * scsi_request_sense - Request sense data from a particular target.
1115 * @scmd: SCSI cmd for request sense.
1116 *
1117 * Notes:
1118 * Some hosts automatically obtain this information, others require
1119 * that we obtain it on our own. This function will *not* return until
1120 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static int scsi_request_sense(struct scsi_cmnd *scmd)
1123{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001124 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
James Bottomley24510792013-11-11 13:44:53 +01001127static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1128{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001129 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001130 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1131 if (sdrv->eh_action)
1132 rtn = sdrv->eh_action(scmd, rtn);
1133 }
1134 return rtn;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137/**
1138 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1139 * @scmd: Original SCSI cmd that eh has finished.
1140 * @done_q: Queue for processed commands.
1141 *
1142 * Notes:
1143 * We don't want to use the normal command completion while we are are
1144 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001145 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * keep a list of pending commands for final completion, and once we
1147 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001148 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001149void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 list_move_tail(&scmd->eh_entry, done_q);
1152}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001153EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155/**
1156 * scsi_eh_get_sense - Get device sense data.
1157 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001158 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *
1160 * Description:
1161 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001162 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 *
1164 * Notes:
1165 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001166 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * it down before we request it.
1168 *
1169 * All drivers should request sense information internally these days,
1170 * so for now all I have to say is tough noogies if you end up in here.
1171 *
1172 * XXX: Long term this code should go away, but that needs an audit of
1173 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001174 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001175int scsi_eh_get_sense(struct list_head *work_q,
1176 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001178 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001179 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int rtn;
1181
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001182 /*
1183 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1184 * should not get sense.
1185 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001186 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001187 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 SCSI_SENSE_VALID(scmd))
1189 continue;
1190
Hannes Reineckeb4562022013-10-23 10:51:21 +02001191 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001192 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001193 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001194 scmd_printk(KERN_INFO, scmd,
1195 "%s: skip request sense, past eh deadline\n",
1196 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001197 break;
1198 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001199 if (status_byte(scmd->result) != CHECK_CONDITION)
1200 /*
1201 * don't request sense if there's no check condition
1202 * status because the error we're processing isn't one
1203 * that has a sense code (and some devices get
1204 * confused by sense requests out of the blue)
1205 */
1206 continue;
1207
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001208 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1209 "%s: requesting sense\n",
1210 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 rtn = scsi_request_sense(scmd);
1212 if (rtn != SUCCESS)
1213 continue;
1214
Hannes Reinecke91921e02014-06-25 16:39:59 +02001215 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001216 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001217 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 rtn = scsi_decide_disposition(scmd);
1220
1221 /*
1222 * if the result was normal, then just pass it along to the
1223 * upper level.
1224 */
1225 if (rtn == SUCCESS)
1226 /* we don't want this command reissued, just
1227 * finished with the sense data, so set
1228 * retries to the max allowed to ensure it
1229 * won't get reissued */
1230 scmd->retries = scmd->allowed;
1231 else if (rtn != NEEDS_RETRY)
1232 continue;
1233
1234 scsi_eh_finish_cmd(scmd, done_q);
1235 }
1236
1237 return list_empty(work_q);
1238}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001239EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001243 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
1245 * Return value:
1246 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static int scsi_eh_tur(struct scsi_cmnd *scmd)
1249{
1250 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1251 int retry_cnt = 1, rtn;
1252
1253retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001254 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1255 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Hannes Reinecke91921e02014-06-25 16:39:59 +02001257 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001258 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001259
1260 switch (rtn) {
1261 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (retry_cnt--)
1263 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001264 /*FALLTHRU*/
1265 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001266 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001267 default:
1268 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272/**
David Jeffery3eef6252011-05-19 14:41:12 -04001273 * scsi_eh_test_devices - check if devices are responding from error recovery.
1274 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001275 * @work_q: queue for commands which still need more error recovery
1276 * @done_q: queue for commands which are finished
1277 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001278 *
1279 * Decription:
1280 * Tests if devices are in a working state. Commands to devices now in
1281 * a working state are sent to the done_q while commands to devices which
1282 * are still failing to respond are returned to the work_q for more
1283 * processing.
1284 **/
1285static int scsi_eh_test_devices(struct list_head *cmd_list,
1286 struct list_head *work_q,
1287 struct list_head *done_q, int try_stu)
1288{
1289 struct scsi_cmnd *scmd, *next;
1290 struct scsi_device *sdev;
1291 int finish_cmds;
1292
1293 while (!list_empty(cmd_list)) {
1294 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1295 sdev = scmd->device;
1296
Hannes Reineckeb4562022013-10-23 10:51:21 +02001297 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001298 if (scsi_host_eh_past_deadline(sdev->host)) {
1299 /* Push items back onto work_q */
1300 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001301 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001302 sdev_printk(KERN_INFO, sdev,
1303 "%s: skip test device, past eh deadline",
1304 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001305 break;
1306 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001307 }
1308
David Jeffery3eef6252011-05-19 14:41:12 -04001309 finish_cmds = !scsi_device_online(scmd->device) ||
1310 (try_stu && !scsi_eh_try_stu(scmd) &&
1311 !scsi_eh_tur(scmd)) ||
1312 !scsi_eh_tur(scmd);
1313
1314 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1315 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001316 if (finish_cmds &&
1317 (try_stu ||
1318 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001319 scsi_eh_finish_cmd(scmd, done_q);
1320 else
1321 list_move_tail(&scmd->eh_entry, work_q);
1322 }
1323 }
1324 return list_empty(work_q);
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001329 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
1331 * Return value:
1332 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1335{
1336 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Christoph Hellwig631c2282006-07-08 20:42:15 +02001338 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001339 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Brian Kinged773e62007-03-29 15:25:52 -05001341 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001342 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001343
Christoph Hellwig631c2282006-07-08 20:42:15 +02001344 if (rtn == SUCCESS)
1345 return 0;
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 1;
1349}
1350
1351 /**
1352 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001353 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001354 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001355 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
1357 * Notes:
1358 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001359 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361static int scsi_eh_stu(struct Scsi_Host *shost,
1362 struct list_head *work_q,
1363 struct list_head *done_q)
1364{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001365 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct scsi_device *sdev;
1367
1368 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001369 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001370 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001371 sdev_printk(KERN_INFO, sdev,
1372 "%s: skip START_UNIT, past eh deadline\n",
1373 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001374 break;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 stu_scmd = NULL;
1377 list_for_each_entry(scmd, work_q, eh_entry)
1378 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1379 scsi_check_sense(scmd) == FAILED ) {
1380 stu_scmd = scmd;
1381 break;
1382 }
1383
1384 if (!stu_scmd)
1385 continue;
1386
Hannes Reinecke91921e02014-06-25 16:39:59 +02001387 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001388 sdev_printk(KERN_INFO, sdev,
1389 "%s: Sending START_UNIT\n",
1390 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (!scsi_eh_try_stu(stu_scmd)) {
1393 if (!scsi_device_online(sdev) ||
1394 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001395 list_for_each_entry_safe(scmd, next,
1396 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001397 if (scmd->device == sdev &&
1398 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 scsi_eh_finish_cmd(scmd, done_q);
1400 }
1401 }
1402 } else {
1403 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001404 sdev_printk(KERN_INFO, sdev,
1405 "%s: START_UNIT failed\n",
1406 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 }
1409
1410 return list_empty(work_q);
1411}
1412
1413
1414/**
1415 * scsi_eh_bus_device_reset - send bdr if needed
1416 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001417 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001418 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 *
1420 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001421 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * devices that are jammed or not - if we have multiple devices, it
1423 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001424 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1427 struct list_head *work_q,
1428 struct list_head *done_q)
1429{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001430 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct scsi_device *sdev;
1432 int rtn;
1433
1434 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001435 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001436 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001437 sdev_printk(KERN_INFO, sdev,
1438 "%s: skip BDR, past eh deadline\n",
1439 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001440 break;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 bdr_scmd = NULL;
1443 list_for_each_entry(scmd, work_q, eh_entry)
1444 if (scmd->device == sdev) {
1445 bdr_scmd = scmd;
1446 break;
1447 }
1448
1449 if (!bdr_scmd)
1450 continue;
1451
Hannes Reinecke91921e02014-06-25 16:39:59 +02001452 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001453 sdev_printk(KERN_INFO, sdev,
1454 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001456 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001458 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001460 list_for_each_entry_safe(scmd, next,
1461 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001462 if (scmd->device == sdev &&
1463 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 scsi_eh_finish_cmd(scmd,
1465 done_q);
1466 }
1467 }
1468 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001469 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001470 sdev_printk(KERN_INFO, sdev,
1471 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473 }
1474
1475 return list_empty(work_q);
1476}
1477
1478/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001479 * scsi_eh_target_reset - send target reset if needed
1480 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001481 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001482 * @done_q: &list_head for processed commands.
1483 *
1484 * Notes:
1485 * Try a target reset.
1486 */
1487static int scsi_eh_target_reset(struct Scsi_Host *shost,
1488 struct list_head *work_q,
1489 struct list_head *done_q)
1490{
James Bottomley98db5192010-10-25 15:53:41 -05001491 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001492 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001493
James Bottomley98db5192010-10-25 15:53:41 -05001494 list_splice_init(work_q, &tmp_list);
1495
1496 while (!list_empty(&tmp_list)) {
1497 struct scsi_cmnd *next, *scmd;
1498 int rtn;
1499 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001500
Hannes Reineckeb4562022013-10-23 10:51:21 +02001501 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001502 /* push back on work queue for further processing */
1503 list_splice_init(&check_list, work_q);
1504 list_splice_init(&tmp_list, work_q);
1505 SCSI_LOG_ERROR_RECOVERY(3,
1506 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001507 "%s: Skip target reset, past eh deadline\n",
1508 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001509 return list_empty(work_q);
1510 }
James Bottomley98db5192010-10-25 15:53:41 -05001511
1512 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1513 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001514
Hannes Reinecke91921e02014-06-25 16:39:59 +02001515 SCSI_LOG_ERROR_RECOVERY(3,
1516 shost_printk(KERN_INFO, shost,
1517 "%s: Sending target reset to target %d\n",
1518 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001519 rtn = scsi_try_target_reset(scmd);
1520 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001521 SCSI_LOG_ERROR_RECOVERY(3,
1522 shost_printk(KERN_INFO, shost,
1523 "%s: Target reset failed"
1524 " target: %d\n",
1525 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001526 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1527 if (scmd_id(scmd) != id)
1528 continue;
1529
David Jeffery3eef6252011-05-19 14:41:12 -04001530 if (rtn == SUCCESS)
1531 list_move_tail(&scmd->eh_entry, &check_list);
1532 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001533 scsi_eh_finish_cmd(scmd, done_q);
1534 else
1535 /* push back on work queue for further processing */
1536 list_move(&scmd->eh_entry, work_q);
1537 }
1538 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001539
David Jeffery3eef6252011-05-19 14:41:12 -04001540 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001541}
1542
1543/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001544 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001545 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001546 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001547 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1550 struct list_head *work_q,
1551 struct list_head *done_q)
1552{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001553 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001554 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 unsigned int channel;
1556 int rtn;
1557
1558 /*
1559 * we really want to loop over the various channels, and do this on
1560 * a channel by channel basis. we should also check to see if any
1561 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001562 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 */
1564
1565 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001566 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001567 list_splice_init(&check_list, work_q);
1568 SCSI_LOG_ERROR_RECOVERY(3,
1569 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001570 "%s: skip BRST, past eh deadline\n",
1571 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001572 return list_empty(work_q);
1573 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 chan_scmd = NULL;
1576 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001577 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 chan_scmd = scmd;
1579 break;
1580 /*
1581 * FIXME add back in some support for
1582 * soft_reset devices.
1583 */
1584 }
1585 }
1586
1587 if (!chan_scmd)
1588 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001589 SCSI_LOG_ERROR_RECOVERY(3,
1590 shost_printk(KERN_INFO, shost,
1591 "%s: Sending BRST chan: %d\n",
1592 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001594 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001595 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001596 if (channel == scmd_channel(scmd)) {
1597 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 scsi_eh_finish_cmd(scmd,
1599 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001600 else
1601 list_move_tail(&scmd->eh_entry,
1602 &check_list);
1603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001606 SCSI_LOG_ERROR_RECOVERY(3,
1607 shost_printk(KERN_INFO, shost,
1608 "%s: BRST failed chan: %d\n",
1609 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611 }
David Jeffery3eef6252011-05-19 14:41:12 -04001612 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001616 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001617 * @shost: host to be reset.
1618 * @work_q: &list_head for pending commands.
1619 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001620 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001621static int scsi_eh_host_reset(struct Scsi_Host *shost,
1622 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct list_head *done_q)
1624{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001625 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001626 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 if (!list_empty(work_q)) {
1630 scmd = list_entry(work_q->next,
1631 struct scsi_cmnd, eh_entry);
1632
Hannes Reinecke91921e02014-06-25 16:39:59 +02001633 SCSI_LOG_ERROR_RECOVERY(3,
1634 shost_printk(KERN_INFO, shost,
1635 "%s: Sending HRST\n",
1636 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001639 if (rtn == SUCCESS) {
1640 list_splice_init(work_q, &check_list);
1641 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001642 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 scsi_eh_finish_cmd(scmd, done_q);
1644 }
1645 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001646 SCSI_LOG_ERROR_RECOVERY(3,
1647 shost_printk(KERN_INFO, shost,
1648 "%s: HRST failed\n",
1649 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 }
David Jeffery3eef6252011-05-19 14:41:12 -04001652 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655/**
1656 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001657 * @work_q: &list_head for pending commands.
1658 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static void scsi_eh_offline_sdevs(struct list_head *work_q,
1661 struct list_head *done_q)
1662{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001663 struct scsi_cmnd *scmd, *next;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001664 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001666 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001667 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1668 "not ready after error recovery\n");
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001669 sdev = scmd->device;
1670
1671 mutex_lock(&sdev->state_mutex);
1672 scsi_device_set_state(sdev, SDEV_OFFLINE);
1673 mutex_unlock(&sdev->state_mutex);
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 scsi_eh_finish_cmd(scmd, done_q);
1676 }
1677 return;
1678}
1679
1680/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001681 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001682 * @scmd: SCSI cmd to examine.
1683 */
1684int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1685{
1686 switch (host_byte(scmd->result)) {
1687 case DID_OK:
1688 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001689 case DID_TIME_OUT:
1690 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001691 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001692 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001693 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001694 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001695 case DID_ERROR:
1696 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1697 status_byte(scmd->result) == RESERVATION_CONFLICT)
1698 return 0;
1699 /* fall through */
1700 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001701 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001702 }
1703
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001704 if (status_byte(scmd->result) != CHECK_CONDITION)
1705 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001706
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001707check_type:
1708 /*
1709 * assume caller has checked sense and determined
1710 * the check condition was retryable.
1711 */
1712 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001713 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001714 return 1;
1715 else
1716 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001717}
1718
1719/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1721 * @scmd: SCSI cmd to examine.
1722 *
1723 * Notes:
1724 * This is *only* called when we are examining the status after sending
1725 * out the actual data command. any commands that are queued for error
1726 * recovery (e.g. test_unit_ready) do *not* come through here.
1727 *
1728 * When this routine returns failed, it means the error handler thread
1729 * is woken. In cases where the error code indicates an error that
1730 * doesn't require the error handler read (i.e. we don't need to
1731 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733int scsi_decide_disposition(struct scsi_cmnd *scmd)
1734{
1735 int rtn;
1736
1737 /*
1738 * if the device is offline, then we clearly just pass the result back
1739 * up to the top level.
1740 */
1741 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001742 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1743 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return SUCCESS;
1745 }
1746
1747 /*
1748 * first check the host byte, to see if there is anything in there
1749 * that would indicate what we need to do.
1750 */
1751 switch (host_byte(scmd->result)) {
1752 case DID_PASSTHROUGH:
1753 /*
1754 * no matter what, pass this through to the upper layer.
1755 * nuke this special code so that it looks like we are saying
1756 * did_ok.
1757 */
1758 scmd->result &= 0xff00ffff;
1759 return SUCCESS;
1760 case DID_OK:
1761 /*
1762 * looks good. drop through, and check the next byte.
1763 */
1764 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001765 case DID_ABORT:
1766 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001767 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001768 return SUCCESS;
1769 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001770 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 case DID_NO_CONNECT:
1772 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /*
1774 * note - this means that we just report the status back
1775 * to the top level driver, not that we actually think
1776 * that it indicates SUCCESS.
1777 */
1778 return SUCCESS;
Petros Koutoupisad950282017-10-30 16:38:10 -05001779 case DID_SOFT_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
1781 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001782 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * in order to avoid endless loops (db)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto maybe_retry;
1786 case DID_IMM_RETRY:
1787 return NEEDS_RETRY;
1788
bf341912005-04-12 17:49:09 -05001789 case DID_REQUEUE:
1790 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001791 case DID_TRANSPORT_DISRUPTED:
1792 /*
1793 * LLD/transport was disrupted during processing of the IO.
1794 * The transport class is now blocked/blocking,
1795 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001796 * based on its timers and recovery capablilities if
1797 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001798 */
Mike Christie939c22882008-11-04 19:47:19 -06001799 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001800 case DID_TRANSPORT_FAILFAST:
1801 /*
1802 * The transport decided to failfast the IO (most likely
1803 * the fast io fail tmo fired), so send IO directly upwards.
1804 */
1805 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 case DID_ERROR:
1807 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1808 status_byte(scmd->result) == RESERVATION_CONFLICT)
1809 /*
1810 * execute reservation conflict processing code
1811 * lower down
1812 */
1813 break;
1814 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 case DID_BUS_BUSY:
1816 case DID_PARITY:
1817 goto maybe_retry;
1818 case DID_TIME_OUT:
1819 /*
1820 * when we scan the bus, we get timeout messages for
1821 * these commands if there is no device available.
1822 * other hosts report did_no_connect for the same thing.
1823 */
1824 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1825 scmd->cmnd[0] == INQUIRY)) {
1826 return SUCCESS;
1827 } else {
1828 return FAILED;
1829 }
1830 case DID_RESET:
1831 return SUCCESS;
1832 default:
1833 return FAILED;
1834 }
1835
1836 /*
1837 * next, check the message byte.
1838 */
1839 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1840 return FAILED;
1841
1842 /*
1843 * check the status byte to see if this indicates anything special.
1844 */
1845 switch (status_byte(scmd->result)) {
1846 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001847 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 /*
1849 * the case of trying to send too many commands to a
1850 * tagged queueing device.
1851 */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001852 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 case BUSY:
1854 /*
1855 * device can't talk to us at the moment. Should only
1856 * occur (SAM-3) when the task queue is empty, so will cause
1857 * the empty queue handling to trigger a stall in the
1858 * device.
1859 */
1860 return ADD_TO_MLQUEUE;
1861 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001862 if (scmd->cmnd[0] == REPORT_LUNS)
1863 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001864 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001865 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001868 case TASK_ABORTED:
1869 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 case CHECK_CONDITION:
1871 rtn = scsi_check_sense(scmd);
1872 if (rtn == NEEDS_RETRY)
1873 goto maybe_retry;
1874 /* if rtn == FAILED, we have no sense information;
1875 * returning FAILED will wake the error handler thread
1876 * to collect the sense and redo the decide
1877 * disposition */
1878 return rtn;
1879 case CONDITION_GOOD:
1880 case INTERMEDIATE_GOOD:
1881 case INTERMEDIATE_C_GOOD:
1882 case ACA_ACTIVE:
1883 /*
1884 * who knows? FIXME(eric)
1885 */
1886 return SUCCESS;
1887
1888 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001889 sdev_printk(KERN_INFO, scmd->device,
1890 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001891 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return SUCCESS; /* causes immediate i/o error */
1893 default:
1894 return FAILED;
1895 }
1896 return FAILED;
1897
Jianchao Wang8ef7fe42018-02-26 15:59:16 +08001898maybe_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /* we requeue for retry because the error was retryable, and
1901 * the request was not marked fast fail. Note that above,
1902 * even if the request is marked fast fail, we still requeue
1903 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001904 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001905 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return NEEDS_RETRY;
1907 } else {
1908 /*
1909 * no more retries - report this one back to upper level.
1910 */
1911 return SUCCESS;
1912 }
1913}
1914
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001915static void eh_lock_door_done(struct request *req, blk_status_t status)
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001916{
1917 __blk_put_request(req->q, req);
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 * scsi_eh_lock_door - Prevent medium removal for the specified device
1922 * @sdev: SCSI device to prevent medium removal
1923 *
1924 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001925 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 *
1927 * Notes:
1928 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1929 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931static void scsi_eh_lock_door(struct scsi_device *sdev)
1932{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001933 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001934 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
James Bottomley91bc31f2009-05-17 09:30:48 -05001936 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001937 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001938 * request becomes available
1939 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001940 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001941 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001942 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001943 rq = scsi_req(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001945 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1946 rq->cmd[1] = 0;
1947 rq->cmd[2] = 0;
1948 rq->cmd[3] = 0;
1949 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1950 rq->cmd[5] = 0;
1951 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001952
Christoph Hellwige8064022016-10-20 15:12:13 +02001953 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001954 req->timeout = 10 * HZ;
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001955 rq->retries = 5;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001956
1957 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960/**
1961 * scsi_restart_operations - restart io operations to the specified host.
1962 * @shost: Host we are restarting.
1963 *
1964 * Notes:
1965 * When we entered the error handler, we blocked all further i/o to
1966 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968static void scsi_restart_operations(struct Scsi_Host *shost)
1969{
1970 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001971 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /*
1974 * If the door was locked, we need to insert a door lock request
1975 * onto the head of the SCSI request queue for the device. There
1976 * is no point trying to lock the door of an off-line device.
1977 */
1978 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001979 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001981 sdev->was_reset = 0;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
1985 /*
1986 * next free up anything directly waiting upon the host. this
1987 * will be requests for character device operations, and also for
1988 * ioctls to queued block devices.
1989 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02001990 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02001991 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
James Bottomley939647e2005-09-18 15:05:20 -05001993 spin_lock_irqsave(shost->host_lock, flags);
1994 if (scsi_host_set_state(shost, SHOST_RUNNING))
1995 if (scsi_host_set_state(shost, SHOST_CANCEL))
1996 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1997 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 wake_up(&shost->host_wait);
2000
2001 /*
2002 * finally we need to re-initiate requests that may be pending. we will
2003 * have had everything blocked while error handling is taking place, and
2004 * now that error recovery is done, we will need to ensure that these
2005 * requests are started.
2006 */
2007 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002008
2009 /*
2010 * if eh is active and host_eh_scheduled is pending we need to re-run
2011 * recovery. we do this check after scsi_run_host_queues() to allow
2012 * everything pent up since the last eh run a chance to make forward
2013 * progress before we sync again. Either we'll immediately re-run
2014 * recovery or scsi_device_unbusy() will wake us again when these
2015 * pending commands complete.
2016 */
2017 spin_lock_irqsave(shost->host_lock, flags);
2018 if (shost->host_eh_scheduled)
2019 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2020 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2021 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
2024/**
2025 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002026 * @shost: host to be recovered.
2027 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002028 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002029 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002030void scsi_eh_ready_devs(struct Scsi_Host *shost,
2031 struct list_head *work_q,
2032 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 if (!scsi_eh_stu(shost, work_q, done_q))
2035 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002036 if (!scsi_eh_target_reset(shost, work_q, done_q))
2037 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002038 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002039 scsi_eh_offline_sdevs(work_q,
2040 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002042EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044/**
2045 * scsi_eh_flush_done_q - finish processed commands or retry them.
2046 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002047 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002048void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002050 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002052 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2053 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002055 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002056 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002057 SCSI_LOG_ERROR_RECOVERY(3,
2058 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002059 "%s: flush retry cmd\n",
2060 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2062 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002063 /*
2064 * If just we got sense for the device (called
2065 * scsi_eh_get_sense), scmd->result is already
2066 * set, do not set DRIVER_TIMEOUT.
2067 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (!scmd->result)
2069 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002070 SCSI_LOG_ERROR_RECOVERY(3,
2071 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002072 "%s: flush finish cmd\n",
2073 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 scsi_finish_command(scmd);
2075 }
2076 }
2077}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002078EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080/**
2081 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2082 * @shost: Host to unjam.
2083 *
2084 * Notes:
2085 * When we come in here, we *know* that all commands on the bus have
2086 * either completed, failed or timed out. we also know that no further
2087 * commands are being sent to the host, so things are relatively quiet
2088 * and we have freedom to fiddle with things as we wish.
2089 *
2090 * This is only the *default* implementation. it is possible for
2091 * individual drivers to supply their own version of this function, and
2092 * if the maintainer wishes to do this, it is strongly suggested that
2093 * this function be taken as a template and modified. this function
2094 * was designed to correctly handle problems for about 95% of the
2095 * different cases out there, and it should always provide at least a
2096 * reasonable amount of error recovery.
2097 *
2098 * Any command marked 'failed' or 'timeout' must eventually have
2099 * scsi_finish_cmd() called for it. we do all of the retry stuff
2100 * here, so when we restart the host after we return it should have an
2101 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103static void scsi_unjam_host(struct Scsi_Host *shost)
2104{
2105 unsigned long flags;
2106 LIST_HEAD(eh_work_q);
2107 LIST_HEAD(eh_done_q);
2108
2109 spin_lock_irqsave(shost->host_lock, flags);
2110 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2111 spin_unlock_irqrestore(shost->host_lock, flags);
2112
2113 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2114
2115 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002116 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Hannes Reineckeb4562022013-10-23 10:51:21 +02002118 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002119 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002120 shost->last_reset = 0;
2121 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 scsi_eh_flush_done_q(&eh_done_q);
2123}
2124
2125/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002126 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 * @data: Host for which we are running.
2128 *
2129 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002130 * This is the main error handling loop. This is run as a kernel thread
2131 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133int scsi_error_handler(void *data)
2134{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002135 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002138 * We use TASK_INTERRUPTIBLE so that the thread is not
2139 * counted against the load average as a running process.
2140 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002141 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 */
Michal Hocko537b6042015-08-27 20:16:37 +02002143 while (true) {
2144 /*
2145 * The sequence in kthread_stop() sets the stop flag first
2146 * then wakes the process. To avoid missed wakeups, the task
2147 * should always be in a non running state before the stop
2148 * flag is checked
2149 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002150 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002151 if (kthread_should_stop())
2152 break;
2153
Tejun Heoee7863b2006-05-15 20:57:20 +09002154 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002155 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002156 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002157 shost_printk(KERN_INFO, shost,
2158 "scsi_eh_%d: sleeping\n",
2159 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002160 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002161 continue;
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
James Bottomley3ed7a472005-09-19 09:50:04 -05002164 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002165 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002166 shost_printk(KERN_INFO, shost,
2167 "scsi_eh_%d: waking up %d/%d/%d\n",
2168 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002169 shost->host_failed,
2170 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
2173 * We have a host that is failing for some reason. Figure out
2174 * what we need to do to get it up and online again (if we can).
2175 * If we fail, we end up taking the thing offline.
2176 */
Lin Mingae0751f2011-12-05 09:20:24 +08002177 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002178 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002179 shost_printk(KERN_ERR, shost,
2180 "scsi_eh_%d: unable to autoresume\n",
2181 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002182 continue;
2183 }
2184
Christoph Hellwig9227c332006-04-01 19:21:04 +02002185 if (shost->transportt->eh_strategy_handler)
2186 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 else
2188 scsi_unjam_host(shost);
2189
Wei Fang72d8c362016-06-07 14:53:56 +08002190 /* All scmds have been handled */
2191 shost->host_failed = 0;
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * Note - if the above fails completely, the action is to take
2195 * individual devices offline and flush the queue of any
2196 * outstanding requests that may have been pending. When we
2197 * restart, we restart any I/O to any other devices on the bus
2198 * which are still online.
2199 */
2200 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002201 if (!shost->eh_noresume)
2202 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002204 __set_current_state(TASK_RUNNING);
2205
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002206 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002207 shost_printk(KERN_INFO, shost,
2208 "Error handler scsi_eh_%d exiting\n",
2209 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002210 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return 0;
2212}
2213
2214/*
2215 * Function: scsi_report_bus_reset()
2216 *
2217 * Purpose: Utility function used by low-level drivers to report that
2218 * they have observed a bus reset on the bus being handled.
2219 *
2220 * Arguments: shost - Host in question
2221 * channel - channel on which reset was observed.
2222 *
2223 * Returns: Nothing
2224 *
2225 * Lock status: Host lock must be held.
2226 *
2227 * Notes: This only needs to be called if the reset is one which
2228 * originates from an unknown location. Resets originated
2229 * by the mid-level itself don't need to call this, but there
2230 * should be no harm.
2231 *
2232 * The main purpose of this is to make sure that a CHECK_CONDITION
2233 * is properly treated.
2234 */
2235void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2236{
2237 struct scsi_device *sdev;
2238
2239 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002240 if (channel == sdev_channel(sdev))
2241 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243}
2244EXPORT_SYMBOL(scsi_report_bus_reset);
2245
2246/*
2247 * Function: scsi_report_device_reset()
2248 *
2249 * Purpose: Utility function used by low-level drivers to report that
2250 * they have observed a device reset on the device being handled.
2251 *
2252 * Arguments: shost - Host in question
2253 * channel - channel on which reset was observed
2254 * target - target on which reset was observed
2255 *
2256 * Returns: Nothing
2257 *
2258 * Lock status: Host lock must be held
2259 *
2260 * Notes: This only needs to be called if the reset is one which
2261 * originates from an unknown location. Resets originated
2262 * by the mid-level itself don't need to call this, but there
2263 * should be no harm.
2264 *
2265 * The main purpose of this is to make sure that a CHECK_CONDITION
2266 * is properly treated.
2267 */
2268void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2269{
2270 struct scsi_device *sdev;
2271
2272 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002273 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002274 target == sdev_id(sdev))
2275 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277}
2278EXPORT_SYMBOL(scsi_report_device_reset);
2279
2280static void
2281scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2282{
2283}
2284
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002285/**
2286 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2287 * @dev: scsi_device to operate on
2288 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 */
2290int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002291scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Alan Sternbc4f2402010-06-17 10:41:42 -04002293 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002294 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002295 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002296 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002297 int error = 0, rtn, val;
2298
2299 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2300 return -EACCES;
2301
2302 error = get_user(val, arg);
2303 if (error)
2304 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Alan Sternbc4f2402010-06-17 10:41:42 -04002306 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002307 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002308
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002309 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002310 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2311 shost->hostt->cmd_size, GFP_KERNEL);
2312 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002313 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002314 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002315
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002316 scmd = (struct scsi_cmnd *)(rq + 1);
2317 scsi_init_command(dev, scmd);
2318 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002319 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002322 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 scmd->cmd_len = 0;
2325
2326 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
James Smartd7a1bb02006-03-08 14:50:12 -05002328 spin_lock_irqsave(shost->host_lock, flags);
2329 shost->tmf_in_progress = 1;
2330 spin_unlock_irqrestore(shost->host_lock, flags);
2331
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002332 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2333 case SG_SCSI_RESET_NOTHING:
2334 rtn = SUCCESS;
2335 break;
2336 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002338 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 break;
2340 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002341 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002342 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002343 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002344 break;
2345 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002346 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002348 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 break;
2350 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002351 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002353 if (rtn == SUCCESS)
2354 break;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002355 /* FALLTHROUGH */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07002356 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002361 error = (rtn == SUCCESS) ? 0 : -EIO;
2362
James Smartd7a1bb02006-03-08 14:50:12 -05002363 spin_lock_irqsave(shost->host_lock, flags);
2364 shost->tmf_in_progress = 0;
2365 spin_unlock_irqrestore(shost->host_lock, flags);
2366
2367 /*
2368 * be sure to wake up anyone who was sleeping or had their queue
2369 * suspended while we performed the TMF.
2370 */
2371 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002372 shost_printk(KERN_INFO, shost,
2373 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002374
2375 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002376 scsi_run_host_queues(shost);
2377
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002378 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002379 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002380
Christoph Hellwig04796332014-02-20 14:20:55 -08002381out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002382 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002383 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002385EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002387bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2388 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
2390 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002391 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393EXPORT_SYMBOL(scsi_command_normalize_sense);
2394
2395/**
Rob Landleyeb448202007-11-03 13:30:39 -05002396 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * @sense_buffer: byte array of sense data
2398 * @sb_len: number of valid bytes in sense_buffer
2399 * @info_out: pointer to 64 integer where 8 or 4 byte information
2400 * field will be placed if found.
2401 *
2402 * Return value:
Damien Le Moal29087692017-04-24 16:51:12 +09002403 * true if information field found, false if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002404 */
Damien Le Moal29087692017-04-24 16:51:12 +09002405bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2406 u64 *info_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 const u8 * ucp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 if (sb_len < 7)
Damien Le Moal29087692017-04-24 16:51:12 +09002411 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 switch (sense_buffer[0] & 0x7f) {
2413 case 0x70:
2414 case 0x71:
2415 if (sense_buffer[0] & 0x80) {
Damien Le Moal29087692017-04-24 16:51:12 +09002416 *info_out = get_unaligned_be32(&sense_buffer[3]);
2417 return true;
2418 }
2419 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 case 0x72:
2421 case 0x73:
2422 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2423 0 /* info desc */);
2424 if (ucp && (0xa == ucp[1])) {
Damien Le Moal29087692017-04-24 16:51:12 +09002425 *info_out = get_unaligned_be64(&ucp[4]);
2426 return true;
2427 }
2428 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 default:
Damien Le Moal29087692017-04-24 16:51:12 +09002430 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432}
2433EXPORT_SYMBOL(scsi_get_sense_info_fld);