blob: 370f6c045b60e32f6c20dae425dadd9b669789f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 *
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
Jesper Juhl0bf8c862011-03-21 20:47:31 +01006 * Leonard Zubkoff and David Miller at Linux Expo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * ideas originating from all over the place.
8 *
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
11 *
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
Jesper Juhl0bf8c862011-03-21 20:47:31 +010013 * minor cleanups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 */
16
17#include <linux/module.h>
18#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/timer.h>
21#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070023#include <linux/freezer.h>
Christoph Hellwigc5478de2005-09-06 14:04:26 +020024#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
26#include <linux/blkdev.h>
27#include <linux/delay.h>
Hannes Reineckefc736482013-04-25 08:10:00 +020028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020031#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -050034#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <scsi/scsi_eh.h>
Sagi Grimberg7708c162015-07-08 17:58:52 +030036#include <scsi/scsi_common.h>
James Smartc829c392006-03-13 08:28:57 -050037#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <scsi/scsi_host.h>
39#include <scsi/scsi_ioctl.h>
Christoph Hellwigee14c672015-08-27 14:16:59 +020040#include <scsi/scsi_dh.h>
Christoph Hellwig176aa9d2014-10-11 12:06:47 +020041#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "scsi_priv.h"
44#include "scsi_logging.h"
Adrian Bunk79ee8302007-08-10 14:50:42 -070045#include "scsi_transport_api.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Kei Tokunagabf816232010-04-01 20:41:40 +090047#include <trace/events/scsi.h>
48
James Bottomley14216562012-07-25 23:55:55 +040049static void scsi_eh_done(struct scsi_cmnd *scmd);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * These should *probably* be handled by the host itself.
53 * Since it is allowed to sleep, it probably should.
54 */
55#define BUS_RESET_SETTLE_TIME (10)
56#define HOST_RESET_SETTLE_TIME (10)
57
David Jeffery3eef6252011-05-19 14:41:12 -040058static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +010059static int scsi_try_to_abort_cmd(struct scsi_host_template *,
60 struct scsi_cmnd *);
David Jeffery3eef6252011-05-19 14:41:12 -040061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* called with shost->host_lock held */
63void scsi_eh_wakeup(struct Scsi_Host *shost)
64{
Christoph Hellwig74665012014-01-22 15:29:29 +010065 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090066 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -050067 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020068 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
69 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71}
Tejun Heof8bbfc22006-05-19 21:07:05 +090072
73/**
74 * scsi_schedule_eh - schedule EH for SCSI host
75 * @shost: SCSI host to invoke error handling on.
76 *
77 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080078 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090079void scsi_schedule_eh(struct Scsi_Host *shost)
80{
81 unsigned long flags;
82
83 spin_lock_irqsave(shost->host_lock, flags);
84
85 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
86 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
87 shost->host_eh_scheduled++;
88 scsi_eh_wakeup(shost);
89 }
90
91 spin_unlock_irqrestore(shost->host_lock, flags);
92}
93EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Hannes Reineckeb4562022013-10-23 10:51:21 +020095static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
96{
Ren Mingxinbb3b6212013-11-11 13:44:56 +010097 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +020098 return 0;
99
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100100 /*
101 * 32bit accesses are guaranteed to be atomic
102 * (on all supported architectures), so instead
103 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100104 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100105 * time_before call.
106 */
107 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100108 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200109 return 0;
110
111 return 1;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100115 * scmd_eh_abort_handler - Handle command aborts
116 * @work: command to be aborted.
117 */
118void
119scmd_eh_abort_handler(struct work_struct *work)
120{
121 struct scsi_cmnd *scmd =
122 container_of(work, struct scsi_cmnd, abort_work.work);
123 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100124 int rtn;
125
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100126 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100127 SCSI_LOG_ERROR_RECOVERY(3,
128 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100129 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100130 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100131 SCSI_LOG_ERROR_RECOVERY(3,
132 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100133 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100134 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
135 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200136 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100137 if (scsi_host_eh_past_deadline(sdev->host)) {
138 SCSI_LOG_ERROR_RECOVERY(3,
139 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100140 "eh timeout, not retrying "
141 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100142 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100143 (++scmd->retries <= scmd->allowed)) {
144 SCSI_LOG_ERROR_RECOVERY(3,
145 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100146 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100147 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100148 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100149 } else {
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100152 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100153 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100154 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100155 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100156 } else {
157 SCSI_LOG_ERROR_RECOVERY(3,
158 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100159 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200160 (rtn == FAST_IO_FAIL) ?
161 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100162 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100163 }
164
165 if (!scsi_eh_scmd_add(scmd, 0)) {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100168 "terminate aborted command\n"));
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200169 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100170 scsi_finish_command(scmd);
171 }
172}
173
174/**
175 * scsi_abort_command - schedule a command abort
176 * @scmd: scmd to abort.
177 *
178 * We only need to abort commands after a command timeout
179 */
180static int
181scsi_abort_command(struct scsi_cmnd *scmd)
182{
183 struct scsi_device *sdev = scmd->device;
184 struct Scsi_Host *shost = sdev->host;
185 unsigned long flags;
186
187 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
188 /*
189 * Retry after abort failed, escalate to next level.
190 */
Hannes Reinecke7daf4802014-03-31 16:37:34 +0200191 scmd->eh_eflags &= ~SCSI_EH_ABORT_SCHEDULED;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100192 SCSI_LOG_ERROR_RECOVERY(3,
193 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100194 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200195 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100196 return FAILED;
197 }
198
199 /*
200 * Do not try a command abort if
201 * SCSI EH has already started.
202 */
203 spin_lock_irqsave(shost->host_lock, flags);
204 if (scsi_host_in_recovery(shost)) {
205 spin_unlock_irqrestore(shost->host_lock, flags);
206 SCSI_LOG_ERROR_RECOVERY(3,
207 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100208 "not aborting, host in recovery\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100209 return FAILED;
210 }
211
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100212 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100213 shost->last_reset = jiffies;
214 spin_unlock_irqrestore(shost->host_lock, flags);
215
216 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
217 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100218 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100219 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
220 return SUCCESS;
221}
222
223/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200224 * scsi_eh_reset - call into ->eh_action to reset internal counters
225 * @scmd: scmd to run eh on.
226 *
227 * The scsi driver might be carrying internal state about the
228 * devices, so we need to call into the driver to reset the
229 * internal state once the error handler is started.
230 */
231static void scsi_eh_reset(struct scsi_cmnd *scmd)
232{
233 if (!blk_rq_is_passthrough(scmd->request)) {
234 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
235 if (sdrv->eh_reset)
236 sdrv->eh_reset(scmd);
237 }
238}
239
240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * scsi_eh_scmd_add - add scsi cmd to error handling.
242 * @scmd: scmd to run eh on.
243 * @eh_flag: optional SCSI_EH flag.
244 *
245 * Return value:
246 * 0 on failure.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
249{
250 struct Scsi_Host *shost = scmd->device->host;
251 unsigned long flags;
James Bottomley939647e2005-09-18 15:05:20 -0500252 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
James Bottomley3ed7a472005-09-19 09:50:04 -0500254 if (!shost->ehandler)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256
257 spin_lock_irqsave(shost->host_lock, flags);
James Bottomley939647e2005-09-18 15:05:20 -0500258 if (scsi_host_set_state(shost, SHOST_RECOVERY))
259 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
260 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100262 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200263 shost->last_reset = jiffies;
264
James Bottomley939647e2005-09-18 15:05:20 -0500265 ret = 1;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100266 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
267 eh_flag &= ~SCSI_EH_CANCEL_CMD;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200268 scmd->eh_eflags |= eh_flag;
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200269 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 shost->host_failed++;
272 scsi_eh_wakeup(shost);
James Bottomley939647e2005-09-18 15:05:20 -0500273 out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomley939647e2005-09-18 15:05:20 -0500275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700280 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *
282 * Notes:
283 * We do not need to lock this. There is the potential for a race
284 * only in that the normal completion handling might run, but if the
285 * normal completion function determines that the timer has already
286 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800287 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700288enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Jens Axboe242f9dc2008-09-14 05:55:09 -0700290 struct scsi_cmnd *scmd = req->special;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700291 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100292 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500293
Kei Tokunagabf816232010-04-01 20:41:40 +0900294 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 scsi_log_completion(scmd, TIMEOUT_ERROR);
296
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100297 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200298 host->last_reset = jiffies;
299
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100300 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100301 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500302
Hannes Reineckea33c0702014-06-13 14:01:45 +0200303 if (rtn == BLK_EH_NOT_HANDLED) {
304 if (!host->hostt->no_async_abort &&
305 scsi_abort_command(scmd) == SUCCESS)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100306 return BLK_EH_NOT_HANDLED;
307
Hannes Reineckea33c0702014-06-13 14:01:45 +0200308 set_host_byte(scmd, DID_TIME_OUT);
309 if (!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))
310 rtn = BLK_EH_HANDLED;
311 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700312
Christoph Hellwigfa990782008-11-05 15:03:15 +0100313 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/**
317 * scsi_block_when_processing_errors - Prevent cmds from being queued.
318 * @sdev: Device on which we are performing recovery.
319 *
320 * Description:
321 * We block until the host is out of error recovery, and then check to
322 * see whether the host or the device is offline.
323 *
324 * Return value:
325 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327int scsi_block_when_processing_errors(struct scsi_device *sdev)
328{
329 int online;
330
James Bottomley939647e2005-09-18 15:05:20 -0500331 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 online = scsi_device_online(sdev);
334
Hannes Reinecke91921e02014-06-25 16:39:59 +0200335 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
336 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return online;
339}
340EXPORT_SYMBOL(scsi_block_when_processing_errors);
341
342#ifdef CONFIG_SCSI_LOGGING
343/**
344 * scsi_eh_prt_fail_stats - Log info on failures.
345 * @shost: scsi host being recovered.
346 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
349 struct list_head *work_q)
350{
351 struct scsi_cmnd *scmd;
352 struct scsi_device *sdev;
353 int total_failures = 0;
354 int cmd_failed = 0;
355 int cmd_cancel = 0;
356 int devices_failed = 0;
357
358 shost_for_each_device(sdev, shost) {
359 list_for_each_entry(scmd, work_q, eh_entry) {
360 if (scmd->device == sdev) {
361 ++total_failures;
Christoph Hellwig3111b0d2005-06-19 13:43:26 +0200362 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100364 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ++cmd_failed;
366 }
367 }
368
369 if (cmd_cancel || cmd_failed) {
370 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200371 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500372 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700373 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500374 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 cmd_cancel = 0;
376 cmd_failed = 0;
377 ++devices_failed;
378 }
379 }
380
Hannes Reinecke91921e02014-06-25 16:39:59 +0200381 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
382 "Total of %d commands on %d"
383 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100384 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386#endif
387
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400388 /**
389 * scsi_report_lun_change - Set flag on all *other* devices on the same target
390 * to indicate that a UNIT ATTENTION is expected.
391 * @sdev: Device reporting the UNIT ATTENTION
392 */
393static void scsi_report_lun_change(struct scsi_device *sdev)
394{
395 sdev->sdev_target->expecting_lun_change = 1;
396}
397
398/**
399 * scsi_report_sense - Examine scsi sense information and log messages for
400 * certain conditions, also issue uevents for some of them.
401 * @sdev: Device reporting the sense code
402 * @sshdr: sshdr to be examined
403 */
404static void scsi_report_sense(struct scsi_device *sdev,
405 struct scsi_sense_hdr *sshdr)
406{
407 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
408
409 if (sshdr->sense_key == UNIT_ATTENTION) {
410 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
411 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
412 sdev_printk(KERN_WARNING, sdev,
413 "Inquiry data has changed");
414 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
415 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
416 scsi_report_lun_change(sdev);
417 sdev_printk(KERN_WARNING, sdev,
418 "Warning! Received an indication that the "
419 "LUN assignments on this target have "
420 "changed. The Linux SCSI layer does not "
421 "automatically remap LUN assignments.\n");
422 } else if (sshdr->asc == 0x3f)
423 sdev_printk(KERN_WARNING, sdev,
424 "Warning! Received an indication that the "
425 "operating parameters on this target have "
426 "changed. The Linux SCSI layer does not "
427 "automatically adjust these parameters.\n");
428
429 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
430 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
431 sdev_printk(KERN_WARNING, sdev,
432 "Warning! Received an indication that the "
433 "LUN reached a thin provisioning soft "
434 "threshold.\n");
435 }
436
437 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
438 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
439 sdev_printk(KERN_WARNING, sdev,
440 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200441 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
442 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
443 sdev_printk(KERN_WARNING, sdev,
444 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400445 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
446 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
447 sdev_printk(KERN_WARNING, sdev,
448 "Capacity data has changed");
449 } else if (sshdr->asc == 0x2a)
450 sdev_printk(KERN_WARNING, sdev,
451 "Parameters changed");
452 }
453
454 if (evt_type != SDEV_EVT_MAXBITS) {
455 set_bit(evt_type, sdev->pending_events);
456 schedule_work(&sdev->event_work);
457 }
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/**
461 * scsi_check_sense - Examine scsi cmd sense
462 * @scmd: Cmd to have sense checked.
463 *
464 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200465 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
467 * Notes:
468 * When a deferred error is detected the current command has
469 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800470 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200471int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700473 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct scsi_sense_hdr sshdr;
475
476 if (! scsi_command_normalize_sense(scmd, &sshdr))
477 return FAILED; /* no valid sense data */
478
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400479 scsi_report_sense(sdev, &sshdr);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (scsi_sense_is_deferred(&sshdr))
482 return NEEDS_RETRY;
483
Christoph Hellwigee14c672015-08-27 14:16:59 +0200484 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700485 int rc;
486
Christoph Hellwigee14c672015-08-27 14:16:59 +0200487 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700488 if (rc != SCSI_RETURN_NOT_HANDLED)
489 return rc;
490 /* handler does not care. Drop down to default handling */
491 }
492
Christoph Hellwige925cc42014-11-06 15:11:22 -0600493 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
494 /*
495 * nasty: for mid-layer issued TURs, we need to return the
496 * actual sense data without any recovery attempt. For eh
497 * issued ones, we need to try to recover and interpret
498 */
499 return SUCCESS;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * Previous logic looked for FILEMARK, EOM or ILI which are
503 * mainly associated with tapes and returned SUCCESS.
504 */
505 if (sshdr.response_code == 0x70) {
506 /* fixed format */
507 if (scmd->sense_buffer[2] & 0xe0)
508 return SUCCESS;
509 } else {
510 /*
511 * descriptor format: look for "stream commands sense data
512 * descriptor" (see SSC-3). Assume single sense data
513 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
514 */
515 if ((sshdr.additional_length > 3) &&
516 (scmd->sense_buffer[8] == 0x4) &&
517 (scmd->sense_buffer[11] & 0xe0))
518 return SUCCESS;
519 }
520
521 switch (sshdr.sense_key) {
522 case NO_SENSE:
523 return SUCCESS;
524 case RECOVERED_ERROR:
525 return /* soft_error */ SUCCESS;
526
527 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400528 if (sshdr.asc == 0x10) /* DIF */
529 return SUCCESS;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return NEEDS_RETRY;
532 case NOT_READY:
533 case UNIT_ATTENTION:
534 /*
535 * if we are expecting a cc/ua because of a bus reset that we
536 * performed, treat this just as a retry. otherwise this is
537 * information that we should pass up to the upper-level driver
538 * so that we can deal with it there.
539 */
540 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900541 /*
542 * Because some device does not queue unit
543 * attentions correctly, we carefully check
544 * additional sense code and qualifier so as
545 * not to squash media change unit attention.
546 */
547 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
548 scmd->device->expecting_cc_ua = 0;
549 return NEEDS_RETRY;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400553 * we might also expect a cc/ua if another LUN on the target
554 * reported a UA with an ASC/ASCQ of 3F 0E -
555 * REPORTED LUNS DATA HAS CHANGED.
556 */
557 if (scmd->device->sdev_target->expecting_lun_change &&
558 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
559 return NEEDS_RETRY;
560 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100561 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * should retry.
563 */
564 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
565 return NEEDS_RETRY;
566 /*
567 * if the device is not started, we need to wake
568 * the error handler to start the motor
569 */
570 if (scmd->device->allow_restart &&
571 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
572 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100573 /*
574 * Pass the UA upwards for a determination in the completion
575 * functions.
576 */
577 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100579 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200580 case DATA_PROTECT:
581 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
582 /* Thin provisioning hard threshold reached */
583 set_host_byte(scmd, DID_ALLOC_FAILURE);
584 return SUCCESS;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 case COPY_ABORTED:
587 case VOLUME_OVERFLOW:
588 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100589 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200590 set_host_byte(scmd, DID_TARGET_FAILURE);
591 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800594 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
595 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
596 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200597 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200598 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return NEEDS_RETRY;
601
602 case HARDWARE_ERROR:
603 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700604 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200606 set_host_byte(scmd, DID_TARGET_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500609 if (sshdr.asc == 0x20 || /* Invalid command operation code */
610 sshdr.asc == 0x21 || /* Logical block address out of range */
611 sshdr.asc == 0x24 || /* Invalid field in cdb */
612 sshdr.asc == 0x26) { /* Parameter value invalid */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200613 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500614 }
615 return SUCCESS;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 default:
618 return SUCCESS;
619 }
620}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200621EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Vasu Dev4a840672009-10-22 15:46:33 -0700623static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
624{
625 struct scsi_host_template *sht = sdev->host->hostt;
626 struct scsi_device *tmp_sdev;
627
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100628 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700629 sdev->queue_depth >= sdev->max_queue_depth)
630 return;
631
632 if (time_before(jiffies,
633 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
634 return;
635
636 if (time_before(jiffies,
637 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
638 return;
639
640 /*
641 * Walk all devices of a target and do
642 * ramp up on them.
643 */
644 shost_for_each_device(tmp_sdev, sdev->host) {
645 if (tmp_sdev->channel != sdev->channel ||
646 tmp_sdev->id != sdev->id ||
647 tmp_sdev->queue_depth == sdev->max_queue_depth)
648 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100649
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100650 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700651 sdev->last_queue_ramp_up = jiffies;
652 }
653}
654
Mike Christie42a6a912009-10-15 17:46:44 -0700655static void scsi_handle_queue_full(struct scsi_device *sdev)
656{
657 struct scsi_host_template *sht = sdev->host->hostt;
658 struct scsi_device *tmp_sdev;
659
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100660 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700661 return;
662
663 shost_for_each_device(tmp_sdev, sdev->host) {
664 if (tmp_sdev->channel != sdev->channel ||
665 tmp_sdev->id != sdev->id)
666 continue;
667 /*
668 * We do not know the number of commands that were at
669 * the device when we got the queue full so we start
670 * from the highest possible value and work our way down.
671 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100672 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700673 }
674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/**
677 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
678 * @scmd: SCSI cmd to examine.
679 *
680 * Notes:
681 * This is *only* called when we are examining the status of commands
682 * queued during error recovery. the main difference here is that we
683 * don't allow for the possibility of retries here, and we are a lot
684 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
687{
688 /*
689 * first check the host byte, to see if there is anything in there
690 * that would indicate what we need to do.
691 */
692 if (host_byte(scmd->result) == DID_RESET) {
693 /*
694 * rats. we are already in the error handler, so we now
695 * get to try and figure out what to do next. if the sense
696 * is valid, we have a pretty good idea of what to do.
697 * if not, we mark it as FAILED.
698 */
699 return scsi_check_sense(scmd);
700 }
701 if (host_byte(scmd->result) != DID_OK)
702 return FAILED;
703
704 /*
705 * next, check the message byte.
706 */
707 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
708 return FAILED;
709
710 /*
711 * now, check the status byte to see if this indicates
712 * anything special.
713 */
714 switch (status_byte(scmd->result)) {
715 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700716 scsi_handle_queue_ramp_up(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 case COMMAND_TERMINATED:
718 return SUCCESS;
719 case CHECK_CONDITION:
720 return scsi_check_sense(scmd);
721 case CONDITION_GOOD:
722 case INTERMEDIATE_GOOD:
723 case INTERMEDIATE_C_GOOD:
724 /*
725 * who knows? FIXME(eric)
726 */
727 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500728 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500729 if (scmd->cmnd[0] == TEST_UNIT_READY)
730 /* it is a success, we probed the device and
731 * found it */
732 return SUCCESS;
733 /* otherwise, we failed to send the command */
734 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700736 scsi_handle_queue_full(scmd->device);
737 /* fall through */
738 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200739 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 default:
741 return FAILED;
742 }
743 return FAILED;
744}
745
746/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * scsi_eh_done - Completion function for error handling.
748 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static void scsi_eh_done(struct scsi_cmnd *scmd)
751{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100752 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600753
Hannes Reinecke91921e02014-06-25 16:39:59 +0200754 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100755 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600756
757 eh_action = scmd->device->host->eh_action;
758 if (eh_action)
759 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/**
Brian King292148f2007-01-30 17:51:17 -0600763 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200764 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800765 */
Brian King292148f2007-01-30 17:51:17 -0600766static int scsi_try_host_reset(struct scsi_cmnd *scmd)
767{
768 unsigned long flags;
769 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100770 struct Scsi_Host *host = scmd->device->host;
771 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600772
Hannes Reinecke91921e02014-06-25 16:39:59 +0200773 SCSI_LOG_ERROR_RECOVERY(3,
774 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600775
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100776 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600777 return FAILED;
778
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100779 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600780
781 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100782 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600783 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100784 spin_lock_irqsave(host->host_lock, flags);
785 scsi_report_bus_reset(host, scmd_channel(scmd));
786 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600787 }
788
789 return rtn;
790}
791
792/**
793 * scsi_try_bus_reset - ask host to perform a bus reset
794 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800795 */
Brian King292148f2007-01-30 17:51:17 -0600796static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
797{
798 unsigned long flags;
799 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100800 struct Scsi_Host *host = scmd->device->host;
801 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600802
Hannes Reinecke91921e02014-06-25 16:39:59 +0200803 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
804 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600805
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100806 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600807 return FAILED;
808
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100809 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600810
811 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100812 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600813 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100814 spin_lock_irqsave(host->host_lock, flags);
815 scsi_report_bus_reset(host, scmd_channel(scmd));
816 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600817 }
818
819 return rtn;
820}
821
Mike Christie30bd7df2008-02-29 18:25:19 -0600822static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
823{
824 sdev->was_reset = 1;
825 sdev->expecting_cc_ua = 1;
826}
827
828/**
829 * scsi_try_target_reset - Ask host to perform a target reset
830 * @scmd: SCSI cmd used to send a target reset
831 *
832 * Notes:
833 * There is no timeout for this operation. if this operation is
834 * unreliable for a given host, then the host itself needs to put a
835 * timer on it, and set the host back to a consistent state prior to
836 * returning.
837 */
838static int scsi_try_target_reset(struct scsi_cmnd *scmd)
839{
840 unsigned long flags;
841 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100842 struct Scsi_Host *host = scmd->device->host;
843 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600844
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100845 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600846 return FAILED;
847
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100848 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600849 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100850 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600851 __starget_for_each_device(scsi_target(scmd->device), NULL,
852 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100853 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600854 }
855
856 return rtn;
857}
858
Brian King292148f2007-01-30 17:51:17 -0600859/**
860 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
861 * @scmd: SCSI cmd used to send BDR
862 *
863 * Notes:
864 * There is no timeout for this operation. if this operation is
865 * unreliable for a given host, then the host itself needs to put a
866 * timer on it, and set the host back to a consistent state prior to
867 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800868 */
Brian King292148f2007-01-30 17:51:17 -0600869static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
870{
871 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100872 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600873
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100874 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600875 return FAILED;
876
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100877 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600878 if (rtn == SUCCESS)
879 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600880 return rtn;
881}
882
Hannes Reinecke883a0302014-10-24 14:27:04 +0200883/**
884 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800885 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200886 * @scmd: SCSI cmd used to send a target reset
887 *
888 * Return value:
889 * SUCCESS, FAILED, or FAST_IO_FAIL
890 *
891 * Notes:
892 * SUCCESS does not necessarily indicate that the command
893 * has been aborted; it only indicates that the LLDDs
894 * has cleared all references to that command.
895 * LLDDs should return FAILED only if an abort was required
896 * but could not be executed. LLDDs should return FAST_IO_FAIL
897 * if the device is temporarily unavailable (eg due to a
898 * link down on FibreChannel)
899 */
900static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
901 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600902{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100903 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600904 return FAILED;
905
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100906 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600907}
908
Brian King292148f2007-01-30 17:51:17 -0600909static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
910{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100911 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600912 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600913 if (scsi_try_target_reset(scmd) != SUCCESS)
914 if (scsi_try_bus_reset(scmd) != SUCCESS)
915 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600916}
917
918/**
Santosh Y3b729f72012-04-08 18:47:09 +0530919 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100920 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200921 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200922 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300923 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200924 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100925 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200926 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200927 * as part of the error recovery process. If @sense_bytes is 0 the command
928 * sent must be one that does not transfer any data. If @sense_bytes != 0
929 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
930 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800931 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200932void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
933 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
f59114b2005-04-17 15:00:23 -0500935 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Christoph Hellwig631c2282006-07-08 20:42:15 +0200937 /*
938 * We need saved copies of a number of fields - this is because
939 * error handling may need to overwrite these with different values
940 * to run different commands, and once error handling is complete,
941 * we will need to restore these values prior to running the actual
942 * command.
943 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200944 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300945 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200946 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200947 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200948 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200949 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400950 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400951 ses->prot_op = scmd->prot_op;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200952
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400953 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700954 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300955 scmd->cmnd = ses->eh_cmnd;
956 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200957 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200958 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700959 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200960
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200961 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200962 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
963 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200964 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200965 scmd->sdb.length);
966 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200967 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400968 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200969 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200970 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200971 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200972 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200973 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200974 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300975 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200976 memcpy(scmd->cmnd, cmnd, cmnd_size);
977 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
978 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200979 }
980
981 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200982
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200983 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500985 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Christoph Hellwig631c2282006-07-08 20:42:15 +0200987 /*
988 * Zero the sense buffer. The scsi spec mandates that any
989 * untransferred sense data should be interpreted as being zero.
990 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900991 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200992}
993EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200994
Boaz Harroshe1c23462007-10-08 16:36:45 +0200995/**
Santosh Y3b729f72012-04-08 18:47:09 +0530996 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200997 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200998 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +0200999 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001000 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001001 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001002void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1003{
1004 /*
1005 * Restore original data
1006 */
1007 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001008 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001009 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001010 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001011 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001012 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001013 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001014 scmd->prot_op = ses->prot_op;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001015}
1016EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1017
1018/**
Santosh Y3b729f72012-04-08 18:47:09 +05301019 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001020 * @scmd: SCSI command structure to hijack
1021 * @cmnd: CDB to send
1022 * @cmnd_size: size in bytes of @cmnd
1023 * @timeout: timeout for this request
1024 * @sense_bytes: size of sense data to copy or 0
1025 *
1026 * This function is used to send a scsi command down to a target device
1027 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1028 *
1029 * Return value:
1030 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001031 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001032static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1033 int cmnd_size, int timeout, unsigned sense_bytes)
1034{
1035 struct scsi_device *sdev = scmd->device;
1036 struct Scsi_Host *shost = sdev->host;
1037 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001038 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001039 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001040 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001041 int rtn;
1042
Hannes Reineckefc736482013-04-25 08:10:00 +02001043retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001044 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001045 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001048 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001049 rtn = shost->hostt->queuecommand(shost, scmd);
1050 if (rtn) {
1051 if (timeleft > stall_for) {
1052 scsi_eh_restore_cmnd(scmd, &ses);
1053 timeleft -= stall_for;
1054 msleep(jiffies_to_msecs(stall_for));
1055 goto retry;
1056 }
1057 /* signal not to enter either branch of the if () below */
1058 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001059 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001060 } else {
1061 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001062 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
f59114b2005-04-17 15:00:23 -05001065 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Hannes Reineckefc736482013-04-25 08:10:00 +02001067 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Hannes Reinecke91921e02014-06-25 16:39:59 +02001069 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001070 "%s timeleft: %ld\n",
1071 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001074 * If there is time left scsi_eh_done got called, and we will examine
1075 * the actual status codes to see whether the command actually did
1076 * complete normally, else if we have a zero return and no time left,
1077 * the command must still be pending, so abort it and return FAILED.
1078 * If we never actually managed to issue the command, because
1079 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1080 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001082 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001084 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1085 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 switch (rtn) {
1088 case SUCCESS:
1089 case NEEDS_RETRY:
1090 case FAILED:
1091 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001092 case ADD_TO_MLQUEUE:
1093 rtn = NEEDS_RETRY;
1094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 default:
1096 rtn = FAILED;
1097 break;
1098 }
Alan Stern511833a2014-11-21 10:44:49 -05001099 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001100 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001101 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103
Boaz Harroshe1c23462007-10-08 16:36:45 +02001104 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return rtn;
1107}
1108
1109/**
1110 * scsi_request_sense - Request sense data from a particular target.
1111 * @scmd: SCSI cmd for request sense.
1112 *
1113 * Notes:
1114 * Some hosts automatically obtain this information, others require
1115 * that we obtain it on our own. This function will *not* return until
1116 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static int scsi_request_sense(struct scsi_cmnd *scmd)
1119{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001120 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
James Bottomley24510792013-11-11 13:44:53 +01001123static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1124{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001125 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001126 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1127 if (sdrv->eh_action)
1128 rtn = sdrv->eh_action(scmd, rtn);
1129 }
1130 return rtn;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/**
1134 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1135 * @scmd: Original SCSI cmd that eh has finished.
1136 * @done_q: Queue for processed commands.
1137 *
1138 * Notes:
1139 * We don't want to use the normal command completion while we are are
1140 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001141 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * keep a list of pending commands for final completion, and once we
1143 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001144 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001145void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001147 scmd->eh_eflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 list_move_tail(&scmd->eh_entry, done_q);
1149}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001150EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152/**
1153 * scsi_eh_get_sense - Get device sense data.
1154 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001155 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 *
1157 * Description:
1158 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001159 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 *
1161 * Notes:
1162 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001163 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * it down before we request it.
1165 *
1166 * All drivers should request sense information internally these days,
1167 * so for now all I have to say is tough noogies if you end up in here.
1168 *
1169 * XXX: Long term this code should go away, but that needs an audit of
1170 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001171 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001172int scsi_eh_get_sense(struct list_head *work_q,
1173 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001175 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001176 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int rtn;
1178
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001179 /*
1180 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1181 * should not get sense.
1182 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001183 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001184 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001185 (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 SCSI_SENSE_VALID(scmd))
1187 continue;
1188
Hannes Reineckeb4562022013-10-23 10:51:21 +02001189 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001190 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001191 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001192 scmd_printk(KERN_INFO, scmd,
1193 "%s: skip request sense, past eh deadline\n",
1194 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001195 break;
1196 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001197 if (status_byte(scmd->result) != CHECK_CONDITION)
1198 /*
1199 * don't request sense if there's no check condition
1200 * status because the error we're processing isn't one
1201 * that has a sense code (and some devices get
1202 * confused by sense requests out of the blue)
1203 */
1204 continue;
1205
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001206 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1207 "%s: requesting sense\n",
1208 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 rtn = scsi_request_sense(scmd);
1210 if (rtn != SUCCESS)
1211 continue;
1212
Hannes Reinecke91921e02014-06-25 16:39:59 +02001213 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001214 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001215 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 rtn = scsi_decide_disposition(scmd);
1218
1219 /*
1220 * if the result was normal, then just pass it along to the
1221 * upper level.
1222 */
1223 if (rtn == SUCCESS)
1224 /* we don't want this command reissued, just
1225 * finished with the sense data, so set
1226 * retries to the max allowed to ensure it
1227 * won't get reissued */
1228 scmd->retries = scmd->allowed;
1229 else if (rtn != NEEDS_RETRY)
1230 continue;
1231
1232 scsi_eh_finish_cmd(scmd, done_q);
1233 }
1234
1235 return list_empty(work_q);
1236}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001237EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001241 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *
1243 * Return value:
1244 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static int scsi_eh_tur(struct scsi_cmnd *scmd)
1247{
1248 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1249 int retry_cnt = 1, rtn;
1250
1251retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001252 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1253 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Hannes Reinecke91921e02014-06-25 16:39:59 +02001255 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001256 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001257
1258 switch (rtn) {
1259 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (retry_cnt--)
1261 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001262 /*FALLTHRU*/
1263 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001264 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001265 default:
1266 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270/**
David Jeffery3eef6252011-05-19 14:41:12 -04001271 * scsi_eh_test_devices - check if devices are responding from error recovery.
1272 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001273 * @work_q: queue for commands which still need more error recovery
1274 * @done_q: queue for commands which are finished
1275 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001276 *
1277 * Decription:
1278 * Tests if devices are in a working state. Commands to devices now in
1279 * a working state are sent to the done_q while commands to devices which
1280 * are still failing to respond are returned to the work_q for more
1281 * processing.
1282 **/
1283static int scsi_eh_test_devices(struct list_head *cmd_list,
1284 struct list_head *work_q,
1285 struct list_head *done_q, int try_stu)
1286{
1287 struct scsi_cmnd *scmd, *next;
1288 struct scsi_device *sdev;
1289 int finish_cmds;
1290
1291 while (!list_empty(cmd_list)) {
1292 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1293 sdev = scmd->device;
1294
Hannes Reineckeb4562022013-10-23 10:51:21 +02001295 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001296 if (scsi_host_eh_past_deadline(sdev->host)) {
1297 /* Push items back onto work_q */
1298 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001299 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001300 sdev_printk(KERN_INFO, sdev,
1301 "%s: skip test device, past eh deadline",
1302 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001303 break;
1304 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001305 }
1306
David Jeffery3eef6252011-05-19 14:41:12 -04001307 finish_cmds = !scsi_device_online(scmd->device) ||
1308 (try_stu && !scsi_eh_try_stu(scmd) &&
1309 !scsi_eh_tur(scmd)) ||
1310 !scsi_eh_tur(scmd);
1311
1312 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1313 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001314 if (finish_cmds &&
1315 (try_stu ||
1316 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001317 scsi_eh_finish_cmd(scmd, done_q);
1318 else
1319 list_move_tail(&scmd->eh_entry, work_q);
1320 }
1321 }
1322 return list_empty(work_q);
1323}
1324
1325
1326/**
Rob Landleyeb448202007-11-03 13:30:39 -05001327 * scsi_eh_abort_cmds - abort pending commands.
1328 * @work_q: &list_head for pending commands.
1329 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
1331 * Decription:
1332 * Try and see whether or not it makes sense to try and abort the
Rob Landleyeb448202007-11-03 13:30:39 -05001333 * running command. This only works out to be the case if we have one
1334 * command that has timed out. If the command simply failed, it makes
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * no sense to try and abort the command, since as far as the shost
1336 * adapter is concerned, it isn't running.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338static int scsi_eh_abort_cmds(struct list_head *work_q,
1339 struct list_head *done_q)
1340{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001341 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001342 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int rtn;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001344 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001346 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001347 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 continue;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001349 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001350 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001351 list_splice_init(&check_list, work_q);
1352 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001353 scmd_printk(KERN_INFO, scmd,
1354 "%s: skip aborting cmd, past eh deadline\n",
1355 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001356 return list_empty(work_q);
1357 }
Hannes Reinecke91921e02014-06-25 16:39:59 +02001358 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001359 scmd_printk(KERN_INFO, scmd,
1360 "%s: aborting cmd\n", current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001361 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
Hannes Reinecke6fd046f2013-10-23 10:51:22 +02001362 if (rtn == FAILED) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001363 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001364 scmd_printk(KERN_INFO, scmd,
1365 "%s: aborting cmd failed\n",
1366 current->comm));
Hannes Reinecke6fd046f2013-10-23 10:51:22 +02001367 list_splice_init(&check_list, work_q);
1368 return list_empty(work_q);
1369 }
1370 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
1371 if (rtn == FAST_IO_FAIL)
1372 scsi_eh_finish_cmd(scmd, done_q);
1373 else
1374 list_move_tail(&scmd->eh_entry, &check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
David Jeffery3eef6252011-05-19 14:41:12 -04001377 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001382 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
1384 * Return value:
1385 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1388{
1389 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Christoph Hellwig631c2282006-07-08 20:42:15 +02001391 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001392 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Brian Kinged773e62007-03-29 15:25:52 -05001394 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001395 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001396
Christoph Hellwig631c2282006-07-08 20:42:15 +02001397 if (rtn == SUCCESS)
1398 return 0;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return 1;
1402}
1403
1404 /**
1405 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001406 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001407 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001408 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 *
1410 * Notes:
1411 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001412 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414static int scsi_eh_stu(struct Scsi_Host *shost,
1415 struct list_head *work_q,
1416 struct list_head *done_q)
1417{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001418 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct scsi_device *sdev;
1420
1421 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001422 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001423 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001424 sdev_printk(KERN_INFO, sdev,
1425 "%s: skip START_UNIT, past eh deadline\n",
1426 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001427 break;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 stu_scmd = NULL;
1430 list_for_each_entry(scmd, work_q, eh_entry)
1431 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1432 scsi_check_sense(scmd) == FAILED ) {
1433 stu_scmd = scmd;
1434 break;
1435 }
1436
1437 if (!stu_scmd)
1438 continue;
1439
Hannes Reinecke91921e02014-06-25 16:39:59 +02001440 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001441 sdev_printk(KERN_INFO, sdev,
1442 "%s: Sending START_UNIT\n",
1443 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (!scsi_eh_try_stu(stu_scmd)) {
1446 if (!scsi_device_online(sdev) ||
1447 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001448 list_for_each_entry_safe(scmd, next,
1449 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001450 if (scmd->device == sdev &&
1451 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 scsi_eh_finish_cmd(scmd, done_q);
1453 }
1454 }
1455 } else {
1456 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001457 sdev_printk(KERN_INFO, sdev,
1458 "%s: START_UNIT failed\n",
1459 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 }
1462
1463 return list_empty(work_q);
1464}
1465
1466
1467/**
1468 * scsi_eh_bus_device_reset - send bdr if needed
1469 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001470 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001471 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 *
1473 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001474 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * devices that are jammed or not - if we have multiple devices, it
1476 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001477 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001478 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1480 struct list_head *work_q,
1481 struct list_head *done_q)
1482{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001483 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 struct scsi_device *sdev;
1485 int rtn;
1486
1487 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001488 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001489 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001490 sdev_printk(KERN_INFO, sdev,
1491 "%s: skip BDR, past eh deadline\n",
1492 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001493 break;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 bdr_scmd = NULL;
1496 list_for_each_entry(scmd, work_q, eh_entry)
1497 if (scmd->device == sdev) {
1498 bdr_scmd = scmd;
1499 break;
1500 }
1501
1502 if (!bdr_scmd)
1503 continue;
1504
Hannes Reinecke91921e02014-06-25 16:39:59 +02001505 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001506 sdev_printk(KERN_INFO, sdev,
1507 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001509 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001511 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001513 list_for_each_entry_safe(scmd, next,
1514 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001515 if (scmd->device == sdev &&
1516 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 scsi_eh_finish_cmd(scmd,
1518 done_q);
1519 }
1520 }
1521 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001522 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001523 sdev_printk(KERN_INFO, sdev,
1524 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 }
1527
1528 return list_empty(work_q);
1529}
1530
1531/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001532 * scsi_eh_target_reset - send target reset if needed
1533 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001534 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001535 * @done_q: &list_head for processed commands.
1536 *
1537 * Notes:
1538 * Try a target reset.
1539 */
1540static int scsi_eh_target_reset(struct Scsi_Host *shost,
1541 struct list_head *work_q,
1542 struct list_head *done_q)
1543{
James Bottomley98db5192010-10-25 15:53:41 -05001544 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001545 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001546
James Bottomley98db5192010-10-25 15:53:41 -05001547 list_splice_init(work_q, &tmp_list);
1548
1549 while (!list_empty(&tmp_list)) {
1550 struct scsi_cmnd *next, *scmd;
1551 int rtn;
1552 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001553
Hannes Reineckeb4562022013-10-23 10:51:21 +02001554 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001555 /* push back on work queue for further processing */
1556 list_splice_init(&check_list, work_q);
1557 list_splice_init(&tmp_list, work_q);
1558 SCSI_LOG_ERROR_RECOVERY(3,
1559 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001560 "%s: Skip target reset, past eh deadline\n",
1561 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001562 return list_empty(work_q);
1563 }
James Bottomley98db5192010-10-25 15:53:41 -05001564
1565 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1566 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001567
Hannes Reinecke91921e02014-06-25 16:39:59 +02001568 SCSI_LOG_ERROR_RECOVERY(3,
1569 shost_printk(KERN_INFO, shost,
1570 "%s: Sending target reset to target %d\n",
1571 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001572 rtn = scsi_try_target_reset(scmd);
1573 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001574 SCSI_LOG_ERROR_RECOVERY(3,
1575 shost_printk(KERN_INFO, shost,
1576 "%s: Target reset failed"
1577 " target: %d\n",
1578 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001579 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1580 if (scmd_id(scmd) != id)
1581 continue;
1582
David Jeffery3eef6252011-05-19 14:41:12 -04001583 if (rtn == SUCCESS)
1584 list_move_tail(&scmd->eh_entry, &check_list);
1585 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001586 scsi_eh_finish_cmd(scmd, done_q);
1587 else
1588 /* push back on work queue for further processing */
1589 list_move(&scmd->eh_entry, work_q);
1590 }
1591 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001592
David Jeffery3eef6252011-05-19 14:41:12 -04001593 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001594}
1595
1596/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001597 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001598 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001599 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001600 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1603 struct list_head *work_q,
1604 struct list_head *done_q)
1605{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001606 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001607 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 unsigned int channel;
1609 int rtn;
1610
1611 /*
1612 * we really want to loop over the various channels, and do this on
1613 * a channel by channel basis. we should also check to see if any
1614 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001615 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
1617
1618 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001619 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001620 list_splice_init(&check_list, work_q);
1621 SCSI_LOG_ERROR_RECOVERY(3,
1622 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001623 "%s: skip BRST, past eh deadline\n",
1624 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001625 return list_empty(work_q);
1626 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 chan_scmd = NULL;
1629 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001630 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 chan_scmd = scmd;
1632 break;
1633 /*
1634 * FIXME add back in some support for
1635 * soft_reset devices.
1636 */
1637 }
1638 }
1639
1640 if (!chan_scmd)
1641 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001642 SCSI_LOG_ERROR_RECOVERY(3,
1643 shost_printk(KERN_INFO, shost,
1644 "%s: Sending BRST chan: %d\n",
1645 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001647 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001648 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001649 if (channel == scmd_channel(scmd)) {
1650 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 scsi_eh_finish_cmd(scmd,
1652 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001653 else
1654 list_move_tail(&scmd->eh_entry,
1655 &check_list);
1656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001659 SCSI_LOG_ERROR_RECOVERY(3,
1660 shost_printk(KERN_INFO, shost,
1661 "%s: BRST failed chan: %d\n",
1662 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664 }
David Jeffery3eef6252011-05-19 14:41:12 -04001665 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
1668/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001669 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001670 * @shost: host to be reset.
1671 * @work_q: &list_head for pending commands.
1672 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001673 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001674static int scsi_eh_host_reset(struct Scsi_Host *shost,
1675 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct list_head *done_q)
1677{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001678 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001679 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (!list_empty(work_q)) {
1683 scmd = list_entry(work_q->next,
1684 struct scsi_cmnd, eh_entry);
1685
Hannes Reinecke91921e02014-06-25 16:39:59 +02001686 SCSI_LOG_ERROR_RECOVERY(3,
1687 shost_printk(KERN_INFO, shost,
1688 "%s: Sending HRST\n",
1689 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001692 if (rtn == SUCCESS) {
1693 list_splice_init(work_q, &check_list);
1694 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001695 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 scsi_eh_finish_cmd(scmd, done_q);
1697 }
1698 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001699 SCSI_LOG_ERROR_RECOVERY(3,
1700 shost_printk(KERN_INFO, shost,
1701 "%s: HRST failed\n",
1702 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 }
David Jeffery3eef6252011-05-19 14:41:12 -04001705 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708/**
1709 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001710 * @work_q: &list_head for pending commands.
1711 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713static void scsi_eh_offline_sdevs(struct list_head *work_q,
1714 struct list_head *done_q)
1715{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001716 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001718 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001719 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1720 "not ready after error recovery\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
Christoph Hellwig3111b0d2005-06-19 13:43:26 +02001722 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
1724 * FIXME: Handle lost cmds.
1725 */
1726 }
1727 scsi_eh_finish_cmd(scmd, done_q);
1728 }
1729 return;
1730}
1731
1732/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001733 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001734 * @scmd: SCSI cmd to examine.
1735 */
1736int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1737{
1738 switch (host_byte(scmd->result)) {
1739 case DID_OK:
1740 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001741 case DID_TIME_OUT:
1742 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001743 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001744 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001745 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001746 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001747 case DID_ERROR:
1748 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1749 status_byte(scmd->result) == RESERVATION_CONFLICT)
1750 return 0;
1751 /* fall through */
1752 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001753 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001754 }
1755
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001756 if (status_byte(scmd->result) != CHECK_CONDITION)
1757 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001758
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001759check_type:
1760 /*
1761 * assume caller has checked sense and determined
1762 * the check condition was retryable.
1763 */
1764 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001765 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001766 return 1;
1767 else
1768 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001769}
1770
1771/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1773 * @scmd: SCSI cmd to examine.
1774 *
1775 * Notes:
1776 * This is *only* called when we are examining the status after sending
1777 * out the actual data command. any commands that are queued for error
1778 * recovery (e.g. test_unit_ready) do *not* come through here.
1779 *
1780 * When this routine returns failed, it means the error handler thread
1781 * is woken. In cases where the error code indicates an error that
1782 * doesn't require the error handler read (i.e. we don't need to
1783 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785int scsi_decide_disposition(struct scsi_cmnd *scmd)
1786{
1787 int rtn;
1788
1789 /*
1790 * if the device is offline, then we clearly just pass the result back
1791 * up to the top level.
1792 */
1793 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001794 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1795 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return SUCCESS;
1797 }
1798
1799 /*
1800 * first check the host byte, to see if there is anything in there
1801 * that would indicate what we need to do.
1802 */
1803 switch (host_byte(scmd->result)) {
1804 case DID_PASSTHROUGH:
1805 /*
1806 * no matter what, pass this through to the upper layer.
1807 * nuke this special code so that it looks like we are saying
1808 * did_ok.
1809 */
1810 scmd->result &= 0xff00ffff;
1811 return SUCCESS;
1812 case DID_OK:
1813 /*
1814 * looks good. drop through, and check the next byte.
1815 */
1816 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001817 case DID_ABORT:
1818 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001819 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001820 return SUCCESS;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 case DID_NO_CONNECT:
1823 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * note - this means that we just report the status back
1826 * to the top level driver, not that we actually think
1827 * that it indicates SUCCESS.
1828 */
1829 return SUCCESS;
1830 /*
1831 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001832 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * in order to avoid endless loops (db)
1834 *
1835 * actually this is a bug in this function here. we should
1836 * be mindful of the maximum number of retries specified
1837 * and not get stuck in a loop.
1838 */
1839 case DID_SOFT_ERROR:
1840 goto maybe_retry;
1841 case DID_IMM_RETRY:
1842 return NEEDS_RETRY;
1843
bf341912005-04-12 17:49:09 -05001844 case DID_REQUEUE:
1845 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001846 case DID_TRANSPORT_DISRUPTED:
1847 /*
1848 * LLD/transport was disrupted during processing of the IO.
1849 * The transport class is now blocked/blocking,
1850 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001851 * based on its timers and recovery capablilities if
1852 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001853 */
Mike Christie939c22882008-11-04 19:47:19 -06001854 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001855 case DID_TRANSPORT_FAILFAST:
1856 /*
1857 * The transport decided to failfast the IO (most likely
1858 * the fast io fail tmo fired), so send IO directly upwards.
1859 */
1860 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 case DID_ERROR:
1862 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1863 status_byte(scmd->result) == RESERVATION_CONFLICT)
1864 /*
1865 * execute reservation conflict processing code
1866 * lower down
1867 */
1868 break;
1869 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 case DID_BUS_BUSY:
1871 case DID_PARITY:
1872 goto maybe_retry;
1873 case DID_TIME_OUT:
1874 /*
1875 * when we scan the bus, we get timeout messages for
1876 * these commands if there is no device available.
1877 * other hosts report did_no_connect for the same thing.
1878 */
1879 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1880 scmd->cmnd[0] == INQUIRY)) {
1881 return SUCCESS;
1882 } else {
1883 return FAILED;
1884 }
1885 case DID_RESET:
1886 return SUCCESS;
1887 default:
1888 return FAILED;
1889 }
1890
1891 /*
1892 * next, check the message byte.
1893 */
1894 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1895 return FAILED;
1896
1897 /*
1898 * check the status byte to see if this indicates anything special.
1899 */
1900 switch (status_byte(scmd->result)) {
1901 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001902 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /*
1904 * the case of trying to send too many commands to a
1905 * tagged queueing device.
1906 */
1907 case BUSY:
1908 /*
1909 * device can't talk to us at the moment. Should only
1910 * occur (SAM-3) when the task queue is empty, so will cause
1911 * the empty queue handling to trigger a stall in the
1912 * device.
1913 */
1914 return ADD_TO_MLQUEUE;
1915 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001916 if (scmd->cmnd[0] == REPORT_LUNS)
1917 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001918 scsi_handle_queue_ramp_up(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001921 case TASK_ABORTED:
1922 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 case CHECK_CONDITION:
1924 rtn = scsi_check_sense(scmd);
1925 if (rtn == NEEDS_RETRY)
1926 goto maybe_retry;
1927 /* if rtn == FAILED, we have no sense information;
1928 * returning FAILED will wake the error handler thread
1929 * to collect the sense and redo the decide
1930 * disposition */
1931 return rtn;
1932 case CONDITION_GOOD:
1933 case INTERMEDIATE_GOOD:
1934 case INTERMEDIATE_C_GOOD:
1935 case ACA_ACTIVE:
1936 /*
1937 * who knows? FIXME(eric)
1938 */
1939 return SUCCESS;
1940
1941 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001942 sdev_printk(KERN_INFO, scmd->device,
1943 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001944 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return SUCCESS; /* causes immediate i/o error */
1946 default:
1947 return FAILED;
1948 }
1949 return FAILED;
1950
1951 maybe_retry:
1952
1953 /* we requeue for retry because the error was retryable, and
1954 * the request was not marked fast fail. Note that above,
1955 * even if the request is marked fast fail, we still requeue
1956 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001957 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001958 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return NEEDS_RETRY;
1960 } else {
1961 /*
1962 * no more retries - report this one back to upper level.
1963 */
1964 return SUCCESS;
1965 }
1966}
1967
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001968static void eh_lock_door_done(struct request *req, int uptodate)
1969{
1970 __blk_put_request(req->q, req);
1971}
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 * scsi_eh_lock_door - Prevent medium removal for the specified device
1975 * @sdev: SCSI device to prevent medium removal
1976 *
1977 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001978 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 *
1980 * Notes:
1981 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1982 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984static void scsi_eh_lock_door(struct scsi_device *sdev)
1985{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001986 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001987 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
James Bottomley91bc31f2009-05-17 09:30:48 -05001989 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001990 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001991 * request becomes available
1992 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001993 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001994 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001995 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001996 rq = scsi_req(req);
1997 scsi_req_init(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001999 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
2000 rq->cmd[1] = 0;
2001 rq->cmd[2] = 0;
2002 rq->cmd[3] = 0;
2003 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
2004 rq->cmd[5] = 0;
2005 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002006
Christoph Hellwige8064022016-10-20 15:12:13 +02002007 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09002008 req->timeout = 10 * HZ;
2009 req->retries = 5;
2010
2011 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/**
2015 * scsi_restart_operations - restart io operations to the specified host.
2016 * @shost: Host we are restarting.
2017 *
2018 * Notes:
2019 * When we entered the error handler, we blocked all further i/o to
2020 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static void scsi_restart_operations(struct Scsi_Host *shost)
2023{
2024 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05002025 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /*
2028 * If the door was locked, we need to insert a door lock request
2029 * onto the head of the SCSI request queue for the device. There
2030 * is no point trying to lock the door of an off-line device.
2031 */
2032 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01002033 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01002035 sdev->was_reset = 0;
2036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038
2039 /*
2040 * next free up anything directly waiting upon the host. this
2041 * will be requests for character device operations, and also for
2042 * ioctls to queued block devices.
2043 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02002044 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002045 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
James Bottomley939647e2005-09-18 15:05:20 -05002047 spin_lock_irqsave(shost->host_lock, flags);
2048 if (scsi_host_set_state(shost, SHOST_RUNNING))
2049 if (scsi_host_set_state(shost, SHOST_CANCEL))
2050 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2051 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 wake_up(&shost->host_wait);
2054
2055 /*
2056 * finally we need to re-initiate requests that may be pending. we will
2057 * have had everything blocked while error handling is taking place, and
2058 * now that error recovery is done, we will need to ensure that these
2059 * requests are started.
2060 */
2061 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002062
2063 /*
2064 * if eh is active and host_eh_scheduled is pending we need to re-run
2065 * recovery. we do this check after scsi_run_host_queues() to allow
2066 * everything pent up since the last eh run a chance to make forward
2067 * progress before we sync again. Either we'll immediately re-run
2068 * recovery or scsi_device_unbusy() will wake us again when these
2069 * pending commands complete.
2070 */
2071 spin_lock_irqsave(shost->host_lock, flags);
2072 if (shost->host_eh_scheduled)
2073 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2074 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2075 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
2078/**
2079 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002080 * @shost: host to be recovered.
2081 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002082 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002083 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002084void scsi_eh_ready_devs(struct Scsi_Host *shost,
2085 struct list_head *work_q,
2086 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 if (!scsi_eh_stu(shost, work_q, done_q))
2089 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002090 if (!scsi_eh_target_reset(shost, work_q, done_q))
2091 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002092 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002093 scsi_eh_offline_sdevs(work_q,
2094 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002096EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098/**
2099 * scsi_eh_flush_done_q - finish processed commands or retry them.
2100 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002101 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002102void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002104 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002106 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2107 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002109 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002110 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002111 SCSI_LOG_ERROR_RECOVERY(3,
2112 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002113 "%s: flush retry cmd\n",
2114 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2116 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002117 /*
2118 * If just we got sense for the device (called
2119 * scsi_eh_get_sense), scmd->result is already
2120 * set, do not set DRIVER_TIMEOUT.
2121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (!scmd->result)
2123 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002124 SCSI_LOG_ERROR_RECOVERY(3,
2125 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002126 "%s: flush finish cmd\n",
2127 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 scsi_finish_command(scmd);
2129 }
2130 }
2131}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002132EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134/**
2135 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2136 * @shost: Host to unjam.
2137 *
2138 * Notes:
2139 * When we come in here, we *know* that all commands on the bus have
2140 * either completed, failed or timed out. we also know that no further
2141 * commands are being sent to the host, so things are relatively quiet
2142 * and we have freedom to fiddle with things as we wish.
2143 *
2144 * This is only the *default* implementation. it is possible for
2145 * individual drivers to supply their own version of this function, and
2146 * if the maintainer wishes to do this, it is strongly suggested that
2147 * this function be taken as a template and modified. this function
2148 * was designed to correctly handle problems for about 95% of the
2149 * different cases out there, and it should always provide at least a
2150 * reasonable amount of error recovery.
2151 *
2152 * Any command marked 'failed' or 'timeout' must eventually have
2153 * scsi_finish_cmd() called for it. we do all of the retry stuff
2154 * here, so when we restart the host after we return it should have an
2155 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157static void scsi_unjam_host(struct Scsi_Host *shost)
2158{
2159 unsigned long flags;
2160 LIST_HEAD(eh_work_q);
2161 LIST_HEAD(eh_done_q);
2162
2163 spin_lock_irqsave(shost->host_lock, flags);
2164 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2165 spin_unlock_irqrestore(shost->host_lock, flags);
2166
2167 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2168
2169 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2170 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
2171 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2172
Hannes Reineckeb4562022013-10-23 10:51:21 +02002173 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002174 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002175 shost->last_reset = 0;
2176 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 scsi_eh_flush_done_q(&eh_done_q);
2178}
2179
2180/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002181 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 * @data: Host for which we are running.
2183 *
2184 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002185 * This is the main error handling loop. This is run as a kernel thread
2186 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188int scsi_error_handler(void *data)
2189{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002190 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002193 * We use TASK_INTERRUPTIBLE so that the thread is not
2194 * counted against the load average as a running process.
2195 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002196 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 */
Michal Hocko537b6042015-08-27 20:16:37 +02002198 while (true) {
2199 /*
2200 * The sequence in kthread_stop() sets the stop flag first
2201 * then wakes the process. To avoid missed wakeups, the task
2202 * should always be in a non running state before the stop
2203 * flag is checked
2204 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002205 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002206 if (kthread_should_stop())
2207 break;
2208
Tejun Heoee7863b2006-05-15 20:57:20 +09002209 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002210 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002211 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002212 shost_printk(KERN_INFO, shost,
2213 "scsi_eh_%d: sleeping\n",
2214 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002215 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002216 continue;
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
James Bottomley3ed7a472005-09-19 09:50:04 -05002219 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002220 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002221 shost_printk(KERN_INFO, shost,
2222 "scsi_eh_%d: waking up %d/%d/%d\n",
2223 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002224 shost->host_failed,
2225 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /*
2228 * We have a host that is failing for some reason. Figure out
2229 * what we need to do to get it up and online again (if we can).
2230 * If we fail, we end up taking the thing offline.
2231 */
Lin Mingae0751f2011-12-05 09:20:24 +08002232 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002233 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002234 shost_printk(KERN_ERR, shost,
2235 "scsi_eh_%d: unable to autoresume\n",
2236 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002237 continue;
2238 }
2239
Christoph Hellwig9227c332006-04-01 19:21:04 +02002240 if (shost->transportt->eh_strategy_handler)
2241 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 else
2243 scsi_unjam_host(shost);
2244
Wei Fang72d8c362016-06-07 14:53:56 +08002245 /* All scmds have been handled */
2246 shost->host_failed = 0;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /*
2249 * Note - if the above fails completely, the action is to take
2250 * individual devices offline and flush the queue of any
2251 * outstanding requests that may have been pending. When we
2252 * restart, we restart any I/O to any other devices on the bus
2253 * which are still online.
2254 */
2255 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002256 if (!shost->eh_noresume)
2257 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002259 __set_current_state(TASK_RUNNING);
2260
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002261 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002262 shost_printk(KERN_INFO, shost,
2263 "Error handler scsi_eh_%d exiting\n",
2264 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002265 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267}
2268
2269/*
2270 * Function: scsi_report_bus_reset()
2271 *
2272 * Purpose: Utility function used by low-level drivers to report that
2273 * they have observed a bus reset on the bus being handled.
2274 *
2275 * Arguments: shost - Host in question
2276 * channel - channel on which reset was observed.
2277 *
2278 * Returns: Nothing
2279 *
2280 * Lock status: Host lock must be held.
2281 *
2282 * Notes: This only needs to be called if the reset is one which
2283 * originates from an unknown location. Resets originated
2284 * by the mid-level itself don't need to call this, but there
2285 * should be no harm.
2286 *
2287 * The main purpose of this is to make sure that a CHECK_CONDITION
2288 * is properly treated.
2289 */
2290void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2291{
2292 struct scsi_device *sdev;
2293
2294 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002295 if (channel == sdev_channel(sdev))
2296 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
2298}
2299EXPORT_SYMBOL(scsi_report_bus_reset);
2300
2301/*
2302 * Function: scsi_report_device_reset()
2303 *
2304 * Purpose: Utility function used by low-level drivers to report that
2305 * they have observed a device reset on the device being handled.
2306 *
2307 * Arguments: shost - Host in question
2308 * channel - channel on which reset was observed
2309 * target - target on which reset was observed
2310 *
2311 * Returns: Nothing
2312 *
2313 * Lock status: Host lock must be held
2314 *
2315 * Notes: This only needs to be called if the reset is one which
2316 * originates from an unknown location. Resets originated
2317 * by the mid-level itself don't need to call this, but there
2318 * should be no harm.
2319 *
2320 * The main purpose of this is to make sure that a CHECK_CONDITION
2321 * is properly treated.
2322 */
2323void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2324{
2325 struct scsi_device *sdev;
2326
2327 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002328 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002329 target == sdev_id(sdev))
2330 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332}
2333EXPORT_SYMBOL(scsi_report_device_reset);
2334
2335static void
2336scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2337{
2338}
2339
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002340/**
2341 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2342 * @dev: scsi_device to operate on
2343 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 */
2345int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002346scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Alan Sternbc4f2402010-06-17 10:41:42 -04002348 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002349 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002350 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002351 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002352 int error = 0, rtn, val;
2353
2354 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2355 return -EACCES;
2356
2357 error = get_user(val, arg);
2358 if (error)
2359 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Alan Sternbc4f2402010-06-17 10:41:42 -04002361 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002362 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002363
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002364 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002365 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2366 shost->hostt->cmd_size, GFP_KERNEL);
2367 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002368 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002369 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002370
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002371 scmd = (struct scsi_cmnd *)(rq + 1);
2372 scsi_init_command(dev, scmd);
2373 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002374 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002377 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 scmd->cmd_len = 0;
2380
2381 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
James Smartd7a1bb02006-03-08 14:50:12 -05002383 spin_lock_irqsave(shost->host_lock, flags);
2384 shost->tmf_in_progress = 1;
2385 spin_unlock_irqrestore(shost->host_lock, flags);
2386
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002387 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2388 case SG_SCSI_RESET_NOTHING:
2389 rtn = SUCCESS;
2390 break;
2391 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002393 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 break;
2395 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002396 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002397 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002398 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002399 break;
2400 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002401 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002403 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 break;
2405 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002406 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002408 if (rtn == SUCCESS)
2409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 default:
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002411 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002416 error = (rtn == SUCCESS) ? 0 : -EIO;
2417
James Smartd7a1bb02006-03-08 14:50:12 -05002418 spin_lock_irqsave(shost->host_lock, flags);
2419 shost->tmf_in_progress = 0;
2420 spin_unlock_irqrestore(shost->host_lock, flags);
2421
2422 /*
2423 * be sure to wake up anyone who was sleeping or had their queue
2424 * suspended while we performed the TMF.
2425 */
2426 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002427 shost_printk(KERN_INFO, shost,
2428 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002429
2430 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002431 scsi_run_host_queues(shost);
2432
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002433 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002434 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002435
Christoph Hellwig04796332014-02-20 14:20:55 -08002436out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002437 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002438 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002440EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002442bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2443 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002446 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448EXPORT_SYMBOL(scsi_command_normalize_sense);
2449
2450/**
Rob Landleyeb448202007-11-03 13:30:39 -05002451 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 * @sense_buffer: byte array of sense data
2453 * @sb_len: number of valid bytes in sense_buffer
2454 * @info_out: pointer to 64 integer where 8 or 4 byte information
2455 * field will be placed if found.
2456 *
2457 * Return value:
2458 * 1 if information field found, 0 if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2461 u64 * info_out)
2462{
2463 int j;
2464 const u8 * ucp;
2465 u64 ull;
2466
2467 if (sb_len < 7)
2468 return 0;
2469 switch (sense_buffer[0] & 0x7f) {
2470 case 0x70:
2471 case 0x71:
2472 if (sense_buffer[0] & 0x80) {
2473 *info_out = (sense_buffer[3] << 24) +
2474 (sense_buffer[4] << 16) +
2475 (sense_buffer[5] << 8) + sense_buffer[6];
2476 return 1;
2477 } else
2478 return 0;
2479 case 0x72:
2480 case 0x73:
2481 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2482 0 /* info desc */);
2483 if (ucp && (0xa == ucp[1])) {
2484 ull = 0;
2485 for (j = 0; j < 8; ++j) {
2486 if (j > 0)
2487 ull <<= 8;
2488 ull |= ucp[4 + j];
2489 }
2490 *info_out = ull;
2491 return 1;
2492 } else
2493 return 0;
2494 default:
2495 return 0;
2496 }
2497}
2498EXPORT_SYMBOL(scsi_get_sense_info_fld);