blob: 96f988a7efda6d781b60307dab32abdc95177c67 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 *
4 * SCSI error/timeout handling
5 * Initial versions: Eric Youngdale. Based upon conversations with
Jesper Juhl0bf8c862011-03-21 20:47:31 +01006 * Leonard Zubkoff and David Miller at Linux Expo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * ideas originating from all over the place.
8 *
9 * Restructured scsi_unjam_host and associated functions.
10 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
11 *
12 * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
Jesper Juhl0bf8c862011-03-21 20:47:31 +010013 * minor cleanups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 */
16
17#include <linux/module.h>
18#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/timer.h>
21#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070023#include <linux/freezer.h>
Christoph Hellwigc5478de2005-09-06 14:04:26 +020024#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/interrupt.h>
26#include <linux/blkdev.h>
27#include <linux/delay.h>
Hannes Reineckefc736482013-04-25 08:10:00 +020028#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020031#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -050034#include <scsi/scsi_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <scsi/scsi_eh.h>
Sagi Grimberg7708c162015-07-08 17:58:52 +030036#include <scsi/scsi_common.h>
James Smartc829c392006-03-13 08:28:57 -050037#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <scsi/scsi_host.h>
39#include <scsi/scsi_ioctl.h>
Christoph Hellwigee14c672015-08-27 14:16:59 +020040#include <scsi/scsi_dh.h>
Christoph Hellwig176aa9d2014-10-11 12:06:47 +020041#include <scsi/sg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "scsi_priv.h"
44#include "scsi_logging.h"
Adrian Bunk79ee8302007-08-10 14:50:42 -070045#include "scsi_transport_api.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Kei Tokunagabf816232010-04-01 20:41:40 +090047#include <trace/events/scsi.h>
48
Damien Le Moal29087692017-04-24 16:51:12 +090049#include <asm/unaligned.h>
50
James Bottomley14216562012-07-25 23:55:55 +040051static void scsi_eh_done(struct scsi_cmnd *scmd);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * These should *probably* be handled by the host itself.
55 * Since it is allowed to sleep, it probably should.
56 */
57#define BUS_RESET_SETTLE_TIME (10)
58#define HOST_RESET_SETTLE_TIME (10)
59
David Jeffery3eef6252011-05-19 14:41:12 -040060static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +010061static int scsi_try_to_abort_cmd(struct scsi_host_template *,
62 struct scsi_cmnd *);
David Jeffery3eef6252011-05-19 14:41:12 -040063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064void scsi_eh_wakeup(struct Scsi_Host *shost)
65{
Bart Van Asschef0317e82017-12-04 10:06:24 -080066 lockdep_assert_held(shost->host_lock);
67
Christoph Hellwig74665012014-01-22 15:29:29 +010068 if (atomic_read(&shost->host_busy) == shost->host_failed) {
Kei Tokunagabf816232010-04-01 20:41:40 +090069 trace_scsi_eh_wakeup(shost);
James Bottomley3ed7a472005-09-19 09:50:04 -050070 wake_up_process(shost->ehandler);
Hannes Reinecke91921e02014-06-25 16:39:59 +020071 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
72 "Waking error handler thread\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74}
Tejun Heof8bbfc22006-05-19 21:07:05 +090075
76/**
77 * scsi_schedule_eh - schedule EH for SCSI host
78 * @shost: SCSI host to invoke error handling on.
79 *
80 * Schedule SCSI EH without scmd.
Randy Dunlapdc8875e2007-11-15 15:42:30 -080081 */
Tejun Heof8bbfc22006-05-19 21:07:05 +090082void scsi_schedule_eh(struct Scsi_Host *shost)
83{
84 unsigned long flags;
85
86 spin_lock_irqsave(shost->host_lock, flags);
87
88 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
89 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
90 shost->host_eh_scheduled++;
91 scsi_eh_wakeup(shost);
92 }
93
94 spin_unlock_irqrestore(shost->host_lock, flags);
95}
96EXPORT_SYMBOL_GPL(scsi_schedule_eh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Hannes Reineckeb4562022013-10-23 10:51:21 +020098static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
99{
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100100 if (!shost->last_reset || shost->eh_deadline == -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200101 return 0;
102
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100103 /*
104 * 32bit accesses are guaranteed to be atomic
105 * (on all supported architectures), so instead
106 * of using a spinlock we can as well double check
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100107 * if eh_deadline has been set to 'off' during the
Hannes Reinecke76ad3e52013-11-11 13:44:55 +0100108 * time_before call.
109 */
110 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100111 shost->eh_deadline > -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200112 return 0;
113
114 return 1;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100118 * scmd_eh_abort_handler - Handle command aborts
119 * @work: command to be aborted.
Bart Van Assche923f46f2018-02-12 10:38:05 -0800120 *
121 * Note: this function must be called only for a command that has timed out.
122 * Because the block layer marks a request as complete before it calls
123 * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
124 * timed out do not have any effect. Hence it is safe to call
125 * scsi_finish_command() from this function.
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100126 */
127void
128scmd_eh_abort_handler(struct work_struct *work)
129{
130 struct scsi_cmnd *scmd =
131 container_of(work, struct scsi_cmnd, abort_work.work);
132 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100133 int rtn;
134
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100135 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100136 SCSI_LOG_ERROR_RECOVERY(3,
137 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100138 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100139 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100140 SCSI_LOG_ERROR_RECOVERY(3,
141 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100142 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100143 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
144 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200145 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100146 if (scsi_host_eh_past_deadline(sdev->host)) {
147 SCSI_LOG_ERROR_RECOVERY(3,
148 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100149 "eh timeout, not retrying "
150 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100151 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100152 (++scmd->retries <= scmd->allowed)) {
153 SCSI_LOG_ERROR_RECOVERY(3,
154 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100155 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100156 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100157 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100158 } else {
159 SCSI_LOG_ERROR_RECOVERY(3,
160 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100161 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100162 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100163 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100164 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100165 } else {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100168 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200169 (rtn == FAST_IO_FAIL) ?
170 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100171 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100172 }
173
Hannes Reineckea0658632017-04-06 15:36:35 +0200174 scsi_eh_scmd_add(scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100175}
176
177/**
178 * scsi_abort_command - schedule a command abort
179 * @scmd: scmd to abort.
180 *
181 * We only need to abort commands after a command timeout
182 */
183static int
184scsi_abort_command(struct scsi_cmnd *scmd)
185{
186 struct scsi_device *sdev = scmd->device;
187 struct Scsi_Host *shost = sdev->host;
188 unsigned long flags;
189
190 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
191 /*
192 * Retry after abort failed, escalate to next level.
193 */
194 SCSI_LOG_ERROR_RECOVERY(3,
195 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100196 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200197 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100198 return FAILED;
199 }
200
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100201 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100202 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100203 shost->last_reset = jiffies;
204 spin_unlock_irqrestore(shost->host_lock, flags);
205
206 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
207 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100208 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100209 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
210 return SUCCESS;
211}
212
213/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200214 * scsi_eh_reset - call into ->eh_action to reset internal counters
215 * @scmd: scmd to run eh on.
216 *
217 * The scsi driver might be carrying internal state about the
218 * devices, so we need to call into the driver to reset the
219 * internal state once the error handler is started.
220 */
221static void scsi_eh_reset(struct scsi_cmnd *scmd)
222{
223 if (!blk_rq_is_passthrough(scmd->request)) {
224 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
225 if (sdrv->eh_reset)
226 sdrv->eh_reset(scmd);
227 }
228}
229
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800230static void scsi_eh_inc_host_failed(struct rcu_head *head)
231{
232 struct Scsi_Host *shost = container_of(head, typeof(*shost), rcu);
233 unsigned long flags;
234
235 spin_lock_irqsave(shost->host_lock, flags);
236 shost->host_failed++;
237 scsi_eh_wakeup(shost);
238 spin_unlock_irqrestore(shost->host_lock, flags);
239}
240
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200241/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * scsi_eh_scmd_add - add scsi cmd to error handling.
243 * @scmd: scmd to run eh on.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800244 */
Hannes Reineckea0658632017-04-06 15:36:35 +0200245void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct Scsi_Host *shost = scmd->device->host;
248 unsigned long flags;
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200249 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200251 WARN_ON_ONCE(!shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200254 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
255 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
256 WARN_ON_ONCE(ret);
257 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100258 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200259 shost->last_reset = jiffies;
260
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200261 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 spin_unlock_irqrestore(shost->host_lock, flags);
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800264 /*
265 * Ensure that all tasks observe the host state change before the
266 * host_failed change.
267 */
268 call_rcu(&shost->rcu, scsi_eh_inc_host_failed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700273 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 *
275 * Notes:
276 * We do not need to lock this. There is the potential for a race
277 * only in that the normal completion handling might run, but if the
278 * normal completion function determines that the timer has already
279 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800280 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700281enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Bart Van Asschebed22132017-08-25 13:46:32 -0700283 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700284 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100285 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500286
Kei Tokunagabf816232010-04-01 20:41:40 +0900287 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 scsi_log_completion(scmd, TIMEOUT_ERROR);
289
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100290 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200291 host->last_reset = jiffies;
292
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100293 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100294 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500295
Hannes Reineckea33c0702014-06-13 14:01:45 +0200296 if (rtn == BLK_EH_NOT_HANDLED) {
Hannes Reineckea0658632017-04-06 15:36:35 +0200297 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200298 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200299 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200300 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200301 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700302
Christoph Hellwigfa990782008-11-05 15:03:15 +0100303 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/**
307 * scsi_block_when_processing_errors - Prevent cmds from being queued.
308 * @sdev: Device on which we are performing recovery.
309 *
310 * Description:
311 * We block until the host is out of error recovery, and then check to
312 * see whether the host or the device is offline.
313 *
314 * Return value:
315 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317int scsi_block_when_processing_errors(struct scsi_device *sdev)
318{
319 int online;
320
James Bottomley939647e2005-09-18 15:05:20 -0500321 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 online = scsi_device_online(sdev);
324
Hannes Reinecke91921e02014-06-25 16:39:59 +0200325 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
326 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 return online;
329}
330EXPORT_SYMBOL(scsi_block_when_processing_errors);
331
332#ifdef CONFIG_SCSI_LOGGING
333/**
334 * scsi_eh_prt_fail_stats - Log info on failures.
335 * @shost: scsi host being recovered.
336 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
339 struct list_head *work_q)
340{
341 struct scsi_cmnd *scmd;
342 struct scsi_device *sdev;
343 int total_failures = 0;
344 int cmd_failed = 0;
345 int cmd_cancel = 0;
346 int devices_failed = 0;
347
348 shost_for_each_device(sdev, shost) {
349 list_for_each_entry(scmd, work_q, eh_entry) {
350 if (scmd->device == sdev) {
351 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200352 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100354 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 ++cmd_failed;
356 }
357 }
358
359 if (cmd_cancel || cmd_failed) {
360 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200361 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500362 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700363 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500364 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 cmd_cancel = 0;
366 cmd_failed = 0;
367 ++devices_failed;
368 }
369 }
370
Hannes Reinecke91921e02014-06-25 16:39:59 +0200371 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
372 "Total of %d commands on %d"
373 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100374 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376#endif
377
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400378 /**
379 * scsi_report_lun_change - Set flag on all *other* devices on the same target
380 * to indicate that a UNIT ATTENTION is expected.
381 * @sdev: Device reporting the UNIT ATTENTION
382 */
383static void scsi_report_lun_change(struct scsi_device *sdev)
384{
385 sdev->sdev_target->expecting_lun_change = 1;
386}
387
388/**
389 * scsi_report_sense - Examine scsi sense information and log messages for
390 * certain conditions, also issue uevents for some of them.
391 * @sdev: Device reporting the sense code
392 * @sshdr: sshdr to be examined
393 */
394static void scsi_report_sense(struct scsi_device *sdev,
395 struct scsi_sense_hdr *sshdr)
396{
397 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
398
399 if (sshdr->sense_key == UNIT_ATTENTION) {
400 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
401 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
402 sdev_printk(KERN_WARNING, sdev,
403 "Inquiry data has changed");
404 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
405 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
406 scsi_report_lun_change(sdev);
407 sdev_printk(KERN_WARNING, sdev,
408 "Warning! Received an indication that the "
409 "LUN assignments on this target have "
410 "changed. The Linux SCSI layer does not "
411 "automatically remap LUN assignments.\n");
412 } else if (sshdr->asc == 0x3f)
413 sdev_printk(KERN_WARNING, sdev,
414 "Warning! Received an indication that the "
415 "operating parameters on this target have "
416 "changed. The Linux SCSI layer does not "
417 "automatically adjust these parameters.\n");
418
419 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
420 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
421 sdev_printk(KERN_WARNING, sdev,
422 "Warning! Received an indication that the "
423 "LUN reached a thin provisioning soft "
424 "threshold.\n");
425 }
426
Hannes Reineckecf3431b2017-10-17 09:11:24 +0200427 if (sshdr->asc == 0x29) {
428 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
429 sdev_printk(KERN_WARNING, sdev,
430 "Power-on or device reset occurred\n");
431 }
432
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400433 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
434 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
435 sdev_printk(KERN_WARNING, sdev,
436 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200437 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
438 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
439 sdev_printk(KERN_WARNING, sdev,
440 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400441 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
442 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
443 sdev_printk(KERN_WARNING, sdev,
444 "Capacity data has changed");
445 } else if (sshdr->asc == 0x2a)
446 sdev_printk(KERN_WARNING, sdev,
447 "Parameters changed");
448 }
449
450 if (evt_type != SDEV_EVT_MAXBITS) {
451 set_bit(evt_type, sdev->pending_events);
452 schedule_work(&sdev->event_work);
453 }
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/**
457 * scsi_check_sense - Examine scsi cmd sense
458 * @scmd: Cmd to have sense checked.
459 *
460 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200461 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
463 * Notes:
464 * When a deferred error is detected the current command has
465 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800466 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200467int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700469 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct scsi_sense_hdr sshdr;
471
472 if (! scsi_command_normalize_sense(scmd, &sshdr))
473 return FAILED; /* no valid sense data */
474
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400475 scsi_report_sense(sdev, &sshdr);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (scsi_sense_is_deferred(&sshdr))
478 return NEEDS_RETRY;
479
Christoph Hellwigee14c672015-08-27 14:16:59 +0200480 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700481 int rc;
482
Christoph Hellwigee14c672015-08-27 14:16:59 +0200483 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700484 if (rc != SCSI_RETURN_NOT_HANDLED)
485 return rc;
486 /* handler does not care. Drop down to default handling */
487 }
488
Christoph Hellwige925cc42014-11-06 15:11:22 -0600489 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
490 /*
491 * nasty: for mid-layer issued TURs, we need to return the
492 * actual sense data without any recovery attempt. For eh
493 * issued ones, we need to try to recover and interpret
494 */
495 return SUCCESS;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * Previous logic looked for FILEMARK, EOM or ILI which are
499 * mainly associated with tapes and returned SUCCESS.
500 */
501 if (sshdr.response_code == 0x70) {
502 /* fixed format */
503 if (scmd->sense_buffer[2] & 0xe0)
504 return SUCCESS;
505 } else {
506 /*
507 * descriptor format: look for "stream commands sense data
508 * descriptor" (see SSC-3). Assume single sense data
509 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
510 */
511 if ((sshdr.additional_length > 3) &&
512 (scmd->sense_buffer[8] == 0x4) &&
513 (scmd->sense_buffer[11] & 0xe0))
514 return SUCCESS;
515 }
516
517 switch (sshdr.sense_key) {
518 case NO_SENSE:
519 return SUCCESS;
520 case RECOVERED_ERROR:
521 return /* soft_error */ SUCCESS;
522
523 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400524 if (sshdr.asc == 0x10) /* DIF */
525 return SUCCESS;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return NEEDS_RETRY;
528 case NOT_READY:
529 case UNIT_ATTENTION:
530 /*
531 * if we are expecting a cc/ua because of a bus reset that we
532 * performed, treat this just as a retry. otherwise this is
533 * information that we should pass up to the upper-level driver
534 * so that we can deal with it there.
535 */
536 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900537 /*
538 * Because some device does not queue unit
539 * attentions correctly, we carefully check
540 * additional sense code and qualifier so as
541 * not to squash media change unit attention.
542 */
543 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
544 scmd->device->expecting_cc_ua = 0;
545 return NEEDS_RETRY;
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400549 * we might also expect a cc/ua if another LUN on the target
550 * reported a UA with an ASC/ASCQ of 3F 0E -
551 * REPORTED LUNS DATA HAS CHANGED.
552 */
553 if (scmd->device->sdev_target->expecting_lun_change &&
554 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
555 return NEEDS_RETRY;
556 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100557 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * should retry.
559 */
560 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
561 return NEEDS_RETRY;
562 /*
563 * if the device is not started, we need to wake
564 * the error handler to start the motor
565 */
566 if (scmd->device->allow_restart &&
567 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
568 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100569 /*
570 * Pass the UA upwards for a determination in the completion
571 * functions.
572 */
573 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100575 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200576 case DATA_PROTECT:
577 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
578 /* Thin provisioning hard threshold reached */
579 set_host_byte(scmd, DID_ALLOC_FAILURE);
580 return SUCCESS;
581 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700582 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 case COPY_ABORTED:
584 case VOLUME_OVERFLOW:
585 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100586 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200587 set_host_byte(scmd, DID_TARGET_FAILURE);
588 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800591 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
592 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
593 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200594 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200595 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return NEEDS_RETRY;
598
599 case HARDWARE_ERROR:
600 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700601 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200603 set_host_byte(scmd, DID_TARGET_FAILURE);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700604 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500607 if (sshdr.asc == 0x20 || /* Invalid command operation code */
608 sshdr.asc == 0x21 || /* Logical block address out of range */
Hannes Reineckea8bbb2a2017-10-17 09:10:56 +0200609 sshdr.asc == 0x22 || /* Invalid function */
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500610 sshdr.asc == 0x24 || /* Invalid field in cdb */
Martin Wilckd0b7a902017-09-27 14:44:19 +0200611 sshdr.asc == 0x26 || /* Parameter value invalid */
612 sshdr.asc == 0x27) { /* Write protected */
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);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700717 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case COMMAND_TERMINATED:
719 return SUCCESS;
720 case CHECK_CONDITION:
721 return scsi_check_sense(scmd);
722 case CONDITION_GOOD:
723 case INTERMEDIATE_GOOD:
724 case INTERMEDIATE_C_GOOD:
725 /*
726 * who knows? FIXME(eric)
727 */
728 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500729 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500730 if (scmd->cmnd[0] == TEST_UNIT_READY)
731 /* it is a success, we probed the device and
732 * found it */
733 return SUCCESS;
734 /* otherwise, we failed to send the command */
735 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700737 scsi_handle_queue_full(scmd->device);
738 /* fall through */
739 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200740 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 default:
742 return FAILED;
743 }
744 return FAILED;
745}
746
747/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * scsi_eh_done - Completion function for error handling.
749 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static void scsi_eh_done(struct scsi_cmnd *scmd)
752{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100753 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600754
Hannes Reinecke91921e02014-06-25 16:39:59 +0200755 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100756 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600757
758 eh_action = scmd->device->host->eh_action;
759 if (eh_action)
760 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763/**
Brian King292148f2007-01-30 17:51:17 -0600764 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200765 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800766 */
Brian King292148f2007-01-30 17:51:17 -0600767static int scsi_try_host_reset(struct scsi_cmnd *scmd)
768{
769 unsigned long flags;
770 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100771 struct Scsi_Host *host = scmd->device->host;
772 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600773
Hannes Reinecke91921e02014-06-25 16:39:59 +0200774 SCSI_LOG_ERROR_RECOVERY(3,
775 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600776
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100777 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600778 return FAILED;
779
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100780 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600781
782 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100783 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600784 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100785 spin_lock_irqsave(host->host_lock, flags);
786 scsi_report_bus_reset(host, scmd_channel(scmd));
787 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600788 }
789
790 return rtn;
791}
792
793/**
794 * scsi_try_bus_reset - ask host to perform a bus reset
795 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800796 */
Brian King292148f2007-01-30 17:51:17 -0600797static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
798{
799 unsigned long flags;
800 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100801 struct Scsi_Host *host = scmd->device->host;
802 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600803
Hannes Reinecke91921e02014-06-25 16:39:59 +0200804 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
805 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600806
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100807 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600808 return FAILED;
809
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100810 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600811
812 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100813 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600814 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100815 spin_lock_irqsave(host->host_lock, flags);
816 scsi_report_bus_reset(host, scmd_channel(scmd));
817 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600818 }
819
820 return rtn;
821}
822
Mike Christie30bd7df2008-02-29 18:25:19 -0600823static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
824{
825 sdev->was_reset = 1;
826 sdev->expecting_cc_ua = 1;
827}
828
829/**
830 * scsi_try_target_reset - Ask host to perform a target reset
831 * @scmd: SCSI cmd used to send a target reset
832 *
833 * Notes:
834 * There is no timeout for this operation. if this operation is
835 * unreliable for a given host, then the host itself needs to put a
836 * timer on it, and set the host back to a consistent state prior to
837 * returning.
838 */
839static int scsi_try_target_reset(struct scsi_cmnd *scmd)
840{
841 unsigned long flags;
842 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100843 struct Scsi_Host *host = scmd->device->host;
844 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600845
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100846 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600847 return FAILED;
848
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100849 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600850 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100851 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600852 __starget_for_each_device(scsi_target(scmd->device), NULL,
853 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100854 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600855 }
856
857 return rtn;
858}
859
Brian King292148f2007-01-30 17:51:17 -0600860/**
861 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
862 * @scmd: SCSI cmd used to send BDR
863 *
864 * Notes:
865 * There is no timeout for this operation. if this operation is
866 * unreliable for a given host, then the host itself needs to put a
867 * timer on it, and set the host back to a consistent state prior to
868 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800869 */
Brian King292148f2007-01-30 17:51:17 -0600870static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
871{
872 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100873 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600874
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100875 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600876 return FAILED;
877
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100878 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600879 if (rtn == SUCCESS)
880 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600881 return rtn;
882}
883
Hannes Reinecke883a0302014-10-24 14:27:04 +0200884/**
885 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800886 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200887 * @scmd: SCSI cmd used to send a target reset
888 *
889 * Return value:
890 * SUCCESS, FAILED, or FAST_IO_FAIL
891 *
892 * Notes:
893 * SUCCESS does not necessarily indicate that the command
894 * has been aborted; it only indicates that the LLDDs
895 * has cleared all references to that command.
896 * LLDDs should return FAILED only if an abort was required
897 * but could not be executed. LLDDs should return FAST_IO_FAIL
898 * if the device is temporarily unavailable (eg due to a
899 * link down on FibreChannel)
900 */
901static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
902 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600903{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100904 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600905 return FAILED;
906
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100907 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600908}
909
Brian King292148f2007-01-30 17:51:17 -0600910static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
911{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100912 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600913 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600914 if (scsi_try_target_reset(scmd) != SUCCESS)
915 if (scsi_try_bus_reset(scmd) != SUCCESS)
916 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600917}
918
919/**
Santosh Y3b729f72012-04-08 18:47:09 +0530920 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100921 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200922 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200923 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300924 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200925 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100926 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200927 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200928 * as part of the error recovery process. If @sense_bytes is 0 the command
929 * sent must be one that does not transfer any data. If @sense_bytes != 0
930 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
931 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800932 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200933void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
934 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
f59114b2005-04-17 15:00:23 -0500936 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Christoph Hellwig631c2282006-07-08 20:42:15 +0200938 /*
939 * We need saved copies of a number of fields - this is because
940 * error handling may need to overwrite these with different values
941 * to run different commands, and once error handling is complete,
942 * we will need to restore these values prior to running the actual
943 * command.
944 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200945 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300946 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200947 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200948 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200949 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200950 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400951 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400952 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200953 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200954
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400955 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700956 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300957 scmd->cmnd = ses->eh_cmnd;
958 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200959 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200960 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700961 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200962
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200963 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200964 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
965 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200966 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200967 scmd->sdb.length);
968 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200969 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400970 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200971 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200972 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200973 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200974 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200975 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200976 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300977 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200978 memcpy(scmd->cmnd, cmnd, cmnd_size);
979 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
980 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200981 }
982
983 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200984
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200985 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500987 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Christoph Hellwig631c2282006-07-08 20:42:15 +0200989 /*
990 * Zero the sense buffer. The scsi spec mandates that any
991 * untransferred sense data should be interpreted as being zero.
992 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900993 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200994}
995EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200996
Boaz Harroshe1c23462007-10-08 16:36:45 +0200997/**
Santosh Y3b729f72012-04-08 18:47:09 +0530998 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200999 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001000 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +02001001 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +02001002 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001003 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001004void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1005{
1006 /*
1007 * Restore original data
1008 */
1009 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001010 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001011 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001012 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001013 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001014 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001015 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001016 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +02001017 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001018}
1019EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1020
1021/**
Santosh Y3b729f72012-04-08 18:47:09 +05301022 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001023 * @scmd: SCSI command structure to hijack
1024 * @cmnd: CDB to send
1025 * @cmnd_size: size in bytes of @cmnd
1026 * @timeout: timeout for this request
1027 * @sense_bytes: size of sense data to copy or 0
1028 *
1029 * This function is used to send a scsi command down to a target device
1030 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1031 *
1032 * Return value:
1033 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001034 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001035static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1036 int cmnd_size, int timeout, unsigned sense_bytes)
1037{
1038 struct scsi_device *sdev = scmd->device;
1039 struct Scsi_Host *shost = sdev->host;
1040 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001041 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001042 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001043 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001044 int rtn;
1045
Hannes Reineckefc736482013-04-25 08:10:00 +02001046retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001047 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001048 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001051 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001052 rtn = shost->hostt->queuecommand(shost, scmd);
1053 if (rtn) {
1054 if (timeleft > stall_for) {
1055 scsi_eh_restore_cmnd(scmd, &ses);
1056 timeleft -= stall_for;
1057 msleep(jiffies_to_msecs(stall_for));
1058 goto retry;
1059 }
1060 /* signal not to enter either branch of the if () below */
1061 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001062 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001063 } else {
1064 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001065 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
f59114b2005-04-17 15:00:23 -05001068 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Hannes Reineckefc736482013-04-25 08:10:00 +02001070 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Hannes Reinecke91921e02014-06-25 16:39:59 +02001072 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001073 "%s timeleft: %ld\n",
1074 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001077 * If there is time left scsi_eh_done got called, and we will examine
1078 * the actual status codes to see whether the command actually did
1079 * complete normally, else if we have a zero return and no time left,
1080 * the command must still be pending, so abort it and return FAILED.
1081 * If we never actually managed to issue the command, because
1082 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1083 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001085 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001087 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1088 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 switch (rtn) {
1091 case SUCCESS:
1092 case NEEDS_RETRY:
1093 case FAILED:
1094 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001095 case ADD_TO_MLQUEUE:
1096 rtn = NEEDS_RETRY;
1097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 default:
1099 rtn = FAILED;
1100 break;
1101 }
Alan Stern511833a2014-11-21 10:44:49 -05001102 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001103 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001104 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
Boaz Harroshe1c23462007-10-08 16:36:45 +02001107 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return rtn;
1110}
1111
1112/**
1113 * scsi_request_sense - Request sense data from a particular target.
1114 * @scmd: SCSI cmd for request sense.
1115 *
1116 * Notes:
1117 * Some hosts automatically obtain this information, others require
1118 * that we obtain it on our own. This function will *not* return until
1119 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static int scsi_request_sense(struct scsi_cmnd *scmd)
1122{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001123 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
James Bottomley24510792013-11-11 13:44:53 +01001126static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1127{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001128 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001129 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1130 if (sdrv->eh_action)
1131 rtn = sdrv->eh_action(scmd, rtn);
1132 }
1133 return rtn;
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/**
1137 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1138 * @scmd: Original SCSI cmd that eh has finished.
1139 * @done_q: Queue for processed commands.
1140 *
1141 * Notes:
1142 * We don't want to use the normal command completion while we are are
1143 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001144 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * keep a list of pending commands for final completion, and once we
1146 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001147 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001148void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 list_move_tail(&scmd->eh_entry, done_q);
1151}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001152EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154/**
1155 * scsi_eh_get_sense - Get device sense data.
1156 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001157 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 *
1159 * Description:
1160 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001161 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *
1163 * Notes:
1164 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001165 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * it down before we request it.
1167 *
1168 * All drivers should request sense information internally these days,
1169 * so for now all I have to say is tough noogies if you end up in here.
1170 *
1171 * XXX: Long term this code should go away, but that needs an audit of
1172 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001173 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001174int scsi_eh_get_sense(struct list_head *work_q,
1175 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001177 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001178 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int rtn;
1180
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001181 /*
1182 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1183 * should not get sense.
1184 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001185 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001186 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 SCSI_SENSE_VALID(scmd))
1188 continue;
1189
Hannes Reineckeb4562022013-10-23 10:51:21 +02001190 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001191 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001192 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001193 scmd_printk(KERN_INFO, scmd,
1194 "%s: skip request sense, past eh deadline\n",
1195 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001196 break;
1197 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001198 if (status_byte(scmd->result) != CHECK_CONDITION)
1199 /*
1200 * don't request sense if there's no check condition
1201 * status because the error we're processing isn't one
1202 * that has a sense code (and some devices get
1203 * confused by sense requests out of the blue)
1204 */
1205 continue;
1206
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001207 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1208 "%s: requesting sense\n",
1209 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 rtn = scsi_request_sense(scmd);
1211 if (rtn != SUCCESS)
1212 continue;
1213
Hannes Reinecke91921e02014-06-25 16:39:59 +02001214 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001215 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001216 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 rtn = scsi_decide_disposition(scmd);
1219
1220 /*
1221 * if the result was normal, then just pass it along to the
1222 * upper level.
1223 */
1224 if (rtn == SUCCESS)
1225 /* we don't want this command reissued, just
1226 * finished with the sense data, so set
1227 * retries to the max allowed to ensure it
1228 * won't get reissued */
1229 scmd->retries = scmd->allowed;
1230 else if (rtn != NEEDS_RETRY)
1231 continue;
1232
1233 scsi_eh_finish_cmd(scmd, done_q);
1234 }
1235
1236 return list_empty(work_q);
1237}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001238EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001242 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
1244 * Return value:
1245 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247static int scsi_eh_tur(struct scsi_cmnd *scmd)
1248{
1249 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1250 int retry_cnt = 1, rtn;
1251
1252retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001253 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1254 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Hannes Reinecke91921e02014-06-25 16:39:59 +02001256 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001257 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001258
1259 switch (rtn) {
1260 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (retry_cnt--)
1262 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001263 /*FALLTHRU*/
1264 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001265 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001266 default:
1267 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271/**
David Jeffery3eef6252011-05-19 14:41:12 -04001272 * scsi_eh_test_devices - check if devices are responding from error recovery.
1273 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001274 * @work_q: queue for commands which still need more error recovery
1275 * @done_q: queue for commands which are finished
1276 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001277 *
1278 * Decription:
1279 * Tests if devices are in a working state. Commands to devices now in
1280 * a working state are sent to the done_q while commands to devices which
1281 * are still failing to respond are returned to the work_q for more
1282 * processing.
1283 **/
1284static int scsi_eh_test_devices(struct list_head *cmd_list,
1285 struct list_head *work_q,
1286 struct list_head *done_q, int try_stu)
1287{
1288 struct scsi_cmnd *scmd, *next;
1289 struct scsi_device *sdev;
1290 int finish_cmds;
1291
1292 while (!list_empty(cmd_list)) {
1293 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1294 sdev = scmd->device;
1295
Hannes Reineckeb4562022013-10-23 10:51:21 +02001296 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001297 if (scsi_host_eh_past_deadline(sdev->host)) {
1298 /* Push items back onto work_q */
1299 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001300 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001301 sdev_printk(KERN_INFO, sdev,
1302 "%s: skip test device, past eh deadline",
1303 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001304 break;
1305 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001306 }
1307
David Jeffery3eef6252011-05-19 14:41:12 -04001308 finish_cmds = !scsi_device_online(scmd->device) ||
1309 (try_stu && !scsi_eh_try_stu(scmd) &&
1310 !scsi_eh_tur(scmd)) ||
1311 !scsi_eh_tur(scmd);
1312
1313 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1314 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001315 if (finish_cmds &&
1316 (try_stu ||
1317 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001318 scsi_eh_finish_cmd(scmd, done_q);
1319 else
1320 list_move_tail(&scmd->eh_entry, work_q);
1321 }
1322 }
1323 return list_empty(work_q);
1324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001328 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
1330 * Return value:
1331 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1334{
1335 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Christoph Hellwig631c2282006-07-08 20:42:15 +02001337 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001338 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Brian Kinged773e62007-03-29 15:25:52 -05001340 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001341 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001342
Christoph Hellwig631c2282006-07-08 20:42:15 +02001343 if (rtn == SUCCESS)
1344 return 0;
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 1;
1348}
1349
1350 /**
1351 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001352 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001353 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001354 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 *
1356 * Notes:
1357 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001358 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360static int scsi_eh_stu(struct Scsi_Host *shost,
1361 struct list_head *work_q,
1362 struct list_head *done_q)
1363{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001364 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct scsi_device *sdev;
1366
1367 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001368 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001369 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001370 sdev_printk(KERN_INFO, sdev,
1371 "%s: skip START_UNIT, past eh deadline\n",
1372 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001373 break;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 stu_scmd = NULL;
1376 list_for_each_entry(scmd, work_q, eh_entry)
1377 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1378 scsi_check_sense(scmd) == FAILED ) {
1379 stu_scmd = scmd;
1380 break;
1381 }
1382
1383 if (!stu_scmd)
1384 continue;
1385
Hannes Reinecke91921e02014-06-25 16:39:59 +02001386 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001387 sdev_printk(KERN_INFO, sdev,
1388 "%s: Sending START_UNIT\n",
1389 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 if (!scsi_eh_try_stu(stu_scmd)) {
1392 if (!scsi_device_online(sdev) ||
1393 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001394 list_for_each_entry_safe(scmd, next,
1395 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001396 if (scmd->device == sdev &&
1397 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 scsi_eh_finish_cmd(scmd, done_q);
1399 }
1400 }
1401 } else {
1402 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001403 sdev_printk(KERN_INFO, sdev,
1404 "%s: START_UNIT failed\n",
1405 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 }
1408
1409 return list_empty(work_q);
1410}
1411
1412
1413/**
1414 * scsi_eh_bus_device_reset - send bdr if needed
1415 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001416 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001417 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 *
1419 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001420 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * devices that are jammed or not - if we have multiple devices, it
1422 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001423 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1426 struct list_head *work_q,
1427 struct list_head *done_q)
1428{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001429 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 struct scsi_device *sdev;
1431 int rtn;
1432
1433 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001434 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001435 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001436 sdev_printk(KERN_INFO, sdev,
1437 "%s: skip BDR, past eh deadline\n",
1438 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001439 break;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 bdr_scmd = NULL;
1442 list_for_each_entry(scmd, work_q, eh_entry)
1443 if (scmd->device == sdev) {
1444 bdr_scmd = scmd;
1445 break;
1446 }
1447
1448 if (!bdr_scmd)
1449 continue;
1450
Hannes Reinecke91921e02014-06-25 16:39:59 +02001451 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001452 sdev_printk(KERN_INFO, sdev,
1453 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001455 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001457 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001459 list_for_each_entry_safe(scmd, next,
1460 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001461 if (scmd->device == sdev &&
1462 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 scsi_eh_finish_cmd(scmd,
1464 done_q);
1465 }
1466 }
1467 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001468 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001469 sdev_printk(KERN_INFO, sdev,
1470 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 }
1473
1474 return list_empty(work_q);
1475}
1476
1477/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001478 * scsi_eh_target_reset - send target reset if needed
1479 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001480 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001481 * @done_q: &list_head for processed commands.
1482 *
1483 * Notes:
1484 * Try a target reset.
1485 */
1486static int scsi_eh_target_reset(struct Scsi_Host *shost,
1487 struct list_head *work_q,
1488 struct list_head *done_q)
1489{
James Bottomley98db5192010-10-25 15:53:41 -05001490 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001491 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001492
James Bottomley98db5192010-10-25 15:53:41 -05001493 list_splice_init(work_q, &tmp_list);
1494
1495 while (!list_empty(&tmp_list)) {
1496 struct scsi_cmnd *next, *scmd;
1497 int rtn;
1498 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001499
Hannes Reineckeb4562022013-10-23 10:51:21 +02001500 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001501 /* push back on work queue for further processing */
1502 list_splice_init(&check_list, work_q);
1503 list_splice_init(&tmp_list, work_q);
1504 SCSI_LOG_ERROR_RECOVERY(3,
1505 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001506 "%s: Skip target reset, past eh deadline\n",
1507 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001508 return list_empty(work_q);
1509 }
James Bottomley98db5192010-10-25 15:53:41 -05001510
1511 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1512 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001513
Hannes Reinecke91921e02014-06-25 16:39:59 +02001514 SCSI_LOG_ERROR_RECOVERY(3,
1515 shost_printk(KERN_INFO, shost,
1516 "%s: Sending target reset to target %d\n",
1517 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001518 rtn = scsi_try_target_reset(scmd);
1519 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001520 SCSI_LOG_ERROR_RECOVERY(3,
1521 shost_printk(KERN_INFO, shost,
1522 "%s: Target reset failed"
1523 " target: %d\n",
1524 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001525 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1526 if (scmd_id(scmd) != id)
1527 continue;
1528
David Jeffery3eef6252011-05-19 14:41:12 -04001529 if (rtn == SUCCESS)
1530 list_move_tail(&scmd->eh_entry, &check_list);
1531 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001532 scsi_eh_finish_cmd(scmd, done_q);
1533 else
1534 /* push back on work queue for further processing */
1535 list_move(&scmd->eh_entry, work_q);
1536 }
1537 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001538
David Jeffery3eef6252011-05-19 14:41:12 -04001539 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001540}
1541
1542/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001543 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001544 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001545 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001546 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1549 struct list_head *work_q,
1550 struct list_head *done_q)
1551{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001552 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001553 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 unsigned int channel;
1555 int rtn;
1556
1557 /*
1558 * we really want to loop over the various channels, and do this on
1559 * a channel by channel basis. we should also check to see if any
1560 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001561 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 */
1563
1564 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001565 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001566 list_splice_init(&check_list, work_q);
1567 SCSI_LOG_ERROR_RECOVERY(3,
1568 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001569 "%s: skip BRST, past eh deadline\n",
1570 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001571 return list_empty(work_q);
1572 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 chan_scmd = NULL;
1575 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001576 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 chan_scmd = scmd;
1578 break;
1579 /*
1580 * FIXME add back in some support for
1581 * soft_reset devices.
1582 */
1583 }
1584 }
1585
1586 if (!chan_scmd)
1587 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001588 SCSI_LOG_ERROR_RECOVERY(3,
1589 shost_printk(KERN_INFO, shost,
1590 "%s: Sending BRST chan: %d\n",
1591 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001593 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001594 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001595 if (channel == scmd_channel(scmd)) {
1596 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 scsi_eh_finish_cmd(scmd,
1598 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001599 else
1600 list_move_tail(&scmd->eh_entry,
1601 &check_list);
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001605 SCSI_LOG_ERROR_RECOVERY(3,
1606 shost_printk(KERN_INFO, shost,
1607 "%s: BRST failed chan: %d\n",
1608 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 }
David Jeffery3eef6252011-05-19 14:41:12 -04001611 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
1614/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001615 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001616 * @shost: host to be reset.
1617 * @work_q: &list_head for pending commands.
1618 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001619 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001620static int scsi_eh_host_reset(struct Scsi_Host *shost,
1621 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct list_head *done_q)
1623{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001624 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001625 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 if (!list_empty(work_q)) {
1629 scmd = list_entry(work_q->next,
1630 struct scsi_cmnd, eh_entry);
1631
Hannes Reinecke91921e02014-06-25 16:39:59 +02001632 SCSI_LOG_ERROR_RECOVERY(3,
1633 shost_printk(KERN_INFO, shost,
1634 "%s: Sending HRST\n",
1635 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001638 if (rtn == SUCCESS) {
1639 list_splice_init(work_q, &check_list);
1640 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001641 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 scsi_eh_finish_cmd(scmd, done_q);
1643 }
1644 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001645 SCSI_LOG_ERROR_RECOVERY(3,
1646 shost_printk(KERN_INFO, shost,
1647 "%s: HRST failed\n",
1648 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650 }
David Jeffery3eef6252011-05-19 14:41:12 -04001651 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654/**
1655 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001656 * @work_q: &list_head for pending commands.
1657 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static void scsi_eh_offline_sdevs(struct list_head *work_q,
1660 struct list_head *done_q)
1661{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001662 struct scsi_cmnd *scmd, *next;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001663 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001665 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001666 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1667 "not ready after error recovery\n");
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001668 sdev = scmd->device;
1669
1670 mutex_lock(&sdev->state_mutex);
1671 scsi_device_set_state(sdev, SDEV_OFFLINE);
1672 mutex_unlock(&sdev->state_mutex);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 scsi_eh_finish_cmd(scmd, done_q);
1675 }
1676 return;
1677}
1678
1679/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001680 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001681 * @scmd: SCSI cmd to examine.
1682 */
1683int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1684{
1685 switch (host_byte(scmd->result)) {
1686 case DID_OK:
1687 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001688 case DID_TIME_OUT:
1689 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001690 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001691 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001692 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001693 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001694 case DID_ERROR:
1695 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1696 status_byte(scmd->result) == RESERVATION_CONFLICT)
1697 return 0;
1698 /* fall through */
1699 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001700 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001701 }
1702
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001703 if (status_byte(scmd->result) != CHECK_CONDITION)
1704 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001705
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001706check_type:
1707 /*
1708 * assume caller has checked sense and determined
1709 * the check condition was retryable.
1710 */
1711 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001712 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001713 return 1;
1714 else
1715 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001716}
1717
1718/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1720 * @scmd: SCSI cmd to examine.
1721 *
1722 * Notes:
1723 * This is *only* called when we are examining the status after sending
1724 * out the actual data command. any commands that are queued for error
1725 * recovery (e.g. test_unit_ready) do *not* come through here.
1726 *
1727 * When this routine returns failed, it means the error handler thread
1728 * is woken. In cases where the error code indicates an error that
1729 * doesn't require the error handler read (i.e. we don't need to
1730 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732int scsi_decide_disposition(struct scsi_cmnd *scmd)
1733{
1734 int rtn;
1735
1736 /*
1737 * if the device is offline, then we clearly just pass the result back
1738 * up to the top level.
1739 */
1740 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001741 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1742 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return SUCCESS;
1744 }
1745
1746 /*
1747 * first check the host byte, to see if there is anything in there
1748 * that would indicate what we need to do.
1749 */
1750 switch (host_byte(scmd->result)) {
1751 case DID_PASSTHROUGH:
1752 /*
1753 * no matter what, pass this through to the upper layer.
1754 * nuke this special code so that it looks like we are saying
1755 * did_ok.
1756 */
1757 scmd->result &= 0xff00ffff;
1758 return SUCCESS;
1759 case DID_OK:
1760 /*
1761 * looks good. drop through, and check the next byte.
1762 */
1763 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001764 case DID_ABORT:
1765 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001766 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001767 return SUCCESS;
1768 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001769 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 case DID_NO_CONNECT:
1771 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /*
1773 * note - this means that we just report the status back
1774 * to the top level driver, not that we actually think
1775 * that it indicates SUCCESS.
1776 */
1777 return SUCCESS;
Petros Koutoupisad950282017-10-30 16:38:10 -05001778 case DID_SOFT_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001781 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 * in order to avoid endless loops (db)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto maybe_retry;
1785 case DID_IMM_RETRY:
1786 return NEEDS_RETRY;
1787
bf341912005-04-12 17:49:09 -05001788 case DID_REQUEUE:
1789 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001790 case DID_TRANSPORT_DISRUPTED:
1791 /*
1792 * LLD/transport was disrupted during processing of the IO.
1793 * The transport class is now blocked/blocking,
1794 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001795 * based on its timers and recovery capablilities if
1796 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001797 */
Mike Christie939c22882008-11-04 19:47:19 -06001798 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001799 case DID_TRANSPORT_FAILFAST:
1800 /*
1801 * The transport decided to failfast the IO (most likely
1802 * the fast io fail tmo fired), so send IO directly upwards.
1803 */
1804 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 case DID_ERROR:
1806 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1807 status_byte(scmd->result) == RESERVATION_CONFLICT)
1808 /*
1809 * execute reservation conflict processing code
1810 * lower down
1811 */
1812 break;
1813 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 case DID_BUS_BUSY:
1815 case DID_PARITY:
1816 goto maybe_retry;
1817 case DID_TIME_OUT:
1818 /*
1819 * when we scan the bus, we get timeout messages for
1820 * these commands if there is no device available.
1821 * other hosts report did_no_connect for the same thing.
1822 */
1823 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1824 scmd->cmnd[0] == INQUIRY)) {
1825 return SUCCESS;
1826 } else {
1827 return FAILED;
1828 }
1829 case DID_RESET:
1830 return SUCCESS;
1831 default:
1832 return FAILED;
1833 }
1834
1835 /*
1836 * next, check the message byte.
1837 */
1838 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1839 return FAILED;
1840
1841 /*
1842 * check the status byte to see if this indicates anything special.
1843 */
1844 switch (status_byte(scmd->result)) {
1845 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001846 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * the case of trying to send too many commands to a
1849 * tagged queueing device.
1850 */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001851 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 case BUSY:
1853 /*
1854 * device can't talk to us at the moment. Should only
1855 * occur (SAM-3) when the task queue is empty, so will cause
1856 * the empty queue handling to trigger a stall in the
1857 * device.
1858 */
1859 return ADD_TO_MLQUEUE;
1860 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001861 if (scmd->cmnd[0] == REPORT_LUNS)
1862 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001863 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001864 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001867 case TASK_ABORTED:
1868 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 case CHECK_CONDITION:
1870 rtn = scsi_check_sense(scmd);
1871 if (rtn == NEEDS_RETRY)
1872 goto maybe_retry;
1873 /* if rtn == FAILED, we have no sense information;
1874 * returning FAILED will wake the error handler thread
1875 * to collect the sense and redo the decide
1876 * disposition */
1877 return rtn;
1878 case CONDITION_GOOD:
1879 case INTERMEDIATE_GOOD:
1880 case INTERMEDIATE_C_GOOD:
1881 case ACA_ACTIVE:
1882 /*
1883 * who knows? FIXME(eric)
1884 */
1885 return SUCCESS;
1886
1887 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001888 sdev_printk(KERN_INFO, scmd->device,
1889 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001890 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return SUCCESS; /* causes immediate i/o error */
1892 default:
1893 return FAILED;
1894 }
1895 return FAILED;
1896
1897 maybe_retry:
1898
1899 /* we requeue for retry because the error was retryable, and
1900 * the request was not marked fast fail. Note that above,
1901 * even if the request is marked fast fail, we still requeue
1902 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001903 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001904 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return NEEDS_RETRY;
1906 } else {
1907 /*
1908 * no more retries - report this one back to upper level.
1909 */
1910 return SUCCESS;
1911 }
1912}
1913
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001914static void eh_lock_door_done(struct request *req, blk_status_t status)
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001915{
1916 __blk_put_request(req->q, req);
1917}
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * scsi_eh_lock_door - Prevent medium removal for the specified device
1921 * @sdev: SCSI device to prevent medium removal
1922 *
1923 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001924 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 *
1926 * Notes:
1927 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1928 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930static void scsi_eh_lock_door(struct scsi_device *sdev)
1931{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001932 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001933 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
James Bottomley91bc31f2009-05-17 09:30:48 -05001935 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001936 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001937 * request becomes available
1938 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001939 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001940 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001941 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001942 rq = scsi_req(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001944 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1945 rq->cmd[1] = 0;
1946 rq->cmd[2] = 0;
1947 rq->cmd[3] = 0;
1948 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1949 rq->cmd[5] = 0;
1950 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001951
Christoph Hellwige8064022016-10-20 15:12:13 +02001952 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001953 req->timeout = 10 * HZ;
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001954 rq->retries = 5;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001955
1956 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/**
1960 * scsi_restart_operations - restart io operations to the specified host.
1961 * @shost: Host we are restarting.
1962 *
1963 * Notes:
1964 * When we entered the error handler, we blocked all further i/o to
1965 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967static void scsi_restart_operations(struct Scsi_Host *shost)
1968{
1969 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001970 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /*
1973 * If the door was locked, we need to insert a door lock request
1974 * onto the head of the SCSI request queue for the device. There
1975 * is no point trying to lock the door of an off-line device.
1976 */
1977 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001978 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001980 sdev->was_reset = 0;
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
1984 /*
1985 * next free up anything directly waiting upon the host. this
1986 * will be requests for character device operations, and also for
1987 * ioctls to queued block devices.
1988 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02001989 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02001990 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
James Bottomley939647e2005-09-18 15:05:20 -05001992 spin_lock_irqsave(shost->host_lock, flags);
1993 if (scsi_host_set_state(shost, SHOST_RUNNING))
1994 if (scsi_host_set_state(shost, SHOST_CANCEL))
1995 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1996 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 wake_up(&shost->host_wait);
1999
2000 /*
2001 * finally we need to re-initiate requests that may be pending. we will
2002 * have had everything blocked while error handling is taking place, and
2003 * now that error recovery is done, we will need to ensure that these
2004 * requests are started.
2005 */
2006 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002007
2008 /*
2009 * if eh is active and host_eh_scheduled is pending we need to re-run
2010 * recovery. we do this check after scsi_run_host_queues() to allow
2011 * everything pent up since the last eh run a chance to make forward
2012 * progress before we sync again. Either we'll immediately re-run
2013 * recovery or scsi_device_unbusy() will wake us again when these
2014 * pending commands complete.
2015 */
2016 spin_lock_irqsave(shost->host_lock, flags);
2017 if (shost->host_eh_scheduled)
2018 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2019 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2020 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023/**
2024 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002025 * @shost: host to be recovered.
2026 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002027 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002028 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002029void scsi_eh_ready_devs(struct Scsi_Host *shost,
2030 struct list_head *work_q,
2031 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 if (!scsi_eh_stu(shost, work_q, done_q))
2034 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002035 if (!scsi_eh_target_reset(shost, work_q, done_q))
2036 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002037 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002038 scsi_eh_offline_sdevs(work_q,
2039 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002041EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043/**
2044 * scsi_eh_flush_done_q - finish processed commands or retry them.
2045 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002046 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002047void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002049 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002051 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2052 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002054 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002055 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002056 SCSI_LOG_ERROR_RECOVERY(3,
2057 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002058 "%s: flush retry cmd\n",
2059 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2061 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002062 /*
2063 * If just we got sense for the device (called
2064 * scsi_eh_get_sense), scmd->result is already
2065 * set, do not set DRIVER_TIMEOUT.
2066 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!scmd->result)
2068 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002069 SCSI_LOG_ERROR_RECOVERY(3,
2070 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002071 "%s: flush finish cmd\n",
2072 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 scsi_finish_command(scmd);
2074 }
2075 }
2076}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002077EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079/**
2080 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2081 * @shost: Host to unjam.
2082 *
2083 * Notes:
2084 * When we come in here, we *know* that all commands on the bus have
2085 * either completed, failed or timed out. we also know that no further
2086 * commands are being sent to the host, so things are relatively quiet
2087 * and we have freedom to fiddle with things as we wish.
2088 *
2089 * This is only the *default* implementation. it is possible for
2090 * individual drivers to supply their own version of this function, and
2091 * if the maintainer wishes to do this, it is strongly suggested that
2092 * this function be taken as a template and modified. this function
2093 * was designed to correctly handle problems for about 95% of the
2094 * different cases out there, and it should always provide at least a
2095 * reasonable amount of error recovery.
2096 *
2097 * Any command marked 'failed' or 'timeout' must eventually have
2098 * scsi_finish_cmd() called for it. we do all of the retry stuff
2099 * here, so when we restart the host after we return it should have an
2100 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102static void scsi_unjam_host(struct Scsi_Host *shost)
2103{
2104 unsigned long flags;
2105 LIST_HEAD(eh_work_q);
2106 LIST_HEAD(eh_done_q);
2107
2108 spin_lock_irqsave(shost->host_lock, flags);
2109 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2110 spin_unlock_irqrestore(shost->host_lock, flags);
2111
2112 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2113
2114 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002115 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Hannes Reineckeb4562022013-10-23 10:51:21 +02002117 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002118 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002119 shost->last_reset = 0;
2120 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 scsi_eh_flush_done_q(&eh_done_q);
2122}
2123
2124/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002125 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 * @data: Host for which we are running.
2127 *
2128 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002129 * This is the main error handling loop. This is run as a kernel thread
2130 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132int scsi_error_handler(void *data)
2133{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002134 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002137 * We use TASK_INTERRUPTIBLE so that the thread is not
2138 * counted against the load average as a running process.
2139 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002140 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Michal Hocko537b6042015-08-27 20:16:37 +02002142 while (true) {
2143 /*
2144 * The sequence in kthread_stop() sets the stop flag first
2145 * then wakes the process. To avoid missed wakeups, the task
2146 * should always be in a non running state before the stop
2147 * flag is checked
2148 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002149 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002150 if (kthread_should_stop())
2151 break;
2152
Tejun Heoee7863b2006-05-15 20:57:20 +09002153 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002154 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002155 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002156 shost_printk(KERN_INFO, shost,
2157 "scsi_eh_%d: sleeping\n",
2158 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002159 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002160 continue;
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
James Bottomley3ed7a472005-09-19 09:50:04 -05002163 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002164 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002165 shost_printk(KERN_INFO, shost,
2166 "scsi_eh_%d: waking up %d/%d/%d\n",
2167 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002168 shost->host_failed,
2169 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /*
2172 * We have a host that is failing for some reason. Figure out
2173 * what we need to do to get it up and online again (if we can).
2174 * If we fail, we end up taking the thing offline.
2175 */
Lin Mingae0751f2011-12-05 09:20:24 +08002176 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002177 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002178 shost_printk(KERN_ERR, shost,
2179 "scsi_eh_%d: unable to autoresume\n",
2180 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002181 continue;
2182 }
2183
Christoph Hellwig9227c332006-04-01 19:21:04 +02002184 if (shost->transportt->eh_strategy_handler)
2185 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 else
2187 scsi_unjam_host(shost);
2188
Wei Fang72d8c362016-06-07 14:53:56 +08002189 /* All scmds have been handled */
2190 shost->host_failed = 0;
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /*
2193 * Note - if the above fails completely, the action is to take
2194 * individual devices offline and flush the queue of any
2195 * outstanding requests that may have been pending. When we
2196 * restart, we restart any I/O to any other devices on the bus
2197 * which are still online.
2198 */
2199 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002200 if (!shost->eh_noresume)
2201 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002203 __set_current_state(TASK_RUNNING);
2204
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002205 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002206 shost_printk(KERN_INFO, shost,
2207 "Error handler scsi_eh_%d exiting\n",
2208 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002209 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return 0;
2211}
2212
2213/*
2214 * Function: scsi_report_bus_reset()
2215 *
2216 * Purpose: Utility function used by low-level drivers to report that
2217 * they have observed a bus reset on the bus being handled.
2218 *
2219 * Arguments: shost - Host in question
2220 * channel - channel on which reset was observed.
2221 *
2222 * Returns: Nothing
2223 *
2224 * Lock status: Host lock must be held.
2225 *
2226 * Notes: This only needs to be called if the reset is one which
2227 * originates from an unknown location. Resets originated
2228 * by the mid-level itself don't need to call this, but there
2229 * should be no harm.
2230 *
2231 * The main purpose of this is to make sure that a CHECK_CONDITION
2232 * is properly treated.
2233 */
2234void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2235{
2236 struct scsi_device *sdev;
2237
2238 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002239 if (channel == sdev_channel(sdev))
2240 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242}
2243EXPORT_SYMBOL(scsi_report_bus_reset);
2244
2245/*
2246 * Function: scsi_report_device_reset()
2247 *
2248 * Purpose: Utility function used by low-level drivers to report that
2249 * they have observed a device reset on the device being handled.
2250 *
2251 * Arguments: shost - Host in question
2252 * channel - channel on which reset was observed
2253 * target - target on which reset was observed
2254 *
2255 * Returns: Nothing
2256 *
2257 * Lock status: Host lock must be held
2258 *
2259 * Notes: This only needs to be called if the reset is one which
2260 * originates from an unknown location. Resets originated
2261 * by the mid-level itself don't need to call this, but there
2262 * should be no harm.
2263 *
2264 * The main purpose of this is to make sure that a CHECK_CONDITION
2265 * is properly treated.
2266 */
2267void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2268{
2269 struct scsi_device *sdev;
2270
2271 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002272 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002273 target == sdev_id(sdev))
2274 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276}
2277EXPORT_SYMBOL(scsi_report_device_reset);
2278
2279static void
2280scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2281{
2282}
2283
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002284/**
2285 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2286 * @dev: scsi_device to operate on
2287 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
2289int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002290scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Alan Sternbc4f2402010-06-17 10:41:42 -04002292 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002293 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002294 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002295 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002296 int error = 0, rtn, val;
2297
2298 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2299 return -EACCES;
2300
2301 error = get_user(val, arg);
2302 if (error)
2303 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Alan Sternbc4f2402010-06-17 10:41:42 -04002305 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002306 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002307
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002308 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002309 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2310 shost->hostt->cmd_size, GFP_KERNEL);
2311 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002312 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002313 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002314
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002315 scmd = (struct scsi_cmnd *)(rq + 1);
2316 scsi_init_command(dev, scmd);
2317 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002318 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002321 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 scmd->cmd_len = 0;
2324
2325 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
James Smartd7a1bb02006-03-08 14:50:12 -05002327 spin_lock_irqsave(shost->host_lock, flags);
2328 shost->tmf_in_progress = 1;
2329 spin_unlock_irqrestore(shost->host_lock, flags);
2330
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002331 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2332 case SG_SCSI_RESET_NOTHING:
2333 rtn = SUCCESS;
2334 break;
2335 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002337 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 break;
2339 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002340 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002341 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002342 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002343 break;
2344 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002345 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002347 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 break;
2349 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002350 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002352 if (rtn == SUCCESS)
2353 break;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002354 /* FALLTHROUGH */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07002355 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002360 error = (rtn == SUCCESS) ? 0 : -EIO;
2361
James Smartd7a1bb02006-03-08 14:50:12 -05002362 spin_lock_irqsave(shost->host_lock, flags);
2363 shost->tmf_in_progress = 0;
2364 spin_unlock_irqrestore(shost->host_lock, flags);
2365
2366 /*
2367 * be sure to wake up anyone who was sleeping or had their queue
2368 * suspended while we performed the TMF.
2369 */
2370 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002371 shost_printk(KERN_INFO, shost,
2372 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002373
2374 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002375 scsi_run_host_queues(shost);
2376
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002377 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002378 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002379
Christoph Hellwig04796332014-02-20 14:20:55 -08002380out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002381 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002382 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002384EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002386bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2387 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002390 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392EXPORT_SYMBOL(scsi_command_normalize_sense);
2393
2394/**
Rob Landleyeb448202007-11-03 13:30:39 -05002395 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 * @sense_buffer: byte array of sense data
2397 * @sb_len: number of valid bytes in sense_buffer
2398 * @info_out: pointer to 64 integer where 8 or 4 byte information
2399 * field will be placed if found.
2400 *
2401 * Return value:
Damien Le Moal29087692017-04-24 16:51:12 +09002402 * true if information field found, false if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002403 */
Damien Le Moal29087692017-04-24 16:51:12 +09002404bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2405 u64 *info_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 const u8 * ucp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 if (sb_len < 7)
Damien Le Moal29087692017-04-24 16:51:12 +09002410 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 switch (sense_buffer[0] & 0x7f) {
2412 case 0x70:
2413 case 0x71:
2414 if (sense_buffer[0] & 0x80) {
Damien Le Moal29087692017-04-24 16:51:12 +09002415 *info_out = get_unaligned_be32(&sense_buffer[3]);
2416 return true;
2417 }
2418 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 case 0x72:
2420 case 0x73:
2421 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2422 0 /* info desc */);
2423 if (ucp && (0xa == ucp[1])) {
Damien Le Moal29087692017-04-24 16:51:12 +09002424 *info_out = get_unaligned_be64(&ucp[4]);
2425 return true;
2426 }
2427 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 default:
Damien Le Moal29087692017-04-24 16:51:12 +09002429 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431}
2432EXPORT_SYMBOL(scsi_get_sense_info_fld);