blob: ca53a5f785ee239cee9b7de7c10edf75b01ff2d8 [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.
120 */
121void
122scmd_eh_abort_handler(struct work_struct *work)
123{
124 struct scsi_cmnd *scmd =
125 container_of(work, struct scsi_cmnd, abort_work.work);
126 struct scsi_device *sdev = scmd->device;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100127 int rtn;
128
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100129 if (scsi_host_eh_past_deadline(sdev->host)) {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100130 SCSI_LOG_ERROR_RECOVERY(3,
131 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100132 "eh timeout, not aborting\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100133 } else {
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100134 SCSI_LOG_ERROR_RECOVERY(3,
135 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100136 "aborting command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100137 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
138 if (rtn == SUCCESS) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +0200139 set_host_byte(scmd, DID_TIME_OUT);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100140 if (scsi_host_eh_past_deadline(sdev->host)) {
141 SCSI_LOG_ERROR_RECOVERY(3,
142 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100143 "eh timeout, not retrying "
144 "aborted command\n"));
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100145 } else if (!scsi_noretry_cmd(scmd) &&
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100146 (++scmd->retries <= scmd->allowed)) {
147 SCSI_LOG_ERROR_RECOVERY(3,
148 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100149 "retry aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100150 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100151 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100152 } else {
153 SCSI_LOG_ERROR_RECOVERY(3,
154 scmd_printk(KERN_WARNING, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100155 "finish aborted command\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100156 scsi_finish_command(scmd);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100157 return;
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100158 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100159 } else {
160 SCSI_LOG_ERROR_RECOVERY(3,
161 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100162 "cmd abort %s\n",
Hannes Reinecke883a0302014-10-24 14:27:04 +0200163 (rtn == FAST_IO_FAIL) ?
164 "not send" : "failed"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100165 }
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100166 }
167
Hannes Reineckea0658632017-04-06 15:36:35 +0200168 scsi_eh_scmd_add(scmd);
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100169}
170
171/**
172 * scsi_abort_command - schedule a command abort
173 * @scmd: scmd to abort.
174 *
175 * We only need to abort commands after a command timeout
176 */
177static int
178scsi_abort_command(struct scsi_cmnd *scmd)
179{
180 struct scsi_device *sdev = scmd->device;
181 struct Scsi_Host *shost = sdev->host;
182 unsigned long flags;
183
184 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
185 /*
186 * Retry after abort failed, escalate to next level.
187 */
188 SCSI_LOG_ERROR_RECOVERY(3,
189 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100190 "previous abort failed\n"));
Bart Van Asschefcc95a72014-06-02 11:50:52 +0200191 BUG_ON(delayed_work_pending(&scmd->abort_work));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100192 return FAILED;
193 }
194
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100195 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100196 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100197 shost->last_reset = jiffies;
198 spin_unlock_irqrestore(shost->host_lock, flags);
199
200 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
201 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100202 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
Hannes Reineckee494f6a2013-11-11 13:44:54 +0100203 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
204 return SUCCESS;
205}
206
207/**
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200208 * scsi_eh_reset - call into ->eh_action to reset internal counters
209 * @scmd: scmd to run eh on.
210 *
211 * The scsi driver might be carrying internal state about the
212 * devices, so we need to call into the driver to reset the
213 * internal state once the error handler is started.
214 */
215static void scsi_eh_reset(struct scsi_cmnd *scmd)
216{
217 if (!blk_rq_is_passthrough(scmd->request)) {
218 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
219 if (sdrv->eh_reset)
220 sdrv->eh_reset(scmd);
221 }
222}
223
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800224static void scsi_eh_inc_host_failed(struct rcu_head *head)
225{
Bart Van Assche3be88282018-02-22 11:30:20 -0800226 struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
227 struct Scsi_Host *shost = scmd->device->host;
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800228 unsigned long flags;
229
230 spin_lock_irqsave(shost->host_lock, flags);
231 shost->host_failed++;
232 scsi_eh_wakeup(shost);
233 spin_unlock_irqrestore(shost->host_lock, flags);
234}
235
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200236/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * scsi_eh_scmd_add - add scsi cmd to error handling.
238 * @scmd: scmd to run eh on.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800239 */
Hannes Reineckea0658632017-04-06 15:36:35 +0200240void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct Scsi_Host *shost = scmd->device->host;
243 unsigned long flags;
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200244 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200246 WARN_ON_ONCE(!shost->ehandler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 spin_lock_irqsave(shost->host_lock, flags);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200249 if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
250 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
251 WARN_ON_ONCE(ret);
252 }
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100253 if (shost->eh_deadline != -1 && !shost->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200254 shost->last_reset = jiffies;
255
Hannes Reinecke7a38dc02017-04-06 15:36:29 +0200256 scsi_eh_reset(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_unlock_irqrestore(shost->host_lock, flags);
Bart Van Assche3bd6f432017-12-04 10:06:23 -0800259 /*
260 * Ensure that all tasks observe the host state change before the
261 * host_failed change.
262 */
Bart Van Assche3be88282018-02-22 11:30:20 -0800263 call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * scsi_times_out - Timeout function for normal scsi commands.
Jens Axboe242f9dc2008-09-14 05:55:09 -0700268 * @req: request that is timing out.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
270 * Notes:
271 * We do not need to lock this. There is the potential for a race
272 * only in that the normal completion handling might run, but if the
273 * normal completion function determines that the timer has already
274 * fired, then it mustn't do anything.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800275 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700276enum blk_eh_timer_return scsi_times_out(struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Bart Van Asschebed22132017-08-25 13:46:32 -0700278 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700279 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100280 struct Scsi_Host *host = scmd->device->host;
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500281
Kei Tokunagabf816232010-04-01 20:41:40 +0900282 trace_scsi_dispatch_cmd_timeout(scmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 scsi_log_completion(scmd, TIMEOUT_ERROR);
284
Ren Mingxinbb3b6212013-11-11 13:44:56 +0100285 if (host->eh_deadline != -1 && !host->last_reset)
Hannes Reineckeb4562022013-10-23 10:51:21 +0200286 host->last_reset = jiffies;
287
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100288 if (host->hostt->eh_timed_out)
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100289 rtn = host->hostt->eh_timed_out(scmd);
James Bottomley6c5f8ce2007-03-16 17:44:41 -0500290
Hannes Reineckea33c0702014-06-13 14:01:45 +0200291 if (rtn == BLK_EH_NOT_HANDLED) {
Hannes Reineckea0658632017-04-06 15:36:35 +0200292 if (scsi_abort_command(scmd) != SUCCESS) {
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200293 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckea0658632017-04-06 15:36:35 +0200294 scsi_eh_scmd_add(scmd);
Hannes Reinecke2171b6d2017-04-06 15:36:34 +0200295 }
Hannes Reineckea33c0702014-06-13 14:01:45 +0200296 }
Jens Axboe242f9dc2008-09-14 05:55:09 -0700297
Christoph Hellwigfa990782008-11-05 15:03:15 +0100298 return rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/**
302 * scsi_block_when_processing_errors - Prevent cmds from being queued.
303 * @sdev: Device on which we are performing recovery.
304 *
305 * Description:
306 * We block until the host is out of error recovery, and then check to
307 * see whether the host or the device is offline.
308 *
309 * Return value:
310 * 0 when dev was taken offline by error recovery. 1 OK to proceed.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312int scsi_block_when_processing_errors(struct scsi_device *sdev)
313{
314 int online;
315
James Bottomley939647e2005-09-18 15:05:20 -0500316 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 online = scsi_device_online(sdev);
319
Hannes Reinecke91921e02014-06-25 16:39:59 +0200320 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
321 "%s: rtn: %d\n", __func__, online));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 return online;
324}
325EXPORT_SYMBOL(scsi_block_when_processing_errors);
326
327#ifdef CONFIG_SCSI_LOGGING
328/**
329 * scsi_eh_prt_fail_stats - Log info on failures.
330 * @shost: scsi host being recovered.
331 * @work_q: Queue of scsi cmds to process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
334 struct list_head *work_q)
335{
336 struct scsi_cmnd *scmd;
337 struct scsi_device *sdev;
338 int total_failures = 0;
339 int cmd_failed = 0;
340 int cmd_cancel = 0;
341 int devices_failed = 0;
342
343 shost_for_each_device(sdev, shost) {
344 list_for_each_entry(scmd, work_q, eh_entry) {
345 if (scmd->device == sdev) {
346 ++total_failures;
Hannes Reineckea0658632017-04-06 15:36:35 +0200347 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 ++cmd_cancel;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100349 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ++cmd_failed;
351 }
352 }
353
354 if (cmd_cancel || cmd_failed) {
355 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea3a790d2014-10-24 14:27:03 +0200356 shost_printk(KERN_INFO, shost,
James Bottomley9ccfc752005-10-02 11:45:08 -0500357 "%s: cmds failed: %d, cancel: %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700358 __func__, cmd_failed,
James Bottomley9ccfc752005-10-02 11:45:08 -0500359 cmd_cancel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 cmd_cancel = 0;
361 cmd_failed = 0;
362 ++devices_failed;
363 }
364 }
365
Hannes Reinecke91921e02014-06-25 16:39:59 +0200366 SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
367 "Total of %d commands on %d"
368 " devices require eh work\n",
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100369 total_failures, devices_failed));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371#endif
372
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400373 /**
374 * scsi_report_lun_change - Set flag on all *other* devices on the same target
375 * to indicate that a UNIT ATTENTION is expected.
376 * @sdev: Device reporting the UNIT ATTENTION
377 */
378static void scsi_report_lun_change(struct scsi_device *sdev)
379{
380 sdev->sdev_target->expecting_lun_change = 1;
381}
382
383/**
384 * scsi_report_sense - Examine scsi sense information and log messages for
385 * certain conditions, also issue uevents for some of them.
386 * @sdev: Device reporting the sense code
387 * @sshdr: sshdr to be examined
388 */
389static void scsi_report_sense(struct scsi_device *sdev,
390 struct scsi_sense_hdr *sshdr)
391{
392 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
393
394 if (sshdr->sense_key == UNIT_ATTENTION) {
395 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
396 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
397 sdev_printk(KERN_WARNING, sdev,
398 "Inquiry data has changed");
399 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
400 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
401 scsi_report_lun_change(sdev);
402 sdev_printk(KERN_WARNING, sdev,
403 "Warning! Received an indication that the "
404 "LUN assignments on this target have "
405 "changed. The Linux SCSI layer does not "
406 "automatically remap LUN assignments.\n");
407 } else if (sshdr->asc == 0x3f)
408 sdev_printk(KERN_WARNING, sdev,
409 "Warning! Received an indication that the "
410 "operating parameters on this target have "
411 "changed. The Linux SCSI layer does not "
412 "automatically adjust these parameters.\n");
413
414 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
415 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
416 sdev_printk(KERN_WARNING, sdev,
417 "Warning! Received an indication that the "
418 "LUN reached a thin provisioning soft "
419 "threshold.\n");
420 }
421
Hannes Reineckecf3431b2017-10-17 09:11:24 +0200422 if (sshdr->asc == 0x29) {
423 evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
424 sdev_printk(KERN_WARNING, sdev,
425 "Power-on or device reset occurred\n");
426 }
427
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400428 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
429 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
430 sdev_printk(KERN_WARNING, sdev,
431 "Mode parameters changed");
Hannes Reinecke14c3e672015-07-06 13:41:53 +0200432 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
433 evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
434 sdev_printk(KERN_WARNING, sdev,
435 "Asymmetric access state changed");
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400436 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
437 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
438 sdev_printk(KERN_WARNING, sdev,
439 "Capacity data has changed");
440 } else if (sshdr->asc == 0x2a)
441 sdev_printk(KERN_WARNING, sdev,
442 "Parameters changed");
443 }
444
445 if (evt_type != SDEV_EVT_MAXBITS) {
446 set_bit(evt_type, sdev->pending_events);
447 schedule_work(&sdev->event_work);
448 }
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * scsi_check_sense - Examine scsi cmd sense
453 * @scmd: Cmd to have sense checked.
454 *
455 * Return value:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200456 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
458 * Notes:
459 * When a deferred error is detected the current command has
460 * not been executed and needs retrying.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800461 */
Hannes Reinecke3852e372016-04-04 11:44:01 +0200462int scsi_check_sense(struct scsi_cmnd *scmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700464 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct scsi_sense_hdr sshdr;
466
467 if (! scsi_command_normalize_sense(scmd, &sshdr))
468 return FAILED; /* no valid sense data */
469
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400470 scsi_report_sense(sdev, &sshdr);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (scsi_sense_is_deferred(&sshdr))
473 return NEEDS_RETRY;
474
Christoph Hellwigee14c672015-08-27 14:16:59 +0200475 if (sdev->handler && sdev->handler->check_sense) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700476 int rc;
477
Christoph Hellwigee14c672015-08-27 14:16:59 +0200478 rc = sdev->handler->check_sense(sdev, &sshdr);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700479 if (rc != SCSI_RETURN_NOT_HANDLED)
480 return rc;
481 /* handler does not care. Drop down to default handling */
482 }
483
Christoph Hellwige925cc42014-11-06 15:11:22 -0600484 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
485 /*
486 * nasty: for mid-layer issued TURs, we need to return the
487 * actual sense data without any recovery attempt. For eh
488 * issued ones, we need to try to recover and interpret
489 */
490 return SUCCESS;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Previous logic looked for FILEMARK, EOM or ILI which are
494 * mainly associated with tapes and returned SUCCESS.
495 */
496 if (sshdr.response_code == 0x70) {
497 /* fixed format */
498 if (scmd->sense_buffer[2] & 0xe0)
499 return SUCCESS;
500 } else {
501 /*
502 * descriptor format: look for "stream commands sense data
503 * descriptor" (see SSC-3). Assume single sense data
504 * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
505 */
506 if ((sshdr.additional_length > 3) &&
507 (scmd->sense_buffer[8] == 0x4) &&
508 (scmd->sense_buffer[11] & 0xe0))
509 return SUCCESS;
510 }
511
512 switch (sshdr.sense_key) {
513 case NO_SENSE:
514 return SUCCESS;
515 case RECOVERED_ERROR:
516 return /* soft_error */ SUCCESS;
517
518 case ABORTED_COMMAND:
Martin K. Petersen511e44f2008-07-17 04:28:33 -0400519 if (sshdr.asc == 0x10) /* DIF */
520 return SUCCESS;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return NEEDS_RETRY;
523 case NOT_READY:
524 case UNIT_ATTENTION:
525 /*
526 * if we are expecting a cc/ua because of a bus reset that we
527 * performed, treat this just as a retry. otherwise this is
528 * information that we should pass up to the upper-level driver
529 * so that we can deal with it there.
530 */
531 if (scmd->device->expecting_cc_ua) {
TARUISI Hiroakidfcf7772011-08-11 20:25:20 +0900532 /*
533 * Because some device does not queue unit
534 * attentions correctly, we carefully check
535 * additional sense code and qualifier so as
536 * not to squash media change unit attention.
537 */
538 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
539 scmd->device->expecting_cc_ua = 0;
540 return NEEDS_RETRY;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 /*
Ewan D. Milne279afdf2013-08-08 15:07:48 -0400544 * we might also expect a cc/ua if another LUN on the target
545 * reported a UA with an ASC/ASCQ of 3F 0E -
546 * REPORTED LUNS DATA HAS CHANGED.
547 */
548 if (scmd->device->sdev_target->expecting_lun_change &&
549 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
550 return NEEDS_RETRY;
551 /*
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100552 * if the device is in the process of becoming ready, we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * should retry.
554 */
555 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
556 return NEEDS_RETRY;
557 /*
558 * if the device is not started, we need to wake
559 * the error handler to start the motor
560 */
561 if (scmd->device->allow_restart &&
562 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
563 return FAILED;
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100564 /*
565 * Pass the UA upwards for a determination in the completion
566 * functions.
567 */
568 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100570 /* these are not supported */
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +0200571 case DATA_PROTECT:
572 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
573 /* Thin provisioning hard threshold reached */
574 set_host_byte(scmd, DID_ALLOC_FAILURE);
575 return SUCCESS;
576 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700577 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 case COPY_ABORTED:
579 case VOLUME_OVERFLOW:
580 case MISCOMPARE:
Hannes Reinecke63583cc2011-01-18 10:13:11 +0100581 case BLANK_CHECK:
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200582 set_host_byte(scmd, DID_TARGET_FAILURE);
583 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 case MEDIUM_ERROR:
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800586 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
587 sshdr.asc == 0x13 || /* AMNF DATA FIELD */
588 sshdr.asc == 0x14) { /* RECORD NOT FOUND */
Hannes Reinecke7e782af2013-07-01 15:16:26 +0200589 set_host_byte(scmd, DID_MEDIUM_ERROR);
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200590 return SUCCESS;
Luben Tuikovfd1b4942006-11-29 19:45:23 -0800591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return NEEDS_RETRY;
593
594 case HARDWARE_ERROR:
595 if (scmd->device->retry_hwerror)
Mike Andersonbb0003c2008-08-12 12:11:58 -0700596 return ADD_TO_MLQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 else
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200598 set_host_byte(scmd, DID_TARGET_FAILURE);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700599 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 case ILLEGAL_REQUEST:
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500602 if (sshdr.asc == 0x20 || /* Invalid command operation code */
603 sshdr.asc == 0x21 || /* Logical block address out of range */
Hannes Reineckea8bbb2a2017-10-17 09:10:56 +0200604 sshdr.asc == 0x22 || /* Invalid function */
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500605 sshdr.asc == 0x24 || /* Invalid field in cdb */
Martin Wilckd0b7a902017-09-27 14:44:19 +0200606 sshdr.asc == 0x26 || /* Parameter value invalid */
607 sshdr.asc == 0x27) { /* Write protected */
Hannes Reinecke87f14e62013-07-01 15:16:24 +0200608 set_host_byte(scmd, DID_TARGET_FAILURE);
Mike Snitzer47ac56d2012-02-13 18:35:11 -0500609 }
610 return SUCCESS;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 default:
613 return SUCCESS;
614 }
615}
Hannes Reinecke3852e372016-04-04 11:44:01 +0200616EXPORT_SYMBOL_GPL(scsi_check_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Vasu Dev4a840672009-10-22 15:46:33 -0700618static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
619{
620 struct scsi_host_template *sht = sdev->host->hostt;
621 struct scsi_device *tmp_sdev;
622
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100623 if (!sht->track_queue_depth ||
Vasu Dev4a840672009-10-22 15:46:33 -0700624 sdev->queue_depth >= sdev->max_queue_depth)
625 return;
626
627 if (time_before(jiffies,
628 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
629 return;
630
631 if (time_before(jiffies,
632 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
633 return;
634
635 /*
636 * Walk all devices of a target and do
637 * ramp up on them.
638 */
639 shost_for_each_device(tmp_sdev, sdev->host) {
640 if (tmp_sdev->channel != sdev->channel ||
641 tmp_sdev->id != sdev->id ||
642 tmp_sdev->queue_depth == sdev->max_queue_depth)
643 continue;
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100644
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100645 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
Vasu Dev4a840672009-10-22 15:46:33 -0700646 sdev->last_queue_ramp_up = jiffies;
647 }
648}
649
Mike Christie42a6a912009-10-15 17:46:44 -0700650static void scsi_handle_queue_full(struct scsi_device *sdev)
651{
652 struct scsi_host_template *sht = sdev->host->hostt;
653 struct scsi_device *tmp_sdev;
654
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100655 if (!sht->track_queue_depth)
Mike Christie42a6a912009-10-15 17:46:44 -0700656 return;
657
658 shost_for_each_device(tmp_sdev, sdev->host) {
659 if (tmp_sdev->channel != sdev->channel ||
660 tmp_sdev->id != sdev->id)
661 continue;
662 /*
663 * We do not know the number of commands that were at
664 * the device when we got the queue full so we start
665 * from the highest possible value and work our way down.
666 */
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100667 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
Mike Christie42a6a912009-10-15 17:46:44 -0700668 }
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/**
672 * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
673 * @scmd: SCSI cmd to examine.
674 *
675 * Notes:
676 * This is *only* called when we are examining the status of commands
677 * queued during error recovery. the main difference here is that we
678 * don't allow for the possibility of retries here, and we are a lot
679 * more restrictive about what we consider acceptable.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800680 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
682{
683 /*
684 * first check the host byte, to see if there is anything in there
685 * that would indicate what we need to do.
686 */
687 if (host_byte(scmd->result) == DID_RESET) {
688 /*
689 * rats. we are already in the error handler, so we now
690 * get to try and figure out what to do next. if the sense
691 * is valid, we have a pretty good idea of what to do.
692 * if not, we mark it as FAILED.
693 */
694 return scsi_check_sense(scmd);
695 }
696 if (host_byte(scmd->result) != DID_OK)
697 return FAILED;
698
699 /*
700 * next, check the message byte.
701 */
702 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
703 return FAILED;
704
705 /*
706 * now, check the status byte to see if this indicates
707 * anything special.
708 */
709 switch (status_byte(scmd->result)) {
710 case GOOD:
Vasu Dev4a840672009-10-22 15:46:33 -0700711 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -0700712 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 case COMMAND_TERMINATED:
714 return SUCCESS;
715 case CHECK_CONDITION:
716 return scsi_check_sense(scmd);
717 case CONDITION_GOOD:
718 case INTERMEDIATE_GOOD:
719 case INTERMEDIATE_C_GOOD:
720 /*
721 * who knows? FIXME(eric)
722 */
723 return SUCCESS;
Michael Reed5f91bb02009-08-10 11:59:28 -0500724 case RESERVATION_CONFLICT:
James Bottomley67110df2010-08-06 15:17:24 -0500725 if (scmd->cmnd[0] == TEST_UNIT_READY)
726 /* it is a success, we probed the device and
727 * found it */
728 return SUCCESS;
729 /* otherwise, we failed to send the command */
730 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -0700732 scsi_handle_queue_full(scmd->device);
733 /* fall through */
734 case BUSY:
Hannes Reinecke3eb3a922010-07-29 10:10:16 +0200735 return NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 default:
737 return FAILED;
738 }
739 return FAILED;
740}
741
742/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * scsi_eh_done - Completion function for error handling.
744 * @scmd: Cmd that is done.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static void scsi_eh_done(struct scsi_cmnd *scmd)
747{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100748 struct completion *eh_action;
Michael Reed85631672005-12-07 21:46:27 -0600749
Hannes Reinecke91921e02014-06-25 16:39:59 +0200750 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +0100751 "%s result: %x\n", __func__, scmd->result));
Michael Reed85631672005-12-07 21:46:27 -0600752
753 eh_action = scmd->device->host->eh_action;
754 if (eh_action)
755 complete(eh_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/**
Brian King292148f2007-01-30 17:51:17 -0600759 * scsi_try_host_reset - ask host adapter to reset itself
Geert Uytterhoevenc2b3ebd2013-05-17 13:22:29 +0200760 * @scmd: SCSI cmd to send host reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800761 */
Brian King292148f2007-01-30 17:51:17 -0600762static int scsi_try_host_reset(struct scsi_cmnd *scmd)
763{
764 unsigned long flags;
765 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100766 struct Scsi_Host *host = scmd->device->host;
767 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600768
Hannes Reinecke91921e02014-06-25 16:39:59 +0200769 SCSI_LOG_ERROR_RECOVERY(3,
770 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
Brian King292148f2007-01-30 17:51:17 -0600771
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100772 if (!hostt->eh_host_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600773 return FAILED;
774
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100775 rtn = hostt->eh_host_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600776
777 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100778 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600779 ssleep(HOST_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100780 spin_lock_irqsave(host->host_lock, flags);
781 scsi_report_bus_reset(host, scmd_channel(scmd));
782 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600783 }
784
785 return rtn;
786}
787
788/**
789 * scsi_try_bus_reset - ask host to perform a bus reset
790 * @scmd: SCSI cmd to send bus reset.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800791 */
Brian King292148f2007-01-30 17:51:17 -0600792static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
793{
794 unsigned long flags;
795 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100796 struct Scsi_Host *host = scmd->device->host;
797 struct scsi_host_template *hostt = host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600798
Hannes Reinecke91921e02014-06-25 16:39:59 +0200799 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
800 "%s: Snd Bus RST\n", __func__));
Brian King292148f2007-01-30 17:51:17 -0600801
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100802 if (!hostt->eh_bus_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600803 return FAILED;
804
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100805 rtn = hostt->eh_bus_reset_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600806
807 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100808 if (!hostt->skip_settle_delay)
Brian King292148f2007-01-30 17:51:17 -0600809 ssleep(BUS_RESET_SETTLE_TIME);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100810 spin_lock_irqsave(host->host_lock, flags);
811 scsi_report_bus_reset(host, scmd_channel(scmd));
812 spin_unlock_irqrestore(host->host_lock, flags);
Brian King292148f2007-01-30 17:51:17 -0600813 }
814
815 return rtn;
816}
817
Mike Christie30bd7df2008-02-29 18:25:19 -0600818static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
819{
820 sdev->was_reset = 1;
821 sdev->expecting_cc_ua = 1;
822}
823
824/**
825 * scsi_try_target_reset - Ask host to perform a target reset
826 * @scmd: SCSI cmd used to send a target reset
827 *
828 * Notes:
829 * There is no timeout for this operation. if this operation is
830 * unreliable for a given host, then the host itself needs to put a
831 * timer on it, and set the host back to a consistent state prior to
832 * returning.
833 */
834static int scsi_try_target_reset(struct scsi_cmnd *scmd)
835{
836 unsigned long flags;
837 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100838 struct Scsi_Host *host = scmd->device->host;
839 struct scsi_host_template *hostt = host->hostt;
Mike Christie30bd7df2008-02-29 18:25:19 -0600840
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100841 if (!hostt->eh_target_reset_handler)
Mike Christie30bd7df2008-02-29 18:25:19 -0600842 return FAILED;
843
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100844 rtn = hostt->eh_target_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600845 if (rtn == SUCCESS) {
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100846 spin_lock_irqsave(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600847 __starget_for_each_device(scsi_target(scmd->device), NULL,
848 __scsi_report_device_reset);
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100849 spin_unlock_irqrestore(host->host_lock, flags);
Mike Christie30bd7df2008-02-29 18:25:19 -0600850 }
851
852 return rtn;
853}
854
Brian King292148f2007-01-30 17:51:17 -0600855/**
856 * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
857 * @scmd: SCSI cmd used to send BDR
858 *
859 * Notes:
860 * There is no timeout for this operation. if this operation is
861 * unreliable for a given host, then the host itself needs to put a
862 * timer on it, and set the host back to a consistent state prior to
863 * returning.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800864 */
Brian King292148f2007-01-30 17:51:17 -0600865static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
866{
867 int rtn;
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100868 struct scsi_host_template *hostt = scmd->device->host->hostt;
Brian King292148f2007-01-30 17:51:17 -0600869
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100870 if (!hostt->eh_device_reset_handler)
Brian King292148f2007-01-30 17:51:17 -0600871 return FAILED;
872
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100873 rtn = hostt->eh_device_reset_handler(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -0600874 if (rtn == SUCCESS)
875 __scsi_report_device_reset(scmd->device, NULL);
Brian King292148f2007-01-30 17:51:17 -0600876 return rtn;
877}
878
Hannes Reinecke883a0302014-10-24 14:27:04 +0200879/**
880 * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
Randy Dunlap6583f6f2014-12-29 09:40:56 -0800881 * @hostt: SCSI driver host template
Hannes Reinecke883a0302014-10-24 14:27:04 +0200882 * @scmd: SCSI cmd used to send a target reset
883 *
884 * Return value:
885 * SUCCESS, FAILED, or FAST_IO_FAIL
886 *
887 * Notes:
888 * SUCCESS does not necessarily indicate that the command
889 * has been aborted; it only indicates that the LLDDs
890 * has cleared all references to that command.
891 * LLDDs should return FAILED only if an abort was required
892 * but could not be executed. LLDDs should return FAST_IO_FAIL
893 * if the device is temporarily unavailable (eg due to a
894 * link down on FibreChannel)
895 */
896static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
897 struct scsi_cmnd *scmd)
Brian King292148f2007-01-30 17:51:17 -0600898{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100899 if (!hostt->eh_abort_handler)
Brian King292148f2007-01-30 17:51:17 -0600900 return FAILED;
901
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100902 return hostt->eh_abort_handler(scmd);
Brian King292148f2007-01-30 17:51:17 -0600903}
904
Brian King292148f2007-01-30 17:51:17 -0600905static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
906{
Jesper Juhl0bf8c862011-03-21 20:47:31 +0100907 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
Brian King292148f2007-01-30 17:51:17 -0600908 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
Mike Christie30bd7df2008-02-29 18:25:19 -0600909 if (scsi_try_target_reset(scmd) != SUCCESS)
910 if (scsi_try_bus_reset(scmd) != SUCCESS)
911 scsi_try_host_reset(scmd);
Brian King292148f2007-01-30 17:51:17 -0600912}
913
914/**
Santosh Y3b729f72012-04-08 18:47:09 +0530915 * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100916 * @scmd: SCSI command structure to hijack
Boaz Harroshe1c23462007-10-08 16:36:45 +0200917 * @ses: structure to save restore information
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200918 * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300919 * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200920 * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
Christoph Hellwig2dc611d2006-11-04 20:04:21 +0100921 *
Boaz Harroshe1c23462007-10-08 16:36:45 +0200922 * This function is used to save a scsi command information before re-execution
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200923 * as part of the error recovery process. If @sense_bytes is 0 the command
924 * sent must be one that does not transfer any data. If @sense_bytes != 0
925 * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
926 * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800927 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200928void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
929 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
f59114b2005-04-17 15:00:23 -0500931 struct scsi_device *sdev = scmd->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Christoph Hellwig631c2282006-07-08 20:42:15 +0200933 /*
934 * We need saved copies of a number of fields - this is because
935 * error handling may need to overwrite these with different values
936 * to run different commands, and once error handling is complete,
937 * we will need to restore these values prior to running the actual
938 * command.
939 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200940 ses->cmd_len = scmd->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300941 ses->cmnd = scmd->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200942 ses->data_direction = scmd->sc_data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200943 ses->sdb = scmd->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200944 ses->next_rq = scmd->request->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +0200945 ses->result = scmd->result;
Alan Stern12265702008-07-21 10:25:52 -0400946 ses->underflow = scmd->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400947 ses->prot_op = scmd->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +0200948 ses->eh_eflags = scmd->eh_eflags;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200949
Martin K. Petersendb007fc2008-07-17 04:28:31 -0400950 scmd->prot_op = SCSI_PROT_NORMAL;
James Bottomleyc69e6f82014-04-10 13:36:11 -0700951 scmd->eh_eflags = 0;
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300952 scmd->cmnd = ses->eh_cmnd;
953 memset(scmd->cmnd, 0, BLK_MAX_CDB);
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200954 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +0200955 scmd->request->next_rq = NULL;
Alan Stern644373a2014-03-28 10:51:15 -0700956 scmd->result = 0;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200957
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200958 if (sense_bytes) {
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200959 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
960 sense_bytes);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200961 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200962 scmd->sdb.length);
963 scmd->sdb.table.sgl = &ses->sense_sgl;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200964 scmd->sc_data_direction = DMA_FROM_DEVICE;
Tony Battersby0c958ec2015-07-16 11:40:41 -0400965 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200966 scmd->cmnd[0] = REQUEST_SENSE;
Boaz Harrosh30b0c372007-12-13 13:47:40 +0200967 scmd->cmnd[4] = scmd->sdb.length;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200968 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200969 } else {
Christoph Hellwig631c2282006-07-08 20:42:15 +0200970 scmd->sc_data_direction = DMA_NONE;
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200971 if (cmnd) {
Boaz Harrosh64a87b22008-04-30 11:19:47 +0300972 BUG_ON(cmnd_size > BLK_MAX_CDB);
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200973 memcpy(scmd->cmnd, cmnd, cmnd_size);
974 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
975 }
Christoph Hellwig631c2282006-07-08 20:42:15 +0200976 }
977
978 scmd->underflow = 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +0200979
Boaz Harrosh55db6c12007-10-08 16:35:19 +0200980 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
f59114b2005-04-17 15:00:23 -0500982 (sdev->lun << 5 & 0xe0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Christoph Hellwig631c2282006-07-08 20:42:15 +0200984 /*
985 * Zero the sense buffer. The scsi spec mandates that any
986 * untransferred sense data should be interpreted as being zero.
987 */
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +0900988 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Boaz Harroshe1c23462007-10-08 16:36:45 +0200989}
990EXPORT_SYMBOL(scsi_eh_prep_cmnd);
Christoph Hellwig631c2282006-07-08 20:42:15 +0200991
Boaz Harroshe1c23462007-10-08 16:36:45 +0200992/**
Santosh Y3b729f72012-04-08 18:47:09 +0530993 * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +0200994 * @scmd: SCSI command structure to restore
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200995 * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
Boaz Harroshe1c23462007-10-08 16:36:45 +0200996 *
Bartlomiej Zolnierkiewicz477e6082009-04-27 20:54:22 +0200997 * Undo any damage done by above scsi_eh_prep_cmnd().
Randy Dunlapdc8875e2007-11-15 15:42:30 -0800998 */
Boaz Harroshe1c23462007-10-08 16:36:45 +0200999void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1000{
1001 /*
1002 * Restore original data
1003 */
1004 scmd->cmd_len = ses->cmd_len;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03001005 scmd->cmnd = ses->cmnd;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001006 scmd->sc_data_direction = ses->data_direction;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02001007 scmd->sdb = ses->sdb;
Boaz Harrosh6f9a35e2007-12-13 13:50:53 +02001008 scmd->request->next_rq = ses->next_rq;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001009 scmd->result = ses->result;
Alan Stern12265702008-07-21 10:25:52 -04001010 scmd->underflow = ses->underflow;
Martin K. Petersendb007fc2008-07-17 04:28:31 -04001011 scmd->prot_op = ses->prot_op;
Hannes Reinecke8e8c9d02017-04-06 15:36:33 +02001012 scmd->eh_eflags = ses->eh_eflags;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001013}
1014EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1015
1016/**
Santosh Y3b729f72012-04-08 18:47:09 +05301017 * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
Boaz Harroshe1c23462007-10-08 16:36:45 +02001018 * @scmd: SCSI command structure to hijack
1019 * @cmnd: CDB to send
1020 * @cmnd_size: size in bytes of @cmnd
1021 * @timeout: timeout for this request
1022 * @sense_bytes: size of sense data to copy or 0
1023 *
1024 * This function is used to send a scsi command down to a target device
1025 * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1026 *
1027 * Return value:
1028 * SUCCESS or FAILED or NEEDS_RETRY
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001029 */
Boaz Harroshe1c23462007-10-08 16:36:45 +02001030static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1031 int cmnd_size, int timeout, unsigned sense_bytes)
1032{
1033 struct scsi_device *sdev = scmd->device;
1034 struct Scsi_Host *shost = sdev->host;
1035 DECLARE_COMPLETION_ONSTACK(done);
Hannes Reineckefc736482013-04-25 08:10:00 +02001036 unsigned long timeleft = timeout;
Boaz Harroshe1c23462007-10-08 16:36:45 +02001037 struct scsi_eh_save ses;
Hannes Reineckefc736482013-04-25 08:10:00 +02001038 const unsigned long stall_for = msecs_to_jiffies(100);
Boaz Harroshe1c23462007-10-08 16:36:45 +02001039 int rtn;
1040
Hannes Reineckefc736482013-04-25 08:10:00 +02001041retry:
Boaz Harroshe1c23462007-10-08 16:36:45 +02001042 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001043 shost->eh_action = &done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 scsi_log_send(scmd);
Jeff Garzikf2812332010-11-16 02:10:29 -05001046 scmd->scsi_done = scsi_eh_done;
Hannes Reineckefc736482013-04-25 08:10:00 +02001047 rtn = shost->hostt->queuecommand(shost, scmd);
1048 if (rtn) {
1049 if (timeleft > stall_for) {
1050 scsi_eh_restore_cmnd(scmd, &ses);
1051 timeleft -= stall_for;
1052 msleep(jiffies_to_msecs(stall_for));
1053 goto retry;
1054 }
1055 /* signal not to enter either branch of the if () below */
1056 timeleft = 0;
Alan Stern511833a2014-11-21 10:44:49 -05001057 rtn = FAILED;
Hannes Reineckefc736482013-04-25 08:10:00 +02001058 } else {
1059 timeleft = wait_for_completion_timeout(&done, timeout);
Hannes Reineckeac61d192014-05-08 08:09:56 +02001060 rtn = SUCCESS;
Hannes Reineckefc736482013-04-25 08:10:00 +02001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
f59114b2005-04-17 15:00:23 -05001063 shost->eh_action = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Hannes Reineckefc736482013-04-25 08:10:00 +02001065 scsi_log_completion(scmd, rtn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Hannes Reinecke91921e02014-06-25 16:39:59 +02001067 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001068 "%s timeleft: %ld\n",
1069 __func__, timeleft));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /*
Hannes Reineckefc736482013-04-25 08:10:00 +02001072 * If there is time left scsi_eh_done got called, and we will examine
1073 * the actual status codes to see whether the command actually did
1074 * complete normally, else if we have a zero return and no time left,
1075 * the command must still be pending, so abort it and return FAILED.
1076 * If we never actually managed to issue the command, because
1077 * ->queuecommand() kept returning non zero, use the rtn = FAILED
1078 * value above (so don't execute either branch of the if)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001080 if (timeleft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 rtn = scsi_eh_completed_normally(scmd);
Hannes Reinecke91921e02014-06-25 16:39:59 +02001082 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1083 "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 switch (rtn) {
1086 case SUCCESS:
1087 case NEEDS_RETRY:
1088 case FAILED:
1089 break;
Hannes Reinecke6e883b02009-09-17 17:00:26 +02001090 case ADD_TO_MLQUEUE:
1091 rtn = NEEDS_RETRY;
1092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 default:
1094 rtn = FAILED;
1095 break;
1096 }
Alan Stern511833a2014-11-21 10:44:49 -05001097 } else if (rtn != FAILED) {
Brian King292148f2007-01-30 17:51:17 -06001098 scsi_abort_eh_cmnd(scmd);
Christoph Hellwig7dfdc9a2005-10-31 18:49:52 +01001099 rtn = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
Boaz Harroshe1c23462007-10-08 16:36:45 +02001102 scsi_eh_restore_cmnd(scmd, &ses);
Martin K. Petersen18a4d0a2012-02-09 13:48:53 -05001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return rtn;
1105}
1106
1107/**
1108 * scsi_request_sense - Request sense data from a particular target.
1109 * @scmd: SCSI cmd for request sense.
1110 *
1111 * Notes:
1112 * Some hosts automatically obtain this information, others require
1113 * that we obtain it on our own. This function will *not* return until
1114 * the command either times out, or it completes.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116static int scsi_request_sense(struct scsi_cmnd *scmd)
1117{
Martin K. Petersen0816c922013-05-10 10:36:04 -04001118 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
James Bottomley24510792013-11-11 13:44:53 +01001121static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1122{
Christoph Hellwig57292b52017-01-31 16:57:29 +01001123 if (!blk_rq_is_passthrough(scmd->request)) {
James Bottomley24510792013-11-11 13:44:53 +01001124 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1125 if (sdrv->eh_action)
1126 rtn = sdrv->eh_action(scmd, rtn);
1127 }
1128 return rtn;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/**
1132 * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1133 * @scmd: Original SCSI cmd that eh has finished.
1134 * @done_q: Queue for processed commands.
1135 *
1136 * Notes:
1137 * We don't want to use the normal command completion while we are are
1138 * still handling errors - it may cause other commands to be queued,
Rob Landleyeb448202007-11-03 13:30:39 -05001139 * and that would disturb what we are doing. Thus we really want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * keep a list of pending commands for final completion, and once we
1141 * are ready to leave error handling we handle completion for real.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001142 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09001143void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 list_move_tail(&scmd->eh_entry, done_q);
1146}
Tejun Heo041c5fc2006-01-23 13:09:36 +09001147EXPORT_SYMBOL(scsi_eh_finish_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149/**
1150 * scsi_eh_get_sense - Get device sense data.
1151 * @work_q: Queue of commands to process.
Rob Landleyeb448202007-11-03 13:30:39 -05001152 * @done_q: Queue of processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 *
1154 * Description:
1155 * See if we need to request sense information. if so, then get it
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001156 * now, so we have a better idea of what to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 *
1158 * Notes:
1159 * This has the unfortunate side effect that if a shost adapter does
Rob Landleyeb448202007-11-03 13:30:39 -05001160 * not automatically request sense information, we end up shutting
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * it down before we request it.
1162 *
1163 * All drivers should request sense information internally these days,
1164 * so for now all I have to say is tough noogies if you end up in here.
1165 *
1166 * XXX: Long term this code should go away, but that needs an audit of
1167 * all LLDDs first.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001168 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001169int scsi_eh_get_sense(struct list_head *work_q,
1170 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001172 struct scsi_cmnd *scmd, *next;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001173 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int rtn;
1175
jiang.biao2@zte.com.cn709c75b2015-07-31 17:52:10 +08001176 /*
1177 * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1178 * should not get sense.
1179 */
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001180 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Hannes Reineckea0658632017-04-06 15:36:35 +02001181 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 SCSI_SENSE_VALID(scmd))
1183 continue;
1184
Hannes Reineckeb4562022013-10-23 10:51:21 +02001185 shost = scmd->device->host;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001186 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001187 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001188 scmd_printk(KERN_INFO, scmd,
1189 "%s: skip request sense, past eh deadline\n",
1190 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001191 break;
1192 }
James Bottomleyd555a2a2014-03-28 10:50:17 -07001193 if (status_byte(scmd->result) != CHECK_CONDITION)
1194 /*
1195 * don't request sense if there's no check condition
1196 * status because the error we're processing isn't one
1197 * that has a sense code (and some devices get
1198 * confused by sense requests out of the blue)
1199 */
1200 continue;
1201
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001202 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1203 "%s: requesting sense\n",
1204 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 rtn = scsi_request_sense(scmd);
1206 if (rtn != SUCCESS)
1207 continue;
1208
Hannes Reinecke91921e02014-06-25 16:39:59 +02001209 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001210 "sense requested, result %x\n", scmd->result));
Hannes Reinecked811b842014-10-24 14:26:45 +02001211 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 rtn = scsi_decide_disposition(scmd);
1214
1215 /*
1216 * if the result was normal, then just pass it along to the
1217 * upper level.
1218 */
1219 if (rtn == SUCCESS)
1220 /* we don't want this command reissued, just
1221 * finished with the sense data, so set
1222 * retries to the max allowed to ensure it
1223 * won't get reissued */
1224 scmd->retries = scmd->allowed;
1225 else if (rtn != NEEDS_RETRY)
1226 continue;
1227
1228 scsi_eh_finish_cmd(scmd, done_q);
1229 }
1230
1231 return list_empty(work_q);
1232}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08001233EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * scsi_eh_tur - Send TUR to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001237 * @scmd: &scsi_cmnd to send TUR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 *
1239 * Return value:
1240 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static int scsi_eh_tur(struct scsi_cmnd *scmd)
1243{
1244 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1245 int retry_cnt = 1, rtn;
1246
1247retry_tur:
Martin K. Petersen0816c922013-05-10 10:36:04 -04001248 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1249 scmd->device->eh_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Hannes Reinecke91921e02014-06-25 16:39:59 +02001251 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01001252 "%s return: %x\n", __func__, rtn));
Christoph Hellwig631c2282006-07-08 20:42:15 +02001253
1254 switch (rtn) {
1255 case NEEDS_RETRY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (retry_cnt--)
1257 goto retry_tur;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001258 /*FALLTHRU*/
1259 case SUCCESS:
Alan Sterne47373e2005-03-30 15:05:45 -05001260 return 0;
Christoph Hellwig631c2282006-07-08 20:42:15 +02001261 default:
1262 return 1;
Alan Sterne47373e2005-03-30 15:05:45 -05001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266/**
David Jeffery3eef6252011-05-19 14:41:12 -04001267 * scsi_eh_test_devices - check if devices are responding from error recovery.
1268 * @cmd_list: scsi commands in error recovery.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001269 * @work_q: queue for commands which still need more error recovery
1270 * @done_q: queue for commands which are finished
1271 * @try_stu: boolean on if a STU command should be tried in addition to TUR.
David Jeffery3eef6252011-05-19 14:41:12 -04001272 *
1273 * Decription:
1274 * Tests if devices are in a working state. Commands to devices now in
1275 * a working state are sent to the done_q while commands to devices which
1276 * are still failing to respond are returned to the work_q for more
1277 * processing.
1278 **/
1279static int scsi_eh_test_devices(struct list_head *cmd_list,
1280 struct list_head *work_q,
1281 struct list_head *done_q, int try_stu)
1282{
1283 struct scsi_cmnd *scmd, *next;
1284 struct scsi_device *sdev;
1285 int finish_cmds;
1286
1287 while (!list_empty(cmd_list)) {
1288 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1289 sdev = scmd->device;
1290
Hannes Reineckeb4562022013-10-23 10:51:21 +02001291 if (!try_stu) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001292 if (scsi_host_eh_past_deadline(sdev->host)) {
1293 /* Push items back onto work_q */
1294 list_splice_init(cmd_list, work_q);
Hannes Reineckeb4562022013-10-23 10:51:21 +02001295 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001296 sdev_printk(KERN_INFO, sdev,
1297 "%s: skip test device, past eh deadline",
1298 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001299 break;
1300 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001301 }
1302
David Jeffery3eef6252011-05-19 14:41:12 -04001303 finish_cmds = !scsi_device_online(scmd->device) ||
1304 (try_stu && !scsi_eh_try_stu(scmd) &&
1305 !scsi_eh_tur(scmd)) ||
1306 !scsi_eh_tur(scmd);
1307
1308 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1309 if (scmd->device == sdev) {
James Bottomley24510792013-11-11 13:44:53 +01001310 if (finish_cmds &&
1311 (try_stu ||
1312 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
David Jeffery3eef6252011-05-19 14:41:12 -04001313 scsi_eh_finish_cmd(scmd, done_q);
1314 else
1315 list_move_tail(&scmd->eh_entry, work_q);
1316 }
1317 }
1318 return list_empty(work_q);
1319}
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * scsi_eh_try_stu - Send START_UNIT to device.
Rob Landleyeb448202007-11-03 13:30:39 -05001323 * @scmd: &scsi_cmnd to send START_UNIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 *
1325 * Return value:
1326 * 0 - Device is ready. 1 - Device NOT ready.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1329{
1330 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Christoph Hellwig631c2282006-07-08 20:42:15 +02001332 if (scmd->device->allow_restart) {
Brian Kinged773e62007-03-29 15:25:52 -05001333 int i, rtn = NEEDS_RETRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Brian Kinged773e62007-03-29 15:25:52 -05001335 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
James Bottomley9728c082008-11-30 10:32:26 -06001336 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
Brian Kinged773e62007-03-29 15:25:52 -05001337
Christoph Hellwig631c2282006-07-08 20:42:15 +02001338 if (rtn == SUCCESS)
1339 return 0;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 1;
1343}
1344
1345 /**
1346 * scsi_eh_stu - send START_UNIT if needed
Rob Landleyeb448202007-11-03 13:30:39 -05001347 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001348 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001349 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
1351 * Notes:
1352 * If commands are failing due to not ready, initializing command required,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001353 * try revalidating the device, which will end up sending a start unit.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355static int scsi_eh_stu(struct Scsi_Host *shost,
1356 struct list_head *work_q,
1357 struct list_head *done_q)
1358{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001359 struct scsi_cmnd *scmd, *stu_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 struct scsi_device *sdev;
1361
1362 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001363 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001364 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001365 sdev_printk(KERN_INFO, sdev,
1366 "%s: skip START_UNIT, past eh deadline\n",
1367 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001368 break;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 stu_scmd = NULL;
1371 list_for_each_entry(scmd, work_q, eh_entry)
1372 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1373 scsi_check_sense(scmd) == FAILED ) {
1374 stu_scmd = scmd;
1375 break;
1376 }
1377
1378 if (!stu_scmd)
1379 continue;
1380
Hannes Reinecke91921e02014-06-25 16:39:59 +02001381 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001382 sdev_printk(KERN_INFO, sdev,
1383 "%s: Sending START_UNIT\n",
1384 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 if (!scsi_eh_try_stu(stu_scmd)) {
1387 if (!scsi_device_online(sdev) ||
1388 !scsi_eh_tur(stu_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001389 list_for_each_entry_safe(scmd, next,
1390 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001391 if (scmd->device == sdev &&
1392 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 scsi_eh_finish_cmd(scmd, done_q);
1394 }
1395 }
1396 } else {
1397 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001398 sdev_printk(KERN_INFO, sdev,
1399 "%s: START_UNIT failed\n",
1400 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 }
1403
1404 return list_empty(work_q);
1405}
1406
1407
1408/**
1409 * scsi_eh_bus_device_reset - send bdr if needed
1410 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001411 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001412 * @done_q: &list_head for processed commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 *
1414 * Notes:
Rob Landleyeb448202007-11-03 13:30:39 -05001415 * Try a bus device reset. Still, look to see whether we have multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * devices that are jammed or not - if we have multiple devices, it
1417 * makes no sense to try bus_device_reset - we really would need to try
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001418 * a bus_reset instead.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1421 struct list_head *work_q,
1422 struct list_head *done_q)
1423{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001424 struct scsi_cmnd *scmd, *bdr_scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct scsi_device *sdev;
1426 int rtn;
1427
1428 shost_for_each_device(sdev, shost) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001429 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001430 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001431 sdev_printk(KERN_INFO, sdev,
1432 "%s: skip BDR, past eh deadline\n",
1433 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001434 break;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 bdr_scmd = NULL;
1437 list_for_each_entry(scmd, work_q, eh_entry)
1438 if (scmd->device == sdev) {
1439 bdr_scmd = scmd;
1440 break;
1441 }
1442
1443 if (!bdr_scmd)
1444 continue;
1445
Hannes Reinecke91921e02014-06-25 16:39:59 +02001446 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001447 sdev_printk(KERN_INFO, sdev,
1448 "%s: Sending BDR\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 rtn = scsi_try_bus_device_reset(bdr_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001450 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!scsi_device_online(sdev) ||
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001452 rtn == FAST_IO_FAIL ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 !scsi_eh_tur(bdr_scmd)) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001454 list_for_each_entry_safe(scmd, next,
1455 work_q, eh_entry) {
James Bottomley24510792013-11-11 13:44:53 +01001456 if (scmd->device == sdev &&
1457 scsi_eh_action(scmd, rtn) != FAILED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 scsi_eh_finish_cmd(scmd,
1459 done_q);
1460 }
1461 }
1462 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001463 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001464 sdev_printk(KERN_INFO, sdev,
1465 "%s: BDR failed\n", current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 }
1468
1469 return list_empty(work_q);
1470}
1471
1472/**
Mike Christie30bd7df2008-02-29 18:25:19 -06001473 * scsi_eh_target_reset - send target reset if needed
1474 * @shost: scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001475 * @work_q: &list_head for pending commands.
Mike Christie30bd7df2008-02-29 18:25:19 -06001476 * @done_q: &list_head for processed commands.
1477 *
1478 * Notes:
1479 * Try a target reset.
1480 */
1481static int scsi_eh_target_reset(struct Scsi_Host *shost,
1482 struct list_head *work_q,
1483 struct list_head *done_q)
1484{
James Bottomley98db5192010-10-25 15:53:41 -05001485 LIST_HEAD(tmp_list);
David Jeffery3eef6252011-05-19 14:41:12 -04001486 LIST_HEAD(check_list);
Mike Christie30bd7df2008-02-29 18:25:19 -06001487
James Bottomley98db5192010-10-25 15:53:41 -05001488 list_splice_init(work_q, &tmp_list);
1489
1490 while (!list_empty(&tmp_list)) {
1491 struct scsi_cmnd *next, *scmd;
1492 int rtn;
1493 unsigned int id;
Hannes Reineckeb4562022013-10-23 10:51:21 +02001494
Hannes Reineckeb4562022013-10-23 10:51:21 +02001495 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001496 /* push back on work queue for further processing */
1497 list_splice_init(&check_list, work_q);
1498 list_splice_init(&tmp_list, work_q);
1499 SCSI_LOG_ERROR_RECOVERY(3,
1500 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001501 "%s: Skip target reset, past eh deadline\n",
1502 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001503 return list_empty(work_q);
1504 }
James Bottomley98db5192010-10-25 15:53:41 -05001505
1506 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1507 id = scmd_id(scmd);
Mike Christie30bd7df2008-02-29 18:25:19 -06001508
Hannes Reinecke91921e02014-06-25 16:39:59 +02001509 SCSI_LOG_ERROR_RECOVERY(3,
1510 shost_printk(KERN_INFO, shost,
1511 "%s: Sending target reset to target %d\n",
1512 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001513 rtn = scsi_try_target_reset(scmd);
1514 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
Hannes Reinecke91921e02014-06-25 16:39:59 +02001515 SCSI_LOG_ERROR_RECOVERY(3,
1516 shost_printk(KERN_INFO, shost,
1517 "%s: Target reset failed"
1518 " target: %d\n",
1519 current->comm, id));
James Bottomley98db5192010-10-25 15:53:41 -05001520 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1521 if (scmd_id(scmd) != id)
1522 continue;
1523
David Jeffery3eef6252011-05-19 14:41:12 -04001524 if (rtn == SUCCESS)
1525 list_move_tail(&scmd->eh_entry, &check_list);
1526 else if (rtn == FAST_IO_FAIL)
James Bottomley98db5192010-10-25 15:53:41 -05001527 scsi_eh_finish_cmd(scmd, done_q);
1528 else
1529 /* push back on work queue for further processing */
1530 list_move(&scmd->eh_entry, work_q);
1531 }
1532 }
Mike Christie30bd7df2008-02-29 18:25:19 -06001533
David Jeffery3eef6252011-05-19 14:41:12 -04001534 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Mike Christie30bd7df2008-02-29 18:25:19 -06001535}
1536
1537/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001538 * scsi_eh_bus_reset - send a bus reset
Rob Landleyeb448202007-11-03 13:30:39 -05001539 * @shost: &scsi host being recovered.
Randy Dunlap74cf2982014-08-16 14:15:11 -07001540 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05001541 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1544 struct list_head *work_q,
1545 struct list_head *done_q)
1546{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001547 struct scsi_cmnd *scmd, *chan_scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001548 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 unsigned int channel;
1550 int rtn;
1551
1552 /*
1553 * we really want to loop over the various channels, and do this on
1554 * a channel by channel basis. we should also check to see if any
1555 * of the failed commands are on soft_reset devices, and if so, skip
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001556 * the reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
1558
1559 for (channel = 0; channel <= shost->max_channel; channel++) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001560 if (scsi_host_eh_past_deadline(shost)) {
Hannes Reineckeb4562022013-10-23 10:51:21 +02001561 list_splice_init(&check_list, work_q);
1562 SCSI_LOG_ERROR_RECOVERY(3,
1563 shost_printk(KERN_INFO, shost,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02001564 "%s: skip BRST, past eh deadline\n",
1565 current->comm));
Hannes Reineckeb4562022013-10-23 10:51:21 +02001566 return list_empty(work_q);
1567 }
Hannes Reineckeb4562022013-10-23 10:51:21 +02001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 chan_scmd = NULL;
1570 list_for_each_entry(scmd, work_q, eh_entry) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04001571 if (channel == scmd_channel(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 chan_scmd = scmd;
1573 break;
1574 /*
1575 * FIXME add back in some support for
1576 * soft_reset devices.
1577 */
1578 }
1579 }
1580
1581 if (!chan_scmd)
1582 continue;
Hannes Reinecke91921e02014-06-25 16:39:59 +02001583 SCSI_LOG_ERROR_RECOVERY(3,
1584 shost_printk(KERN_INFO, shost,
1585 "%s: Sending BRST chan: %d\n",
1586 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 rtn = scsi_try_bus_reset(chan_scmd);
Christof Schmitt2f2eb582010-03-24 16:50:30 +01001588 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001589 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
David Jeffery3eef6252011-05-19 14:41:12 -04001590 if (channel == scmd_channel(scmd)) {
1591 if (rtn == FAST_IO_FAIL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 scsi_eh_finish_cmd(scmd,
1593 done_q);
David Jeffery3eef6252011-05-19 14:41:12 -04001594 else
1595 list_move_tail(&scmd->eh_entry,
1596 &check_list);
1597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001600 SCSI_LOG_ERROR_RECOVERY(3,
1601 shost_printk(KERN_INFO, shost,
1602 "%s: BRST failed chan: %d\n",
1603 current->comm, channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 }
David Jeffery3eef6252011-05-19 14:41:12 -04001606 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/**
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001610 * scsi_eh_host_reset - send a host reset
Randy Dunlap74cf2982014-08-16 14:15:11 -07001611 * @shost: host to be reset.
1612 * @work_q: &list_head for pending commands.
1613 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001614 */
Hannes Reinecke91921e02014-06-25 16:39:59 +02001615static int scsi_eh_host_reset(struct Scsi_Host *shost,
1616 struct list_head *work_q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct list_head *done_q)
1618{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001619 struct scsi_cmnd *scmd, *next;
David Jeffery3eef6252011-05-19 14:41:12 -04001620 LIST_HEAD(check_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int rtn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (!list_empty(work_q)) {
1624 scmd = list_entry(work_q->next,
1625 struct scsi_cmnd, eh_entry);
1626
Hannes Reinecke91921e02014-06-25 16:39:59 +02001627 SCSI_LOG_ERROR_RECOVERY(3,
1628 shost_printk(KERN_INFO, shost,
1629 "%s: Sending HRST\n",
1630 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 rtn = scsi_try_host_reset(scmd);
David Jeffery3eef6252011-05-19 14:41:12 -04001633 if (rtn == SUCCESS) {
1634 list_splice_init(work_q, &check_list);
1635 } else if (rtn == FAST_IO_FAIL) {
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001636 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 scsi_eh_finish_cmd(scmd, done_q);
1638 }
1639 } else {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001640 SCSI_LOG_ERROR_RECOVERY(3,
1641 shost_printk(KERN_INFO, shost,
1642 "%s: HRST failed\n",
1643 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645 }
David Jeffery3eef6252011-05-19 14:41:12 -04001646 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649/**
1650 * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
Randy Dunlap74cf2982014-08-16 14:15:11 -07001651 * @work_q: &list_head for pending commands.
1652 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654static void scsi_eh_offline_sdevs(struct list_head *work_q,
1655 struct list_head *done_q)
1656{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001657 struct scsi_cmnd *scmd, *next;
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001658 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02001660 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
Matthew Wilcox31765d72007-08-17 11:02:10 -06001661 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1662 "not ready after error recovery\n");
Bart Van Assche0db6ca82017-06-02 14:21:55 -07001663 sdev = scmd->device;
1664
1665 mutex_lock(&sdev->state_mutex);
1666 scsi_device_set_state(sdev, SDEV_OFFLINE);
1667 mutex_unlock(&sdev->state_mutex);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 scsi_eh_finish_cmd(scmd, done_q);
1670 }
1671 return;
1672}
1673
1674/**
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001675 * scsi_noretry_cmd - determine if command should be failed fast
Mike Christie4a274462008-08-19 18:45:31 -05001676 * @scmd: SCSI cmd to examine.
1677 */
1678int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1679{
1680 switch (host_byte(scmd->result)) {
1681 case DID_OK:
1682 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001683 case DID_TIME_OUT:
1684 goto check_type;
Mike Christie4a274462008-08-19 18:45:31 -05001685 case DID_BUS_BUSY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001686 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
Mike Christie4a274462008-08-19 18:45:31 -05001687 case DID_PARITY:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001688 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
Mike Christie4a274462008-08-19 18:45:31 -05001689 case DID_ERROR:
1690 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1691 status_byte(scmd->result) == RESERVATION_CONFLICT)
1692 return 0;
1693 /* fall through */
1694 case DID_SOFT_ERROR:
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001695 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
Mike Christie4a274462008-08-19 18:45:31 -05001696 }
1697
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001698 if (status_byte(scmd->result) != CHECK_CONDITION)
1699 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001700
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001701check_type:
1702 /*
1703 * assume caller has checked sense and determined
1704 * the check condition was retryable.
1705 */
1706 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
Christoph Hellwig57292b52017-01-31 16:57:29 +01001707 blk_rq_is_passthrough(scmd->request))
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001708 return 1;
1709 else
1710 return 0;
Mike Christie4a274462008-08-19 18:45:31 -05001711}
1712
1713/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * scsi_decide_disposition - Disposition a cmd on return from LLD.
1715 * @scmd: SCSI cmd to examine.
1716 *
1717 * Notes:
1718 * This is *only* called when we are examining the status after sending
1719 * out the actual data command. any commands that are queued for error
1720 * recovery (e.g. test_unit_ready) do *not* come through here.
1721 *
1722 * When this routine returns failed, it means the error handler thread
1723 * is woken. In cases where the error code indicates an error that
1724 * doesn't require the error handler read (i.e. we don't need to
1725 * abort/reset), this function should return SUCCESS.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727int scsi_decide_disposition(struct scsi_cmnd *scmd)
1728{
1729 int rtn;
1730
1731 /*
1732 * if the device is offline, then we clearly just pass the result back
1733 * up to the top level.
1734 */
1735 if (!scsi_device_online(scmd->device)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02001736 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1737 "%s: device offline - report as SUCCESS\n", __func__));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return SUCCESS;
1739 }
1740
1741 /*
1742 * first check the host byte, to see if there is anything in there
1743 * that would indicate what we need to do.
1744 */
1745 switch (host_byte(scmd->result)) {
1746 case DID_PASSTHROUGH:
1747 /*
1748 * no matter what, pass this through to the upper layer.
1749 * nuke this special code so that it looks like we are saying
1750 * did_ok.
1751 */
1752 scmd->result &= 0xff00ffff;
1753 return SUCCESS;
1754 case DID_OK:
1755 /*
1756 * looks good. drop through, and check the next byte.
1757 */
1758 break;
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001759 case DID_ABORT:
1760 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
Ulrich Obergfell8922a902014-06-04 13:34:57 +02001761 set_host_byte(scmd, DID_TIME_OUT);
Hannes Reineckee494f6a2013-11-11 13:44:54 +01001762 return SUCCESS;
1763 }
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001764 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 case DID_NO_CONNECT:
1766 case DID_BAD_TARGET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * note - this means that we just report the status back
1769 * to the top level driver, not that we actually think
1770 * that it indicates SUCCESS.
1771 */
1772 return SUCCESS;
Petros Koutoupisad950282017-10-30 16:38:10 -05001773 case DID_SOFT_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /*
1775 * when the low level driver returns did_soft_error,
Jesper Juhl0bf8c862011-03-21 20:47:31 +01001776 * it is responsible for keeping an internal retry counter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 * in order to avoid endless loops (db)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto maybe_retry;
1780 case DID_IMM_RETRY:
1781 return NEEDS_RETRY;
1782
bf341912005-04-12 17:49:09 -05001783 case DID_REQUEUE:
1784 return ADD_TO_MLQUEUE;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001785 case DID_TRANSPORT_DISRUPTED:
1786 /*
1787 * LLD/transport was disrupted during processing of the IO.
1788 * The transport class is now blocked/blocking,
1789 * and the transport will decide what to do with the IO
Mike Christie939c22882008-11-04 19:47:19 -06001790 * based on its timers and recovery capablilities if
1791 * there are enough retries.
Mike Christiea4dfaa62008-08-19 18:45:25 -05001792 */
Mike Christie939c22882008-11-04 19:47:19 -06001793 goto maybe_retry;
Mike Christiea4dfaa62008-08-19 18:45:25 -05001794 case DID_TRANSPORT_FAILFAST:
1795 /*
1796 * The transport decided to failfast the IO (most likely
1797 * the fast io fail tmo fired), so send IO directly upwards.
1798 */
1799 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 case DID_ERROR:
1801 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1802 status_byte(scmd->result) == RESERVATION_CONFLICT)
1803 /*
1804 * execute reservation conflict processing code
1805 * lower down
1806 */
1807 break;
1808 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 case DID_BUS_BUSY:
1810 case DID_PARITY:
1811 goto maybe_retry;
1812 case DID_TIME_OUT:
1813 /*
1814 * when we scan the bus, we get timeout messages for
1815 * these commands if there is no device available.
1816 * other hosts report did_no_connect for the same thing.
1817 */
1818 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1819 scmd->cmnd[0] == INQUIRY)) {
1820 return SUCCESS;
1821 } else {
1822 return FAILED;
1823 }
1824 case DID_RESET:
1825 return SUCCESS;
1826 default:
1827 return FAILED;
1828 }
1829
1830 /*
1831 * next, check the message byte.
1832 */
1833 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1834 return FAILED;
1835
1836 /*
1837 * check the status byte to see if this indicates anything special.
1838 */
1839 switch (status_byte(scmd->result)) {
1840 case QUEUE_FULL:
Mike Christie42a6a912009-10-15 17:46:44 -07001841 scsi_handle_queue_full(scmd->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /*
1843 * the case of trying to send too many commands to a
1844 * tagged queueing device.
1845 */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001846 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 case BUSY:
1848 /*
1849 * device can't talk to us at the moment. Should only
1850 * occur (SAM-3) when the task queue is empty, so will cause
1851 * the empty queue handling to trigger a stall in the
1852 * device.
1853 */
1854 return ADD_TO_MLQUEUE;
1855 case GOOD:
Ewan D. Milne279afdf2013-08-08 15:07:48 -04001856 if (scmd->cmnd[0] == REPORT_LUNS)
1857 scmd->device->sdev_target->expecting_lun_change = 0;
Vasu Dev4a840672009-10-22 15:46:33 -07001858 scsi_handle_queue_ramp_up(scmd->device);
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07001859 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 case COMMAND_TERMINATED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return SUCCESS;
Vladislav Bolkhovitina9b589d2008-11-06 13:57:52 +03001862 case TASK_ABORTED:
1863 goto maybe_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 case CHECK_CONDITION:
1865 rtn = scsi_check_sense(scmd);
1866 if (rtn == NEEDS_RETRY)
1867 goto maybe_retry;
1868 /* if rtn == FAILED, we have no sense information;
1869 * returning FAILED will wake the error handler thread
1870 * to collect the sense and redo the decide
1871 * disposition */
1872 return rtn;
1873 case CONDITION_GOOD:
1874 case INTERMEDIATE_GOOD:
1875 case INTERMEDIATE_C_GOOD:
1876 case ACA_ACTIVE:
1877 /*
1878 * who knows? FIXME(eric)
1879 */
1880 return SUCCESS;
1881
1882 case RESERVATION_CONFLICT:
James Bottomley9ccfc752005-10-02 11:45:08 -05001883 sdev_printk(KERN_INFO, scmd->device,
1884 "reservation conflict\n");
Moger, Babu2082ebc2012-01-24 20:38:46 +00001885 set_host_byte(scmd, DID_NEXUS_FAILURE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return SUCCESS; /* causes immediate i/o error */
1887 default:
1888 return FAILED;
1889 }
1890 return FAILED;
1891
1892 maybe_retry:
1893
1894 /* we requeue for retry because the error was retryable, and
1895 * the request was not marked fast fail. Note that above,
1896 * even if the request is marked fast fail, we still requeue
1897 * for queue congestion conditions (QUEUE_FULL or BUSY) */
Brian King8884efa2006-02-24 17:10:04 -06001898 if ((++scmd->retries) <= scmd->allowed
Mike Christie4a274462008-08-19 18:45:31 -05001899 && !scsi_noretry_cmd(scmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return NEEDS_RETRY;
1901 } else {
1902 /*
1903 * no more retries - report this one back to upper level.
1904 */
1905 return SUCCESS;
1906 }
1907}
1908
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001909static void eh_lock_door_done(struct request *req, blk_status_t status)
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001910{
1911 __blk_put_request(req->q, req);
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * scsi_eh_lock_door - Prevent medium removal for the specified device
1916 * @sdev: SCSI device to prevent medium removal
1917 *
1918 * Locking:
James Bottomley91bc31f2009-05-17 09:30:48 -05001919 * We must be called from process context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 *
1921 * Notes:
1922 * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1923 * head of the devices request queue, and continue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925static void scsi_eh_lock_door(struct scsi_device *sdev)
1926{
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001927 struct request *req;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001928 struct scsi_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
James Bottomley91bc31f2009-05-17 09:30:48 -05001930 /*
Mel Gorman71baba42015-11-06 16:28:28 -08001931 * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a
James Bottomley91bc31f2009-05-17 09:30:48 -05001932 * request becomes available
1933 */
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001934 req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL);
Joe Lawrencea492f072014-08-28 08:15:21 -06001935 if (IS_ERR(req))
Joe Lawrenceeb571ee2014-07-02 15:35:16 -04001936 return;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001937 rq = scsi_req(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001939 rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1940 rq->cmd[1] = 0;
1941 rq->cmd[2] = 0;
1942 rq->cmd[3] = 0;
1943 rq->cmd[4] = SCSI_REMOVAL_PREVENT;
1944 rq->cmd[5] = 0;
1945 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001946
Christoph Hellwige8064022016-10-20 15:12:13 +02001947 req->rq_flags |= RQF_QUIET;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001948 req->timeout = 10 * HZ;
Christoph Hellwig64c7f1d2017-04-05 19:18:12 +02001949 rq->retries = 5;
FUJITA Tomonorif0787272008-12-14 01:23:45 +09001950
1951 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/**
1955 * scsi_restart_operations - restart io operations to the specified host.
1956 * @shost: Host we are restarting.
1957 *
1958 * Notes:
1959 * When we entered the error handler, we blocked all further i/o to
1960 * this device. we need to 'reverse' this process.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08001961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962static void scsi_restart_operations(struct Scsi_Host *shost)
1963{
1964 struct scsi_device *sdev;
James Bottomley939647e2005-09-18 15:05:20 -05001965 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /*
1968 * If the door was locked, we need to insert a door lock request
1969 * onto the head of the SCSI request queue for the device. There
1970 * is no point trying to lock the door of an off-line device.
1971 */
1972 shost_for_each_device(sdev, shost) {
Christoph Hellwig48379272014-11-03 19:36:40 +01001973 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 scsi_eh_lock_door(sdev);
Christoph Hellwig48379272014-11-03 19:36:40 +01001975 sdev->was_reset = 0;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978
1979 /*
1980 * next free up anything directly waiting upon the host. this
1981 * will be requests for character device operations, and also for
1982 * ioctls to queued block devices.
1983 */
Hannes Reineckeb4562022013-10-23 10:51:21 +02001984 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02001985 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
James Bottomley939647e2005-09-18 15:05:20 -05001987 spin_lock_irqsave(shost->host_lock, flags);
1988 if (scsi_host_set_state(shost, SHOST_RUNNING))
1989 if (scsi_host_set_state(shost, SHOST_CANCEL))
1990 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1991 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 wake_up(&shost->host_wait);
1994
1995 /*
1996 * finally we need to re-initiate requests that may be pending. we will
1997 * have had everything blocked while error handling is taking place, and
1998 * now that error recovery is done, we will need to ensure that these
1999 * requests are started.
2000 */
2001 scsi_run_host_queues(shost);
Dan Williams57fc2e32012-06-21 23:25:32 -07002002
2003 /*
2004 * if eh is active and host_eh_scheduled is pending we need to re-run
2005 * recovery. we do this check after scsi_run_host_queues() to allow
2006 * everything pent up since the last eh run a chance to make forward
2007 * progress before we sync again. Either we'll immediately re-run
2008 * recovery or scsi_device_unbusy() will wake us again when these
2009 * pending commands complete.
2010 */
2011 spin_lock_irqsave(shost->host_lock, flags);
2012 if (shost->host_eh_scheduled)
2013 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2014 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2015 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
2018/**
2019 * scsi_eh_ready_devs - check device ready state and recover if not.
Randy Dunlap74cf2982014-08-16 14:15:11 -07002020 * @shost: host to be recovered.
2021 * @work_q: &list_head for pending commands.
Rob Landleyeb448202007-11-03 13:30:39 -05002022 * @done_q: &list_head for processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002023 */
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002024void scsi_eh_ready_devs(struct Scsi_Host *shost,
2025 struct list_head *work_q,
2026 struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 if (!scsi_eh_stu(shost, work_q, done_q))
2029 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002030 if (!scsi_eh_target_reset(shost, work_q, done_q))
2031 if (!scsi_eh_bus_reset(shost, work_q, done_q))
Hannes Reinecke91921e02014-06-25 16:39:59 +02002032 if (!scsi_eh_host_reset(shost, work_q, done_q))
Mike Christie30bd7df2008-02-29 18:25:19 -06002033 scsi_eh_offline_sdevs(work_q,
2034 done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
Darrick J. Wongdca84e42007-01-26 14:08:49 -08002036EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038/**
2039 * scsi_eh_flush_done_q - finish processed commands or retry them.
2040 * @done_q: list_head of processed commands.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002041 */
Tejun Heo041c5fc2006-01-23 13:09:36 +09002042void scsi_eh_flush_done_q(struct list_head *done_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002044 struct scsi_cmnd *scmd, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Christoph Hellwig937abeaa2005-06-19 13:43:56 +02002046 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2047 list_del_init(&scmd->eh_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (scsi_device_online(scmd->device) &&
Mike Christie4a274462008-08-19 18:45:31 -05002049 !scsi_noretry_cmd(scmd) &&
Brian King8884efa2006-02-24 17:10:04 -06002050 (++scmd->retries <= scmd->allowed)) {
Hannes Reinecke91921e02014-06-25 16:39:59 +02002051 SCSI_LOG_ERROR_RECOVERY(3,
2052 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002053 "%s: flush retry cmd\n",
2054 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2056 } else {
Patrick Mansfield 793698c2005-05-16 17:42:15 -07002057 /*
2058 * If just we got sense for the device (called
2059 * scsi_eh_get_sense), scmd->result is already
2060 * set, do not set DRIVER_TIMEOUT.
2061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!scmd->result)
2063 scmd->result |= (DRIVER_TIMEOUT << 24);
Hannes Reinecke91921e02014-06-25 16:39:59 +02002064 SCSI_LOG_ERROR_RECOVERY(3,
2065 scmd_printk(KERN_INFO, scmd,
Hannes Reinecke470613b2015-01-08 07:43:50 +01002066 "%s: flush finish cmd\n",
2067 current->comm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 scsi_finish_command(scmd);
2069 }
2070 }
2071}
Tejun Heo041c5fc2006-01-23 13:09:36 +09002072EXPORT_SYMBOL(scsi_eh_flush_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074/**
2075 * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2076 * @shost: Host to unjam.
2077 *
2078 * Notes:
2079 * When we come in here, we *know* that all commands on the bus have
2080 * either completed, failed or timed out. we also know that no further
2081 * commands are being sent to the host, so things are relatively quiet
2082 * and we have freedom to fiddle with things as we wish.
2083 *
2084 * This is only the *default* implementation. it is possible for
2085 * individual drivers to supply their own version of this function, and
2086 * if the maintainer wishes to do this, it is strongly suggested that
2087 * this function be taken as a template and modified. this function
2088 * was designed to correctly handle problems for about 95% of the
2089 * different cases out there, and it should always provide at least a
2090 * reasonable amount of error recovery.
2091 *
2092 * Any command marked 'failed' or 'timeout' must eventually have
2093 * scsi_finish_cmd() called for it. we do all of the retry stuff
2094 * here, so when we restart the host after we return it should have an
2095 * empty queue.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002096 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097static void scsi_unjam_host(struct Scsi_Host *shost)
2098{
2099 unsigned long flags;
2100 LIST_HEAD(eh_work_q);
2101 LIST_HEAD(eh_done_q);
2102
2103 spin_lock_irqsave(shost->host_lock, flags);
2104 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2105 spin_unlock_irqrestore(shost->host_lock, flags);
2106
2107 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2108
2109 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
Hannes Reineckea0658632017-04-06 15:36:35 +02002110 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Hannes Reineckeb4562022013-10-23 10:51:21 +02002112 spin_lock_irqsave(shost->host_lock, flags);
Ren Mingxinbb3b6212013-11-11 13:44:56 +01002113 if (shost->eh_deadline != -1)
Hannes Reineckeb4562022013-10-23 10:51:21 +02002114 shost->last_reset = 0;
2115 spin_unlock_irqrestore(shost->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 scsi_eh_flush_done_q(&eh_done_q);
2117}
2118
2119/**
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002120 * scsi_error_handler - SCSI error handler thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * @data: Host for which we are running.
2122 *
2123 * Notes:
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002124 * This is the main error handling loop. This is run as a kernel thread
2125 * for every SCSI host and handles all error handling activity.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127int scsi_error_handler(void *data)
2128{
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002129 struct Scsi_Host *shost = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002132 * We use TASK_INTERRUPTIBLE so that the thread is not
2133 * counted against the load average as a running process.
2134 * We never actually get interrupted because kthread_run
Frederik Schwarzerc03264a2008-12-23 00:54:00 +01002135 * disables signal delivery for the created thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 */
Michal Hocko537b6042015-08-27 20:16:37 +02002137 while (true) {
2138 /*
2139 * The sequence in kthread_stop() sets the stop flag first
2140 * then wakes the process. To avoid missed wakeups, the task
2141 * should always be in a non running state before the stop
2142 * flag is checked
2143 */
Dan Williamsb9d5c6b2012-06-21 23:25:42 -07002144 set_current_state(TASK_INTERRUPTIBLE);
Michal Hocko537b6042015-08-27 20:16:37 +02002145 if (kthread_should_stop())
2146 break;
2147
Tejun Heoee7863b2006-05-15 20:57:20 +09002148 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
Christoph Hellwig74665012014-01-22 15:29:29 +01002149 shost->host_failed != atomic_read(&shost->host_busy)) {
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002150 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002151 shost_printk(KERN_INFO, shost,
2152 "scsi_eh_%d: sleeping\n",
2153 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002154 schedule();
James Bottomley3ed7a472005-09-19 09:50:04 -05002155 continue;
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
James Bottomley3ed7a472005-09-19 09:50:04 -05002158 __set_current_state(TASK_RUNNING);
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002159 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002160 shost_printk(KERN_INFO, shost,
2161 "scsi_eh_%d: waking up %d/%d/%d\n",
2162 shost->host_no, shost->host_eh_scheduled,
Christoph Hellwig74665012014-01-22 15:29:29 +01002163 shost->host_failed,
2164 atomic_read(&shost->host_busy)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
2167 * We have a host that is failing for some reason. Figure out
2168 * what we need to do to get it up and online again (if we can).
2169 * If we fail, we end up taking the thing offline.
2170 */
Lin Mingae0751f2011-12-05 09:20:24 +08002171 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
Alan Sternbc4f2402010-06-17 10:41:42 -04002172 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reineckea222b1e2014-10-24 14:27:02 +02002173 shost_printk(KERN_ERR, shost,
2174 "scsi_eh_%d: unable to autoresume\n",
2175 shost->host_no));
Alan Sternbc4f2402010-06-17 10:41:42 -04002176 continue;
2177 }
2178
Christoph Hellwig9227c332006-04-01 19:21:04 +02002179 if (shost->transportt->eh_strategy_handler)
2180 shost->transportt->eh_strategy_handler(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 else
2182 scsi_unjam_host(shost);
2183
Wei Fang72d8c362016-06-07 14:53:56 +08002184 /* All scmds have been handled */
2185 shost->host_failed = 0;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /*
2188 * Note - if the above fails completely, the action is to take
2189 * individual devices offline and flush the queue of any
2190 * outstanding requests that may have been pending. When we
2191 * restart, we restart any I/O to any other devices on the bus
2192 * which are still online.
2193 */
2194 scsi_restart_operations(shost);
Lin Mingae0751f2011-12-05 09:20:24 +08002195 if (!shost->eh_noresume)
2196 scsi_autopm_put_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Steven Rostedt461a0ff2005-10-19 08:22:13 -04002198 __set_current_state(TASK_RUNNING);
2199
Christoph Hellwigad42eb12005-10-29 01:01:55 +02002200 SCSI_LOG_ERROR_RECOVERY(1,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002201 shost_printk(KERN_INFO, shost,
2202 "Error handler scsi_eh_%d exiting\n",
2203 shost->host_no));
James Bottomley3ed7a472005-09-19 09:50:04 -05002204 shost->ehandler = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return 0;
2206}
2207
2208/*
2209 * Function: scsi_report_bus_reset()
2210 *
2211 * Purpose: Utility function used by low-level drivers to report that
2212 * they have observed a bus reset on the bus being handled.
2213 *
2214 * Arguments: shost - Host in question
2215 * channel - channel on which reset was observed.
2216 *
2217 * Returns: Nothing
2218 *
2219 * Lock status: Host lock must be held.
2220 *
2221 * Notes: This only needs to be called if the reset is one which
2222 * originates from an unknown location. Resets originated
2223 * by the mid-level itself don't need to call this, but there
2224 * should be no harm.
2225 *
2226 * The main purpose of this is to make sure that a CHECK_CONDITION
2227 * is properly treated.
2228 */
2229void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2230{
2231 struct scsi_device *sdev;
2232
2233 __shost_for_each_device(sdev, shost) {
Mike Christie30bd7df2008-02-29 18:25:19 -06002234 if (channel == sdev_channel(sdev))
2235 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237}
2238EXPORT_SYMBOL(scsi_report_bus_reset);
2239
2240/*
2241 * Function: scsi_report_device_reset()
2242 *
2243 * Purpose: Utility function used by low-level drivers to report that
2244 * they have observed a device reset on the device being handled.
2245 *
2246 * Arguments: shost - Host in question
2247 * channel - channel on which reset was observed
2248 * target - target on which reset was observed
2249 *
2250 * Returns: Nothing
2251 *
2252 * Lock status: Host lock must be held
2253 *
2254 * Notes: This only needs to be called if the reset is one which
2255 * originates from an unknown location. Resets originated
2256 * by the mid-level itself don't need to call this, but there
2257 * should be no harm.
2258 *
2259 * The main purpose of this is to make sure that a CHECK_CONDITION
2260 * is properly treated.
2261 */
2262void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2263{
2264 struct scsi_device *sdev;
2265
2266 __shost_for_each_device(sdev, shost) {
Jeff Garzik422c0d62005-10-24 18:05:09 -04002267 if (channel == sdev_channel(sdev) &&
Mike Christie30bd7df2008-02-29 18:25:19 -06002268 target == sdev_id(sdev))
2269 __scsi_report_device_reset(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271}
2272EXPORT_SYMBOL(scsi_report_device_reset);
2273
2274static void
2275scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2276{
2277}
2278
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002279/**
2280 * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2281 * @dev: scsi_device to operate on
2282 * @arg: reset type (see sg.h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 */
2284int
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002285scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Alan Sternbc4f2402010-06-17 10:41:42 -04002287 struct scsi_cmnd *scmd;
James Smartd7a1bb02006-03-08 14:50:12 -05002288 struct Scsi_Host *shost = dev->host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002289 struct request *rq;
James Smartd7a1bb02006-03-08 14:50:12 -05002290 unsigned long flags;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002291 int error = 0, rtn, val;
2292
2293 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2294 return -EACCES;
2295
2296 error = get_user(val, arg);
2297 if (error)
2298 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Alan Sternbc4f2402010-06-17 10:41:42 -04002300 if (scsi_autopm_get_host(shost) < 0)
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002301 return -EIO;
Alan Sternbc4f2402010-06-17 10:41:42 -04002302
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002303 error = -EIO;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002304 rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2305 shost->hostt->cmd_size, GFP_KERNEL);
2306 if (!rq)
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002307 goto out_put_autopm_host;
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002308 blk_rq_init(NULL, rq);
Christoph Hellwig95eeb5f2014-05-01 16:51:05 +02002309
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002310 scmd = (struct scsi_cmnd *)(rq + 1);
2311 scsi_init_command(dev, scmd);
2312 scmd->request = rq;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01002313 scmd->cmnd = scsi_req(rq)->cmd;
Boaz Harrosh64a87b22008-04-30 11:19:47 +03002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 scmd->scsi_done = scsi_reset_provider_done_command;
Boaz Harrosh30b0c372007-12-13 13:47:40 +02002316 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 scmd->cmd_len = 0;
2319
2320 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
James Smartd7a1bb02006-03-08 14:50:12 -05002322 spin_lock_irqsave(shost->host_lock, flags);
2323 shost->tmf_in_progress = 1;
2324 spin_unlock_irqrestore(shost->host_lock, flags);
2325
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002326 switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2327 case SG_SCSI_RESET_NOTHING:
2328 rtn = SUCCESS;
2329 break;
2330 case SG_SCSI_RESET_DEVICE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 rtn = scsi_try_bus_device_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002332 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 break;
2334 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002335 case SG_SCSI_RESET_TARGET:
Mike Christie30bd7df2008-02-29 18:25:19 -06002336 rtn = scsi_try_target_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002337 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Mike Christie30bd7df2008-02-29 18:25:19 -06002338 break;
2339 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002340 case SG_SCSI_RESET_BUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 rtn = scsi_try_bus_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002342 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 break;
2344 /* FALLTHROUGH */
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002345 case SG_SCSI_RESET_HOST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 rtn = scsi_try_host_reset(scmd);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002347 if (rtn == SUCCESS)
2348 break;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002349 /* FALLTHROUGH */
Bart Van Assche3bf2ff62017-08-25 13:46:29 -07002350 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 rtn = FAILED;
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
2354
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002355 error = (rtn == SUCCESS) ? 0 : -EIO;
2356
James Smartd7a1bb02006-03-08 14:50:12 -05002357 spin_lock_irqsave(shost->host_lock, flags);
2358 shost->tmf_in_progress = 0;
2359 spin_unlock_irqrestore(shost->host_lock, flags);
2360
2361 /*
2362 * be sure to wake up anyone who was sleeping or had their queue
2363 * suspended while we performed the TMF.
2364 */
2365 SCSI_LOG_ERROR_RECOVERY(3,
Hannes Reinecke91921e02014-06-25 16:39:59 +02002366 shost_printk(KERN_INFO, shost,
2367 "waking up host to restart after TMF\n"));
James Smartd7a1bb02006-03-08 14:50:12 -05002368
2369 wake_up(&shost->host_wait);
James Smartd7a1bb02006-03-08 14:50:12 -05002370 scsi_run_host_queues(shost);
2371
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002372 scsi_put_command(scmd);
Christoph Hellwige9c787e2017-01-02 21:55:26 +03002373 kfree(rq);
Christoph Hellwig0f121dd2014-09-05 18:00:05 -07002374
Christoph Hellwig04796332014-02-20 14:20:55 -08002375out_put_autopm_host:
Alan Sternbc4f2402010-06-17 10:41:42 -04002376 scsi_autopm_put_host(shost);
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002377 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
Christoph Hellwig176aa9d2014-10-11 12:06:47 +02002379EXPORT_SYMBOL(scsi_ioctl_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Hannes Reinecke4753cbc2014-10-24 14:26:52 +02002381bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2382 struct scsi_sense_hdr *sshdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 return scsi_normalize_sense(cmd->sense_buffer,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002385 SCSI_SENSE_BUFFERSIZE, sshdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387EXPORT_SYMBOL(scsi_command_normalize_sense);
2388
2389/**
Rob Landleyeb448202007-11-03 13:30:39 -05002390 * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * @sense_buffer: byte array of sense data
2392 * @sb_len: number of valid bytes in sense_buffer
2393 * @info_out: pointer to 64 integer where 8 or 4 byte information
2394 * field will be placed if found.
2395 *
2396 * Return value:
Damien Le Moal29087692017-04-24 16:51:12 +09002397 * true if information field found, false if not found.
Randy Dunlapdc8875e2007-11-15 15:42:30 -08002398 */
Damien Le Moal29087692017-04-24 16:51:12 +09002399bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2400 u64 *info_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 const u8 * ucp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 if (sb_len < 7)
Damien Le Moal29087692017-04-24 16:51:12 +09002405 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 switch (sense_buffer[0] & 0x7f) {
2407 case 0x70:
2408 case 0x71:
2409 if (sense_buffer[0] & 0x80) {
Damien Le Moal29087692017-04-24 16:51:12 +09002410 *info_out = get_unaligned_be32(&sense_buffer[3]);
2411 return true;
2412 }
2413 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 case 0x72:
2415 case 0x73:
2416 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2417 0 /* info desc */);
2418 if (ucp && (0xa == ucp[1])) {
Damien Le Moal29087692017-04-24 16:51:12 +09002419 *info_out = get_unaligned_be64(&ucp[4]);
2420 return true;
2421 }
2422 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 default:
Damien Le Moal29087692017-04-24 16:51:12 +09002424 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426}
2427EXPORT_SYMBOL(scsi_get_sense_info_fld);