Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 6 | * Leonard Zubkoff and David Miller at Linux Expo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 13 | * minor cleanups. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * September 30, 2002 Mike Anderson (andmike@us.ibm.com) |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/timer.h> |
| 21 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 23 | #include <linux/freezer.h> |
Christoph Hellwig | c5478de | 2005-09-06 14:04:26 +0200 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/blkdev.h> |
| 27 | #include <linux/delay.h> |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 28 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <scsi/scsi.h> |
Christoph Hellwig | beb4048 | 2006-06-10 18:01:03 +0200 | [diff] [blame] | 31 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <scsi/scsi_dbg.h> |
| 33 | #include <scsi/scsi_device.h> |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 34 | #include <scsi/scsi_driver.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <scsi/scsi_eh.h> |
Sagi Grimberg | 7708c16 | 2015-07-08 17:58:52 +0300 | [diff] [blame] | 36 | #include <scsi/scsi_common.h> |
James Smart | c829c39 | 2006-03-13 08:28:57 -0500 | [diff] [blame] | 37 | #include <scsi/scsi_transport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <scsi/scsi_host.h> |
| 39 | #include <scsi/scsi_ioctl.h> |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 40 | #include <scsi/scsi_dh.h> |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 41 | #include <scsi/sg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | #include "scsi_priv.h" |
| 44 | #include "scsi_logging.h" |
Adrian Bunk | 79ee830 | 2007-08-10 14:50:42 -0700 | [diff] [blame] | 45 | #include "scsi_transport_api.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Kei Tokunaga | bf81623 | 2010-04-01 20:41:40 +0900 | [diff] [blame] | 47 | #include <trace/events/scsi.h> |
| 48 | |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 49 | #include <asm/unaligned.h> |
| 50 | |
James Bottomley | 1421656 | 2012-07-25 23:55:55 +0400 | [diff] [blame] | 51 | static void scsi_eh_done(struct scsi_cmnd *scmd); |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* |
| 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 Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 60 | static int scsi_eh_try_stu(struct scsi_cmnd *scmd); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 61 | static int scsi_try_to_abort_cmd(struct scsi_host_template *, |
| 62 | struct scsi_cmnd *); |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | void scsi_eh_wakeup(struct Scsi_Host *shost) |
| 65 | { |
Bart Van Assche | f0317e8 | 2017-12-04 10:06:24 -0800 | [diff] [blame] | 66 | lockdep_assert_held(shost->host_lock); |
| 67 | |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 68 | if (atomic_read(&shost->host_busy) == shost->host_failed) { |
Kei Tokunaga | bf81623 | 2010-04-01 20:41:40 +0900 | [diff] [blame] | 69 | trace_scsi_eh_wakeup(shost); |
James Bottomley | 3ed7a47 | 2005-09-19 09:50:04 -0500 | [diff] [blame] | 70 | wake_up_process(shost->ehandler); |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 71 | SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost, |
| 72 | "Waking error handler thread\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
Tejun Heo | f8bbfc2 | 2006-05-19 21:07:05 +0900 | [diff] [blame] | 75 | |
| 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 Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 81 | */ |
Tejun Heo | f8bbfc2 | 2006-05-19 21:07:05 +0900 | [diff] [blame] | 82 | void 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 | } |
| 96 | EXPORT_SYMBOL_GPL(scsi_schedule_eh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 98 | static int scsi_host_eh_past_deadline(struct Scsi_Host *shost) |
| 99 | { |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 100 | if (!shost->last_reset || shost->eh_deadline == -1) |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 101 | return 0; |
| 102 | |
Hannes Reinecke | 76ad3e5 | 2013-11-11 13:44:55 +0100 | [diff] [blame] | 103 | /* |
| 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 Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 107 | * if eh_deadline has been set to 'off' during the |
Hannes Reinecke | 76ad3e5 | 2013-11-11 13:44:55 +0100 | [diff] [blame] | 108 | * time_before call. |
| 109 | */ |
| 110 | if (time_before(jiffies, shost->last_reset + shost->eh_deadline) && |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 111 | shost->eh_deadline > -1) |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 112 | return 0; |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /** |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 118 | * scmd_eh_abort_handler - Handle command aborts |
| 119 | * @work: command to be aborted. |
Bart Van Assche | 923f46f | 2018-02-12 10:38:05 -0800 | [diff] [blame] | 120 | * |
| 121 | * Note: this function must be called only for a command that has timed out. |
| 122 | * Because the block layer marks a request as complete before it calls |
| 123 | * scsi_times_out(), a .scsi_done() call from the LLD for a command that has |
| 124 | * timed out do not have any effect. Hence it is safe to call |
| 125 | * scsi_finish_command() from this function. |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 126 | */ |
| 127 | void |
| 128 | scmd_eh_abort_handler(struct work_struct *work) |
| 129 | { |
| 130 | struct scsi_cmnd *scmd = |
| 131 | container_of(work, struct scsi_cmnd, abort_work.work); |
| 132 | struct scsi_device *sdev = scmd->device; |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 133 | int rtn; |
| 134 | |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 135 | if (scsi_host_eh_past_deadline(sdev->host)) { |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 136 | SCSI_LOG_ERROR_RECOVERY(3, |
| 137 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 138 | "eh timeout, not aborting\n")); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 139 | } else { |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 140 | SCSI_LOG_ERROR_RECOVERY(3, |
| 141 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 142 | "aborting command\n")); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 143 | rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd); |
| 144 | if (rtn == SUCCESS) { |
Ulrich Obergfell | 8922a90 | 2014-06-04 13:34:57 +0200 | [diff] [blame] | 145 | set_host_byte(scmd, DID_TIME_OUT); |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 146 | if (scsi_host_eh_past_deadline(sdev->host)) { |
| 147 | SCSI_LOG_ERROR_RECOVERY(3, |
| 148 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 149 | "eh timeout, not retrying " |
| 150 | "aborted command\n")); |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 151 | } else if (!scsi_noretry_cmd(scmd) && |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 152 | (++scmd->retries <= scmd->allowed)) { |
| 153 | SCSI_LOG_ERROR_RECOVERY(3, |
| 154 | scmd_printk(KERN_WARNING, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 155 | "retry aborted command\n")); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 156 | scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY); |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 157 | return; |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 158 | } else { |
| 159 | SCSI_LOG_ERROR_RECOVERY(3, |
| 160 | scmd_printk(KERN_WARNING, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 161 | "finish aborted command\n")); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 162 | scsi_finish_command(scmd); |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 163 | return; |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 164 | } |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 165 | } else { |
| 166 | SCSI_LOG_ERROR_RECOVERY(3, |
| 167 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 168 | "cmd abort %s\n", |
Hannes Reinecke | 883a030 | 2014-10-24 14:27:04 +0200 | [diff] [blame] | 169 | (rtn == FAST_IO_FAIL) ? |
| 170 | "not send" : "failed")); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 171 | } |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 174 | scsi_eh_scmd_add(scmd); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * scsi_abort_command - schedule a command abort |
| 179 | * @scmd: scmd to abort. |
| 180 | * |
| 181 | * We only need to abort commands after a command timeout |
| 182 | */ |
| 183 | static int |
| 184 | scsi_abort_command(struct scsi_cmnd *scmd) |
| 185 | { |
| 186 | struct scsi_device *sdev = scmd->device; |
| 187 | struct Scsi_Host *shost = sdev->host; |
| 188 | unsigned long flags; |
| 189 | |
| 190 | if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) { |
| 191 | /* |
| 192 | * Retry after abort failed, escalate to next level. |
| 193 | */ |
| 194 | SCSI_LOG_ERROR_RECOVERY(3, |
| 195 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 196 | "previous abort failed\n")); |
Bart Van Assche | fcc95a7 | 2014-06-02 11:50:52 +0200 | [diff] [blame] | 197 | BUG_ON(delayed_work_pending(&scmd->abort_work)); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 198 | return FAILED; |
| 199 | } |
| 200 | |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 201 | spin_lock_irqsave(shost->host_lock, flags); |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 202 | if (shost->eh_deadline != -1 && !shost->last_reset) |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 203 | shost->last_reset = jiffies; |
| 204 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 205 | |
| 206 | scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED; |
| 207 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 208 | scmd_printk(KERN_INFO, scmd, "abort scheduled\n")); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 209 | queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100); |
| 210 | return SUCCESS; |
| 211 | } |
| 212 | |
| 213 | /** |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 214 | * scsi_eh_reset - call into ->eh_action to reset internal counters |
| 215 | * @scmd: scmd to run eh on. |
| 216 | * |
| 217 | * The scsi driver might be carrying internal state about the |
| 218 | * devices, so we need to call into the driver to reset the |
| 219 | * internal state once the error handler is started. |
| 220 | */ |
| 221 | static void scsi_eh_reset(struct scsi_cmnd *scmd) |
| 222 | { |
| 223 | if (!blk_rq_is_passthrough(scmd->request)) { |
| 224 | struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd); |
| 225 | if (sdrv->eh_reset) |
| 226 | sdrv->eh_reset(scmd); |
| 227 | } |
| 228 | } |
| 229 | |
Bart Van Assche | 3bd6f43 | 2017-12-04 10:06:23 -0800 | [diff] [blame] | 230 | static void scsi_eh_inc_host_failed(struct rcu_head *head) |
| 231 | { |
Bart Van Assche | 3be8828 | 2018-02-22 11:30:20 -0800 | [diff] [blame] | 232 | struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu); |
| 233 | struct Scsi_Host *shost = scmd->device->host; |
Bart Van Assche | 3bd6f43 | 2017-12-04 10:06:23 -0800 | [diff] [blame] | 234 | unsigned long flags; |
| 235 | |
| 236 | spin_lock_irqsave(shost->host_lock, flags); |
| 237 | shost->host_failed++; |
| 238 | scsi_eh_wakeup(shost); |
| 239 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 240 | } |
| 241 | |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 242 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | * scsi_eh_scmd_add - add scsi cmd to error handling. |
| 244 | * @scmd: scmd to run eh on. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 245 | */ |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 246 | void scsi_eh_scmd_add(struct scsi_cmnd *scmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
| 248 | struct Scsi_Host *shost = scmd->device->host; |
| 249 | unsigned long flags; |
Hannes Reinecke | 2171b6d | 2017-04-06 15:36:34 +0200 | [diff] [blame] | 250 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Hannes Reinecke | 2171b6d | 2017-04-06 15:36:34 +0200 | [diff] [blame] | 252 | WARN_ON_ONCE(!shost->ehandler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | spin_lock_irqsave(shost->host_lock, flags); |
Hannes Reinecke | 2171b6d | 2017-04-06 15:36:34 +0200 | [diff] [blame] | 255 | if (scsi_host_set_state(shost, SHOST_RECOVERY)) { |
| 256 | ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY); |
| 257 | WARN_ON_ONCE(ret); |
| 258 | } |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 259 | if (shost->eh_deadline != -1 && !shost->last_reset) |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 260 | shost->last_reset = jiffies; |
| 261 | |
Hannes Reinecke | 7a38dc0 | 2017-04-06 15:36:29 +0200 | [diff] [blame] | 262 | scsi_eh_reset(scmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | spin_unlock_irqrestore(shost->host_lock, flags); |
Bart Van Assche | 3bd6f43 | 2017-12-04 10:06:23 -0800 | [diff] [blame] | 265 | /* |
| 266 | * Ensure that all tasks observe the host state change before the |
| 267 | * host_failed change. |
| 268 | */ |
Bart Van Assche | 3be8828 | 2018-02-22 11:30:20 -0800 | [diff] [blame] | 269 | call_rcu(&scmd->rcu, scsi_eh_inc_host_failed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | * scsi_times_out - Timeout function for normal scsi commands. |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 274 | * @req: request that is timing out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * |
| 276 | * Notes: |
| 277 | * We do not need to lock this. There is the potential for a race |
| 278 | * only in that the normal completion handling might run, but if the |
| 279 | * normal completion function determines that the timer has already |
| 280 | * fired, then it mustn't do anything. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 281 | */ |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 282 | enum blk_eh_timer_return scsi_times_out(struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Bart Van Assche | bed2213 | 2017-08-25 13:46:32 -0700 | [diff] [blame] | 284 | struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req); |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 285 | enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED; |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 286 | struct Scsi_Host *host = scmd->device->host; |
James Bottomley | 6c5f8ce | 2007-03-16 17:44:41 -0500 | [diff] [blame] | 287 | |
Kei Tokunaga | bf81623 | 2010-04-01 20:41:40 +0900 | [diff] [blame] | 288 | trace_scsi_dispatch_cmd_timeout(scmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | scsi_log_completion(scmd, TIMEOUT_ERROR); |
| 290 | |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 291 | if (host->eh_deadline != -1 && !host->last_reset) |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 292 | host->last_reset = jiffies; |
| 293 | |
Christoph Hellwig | b6a05c8 | 2017-01-30 13:18:58 +0100 | [diff] [blame] | 294 | if (host->hostt->eh_timed_out) |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 295 | rtn = host->hostt->eh_timed_out(scmd); |
James Bottomley | 6c5f8ce | 2007-03-16 17:44:41 -0500 | [diff] [blame] | 296 | |
Hannes Reinecke | a33c070 | 2014-06-13 14:01:45 +0200 | [diff] [blame] | 297 | if (rtn == BLK_EH_NOT_HANDLED) { |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 298 | if (scsi_abort_command(scmd) != SUCCESS) { |
Hannes Reinecke | 2171b6d | 2017-04-06 15:36:34 +0200 | [diff] [blame] | 299 | set_host_byte(scmd, DID_TIME_OUT); |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 300 | scsi_eh_scmd_add(scmd); |
Hannes Reinecke | 2171b6d | 2017-04-06 15:36:34 +0200 | [diff] [blame] | 301 | } |
Hannes Reinecke | a33c070 | 2014-06-13 14:01:45 +0200 | [diff] [blame] | 302 | } |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 303 | |
Christoph Hellwig | fa99078 | 2008-11-05 15:03:15 +0100 | [diff] [blame] | 304 | return rtn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * scsi_block_when_processing_errors - Prevent cmds from being queued. |
| 309 | * @sdev: Device on which we are performing recovery. |
| 310 | * |
| 311 | * Description: |
| 312 | * We block until the host is out of error recovery, and then check to |
| 313 | * see whether the host or the device is offline. |
| 314 | * |
| 315 | * Return value: |
| 316 | * 0 when dev was taken offline by error recovery. 1 OK to proceed. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 317 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | int scsi_block_when_processing_errors(struct scsi_device *sdev) |
| 319 | { |
| 320 | int online; |
| 321 | |
James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 322 | wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | online = scsi_device_online(sdev); |
| 325 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 326 | SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev, |
| 327 | "%s: rtn: %d\n", __func__, online)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | return online; |
| 330 | } |
| 331 | EXPORT_SYMBOL(scsi_block_when_processing_errors); |
| 332 | |
| 333 | #ifdef CONFIG_SCSI_LOGGING |
| 334 | /** |
| 335 | * scsi_eh_prt_fail_stats - Log info on failures. |
| 336 | * @shost: scsi host being recovered. |
| 337 | * @work_q: Queue of scsi cmds to process. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 338 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost, |
| 340 | struct list_head *work_q) |
| 341 | { |
| 342 | struct scsi_cmnd *scmd; |
| 343 | struct scsi_device *sdev; |
| 344 | int total_failures = 0; |
| 345 | int cmd_failed = 0; |
| 346 | int cmd_cancel = 0; |
| 347 | int devices_failed = 0; |
| 348 | |
| 349 | shost_for_each_device(sdev, shost) { |
| 350 | list_for_each_entry(scmd, work_q, eh_entry) { |
| 351 | if (scmd->device == sdev) { |
| 352 | ++total_failures; |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 353 | if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | ++cmd_cancel; |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 355 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | ++cmd_failed; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (cmd_cancel || cmd_failed) { |
| 361 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a3a790d | 2014-10-24 14:27:03 +0200 | [diff] [blame] | 362 | shost_printk(KERN_INFO, shost, |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 363 | "%s: cmds failed: %d, cancel: %d\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 364 | __func__, cmd_failed, |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 365 | cmd_cancel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | cmd_cancel = 0; |
| 367 | cmd_failed = 0; |
| 368 | ++devices_failed; |
| 369 | } |
| 370 | } |
| 371 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 372 | SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost, |
| 373 | "Total of %d commands on %d" |
| 374 | " devices require eh work\n", |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 375 | total_failures, devices_failed)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | #endif |
| 378 | |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 379 | /** |
| 380 | * scsi_report_lun_change - Set flag on all *other* devices on the same target |
| 381 | * to indicate that a UNIT ATTENTION is expected. |
| 382 | * @sdev: Device reporting the UNIT ATTENTION |
| 383 | */ |
| 384 | static void scsi_report_lun_change(struct scsi_device *sdev) |
| 385 | { |
| 386 | sdev->sdev_target->expecting_lun_change = 1; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * scsi_report_sense - Examine scsi sense information and log messages for |
| 391 | * certain conditions, also issue uevents for some of them. |
| 392 | * @sdev: Device reporting the sense code |
| 393 | * @sshdr: sshdr to be examined |
| 394 | */ |
| 395 | static void scsi_report_sense(struct scsi_device *sdev, |
| 396 | struct scsi_sense_hdr *sshdr) |
| 397 | { |
| 398 | enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */ |
| 399 | |
| 400 | if (sshdr->sense_key == UNIT_ATTENTION) { |
| 401 | if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) { |
| 402 | evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED; |
| 403 | sdev_printk(KERN_WARNING, sdev, |
| 404 | "Inquiry data has changed"); |
| 405 | } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) { |
| 406 | evt_type = SDEV_EVT_LUN_CHANGE_REPORTED; |
| 407 | scsi_report_lun_change(sdev); |
| 408 | sdev_printk(KERN_WARNING, sdev, |
| 409 | "Warning! Received an indication that the " |
| 410 | "LUN assignments on this target have " |
| 411 | "changed. The Linux SCSI layer does not " |
| 412 | "automatically remap LUN assignments.\n"); |
| 413 | } else if (sshdr->asc == 0x3f) |
| 414 | sdev_printk(KERN_WARNING, sdev, |
| 415 | "Warning! Received an indication that the " |
| 416 | "operating parameters on this target have " |
| 417 | "changed. The Linux SCSI layer does not " |
| 418 | "automatically adjust these parameters.\n"); |
| 419 | |
| 420 | if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) { |
| 421 | evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED; |
| 422 | sdev_printk(KERN_WARNING, sdev, |
| 423 | "Warning! Received an indication that the " |
| 424 | "LUN reached a thin provisioning soft " |
| 425 | "threshold.\n"); |
| 426 | } |
| 427 | |
Hannes Reinecke | cf3431b | 2017-10-17 09:11:24 +0200 | [diff] [blame] | 428 | if (sshdr->asc == 0x29) { |
| 429 | evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED; |
| 430 | sdev_printk(KERN_WARNING, sdev, |
| 431 | "Power-on or device reset occurred\n"); |
| 432 | } |
| 433 | |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 434 | if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) { |
| 435 | evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED; |
| 436 | sdev_printk(KERN_WARNING, sdev, |
| 437 | "Mode parameters changed"); |
Hannes Reinecke | 14c3e67 | 2015-07-06 13:41:53 +0200 | [diff] [blame] | 438 | } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) { |
| 439 | evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED; |
| 440 | sdev_printk(KERN_WARNING, sdev, |
| 441 | "Asymmetric access state changed"); |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 442 | } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) { |
| 443 | evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED; |
| 444 | sdev_printk(KERN_WARNING, sdev, |
| 445 | "Capacity data has changed"); |
| 446 | } else if (sshdr->asc == 0x2a) |
| 447 | sdev_printk(KERN_WARNING, sdev, |
| 448 | "Parameters changed"); |
| 449 | } |
| 450 | |
| 451 | if (evt_type != SDEV_EVT_MAXBITS) { |
| 452 | set_bit(evt_type, sdev->pending_events); |
| 453 | schedule_work(&sdev->event_work); |
| 454 | } |
| 455 | } |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | /** |
| 458 | * scsi_check_sense - Examine scsi cmd sense |
| 459 | * @scmd: Cmd to have sense checked. |
| 460 | * |
| 461 | * Return value: |
Hannes Reinecke | 87f14e6 | 2013-07-01 15:16:24 +0200 | [diff] [blame] | 462 | * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * |
| 464 | * Notes: |
| 465 | * When a deferred error is detected the current command has |
| 466 | * not been executed and needs retrying. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 467 | */ |
Hannes Reinecke | 3852e37 | 2016-04-04 11:44:01 +0200 | [diff] [blame] | 468 | int scsi_check_sense(struct scsi_cmnd *scmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 470 | struct scsi_device *sdev = scmd->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | struct scsi_sense_hdr sshdr; |
| 472 | |
| 473 | if (! scsi_command_normalize_sense(scmd, &sshdr)) |
| 474 | return FAILED; /* no valid sense data */ |
| 475 | |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 476 | scsi_report_sense(sdev, &sshdr); |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (scsi_sense_is_deferred(&sshdr)) |
| 479 | return NEEDS_RETRY; |
| 480 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 481 | if (sdev->handler && sdev->handler->check_sense) { |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 482 | int rc; |
| 483 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 484 | rc = sdev->handler->check_sense(sdev, &sshdr); |
Chandra Seetharaman | a6a8d9f | 2008-05-01 14:49:46 -0700 | [diff] [blame] | 485 | if (rc != SCSI_RETURN_NOT_HANDLED) |
| 486 | return rc; |
| 487 | /* handler does not care. Drop down to default handling */ |
| 488 | } |
| 489 | |
Christoph Hellwig | e925cc4 | 2014-11-06 15:11:22 -0600 | [diff] [blame] | 490 | if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done) |
| 491 | /* |
| 492 | * nasty: for mid-layer issued TURs, we need to return the |
| 493 | * actual sense data without any recovery attempt. For eh |
| 494 | * issued ones, we need to try to recover and interpret |
| 495 | */ |
| 496 | return SUCCESS; |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /* |
| 499 | * Previous logic looked for FILEMARK, EOM or ILI which are |
| 500 | * mainly associated with tapes and returned SUCCESS. |
| 501 | */ |
| 502 | if (sshdr.response_code == 0x70) { |
| 503 | /* fixed format */ |
| 504 | if (scmd->sense_buffer[2] & 0xe0) |
| 505 | return SUCCESS; |
| 506 | } else { |
| 507 | /* |
| 508 | * descriptor format: look for "stream commands sense data |
| 509 | * descriptor" (see SSC-3). Assume single sense data |
| 510 | * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG. |
| 511 | */ |
| 512 | if ((sshdr.additional_length > 3) && |
| 513 | (scmd->sense_buffer[8] == 0x4) && |
| 514 | (scmd->sense_buffer[11] & 0xe0)) |
| 515 | return SUCCESS; |
| 516 | } |
| 517 | |
| 518 | switch (sshdr.sense_key) { |
| 519 | case NO_SENSE: |
| 520 | return SUCCESS; |
| 521 | case RECOVERED_ERROR: |
| 522 | return /* soft_error */ SUCCESS; |
| 523 | |
| 524 | case ABORTED_COMMAND: |
Martin K. Petersen | 511e44f | 2008-07-17 04:28:33 -0400 | [diff] [blame] | 525 | if (sshdr.asc == 0x10) /* DIF */ |
| 526 | return SUCCESS; |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return NEEDS_RETRY; |
| 529 | case NOT_READY: |
| 530 | case UNIT_ATTENTION: |
| 531 | /* |
| 532 | * if we are expecting a cc/ua because of a bus reset that we |
| 533 | * performed, treat this just as a retry. otherwise this is |
| 534 | * information that we should pass up to the upper-level driver |
| 535 | * so that we can deal with it there. |
| 536 | */ |
| 537 | if (scmd->device->expecting_cc_ua) { |
TARUISI Hiroaki | dfcf777 | 2011-08-11 20:25:20 +0900 | [diff] [blame] | 538 | /* |
| 539 | * Because some device does not queue unit |
| 540 | * attentions correctly, we carefully check |
| 541 | * additional sense code and qualifier so as |
| 542 | * not to squash media change unit attention. |
| 543 | */ |
| 544 | if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) { |
| 545 | scmd->device->expecting_cc_ua = 0; |
| 546 | return NEEDS_RETRY; |
| 547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | /* |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 550 | * we might also expect a cc/ua if another LUN on the target |
| 551 | * reported a UA with an ASC/ASCQ of 3F 0E - |
| 552 | * REPORTED LUNS DATA HAS CHANGED. |
| 553 | */ |
| 554 | if (scmd->device->sdev_target->expecting_lun_change && |
| 555 | sshdr.asc == 0x3f && sshdr.ascq == 0x0e) |
| 556 | return NEEDS_RETRY; |
| 557 | /* |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 558 | * if the device is in the process of becoming ready, we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | * should retry. |
| 560 | */ |
| 561 | if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01)) |
| 562 | return NEEDS_RETRY; |
| 563 | /* |
| 564 | * if the device is not started, we need to wake |
| 565 | * the error handler to start the motor |
| 566 | */ |
| 567 | if (scmd->device->allow_restart && |
| 568 | (sshdr.asc == 0x04) && (sshdr.ascq == 0x02)) |
| 569 | return FAILED; |
Christoph Hellwig | 02e031c | 2010-11-10 14:54:09 +0100 | [diff] [blame] | 570 | /* |
| 571 | * Pass the UA upwards for a determination in the completion |
| 572 | * functions. |
| 573 | */ |
| 574 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 576 | /* these are not supported */ |
Hannes Reinecke | a9d6ceb8 | 2013-07-01 15:16:25 +0200 | [diff] [blame] | 577 | case DATA_PROTECT: |
| 578 | if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) { |
| 579 | /* Thin provisioning hard threshold reached */ |
| 580 | set_host_byte(scmd, DID_ALLOC_FAILURE); |
| 581 | return SUCCESS; |
| 582 | } |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 583 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | case COPY_ABORTED: |
| 585 | case VOLUME_OVERFLOW: |
| 586 | case MISCOMPARE: |
Hannes Reinecke | 63583cc | 2011-01-18 10:13:11 +0100 | [diff] [blame] | 587 | case BLANK_CHECK: |
Hannes Reinecke | 87f14e6 | 2013-07-01 15:16:24 +0200 | [diff] [blame] | 588 | set_host_byte(scmd, DID_TARGET_FAILURE); |
| 589 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | case MEDIUM_ERROR: |
Luben Tuikov | fd1b494 | 2006-11-29 19:45:23 -0800 | [diff] [blame] | 592 | if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */ |
| 593 | sshdr.asc == 0x13 || /* AMNF DATA FIELD */ |
| 594 | sshdr.asc == 0x14) { /* RECORD NOT FOUND */ |
Hannes Reinecke | 7e782af | 2013-07-01 15:16:26 +0200 | [diff] [blame] | 595 | set_host_byte(scmd, DID_MEDIUM_ERROR); |
Hannes Reinecke | 87f14e6 | 2013-07-01 15:16:24 +0200 | [diff] [blame] | 596 | return SUCCESS; |
Luben Tuikov | fd1b494 | 2006-11-29 19:45:23 -0800 | [diff] [blame] | 597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return NEEDS_RETRY; |
| 599 | |
| 600 | case HARDWARE_ERROR: |
| 601 | if (scmd->device->retry_hwerror) |
Mike Anderson | bb0003c | 2008-08-12 12:11:58 -0700 | [diff] [blame] | 602 | return ADD_TO_MLQUEUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | else |
Hannes Reinecke | 87f14e6 | 2013-07-01 15:16:24 +0200 | [diff] [blame] | 604 | set_host_byte(scmd, DID_TARGET_FAILURE); |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 605 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | case ILLEGAL_REQUEST: |
Mike Snitzer | 47ac56d | 2012-02-13 18:35:11 -0500 | [diff] [blame] | 608 | if (sshdr.asc == 0x20 || /* Invalid command operation code */ |
| 609 | sshdr.asc == 0x21 || /* Logical block address out of range */ |
Hannes Reinecke | a8bbb2a | 2017-10-17 09:10:56 +0200 | [diff] [blame] | 610 | sshdr.asc == 0x22 || /* Invalid function */ |
Mike Snitzer | 47ac56d | 2012-02-13 18:35:11 -0500 | [diff] [blame] | 611 | sshdr.asc == 0x24 || /* Invalid field in cdb */ |
Martin Wilck | d0b7a90 | 2017-09-27 14:44:19 +0200 | [diff] [blame] | 612 | sshdr.asc == 0x26 || /* Parameter value invalid */ |
| 613 | sshdr.asc == 0x27) { /* Write protected */ |
Hannes Reinecke | 87f14e6 | 2013-07-01 15:16:24 +0200 | [diff] [blame] | 614 | set_host_byte(scmd, DID_TARGET_FAILURE); |
Mike Snitzer | 47ac56d | 2012-02-13 18:35:11 -0500 | [diff] [blame] | 615 | } |
| 616 | return SUCCESS; |
| 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | default: |
| 619 | return SUCCESS; |
| 620 | } |
| 621 | } |
Hannes Reinecke | 3852e37 | 2016-04-04 11:44:01 +0200 | [diff] [blame] | 622 | EXPORT_SYMBOL_GPL(scsi_check_sense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Vasu Dev | 4a84067 | 2009-10-22 15:46:33 -0700 | [diff] [blame] | 624 | static void scsi_handle_queue_ramp_up(struct scsi_device *sdev) |
| 625 | { |
| 626 | struct scsi_host_template *sht = sdev->host->hostt; |
| 627 | struct scsi_device *tmp_sdev; |
| 628 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 629 | if (!sht->track_queue_depth || |
Vasu Dev | 4a84067 | 2009-10-22 15:46:33 -0700 | [diff] [blame] | 630 | sdev->queue_depth >= sdev->max_queue_depth) |
| 631 | return; |
| 632 | |
| 633 | if (time_before(jiffies, |
| 634 | sdev->last_queue_ramp_up + sdev->queue_ramp_up_period)) |
| 635 | return; |
| 636 | |
| 637 | if (time_before(jiffies, |
| 638 | sdev->last_queue_full_time + sdev->queue_ramp_up_period)) |
| 639 | return; |
| 640 | |
| 641 | /* |
| 642 | * Walk all devices of a target and do |
| 643 | * ramp up on them. |
| 644 | */ |
| 645 | shost_for_each_device(tmp_sdev, sdev->host) { |
| 646 | if (tmp_sdev->channel != sdev->channel || |
| 647 | tmp_sdev->id != sdev->id || |
| 648 | tmp_sdev->queue_depth == sdev->max_queue_depth) |
| 649 | continue; |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 650 | |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 651 | scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1); |
Vasu Dev | 4a84067 | 2009-10-22 15:46:33 -0700 | [diff] [blame] | 652 | sdev->last_queue_ramp_up = jiffies; |
| 653 | } |
| 654 | } |
| 655 | |
Mike Christie | 42a6a91 | 2009-10-15 17:46:44 -0700 | [diff] [blame] | 656 | static void scsi_handle_queue_full(struct scsi_device *sdev) |
| 657 | { |
| 658 | struct scsi_host_template *sht = sdev->host->hostt; |
| 659 | struct scsi_device *tmp_sdev; |
| 660 | |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 661 | if (!sht->track_queue_depth) |
Mike Christie | 42a6a91 | 2009-10-15 17:46:44 -0700 | [diff] [blame] | 662 | return; |
| 663 | |
| 664 | shost_for_each_device(tmp_sdev, sdev->host) { |
| 665 | if (tmp_sdev->channel != sdev->channel || |
| 666 | tmp_sdev->id != sdev->id) |
| 667 | continue; |
| 668 | /* |
| 669 | * We do not know the number of commands that were at |
| 670 | * the device when we got the queue full so we start |
| 671 | * from the highest possible value and work our way down. |
| 672 | */ |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 673 | scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1); |
Mike Christie | 42a6a91 | 2009-10-15 17:46:44 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /** |
| 678 | * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD. |
| 679 | * @scmd: SCSI cmd to examine. |
| 680 | * |
| 681 | * Notes: |
| 682 | * This is *only* called when we are examining the status of commands |
| 683 | * queued during error recovery. the main difference here is that we |
| 684 | * don't allow for the possibility of retries here, and we are a lot |
| 685 | * more restrictive about what we consider acceptable. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 686 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | static int scsi_eh_completed_normally(struct scsi_cmnd *scmd) |
| 688 | { |
| 689 | /* |
| 690 | * first check the host byte, to see if there is anything in there |
| 691 | * that would indicate what we need to do. |
| 692 | */ |
| 693 | if (host_byte(scmd->result) == DID_RESET) { |
| 694 | /* |
| 695 | * rats. we are already in the error handler, so we now |
| 696 | * get to try and figure out what to do next. if the sense |
| 697 | * is valid, we have a pretty good idea of what to do. |
| 698 | * if not, we mark it as FAILED. |
| 699 | */ |
| 700 | return scsi_check_sense(scmd); |
| 701 | } |
| 702 | if (host_byte(scmd->result) != DID_OK) |
| 703 | return FAILED; |
| 704 | |
| 705 | /* |
| 706 | * next, check the message byte. |
| 707 | */ |
| 708 | if (msg_byte(scmd->result) != COMMAND_COMPLETE) |
| 709 | return FAILED; |
| 710 | |
| 711 | /* |
| 712 | * now, check the status byte to see if this indicates |
| 713 | * anything special. |
| 714 | */ |
| 715 | switch (status_byte(scmd->result)) { |
| 716 | case GOOD: |
Vasu Dev | 4a84067 | 2009-10-22 15:46:33 -0700 | [diff] [blame] | 717 | scsi_handle_queue_ramp_up(scmd->device); |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 718 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | case COMMAND_TERMINATED: |
| 720 | return SUCCESS; |
| 721 | case CHECK_CONDITION: |
| 722 | return scsi_check_sense(scmd); |
| 723 | case CONDITION_GOOD: |
| 724 | case INTERMEDIATE_GOOD: |
| 725 | case INTERMEDIATE_C_GOOD: |
| 726 | /* |
| 727 | * who knows? FIXME(eric) |
| 728 | */ |
| 729 | return SUCCESS; |
Michael Reed | 5f91bb0 | 2009-08-10 11:59:28 -0500 | [diff] [blame] | 730 | case RESERVATION_CONFLICT: |
James Bottomley | 67110df | 2010-08-06 15:17:24 -0500 | [diff] [blame] | 731 | if (scmd->cmnd[0] == TEST_UNIT_READY) |
| 732 | /* it is a success, we probed the device and |
| 733 | * found it */ |
| 734 | return SUCCESS; |
| 735 | /* otherwise, we failed to send the command */ |
| 736 | return FAILED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | case QUEUE_FULL: |
Mike Christie | 42a6a91 | 2009-10-15 17:46:44 -0700 | [diff] [blame] | 738 | scsi_handle_queue_full(scmd->device); |
| 739 | /* fall through */ |
| 740 | case BUSY: |
Hannes Reinecke | 3eb3a92 | 2010-07-29 10:10:16 +0200 | [diff] [blame] | 741 | return NEEDS_RETRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | default: |
| 743 | return FAILED; |
| 744 | } |
| 745 | return FAILED; |
| 746 | } |
| 747 | |
| 748 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | * scsi_eh_done - Completion function for error handling. |
| 750 | * @scmd: Cmd that is done. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 751 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | static void scsi_eh_done(struct scsi_cmnd *scmd) |
| 753 | { |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 754 | struct completion *eh_action; |
Michael Reed | 8563167 | 2005-12-07 21:46:27 -0600 | [diff] [blame] | 755 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 756 | SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 757 | "%s result: %x\n", __func__, scmd->result)); |
Michael Reed | 8563167 | 2005-12-07 21:46:27 -0600 | [diff] [blame] | 758 | |
| 759 | eh_action = scmd->device->host->eh_action; |
| 760 | if (eh_action) |
| 761 | complete(eh_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /** |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 765 | * scsi_try_host_reset - ask host adapter to reset itself |
Geert Uytterhoeven | c2b3ebd | 2013-05-17 13:22:29 +0200 | [diff] [blame] | 766 | * @scmd: SCSI cmd to send host reset. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 767 | */ |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 768 | static int scsi_try_host_reset(struct scsi_cmnd *scmd) |
| 769 | { |
| 770 | unsigned long flags; |
| 771 | int rtn; |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 772 | struct Scsi_Host *host = scmd->device->host; |
| 773 | struct scsi_host_template *hostt = host->hostt; |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 774 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 775 | SCSI_LOG_ERROR_RECOVERY(3, |
| 776 | shost_printk(KERN_INFO, host, "Snd Host RST\n")); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 777 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 778 | if (!hostt->eh_host_reset_handler) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 779 | return FAILED; |
| 780 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 781 | rtn = hostt->eh_host_reset_handler(scmd); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 782 | |
| 783 | if (rtn == SUCCESS) { |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 784 | if (!hostt->skip_settle_delay) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 785 | ssleep(HOST_RESET_SETTLE_TIME); |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 786 | spin_lock_irqsave(host->host_lock, flags); |
| 787 | scsi_report_bus_reset(host, scmd_channel(scmd)); |
| 788 | spin_unlock_irqrestore(host->host_lock, flags); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | return rtn; |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * scsi_try_bus_reset - ask host to perform a bus reset |
| 796 | * @scmd: SCSI cmd to send bus reset. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 797 | */ |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 798 | static int scsi_try_bus_reset(struct scsi_cmnd *scmd) |
| 799 | { |
| 800 | unsigned long flags; |
| 801 | int rtn; |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 802 | struct Scsi_Host *host = scmd->device->host; |
| 803 | struct scsi_host_template *hostt = host->hostt; |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 804 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 805 | SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, |
| 806 | "%s: Snd Bus RST\n", __func__)); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 807 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 808 | if (!hostt->eh_bus_reset_handler) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 809 | return FAILED; |
| 810 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 811 | rtn = hostt->eh_bus_reset_handler(scmd); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 812 | |
| 813 | if (rtn == SUCCESS) { |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 814 | if (!hostt->skip_settle_delay) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 815 | ssleep(BUS_RESET_SETTLE_TIME); |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 816 | spin_lock_irqsave(host->host_lock, flags); |
| 817 | scsi_report_bus_reset(host, scmd_channel(scmd)); |
| 818 | spin_unlock_irqrestore(host->host_lock, flags); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return rtn; |
| 822 | } |
| 823 | |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 824 | static void __scsi_report_device_reset(struct scsi_device *sdev, void *data) |
| 825 | { |
| 826 | sdev->was_reset = 1; |
| 827 | sdev->expecting_cc_ua = 1; |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * scsi_try_target_reset - Ask host to perform a target reset |
| 832 | * @scmd: SCSI cmd used to send a target reset |
| 833 | * |
| 834 | * Notes: |
| 835 | * There is no timeout for this operation. if this operation is |
| 836 | * unreliable for a given host, then the host itself needs to put a |
| 837 | * timer on it, and set the host back to a consistent state prior to |
| 838 | * returning. |
| 839 | */ |
| 840 | static int scsi_try_target_reset(struct scsi_cmnd *scmd) |
| 841 | { |
| 842 | unsigned long flags; |
| 843 | int rtn; |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 844 | struct Scsi_Host *host = scmd->device->host; |
| 845 | struct scsi_host_template *hostt = host->hostt; |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 846 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 847 | if (!hostt->eh_target_reset_handler) |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 848 | return FAILED; |
| 849 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 850 | rtn = hostt->eh_target_reset_handler(scmd); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 851 | if (rtn == SUCCESS) { |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 852 | spin_lock_irqsave(host->host_lock, flags); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 853 | __starget_for_each_device(scsi_target(scmd->device), NULL, |
| 854 | __scsi_report_device_reset); |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 855 | spin_unlock_irqrestore(host->host_lock, flags); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | return rtn; |
| 859 | } |
| 860 | |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 861 | /** |
| 862 | * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev |
| 863 | * @scmd: SCSI cmd used to send BDR |
| 864 | * |
| 865 | * Notes: |
| 866 | * There is no timeout for this operation. if this operation is |
| 867 | * unreliable for a given host, then the host itself needs to put a |
| 868 | * timer on it, and set the host back to a consistent state prior to |
| 869 | * returning. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 870 | */ |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 871 | static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd) |
| 872 | { |
| 873 | int rtn; |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 874 | struct scsi_host_template *hostt = scmd->device->host->hostt; |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 875 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 876 | if (!hostt->eh_device_reset_handler) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 877 | return FAILED; |
| 878 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 879 | rtn = hostt->eh_device_reset_handler(scmd); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 880 | if (rtn == SUCCESS) |
| 881 | __scsi_report_device_reset(scmd->device, NULL); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 882 | return rtn; |
| 883 | } |
| 884 | |
Hannes Reinecke | 883a030 | 2014-10-24 14:27:04 +0200 | [diff] [blame] | 885 | /** |
| 886 | * scsi_try_to_abort_cmd - Ask host to abort a SCSI command |
Randy Dunlap | 6583f6f | 2014-12-29 09:40:56 -0800 | [diff] [blame] | 887 | * @hostt: SCSI driver host template |
Hannes Reinecke | 883a030 | 2014-10-24 14:27:04 +0200 | [diff] [blame] | 888 | * @scmd: SCSI cmd used to send a target reset |
| 889 | * |
| 890 | * Return value: |
| 891 | * SUCCESS, FAILED, or FAST_IO_FAIL |
| 892 | * |
| 893 | * Notes: |
| 894 | * SUCCESS does not necessarily indicate that the command |
| 895 | * has been aborted; it only indicates that the LLDDs |
| 896 | * has cleared all references to that command. |
| 897 | * LLDDs should return FAILED only if an abort was required |
| 898 | * but could not be executed. LLDDs should return FAST_IO_FAIL |
| 899 | * if the device is temporarily unavailable (eg due to a |
| 900 | * link down on FibreChannel) |
| 901 | */ |
| 902 | static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt, |
| 903 | struct scsi_cmnd *scmd) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 904 | { |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 905 | if (!hostt->eh_abort_handler) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 906 | return FAILED; |
| 907 | |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 908 | return hostt->eh_abort_handler(scmd); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 909 | } |
| 910 | |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 911 | static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd) |
| 912 | { |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 913 | if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS) |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 914 | if (scsi_try_bus_device_reset(scmd) != SUCCESS) |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 915 | if (scsi_try_target_reset(scmd) != SUCCESS) |
| 916 | if (scsi_try_bus_reset(scmd) != SUCCESS) |
| 917 | scsi_try_host_reset(scmd); |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /** |
Santosh Y | 3b729f7 | 2012-04-08 18:47:09 +0530 | [diff] [blame] | 921 | * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery |
Christoph Hellwig | 2dc611d | 2006-11-04 20:04:21 +0100 | [diff] [blame] | 922 | * @scmd: SCSI command structure to hijack |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 923 | * @ses: structure to save restore information |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 924 | * @cmnd: CDB to send. Can be NULL if no new cmnd is needed |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 925 | * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB) |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 926 | * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored) |
Christoph Hellwig | 2dc611d | 2006-11-04 20:04:21 +0100 | [diff] [blame] | 927 | * |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 928 | * This function is used to save a scsi command information before re-execution |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 929 | * as part of the error recovery process. If @sense_bytes is 0 the command |
| 930 | * sent must be one that does not transfer any data. If @sense_bytes != 0 |
| 931 | * @cmnd is ignored and this functions sets up a REQUEST_SENSE command |
| 932 | * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 933 | */ |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 934 | void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses, |
| 935 | unsigned char *cmnd, int cmnd_size, unsigned sense_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | { |
| f59114b | 2005-04-17 15:00:23 -0500 | [diff] [blame] | 937 | struct scsi_device *sdev = scmd->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 939 | /* |
| 940 | * We need saved copies of a number of fields - this is because |
| 941 | * error handling may need to overwrite these with different values |
| 942 | * to run different commands, and once error handling is complete, |
| 943 | * we will need to restore these values prior to running the actual |
| 944 | * command. |
| 945 | */ |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 946 | ses->cmd_len = scmd->cmd_len; |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 947 | ses->cmnd = scmd->cmnd; |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 948 | ses->data_direction = scmd->sc_data_direction; |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 949 | ses->sdb = scmd->sdb; |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 950 | ses->next_rq = scmd->request->next_rq; |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 951 | ses->result = scmd->result; |
Alan Stern | 1226570 | 2008-07-21 10:25:52 -0400 | [diff] [blame] | 952 | ses->underflow = scmd->underflow; |
Martin K. Petersen | db007fc | 2008-07-17 04:28:31 -0400 | [diff] [blame] | 953 | ses->prot_op = scmd->prot_op; |
Hannes Reinecke | 8e8c9d0 | 2017-04-06 15:36:33 +0200 | [diff] [blame] | 954 | ses->eh_eflags = scmd->eh_eflags; |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 955 | |
Martin K. Petersen | db007fc | 2008-07-17 04:28:31 -0400 | [diff] [blame] | 956 | scmd->prot_op = SCSI_PROT_NORMAL; |
James Bottomley | c69e6f8 | 2014-04-10 13:36:11 -0700 | [diff] [blame] | 957 | scmd->eh_eflags = 0; |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 958 | scmd->cmnd = ses->eh_cmnd; |
| 959 | memset(scmd->cmnd, 0, BLK_MAX_CDB); |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 960 | memset(&scmd->sdb, 0, sizeof(scmd->sdb)); |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 961 | scmd->request->next_rq = NULL; |
Alan Stern | 644373a | 2014-03-28 10:51:15 -0700 | [diff] [blame] | 962 | scmd->result = 0; |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 963 | |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 964 | if (sense_bytes) { |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 965 | scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE, |
| 966 | sense_bytes); |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 967 | sg_init_one(&ses->sense_sgl, scmd->sense_buffer, |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 968 | scmd->sdb.length); |
| 969 | scmd->sdb.table.sgl = &ses->sense_sgl; |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 970 | scmd->sc_data_direction = DMA_FROM_DEVICE; |
Tony Battersby | 0c958ec | 2015-07-16 11:40:41 -0400 | [diff] [blame] | 971 | scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1; |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 972 | scmd->cmnd[0] = REQUEST_SENSE; |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 973 | scmd->cmnd[4] = scmd->sdb.length; |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 974 | scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]); |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 975 | } else { |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 976 | scmd->sc_data_direction = DMA_NONE; |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 977 | if (cmnd) { |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 978 | BUG_ON(cmnd_size > BLK_MAX_CDB); |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 979 | memcpy(scmd->cmnd, cmnd, cmnd_size); |
| 980 | scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]); |
| 981 | } |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | scmd->underflow = 0; |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 985 | |
Boaz Harrosh | 55db6c1 | 2007-10-08 16:35:19 +0200 | [diff] [blame] | 986 | if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) | |
| f59114b | 2005-04-17 15:00:23 -0500 | [diff] [blame] | 988 | (sdev->lun << 5 & 0xe0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 990 | /* |
| 991 | * Zero the sense buffer. The scsi spec mandates that any |
| 992 | * untransferred sense data should be interpreted as being zero. |
| 993 | */ |
FUJITA Tomonori | b80ca4f | 2008-01-13 15:46:13 +0900 | [diff] [blame] | 994 | memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 995 | } |
| 996 | EXPORT_SYMBOL(scsi_eh_prep_cmnd); |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 997 | |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 998 | /** |
Santosh Y | 3b729f7 | 2012-04-08 18:47:09 +0530 | [diff] [blame] | 999 | * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1000 | * @scmd: SCSI command structure to restore |
Bartlomiej Zolnierkiewicz | 477e608 | 2009-04-27 20:54:22 +0200 | [diff] [blame] | 1001 | * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1002 | * |
Bartlomiej Zolnierkiewicz | 477e608 | 2009-04-27 20:54:22 +0200 | [diff] [blame] | 1003 | * Undo any damage done by above scsi_eh_prep_cmnd(). |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1004 | */ |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1005 | void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses) |
| 1006 | { |
| 1007 | /* |
| 1008 | * Restore original data |
| 1009 | */ |
| 1010 | scmd->cmd_len = ses->cmd_len; |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 1011 | scmd->cmnd = ses->cmnd; |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1012 | scmd->sc_data_direction = ses->data_direction; |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 1013 | scmd->sdb = ses->sdb; |
Boaz Harrosh | 6f9a35e | 2007-12-13 13:50:53 +0200 | [diff] [blame] | 1014 | scmd->request->next_rq = ses->next_rq; |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1015 | scmd->result = ses->result; |
Alan Stern | 1226570 | 2008-07-21 10:25:52 -0400 | [diff] [blame] | 1016 | scmd->underflow = ses->underflow; |
Martin K. Petersen | db007fc | 2008-07-17 04:28:31 -0400 | [diff] [blame] | 1017 | scmd->prot_op = ses->prot_op; |
Hannes Reinecke | 8e8c9d0 | 2017-04-06 15:36:33 +0200 | [diff] [blame] | 1018 | scmd->eh_eflags = ses->eh_eflags; |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1019 | } |
| 1020 | EXPORT_SYMBOL(scsi_eh_restore_cmnd); |
| 1021 | |
| 1022 | /** |
Santosh Y | 3b729f7 | 2012-04-08 18:47:09 +0530 | [diff] [blame] | 1023 | * scsi_send_eh_cmnd - submit a scsi command as part of error recovery |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1024 | * @scmd: SCSI command structure to hijack |
| 1025 | * @cmnd: CDB to send |
| 1026 | * @cmnd_size: size in bytes of @cmnd |
| 1027 | * @timeout: timeout for this request |
| 1028 | * @sense_bytes: size of sense data to copy or 0 |
| 1029 | * |
| 1030 | * This function is used to send a scsi command down to a target device |
| 1031 | * as part of the error recovery process. See also scsi_eh_prep_cmnd() above. |
| 1032 | * |
| 1033 | * Return value: |
| 1034 | * SUCCESS or FAILED or NEEDS_RETRY |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1035 | */ |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1036 | static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd, |
| 1037 | int cmnd_size, int timeout, unsigned sense_bytes) |
| 1038 | { |
| 1039 | struct scsi_device *sdev = scmd->device; |
| 1040 | struct Scsi_Host *shost = sdev->host; |
| 1041 | DECLARE_COMPLETION_ONSTACK(done); |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1042 | unsigned long timeleft = timeout; |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1043 | struct scsi_eh_save ses; |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1044 | const unsigned long stall_for = msecs_to_jiffies(100); |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1045 | int rtn; |
| 1046 | |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1047 | retry: |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1048 | scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes); |
Christoph Hellwig | 7dfdc9a | 2005-10-31 18:49:52 +0100 | [diff] [blame] | 1049 | shost->eh_action = &done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | scsi_log_send(scmd); |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 1052 | scmd->scsi_done = scsi_eh_done; |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1053 | rtn = shost->hostt->queuecommand(shost, scmd); |
| 1054 | if (rtn) { |
| 1055 | if (timeleft > stall_for) { |
| 1056 | scsi_eh_restore_cmnd(scmd, &ses); |
| 1057 | timeleft -= stall_for; |
| 1058 | msleep(jiffies_to_msecs(stall_for)); |
| 1059 | goto retry; |
| 1060 | } |
| 1061 | /* signal not to enter either branch of the if () below */ |
| 1062 | timeleft = 0; |
Alan Stern | 511833a | 2014-11-21 10:44:49 -0500 | [diff] [blame] | 1063 | rtn = FAILED; |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1064 | } else { |
| 1065 | timeleft = wait_for_completion_timeout(&done, timeout); |
Hannes Reinecke | ac61d19 | 2014-05-08 08:09:56 +0200 | [diff] [blame] | 1066 | rtn = SUCCESS; |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
| f59114b | 2005-04-17 15:00:23 -0500 | [diff] [blame] | 1069 | shost->eh_action = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1071 | scsi_log_completion(scmd, rtn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1073 | SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 1074 | "%s timeleft: %ld\n", |
| 1075 | __func__, timeleft)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* |
Hannes Reinecke | fc73648 | 2013-04-25 08:10:00 +0200 | [diff] [blame] | 1078 | * If there is time left scsi_eh_done got called, and we will examine |
| 1079 | * the actual status codes to see whether the command actually did |
| 1080 | * complete normally, else if we have a zero return and no time left, |
| 1081 | * the command must still be pending, so abort it and return FAILED. |
| 1082 | * If we never actually managed to issue the command, because |
| 1083 | * ->queuecommand() kept returning non zero, use the rtn = FAILED |
| 1084 | * value above (so don't execute either branch of the if) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | */ |
Christoph Hellwig | 7dfdc9a | 2005-10-31 18:49:52 +0100 | [diff] [blame] | 1086 | if (timeleft) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | rtn = scsi_eh_completed_normally(scmd); |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1088 | SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, |
| 1089 | "%s: scsi_eh_completed_normally %x\n", __func__, rtn)); |
Christoph Hellwig | 7dfdc9a | 2005-10-31 18:49:52 +0100 | [diff] [blame] | 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | switch (rtn) { |
| 1092 | case SUCCESS: |
| 1093 | case NEEDS_RETRY: |
| 1094 | case FAILED: |
| 1095 | break; |
Hannes Reinecke | 6e883b0 | 2009-09-17 17:00:26 +0200 | [diff] [blame] | 1096 | case ADD_TO_MLQUEUE: |
| 1097 | rtn = NEEDS_RETRY; |
| 1098 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | default: |
| 1100 | rtn = FAILED; |
| 1101 | break; |
| 1102 | } |
Alan Stern | 511833a | 2014-11-21 10:44:49 -0500 | [diff] [blame] | 1103 | } else if (rtn != FAILED) { |
Brian King | 292148f | 2007-01-30 17:51:17 -0600 | [diff] [blame] | 1104 | scsi_abort_eh_cmnd(scmd); |
Christoph Hellwig | 7dfdc9a | 2005-10-31 18:49:52 +0100 | [diff] [blame] | 1105 | rtn = FAILED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Boaz Harrosh | e1c2346 | 2007-10-08 16:36:45 +0200 | [diff] [blame] | 1108 | scsi_eh_restore_cmnd(scmd, &ses); |
Martin K. Petersen | 18a4d0a | 2012-02-09 13:48:53 -0500 | [diff] [blame] | 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return rtn; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * scsi_request_sense - Request sense data from a particular target. |
| 1115 | * @scmd: SCSI cmd for request sense. |
| 1116 | * |
| 1117 | * Notes: |
| 1118 | * Some hosts automatically obtain this information, others require |
| 1119 | * that we obtain it on our own. This function will *not* return until |
| 1120 | * the command either times out, or it completes. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1121 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | static int scsi_request_sense(struct scsi_cmnd *scmd) |
| 1123 | { |
Martin K. Petersen | 0816c92 | 2013-05-10 10:36:04 -0400 | [diff] [blame] | 1124 | return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1127 | static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn) |
| 1128 | { |
Christoph Hellwig | 57292b5 | 2017-01-31 16:57:29 +0100 | [diff] [blame] | 1129 | if (!blk_rq_is_passthrough(scmd->request)) { |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1130 | struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd); |
| 1131 | if (sdrv->eh_action) |
| 1132 | rtn = sdrv->eh_action(scmd, rtn); |
| 1133 | } |
| 1134 | return rtn; |
| 1135 | } |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | /** |
| 1138 | * scsi_eh_finish_cmd - Handle a cmd that eh is finished with. |
| 1139 | * @scmd: Original SCSI cmd that eh has finished. |
| 1140 | * @done_q: Queue for processed commands. |
| 1141 | * |
| 1142 | * Notes: |
| 1143 | * We don't want to use the normal command completion while we are are |
| 1144 | * still handling errors - it may cause other commands to be queued, |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1145 | * and that would disturb what we are doing. Thus we really want to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | * keep a list of pending commands for final completion, and once we |
| 1147 | * are ready to leave error handling we handle completion for real. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1148 | */ |
Tejun Heo | 041c5fc | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1149 | void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | list_move_tail(&scmd->eh_entry, done_q); |
| 1152 | } |
Tejun Heo | 041c5fc | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1153 | EXPORT_SYMBOL(scsi_eh_finish_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | /** |
| 1156 | * scsi_eh_get_sense - Get device sense data. |
| 1157 | * @work_q: Queue of commands to process. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1158 | * @done_q: Queue of processed commands. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | * |
| 1160 | * Description: |
| 1161 | * See if we need to request sense information. if so, then get it |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1162 | * now, so we have a better idea of what to do. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | * |
| 1164 | * Notes: |
| 1165 | * This has the unfortunate side effect that if a shost adapter does |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1166 | * not automatically request sense information, we end up shutting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | * it down before we request it. |
| 1168 | * |
| 1169 | * All drivers should request sense information internally these days, |
| 1170 | * so for now all I have to say is tough noogies if you end up in here. |
| 1171 | * |
| 1172 | * XXX: Long term this code should go away, but that needs an audit of |
| 1173 | * all LLDDs first. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1174 | */ |
Darrick J. Wong | dca84e4 | 2007-01-26 14:08:49 -0800 | [diff] [blame] | 1175 | int scsi_eh_get_sense(struct list_head *work_q, |
| 1176 | struct list_head *done_q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1178 | struct scsi_cmnd *scmd, *next; |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1179 | struct Scsi_Host *shost; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | int rtn; |
| 1181 | |
jiang.biao2@zte.com.cn | 709c75b | 2015-07-31 17:52:10 +0800 | [diff] [blame] | 1182 | /* |
| 1183 | * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO, |
| 1184 | * should not get sense. |
| 1185 | */ |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1186 | list_for_each_entry_safe(scmd, next, work_q, eh_entry) { |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 1187 | if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | SCSI_SENSE_VALID(scmd)) |
| 1189 | continue; |
| 1190 | |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1191 | shost = scmd->device->host; |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1192 | if (scsi_host_eh_past_deadline(shost)) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1193 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1194 | scmd_printk(KERN_INFO, scmd, |
| 1195 | "%s: skip request sense, past eh deadline\n", |
| 1196 | current->comm)); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1197 | break; |
| 1198 | } |
James Bottomley | d555a2a | 2014-03-28 10:50:17 -0700 | [diff] [blame] | 1199 | if (status_byte(scmd->result) != CHECK_CONDITION) |
| 1200 | /* |
| 1201 | * don't request sense if there's no check condition |
| 1202 | * status because the error we're processing isn't one |
| 1203 | * that has a sense code (and some devices get |
| 1204 | * confused by sense requests out of the blue) |
| 1205 | */ |
| 1206 | continue; |
| 1207 | |
Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 1208 | SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd, |
| 1209 | "%s: requesting sense\n", |
| 1210 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | rtn = scsi_request_sense(scmd); |
| 1212 | if (rtn != SUCCESS) |
| 1213 | continue; |
| 1214 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1215 | SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 1216 | "sense requested, result %x\n", scmd->result)); |
Hannes Reinecke | d811b84 | 2014-10-24 14:26:45 +0200 | [diff] [blame] | 1217 | SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | |
| 1219 | rtn = scsi_decide_disposition(scmd); |
| 1220 | |
| 1221 | /* |
| 1222 | * if the result was normal, then just pass it along to the |
| 1223 | * upper level. |
| 1224 | */ |
| 1225 | if (rtn == SUCCESS) |
| 1226 | /* we don't want this command reissued, just |
| 1227 | * finished with the sense data, so set |
| 1228 | * retries to the max allowed to ensure it |
| 1229 | * won't get reissued */ |
| 1230 | scmd->retries = scmd->allowed; |
| 1231 | else if (rtn != NEEDS_RETRY) |
| 1232 | continue; |
| 1233 | |
| 1234 | scsi_eh_finish_cmd(scmd, done_q); |
| 1235 | } |
| 1236 | |
| 1237 | return list_empty(work_q); |
| 1238 | } |
Darrick J. Wong | dca84e4 | 2007-01-26 14:08:49 -0800 | [diff] [blame] | 1239 | EXPORT_SYMBOL_GPL(scsi_eh_get_sense); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | * scsi_eh_tur - Send TUR to device. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1243 | * @scmd: &scsi_cmnd to send TUR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | * |
| 1245 | * Return value: |
| 1246 | * 0 - Device is ready. 1 - Device NOT ready. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1247 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | static int scsi_eh_tur(struct scsi_cmnd *scmd) |
| 1249 | { |
| 1250 | static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0}; |
| 1251 | int retry_cnt = 1, rtn; |
| 1252 | |
| 1253 | retry_tur: |
Martin K. Petersen | 0816c92 | 2013-05-10 10:36:04 -0400 | [diff] [blame] | 1254 | rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, |
| 1255 | scmd->device->eh_timeout, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1257 | SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 1258 | "%s return: %x\n", __func__, rtn)); |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 1259 | |
| 1260 | switch (rtn) { |
| 1261 | case NEEDS_RETRY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | if (retry_cnt--) |
| 1263 | goto retry_tur; |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 1264 | /*FALLTHRU*/ |
| 1265 | case SUCCESS: |
Alan Stern | e47373e | 2005-03-30 15:05:45 -0500 | [diff] [blame] | 1266 | return 0; |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 1267 | default: |
| 1268 | return 1; |
Alan Stern | e47373e | 2005-03-30 15:05:45 -0500 | [diff] [blame] | 1269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /** |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1273 | * scsi_eh_test_devices - check if devices are responding from error recovery. |
| 1274 | * @cmd_list: scsi commands in error recovery. |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1275 | * @work_q: queue for commands which still need more error recovery |
| 1276 | * @done_q: queue for commands which are finished |
| 1277 | * @try_stu: boolean on if a STU command should be tried in addition to TUR. |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1278 | * |
| 1279 | * Decription: |
| 1280 | * Tests if devices are in a working state. Commands to devices now in |
| 1281 | * a working state are sent to the done_q while commands to devices which |
| 1282 | * are still failing to respond are returned to the work_q for more |
| 1283 | * processing. |
| 1284 | **/ |
| 1285 | static int scsi_eh_test_devices(struct list_head *cmd_list, |
| 1286 | struct list_head *work_q, |
| 1287 | struct list_head *done_q, int try_stu) |
| 1288 | { |
| 1289 | struct scsi_cmnd *scmd, *next; |
| 1290 | struct scsi_device *sdev; |
| 1291 | int finish_cmds; |
| 1292 | |
| 1293 | while (!list_empty(cmd_list)) { |
| 1294 | scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry); |
| 1295 | sdev = scmd->device; |
| 1296 | |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1297 | if (!try_stu) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1298 | if (scsi_host_eh_past_deadline(sdev->host)) { |
| 1299 | /* Push items back onto work_q */ |
| 1300 | list_splice_init(cmd_list, work_q); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1301 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1302 | sdev_printk(KERN_INFO, sdev, |
| 1303 | "%s: skip test device, past eh deadline", |
| 1304 | current->comm)); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1305 | break; |
| 1306 | } |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1307 | } |
| 1308 | |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1309 | finish_cmds = !scsi_device_online(scmd->device) || |
| 1310 | (try_stu && !scsi_eh_try_stu(scmd) && |
| 1311 | !scsi_eh_tur(scmd)) || |
| 1312 | !scsi_eh_tur(scmd); |
| 1313 | |
| 1314 | list_for_each_entry_safe(scmd, next, cmd_list, eh_entry) |
| 1315 | if (scmd->device == sdev) { |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1316 | if (finish_cmds && |
| 1317 | (try_stu || |
| 1318 | scsi_eh_action(scmd, SUCCESS) == SUCCESS)) |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1319 | scsi_eh_finish_cmd(scmd, done_q); |
| 1320 | else |
| 1321 | list_move_tail(&scmd->eh_entry, work_q); |
| 1322 | } |
| 1323 | } |
| 1324 | return list_empty(work_q); |
| 1325 | } |
| 1326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | * scsi_eh_try_stu - Send START_UNIT to device. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1329 | * @scmd: &scsi_cmnd to send START_UNIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | * |
| 1331 | * Return value: |
| 1332 | * 0 - Device is ready. 1 - Device NOT ready. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1333 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | static int scsi_eh_try_stu(struct scsi_cmnd *scmd) |
| 1335 | { |
| 1336 | static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 1338 | if (scmd->device->allow_restart) { |
Brian King | ed773e6 | 2007-03-29 15:25:52 -0500 | [diff] [blame] | 1339 | int i, rtn = NEEDS_RETRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
Brian King | ed773e6 | 2007-03-29 15:25:52 -0500 | [diff] [blame] | 1341 | for (i = 0; rtn == NEEDS_RETRY && i < 2; i++) |
James Bottomley | 9728c08 | 2008-11-30 10:32:26 -0600 | [diff] [blame] | 1342 | rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0); |
Brian King | ed773e6 | 2007-03-29 15:25:52 -0500 | [diff] [blame] | 1343 | |
Christoph Hellwig | 631c228 | 2006-07-08 20:42:15 +0200 | [diff] [blame] | 1344 | if (rtn == SUCCESS) |
| 1345 | return 0; |
| 1346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | return 1; |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * scsi_eh_stu - send START_UNIT if needed |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1353 | * @shost: &scsi host being recovered. |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1354 | * @work_q: &list_head for pending commands. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1355 | * @done_q: &list_head for processed commands. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | * |
| 1357 | * Notes: |
| 1358 | * If commands are failing due to not ready, initializing command required, |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1359 | * try revalidating the device, which will end up sending a start unit. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1360 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | static int scsi_eh_stu(struct Scsi_Host *shost, |
| 1362 | struct list_head *work_q, |
| 1363 | struct list_head *done_q) |
| 1364 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1365 | struct scsi_cmnd *scmd, *stu_scmd, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | struct scsi_device *sdev; |
| 1367 | |
| 1368 | shost_for_each_device(sdev, shost) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1369 | if (scsi_host_eh_past_deadline(shost)) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1370 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1371 | sdev_printk(KERN_INFO, sdev, |
| 1372 | "%s: skip START_UNIT, past eh deadline\n", |
| 1373 | current->comm)); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1374 | break; |
| 1375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | stu_scmd = NULL; |
| 1377 | list_for_each_entry(scmd, work_q, eh_entry) |
| 1378 | if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) && |
| 1379 | scsi_check_sense(scmd) == FAILED ) { |
| 1380 | stu_scmd = scmd; |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | if (!stu_scmd) |
| 1385 | continue; |
| 1386 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1387 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1388 | sdev_printk(KERN_INFO, sdev, |
| 1389 | "%s: Sending START_UNIT\n", |
| 1390 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | if (!scsi_eh_try_stu(stu_scmd)) { |
| 1393 | if (!scsi_device_online(sdev) || |
| 1394 | !scsi_eh_tur(stu_scmd)) { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1395 | list_for_each_entry_safe(scmd, next, |
| 1396 | work_q, eh_entry) { |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1397 | if (scmd->device == sdev && |
| 1398 | scsi_eh_action(scmd, SUCCESS) == SUCCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | scsi_eh_finish_cmd(scmd, done_q); |
| 1400 | } |
| 1401 | } |
| 1402 | } else { |
| 1403 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1404 | sdev_printk(KERN_INFO, sdev, |
| 1405 | "%s: START_UNIT failed\n", |
| 1406 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | return list_empty(work_q); |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | /** |
| 1415 | * scsi_eh_bus_device_reset - send bdr if needed |
| 1416 | * @shost: scsi host being recovered. |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1417 | * @work_q: &list_head for pending commands. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1418 | * @done_q: &list_head for processed commands. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | * |
| 1420 | * Notes: |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1421 | * Try a bus device reset. Still, look to see whether we have multiple |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | * devices that are jammed or not - if we have multiple devices, it |
| 1423 | * makes no sense to try bus_device_reset - we really would need to try |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1424 | * a bus_reset instead. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1425 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | static int scsi_eh_bus_device_reset(struct Scsi_Host *shost, |
| 1427 | struct list_head *work_q, |
| 1428 | struct list_head *done_q) |
| 1429 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1430 | struct scsi_cmnd *scmd, *bdr_scmd, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | struct scsi_device *sdev; |
| 1432 | int rtn; |
| 1433 | |
| 1434 | shost_for_each_device(sdev, shost) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1435 | if (scsi_host_eh_past_deadline(shost)) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1436 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1437 | sdev_printk(KERN_INFO, sdev, |
| 1438 | "%s: skip BDR, past eh deadline\n", |
| 1439 | current->comm)); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1440 | break; |
| 1441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | bdr_scmd = NULL; |
| 1443 | list_for_each_entry(scmd, work_q, eh_entry) |
| 1444 | if (scmd->device == sdev) { |
| 1445 | bdr_scmd = scmd; |
| 1446 | break; |
| 1447 | } |
| 1448 | |
| 1449 | if (!bdr_scmd) |
| 1450 | continue; |
| 1451 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1452 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1453 | sdev_printk(KERN_INFO, sdev, |
| 1454 | "%s: Sending BDR\n", current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | rtn = scsi_try_bus_device_reset(bdr_scmd); |
Christof Schmitt | 2f2eb58 | 2010-03-24 16:50:30 +0100 | [diff] [blame] | 1456 | if (rtn == SUCCESS || rtn == FAST_IO_FAIL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | if (!scsi_device_online(sdev) || |
Christof Schmitt | 2f2eb58 | 2010-03-24 16:50:30 +0100 | [diff] [blame] | 1458 | rtn == FAST_IO_FAIL || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | !scsi_eh_tur(bdr_scmd)) { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1460 | list_for_each_entry_safe(scmd, next, |
| 1461 | work_q, eh_entry) { |
James Bottomley | 2451079 | 2013-11-11 13:44:53 +0100 | [diff] [blame] | 1462 | if (scmd->device == sdev && |
| 1463 | scsi_eh_action(scmd, rtn) != FAILED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | scsi_eh_finish_cmd(scmd, |
| 1465 | done_q); |
| 1466 | } |
| 1467 | } |
| 1468 | } else { |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1469 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1470 | sdev_printk(KERN_INFO, sdev, |
| 1471 | "%s: BDR failed\n", current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | return list_empty(work_q); |
| 1476 | } |
| 1477 | |
| 1478 | /** |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 1479 | * scsi_eh_target_reset - send target reset if needed |
| 1480 | * @shost: scsi host being recovered. |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1481 | * @work_q: &list_head for pending commands. |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 1482 | * @done_q: &list_head for processed commands. |
| 1483 | * |
| 1484 | * Notes: |
| 1485 | * Try a target reset. |
| 1486 | */ |
| 1487 | static int scsi_eh_target_reset(struct Scsi_Host *shost, |
| 1488 | struct list_head *work_q, |
| 1489 | struct list_head *done_q) |
| 1490 | { |
James Bottomley | 98db519 | 2010-10-25 15:53:41 -0500 | [diff] [blame] | 1491 | LIST_HEAD(tmp_list); |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1492 | LIST_HEAD(check_list); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 1493 | |
James Bottomley | 98db519 | 2010-10-25 15:53:41 -0500 | [diff] [blame] | 1494 | list_splice_init(work_q, &tmp_list); |
| 1495 | |
| 1496 | while (!list_empty(&tmp_list)) { |
| 1497 | struct scsi_cmnd *next, *scmd; |
| 1498 | int rtn; |
| 1499 | unsigned int id; |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1500 | |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1501 | if (scsi_host_eh_past_deadline(shost)) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1502 | /* push back on work queue for further processing */ |
| 1503 | list_splice_init(&check_list, work_q); |
| 1504 | list_splice_init(&tmp_list, work_q); |
| 1505 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1506 | shost_printk(KERN_INFO, shost, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1507 | "%s: Skip target reset, past eh deadline\n", |
| 1508 | current->comm)); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1509 | return list_empty(work_q); |
| 1510 | } |
James Bottomley | 98db519 | 2010-10-25 15:53:41 -0500 | [diff] [blame] | 1511 | |
| 1512 | scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry); |
| 1513 | id = scmd_id(scmd); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 1514 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1515 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1516 | shost_printk(KERN_INFO, shost, |
| 1517 | "%s: Sending target reset to target %d\n", |
| 1518 | current->comm, id)); |
James Bottomley | 98db519 | 2010-10-25 15:53:41 -0500 | [diff] [blame] | 1519 | rtn = scsi_try_target_reset(scmd); |
| 1520 | if (rtn != SUCCESS && rtn != FAST_IO_FAIL) |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1521 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1522 | shost_printk(KERN_INFO, shost, |
| 1523 | "%s: Target reset failed" |
| 1524 | " target: %d\n", |
| 1525 | current->comm, id)); |
James Bottomley | 98db519 | 2010-10-25 15:53:41 -0500 | [diff] [blame] | 1526 | list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) { |
| 1527 | if (scmd_id(scmd) != id) |
| 1528 | continue; |
| 1529 | |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1530 | if (rtn == SUCCESS) |
| 1531 | list_move_tail(&scmd->eh_entry, &check_list); |
| 1532 | else if (rtn == FAST_IO_FAIL) |
James Bottomley | 98db519 | 2010-10-25 15:53:41 -0500 | [diff] [blame] | 1533 | scsi_eh_finish_cmd(scmd, done_q); |
| 1534 | else |
| 1535 | /* push back on work queue for further processing */ |
| 1536 | list_move(&scmd->eh_entry, work_q); |
| 1537 | } |
| 1538 | } |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 1539 | |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1540 | return scsi_eh_test_devices(&check_list, work_q, done_q, 0); |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | /** |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1544 | * scsi_eh_bus_reset - send a bus reset |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1545 | * @shost: &scsi host being recovered. |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1546 | * @work_q: &list_head for pending commands. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 1547 | * @done_q: &list_head for processed commands. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1548 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | static int scsi_eh_bus_reset(struct Scsi_Host *shost, |
| 1550 | struct list_head *work_q, |
| 1551 | struct list_head *done_q) |
| 1552 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1553 | struct scsi_cmnd *scmd, *chan_scmd, *next; |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1554 | LIST_HEAD(check_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | unsigned int channel; |
| 1556 | int rtn; |
| 1557 | |
| 1558 | /* |
| 1559 | * we really want to loop over the various channels, and do this on |
| 1560 | * a channel by channel basis. we should also check to see if any |
| 1561 | * of the failed commands are on soft_reset devices, and if so, skip |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1562 | * the reset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | */ |
| 1564 | |
| 1565 | for (channel = 0; channel <= shost->max_channel; channel++) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1566 | if (scsi_host_eh_past_deadline(shost)) { |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1567 | list_splice_init(&check_list, work_q); |
| 1568 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1569 | shost_printk(KERN_INFO, shost, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 1570 | "%s: skip BRST, past eh deadline\n", |
| 1571 | current->comm)); |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1572 | return list_empty(work_q); |
| 1573 | } |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | chan_scmd = NULL; |
| 1576 | list_for_each_entry(scmd, work_q, eh_entry) { |
Jeff Garzik | 422c0d6 | 2005-10-24 18:05:09 -0400 | [diff] [blame] | 1577 | if (channel == scmd_channel(scmd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | chan_scmd = scmd; |
| 1579 | break; |
| 1580 | /* |
| 1581 | * FIXME add back in some support for |
| 1582 | * soft_reset devices. |
| 1583 | */ |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | if (!chan_scmd) |
| 1588 | continue; |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1589 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1590 | shost_printk(KERN_INFO, shost, |
| 1591 | "%s: Sending BRST chan: %d\n", |
| 1592 | current->comm, channel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | rtn = scsi_try_bus_reset(chan_scmd); |
Christof Schmitt | 2f2eb58 | 2010-03-24 16:50:30 +0100 | [diff] [blame] | 1594 | if (rtn == SUCCESS || rtn == FAST_IO_FAIL) { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1595 | list_for_each_entry_safe(scmd, next, work_q, eh_entry) { |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1596 | if (channel == scmd_channel(scmd)) { |
| 1597 | if (rtn == FAST_IO_FAIL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | scsi_eh_finish_cmd(scmd, |
| 1599 | done_q); |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1600 | else |
| 1601 | list_move_tail(&scmd->eh_entry, |
| 1602 | &check_list); |
| 1603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | } |
| 1605 | } else { |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1606 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1607 | shost_printk(KERN_INFO, shost, |
| 1608 | "%s: BRST failed chan: %d\n", |
| 1609 | current->comm, channel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | } |
| 1611 | } |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1612 | return scsi_eh_test_devices(&check_list, work_q, done_q, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1616 | * scsi_eh_host_reset - send a host reset |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1617 | * @shost: host to be reset. |
| 1618 | * @work_q: &list_head for pending commands. |
| 1619 | * @done_q: &list_head for processed commands. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1620 | */ |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1621 | static int scsi_eh_host_reset(struct Scsi_Host *shost, |
| 1622 | struct list_head *work_q, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | struct list_head *done_q) |
| 1624 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1625 | struct scsi_cmnd *scmd, *next; |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1626 | LIST_HEAD(check_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | int rtn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | |
| 1629 | if (!list_empty(work_q)) { |
| 1630 | scmd = list_entry(work_q->next, |
| 1631 | struct scsi_cmnd, eh_entry); |
| 1632 | |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1633 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1634 | shost_printk(KERN_INFO, shost, |
| 1635 | "%s: Sending HRST\n", |
| 1636 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
| 1638 | rtn = scsi_try_host_reset(scmd); |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1639 | if (rtn == SUCCESS) { |
| 1640 | list_splice_init(work_q, &check_list); |
| 1641 | } else if (rtn == FAST_IO_FAIL) { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1642 | list_for_each_entry_safe(scmd, next, work_q, eh_entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | scsi_eh_finish_cmd(scmd, done_q); |
| 1644 | } |
| 1645 | } else { |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1646 | SCSI_LOG_ERROR_RECOVERY(3, |
| 1647 | shost_printk(KERN_INFO, shost, |
| 1648 | "%s: HRST failed\n", |
| 1649 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | } |
| 1651 | } |
David Jeffery | 3eef625 | 2011-05-19 14:41:12 -0400 | [diff] [blame] | 1652 | return scsi_eh_test_devices(&check_list, work_q, done_q, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * scsi_eh_offline_sdevs - offline scsi devices that fail to recover |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 1657 | * @work_q: &list_head for pending commands. |
| 1658 | * @done_q: &list_head for processed commands. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1659 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | static void scsi_eh_offline_sdevs(struct list_head *work_q, |
| 1661 | struct list_head *done_q) |
| 1662 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1663 | struct scsi_cmnd *scmd, *next; |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 1664 | struct scsi_device *sdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 1666 | list_for_each_entry_safe(scmd, next, work_q, eh_entry) { |
Matthew Wilcox | 31765d7 | 2007-08-17 11:02:10 -0600 | [diff] [blame] | 1667 | sdev_printk(KERN_INFO, scmd->device, "Device offlined - " |
| 1668 | "not ready after error recovery\n"); |
Bart Van Assche | 0db6ca8 | 2017-06-02 14:21:55 -0700 | [diff] [blame] | 1669 | sdev = scmd->device; |
| 1670 | |
| 1671 | mutex_lock(&sdev->state_mutex); |
| 1672 | scsi_device_set_state(sdev, SDEV_OFFLINE); |
| 1673 | mutex_unlock(&sdev->state_mutex); |
| 1674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | scsi_eh_finish_cmd(scmd, done_q); |
| 1676 | } |
| 1677 | return; |
| 1678 | } |
| 1679 | |
| 1680 | /** |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1681 | * scsi_noretry_cmd - determine if command should be failed fast |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1682 | * @scmd: SCSI cmd to examine. |
| 1683 | */ |
| 1684 | int scsi_noretry_cmd(struct scsi_cmnd *scmd) |
| 1685 | { |
| 1686 | switch (host_byte(scmd->result)) { |
| 1687 | case DID_OK: |
| 1688 | break; |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1689 | case DID_TIME_OUT: |
| 1690 | goto check_type; |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1691 | case DID_BUS_BUSY: |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1692 | return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT); |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1693 | case DID_PARITY: |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1694 | return (scmd->request->cmd_flags & REQ_FAILFAST_DEV); |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1695 | case DID_ERROR: |
| 1696 | if (msg_byte(scmd->result) == COMMAND_COMPLETE && |
| 1697 | status_byte(scmd->result) == RESERVATION_CONFLICT) |
| 1698 | return 0; |
| 1699 | /* fall through */ |
| 1700 | case DID_SOFT_ERROR: |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 1701 | return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER); |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1702 | } |
| 1703 | |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1704 | if (status_byte(scmd->result) != CHECK_CONDITION) |
| 1705 | return 0; |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1706 | |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1707 | check_type: |
| 1708 | /* |
| 1709 | * assume caller has checked sense and determined |
| 1710 | * the check condition was retryable. |
| 1711 | */ |
| 1712 | if (scmd->request->cmd_flags & REQ_FAILFAST_DEV || |
Christoph Hellwig | 57292b5 | 2017-01-31 16:57:29 +0100 | [diff] [blame] | 1713 | blk_rq_is_passthrough(scmd->request)) |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1714 | return 1; |
| 1715 | else |
| 1716 | return 0; |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | * scsi_decide_disposition - Disposition a cmd on return from LLD. |
| 1721 | * @scmd: SCSI cmd to examine. |
| 1722 | * |
| 1723 | * Notes: |
| 1724 | * This is *only* called when we are examining the status after sending |
| 1725 | * out the actual data command. any commands that are queued for error |
| 1726 | * recovery (e.g. test_unit_ready) do *not* come through here. |
| 1727 | * |
| 1728 | * When this routine returns failed, it means the error handler thread |
| 1729 | * is woken. In cases where the error code indicates an error that |
| 1730 | * doesn't require the error handler read (i.e. we don't need to |
| 1731 | * abort/reset), this function should return SUCCESS. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1732 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | int scsi_decide_disposition(struct scsi_cmnd *scmd) |
| 1734 | { |
| 1735 | int rtn; |
| 1736 | |
| 1737 | /* |
| 1738 | * if the device is offline, then we clearly just pass the result back |
| 1739 | * up to the top level. |
| 1740 | */ |
| 1741 | if (!scsi_device_online(scmd->device)) { |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1742 | SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd, |
| 1743 | "%s: device offline - report as SUCCESS\n", __func__)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | return SUCCESS; |
| 1745 | } |
| 1746 | |
| 1747 | /* |
| 1748 | * first check the host byte, to see if there is anything in there |
| 1749 | * that would indicate what we need to do. |
| 1750 | */ |
| 1751 | switch (host_byte(scmd->result)) { |
| 1752 | case DID_PASSTHROUGH: |
| 1753 | /* |
| 1754 | * no matter what, pass this through to the upper layer. |
| 1755 | * nuke this special code so that it looks like we are saying |
| 1756 | * did_ok. |
| 1757 | */ |
| 1758 | scmd->result &= 0xff00ffff; |
| 1759 | return SUCCESS; |
| 1760 | case DID_OK: |
| 1761 | /* |
| 1762 | * looks good. drop through, and check the next byte. |
| 1763 | */ |
| 1764 | break; |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1765 | case DID_ABORT: |
| 1766 | if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) { |
Ulrich Obergfell | 8922a90 | 2014-06-04 13:34:57 +0200 | [diff] [blame] | 1767 | set_host_byte(scmd, DID_TIME_OUT); |
Hannes Reinecke | e494f6a | 2013-11-11 13:44:54 +0100 | [diff] [blame] | 1768 | return SUCCESS; |
| 1769 | } |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 1770 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | case DID_NO_CONNECT: |
| 1772 | case DID_BAD_TARGET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | /* |
| 1774 | * note - this means that we just report the status back |
| 1775 | * to the top level driver, not that we actually think |
| 1776 | * that it indicates SUCCESS. |
| 1777 | */ |
| 1778 | return SUCCESS; |
Petros Koutoupis | ad95028 | 2017-10-30 16:38:10 -0500 | [diff] [blame] | 1779 | case DID_SOFT_ERROR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | /* |
| 1781 | * when the low level driver returns did_soft_error, |
Jesper Juhl | 0bf8c86 | 2011-03-21 20:47:31 +0100 | [diff] [blame] | 1782 | * it is responsible for keeping an internal retry counter |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | * in order to avoid endless loops (db) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | goto maybe_retry; |
| 1786 | case DID_IMM_RETRY: |
| 1787 | return NEEDS_RETRY; |
| 1788 | |
| bf34191 | 2005-04-12 17:49:09 -0500 | [diff] [blame] | 1789 | case DID_REQUEUE: |
| 1790 | return ADD_TO_MLQUEUE; |
Mike Christie | a4dfaa6 | 2008-08-19 18:45:25 -0500 | [diff] [blame] | 1791 | case DID_TRANSPORT_DISRUPTED: |
| 1792 | /* |
| 1793 | * LLD/transport was disrupted during processing of the IO. |
| 1794 | * The transport class is now blocked/blocking, |
| 1795 | * and the transport will decide what to do with the IO |
Mike Christie | 939c2288 | 2008-11-04 19:47:19 -0600 | [diff] [blame] | 1796 | * based on its timers and recovery capablilities if |
| 1797 | * there are enough retries. |
Mike Christie | a4dfaa6 | 2008-08-19 18:45:25 -0500 | [diff] [blame] | 1798 | */ |
Mike Christie | 939c2288 | 2008-11-04 19:47:19 -0600 | [diff] [blame] | 1799 | goto maybe_retry; |
Mike Christie | a4dfaa6 | 2008-08-19 18:45:25 -0500 | [diff] [blame] | 1800 | case DID_TRANSPORT_FAILFAST: |
| 1801 | /* |
| 1802 | * The transport decided to failfast the IO (most likely |
| 1803 | * the fast io fail tmo fired), so send IO directly upwards. |
| 1804 | */ |
| 1805 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | case DID_ERROR: |
| 1807 | if (msg_byte(scmd->result) == COMMAND_COMPLETE && |
| 1808 | status_byte(scmd->result) == RESERVATION_CONFLICT) |
| 1809 | /* |
| 1810 | * execute reservation conflict processing code |
| 1811 | * lower down |
| 1812 | */ |
| 1813 | break; |
| 1814 | /* fallthrough */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | case DID_BUS_BUSY: |
| 1816 | case DID_PARITY: |
| 1817 | goto maybe_retry; |
| 1818 | case DID_TIME_OUT: |
| 1819 | /* |
| 1820 | * when we scan the bus, we get timeout messages for |
| 1821 | * these commands if there is no device available. |
| 1822 | * other hosts report did_no_connect for the same thing. |
| 1823 | */ |
| 1824 | if ((scmd->cmnd[0] == TEST_UNIT_READY || |
| 1825 | scmd->cmnd[0] == INQUIRY)) { |
| 1826 | return SUCCESS; |
| 1827 | } else { |
| 1828 | return FAILED; |
| 1829 | } |
| 1830 | case DID_RESET: |
| 1831 | return SUCCESS; |
| 1832 | default: |
| 1833 | return FAILED; |
| 1834 | } |
| 1835 | |
| 1836 | /* |
| 1837 | * next, check the message byte. |
| 1838 | */ |
| 1839 | if (msg_byte(scmd->result) != COMMAND_COMPLETE) |
| 1840 | return FAILED; |
| 1841 | |
| 1842 | /* |
| 1843 | * check the status byte to see if this indicates anything special. |
| 1844 | */ |
| 1845 | switch (status_byte(scmd->result)) { |
| 1846 | case QUEUE_FULL: |
Mike Christie | 42a6a91 | 2009-10-15 17:46:44 -0700 | [diff] [blame] | 1847 | scsi_handle_queue_full(scmd->device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | /* |
| 1849 | * the case of trying to send too many commands to a |
| 1850 | * tagged queueing device. |
| 1851 | */ |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 1852 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | case BUSY: |
| 1854 | /* |
| 1855 | * device can't talk to us at the moment. Should only |
| 1856 | * occur (SAM-3) when the task queue is empty, so will cause |
| 1857 | * the empty queue handling to trigger a stall in the |
| 1858 | * device. |
| 1859 | */ |
| 1860 | return ADD_TO_MLQUEUE; |
| 1861 | case GOOD: |
Ewan D. Milne | 279afdf | 2013-08-08 15:07:48 -0400 | [diff] [blame] | 1862 | if (scmd->cmnd[0] == REPORT_LUNS) |
| 1863 | scmd->device->sdev_target->expecting_lun_change = 0; |
Vasu Dev | 4a84067 | 2009-10-22 15:46:33 -0700 | [diff] [blame] | 1864 | scsi_handle_queue_ramp_up(scmd->device); |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 1865 | /* FALLTHROUGH */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | case COMMAND_TERMINATED: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | return SUCCESS; |
Vladislav Bolkhovitin | a9b589d | 2008-11-06 13:57:52 +0300 | [diff] [blame] | 1868 | case TASK_ABORTED: |
| 1869 | goto maybe_retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | case CHECK_CONDITION: |
| 1871 | rtn = scsi_check_sense(scmd); |
| 1872 | if (rtn == NEEDS_RETRY) |
| 1873 | goto maybe_retry; |
| 1874 | /* if rtn == FAILED, we have no sense information; |
| 1875 | * returning FAILED will wake the error handler thread |
| 1876 | * to collect the sense and redo the decide |
| 1877 | * disposition */ |
| 1878 | return rtn; |
| 1879 | case CONDITION_GOOD: |
| 1880 | case INTERMEDIATE_GOOD: |
| 1881 | case INTERMEDIATE_C_GOOD: |
| 1882 | case ACA_ACTIVE: |
| 1883 | /* |
| 1884 | * who knows? FIXME(eric) |
| 1885 | */ |
| 1886 | return SUCCESS; |
| 1887 | |
| 1888 | case RESERVATION_CONFLICT: |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 1889 | sdev_printk(KERN_INFO, scmd->device, |
| 1890 | "reservation conflict\n"); |
Moger, Babu | 2082ebc | 2012-01-24 20:38:46 +0000 | [diff] [blame] | 1891 | set_host_byte(scmd, DID_NEXUS_FAILURE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | return SUCCESS; /* causes immediate i/o error */ |
| 1893 | default: |
| 1894 | return FAILED; |
| 1895 | } |
| 1896 | return FAILED; |
| 1897 | |
Jianchao Wang | 8ef7fe4 | 2018-02-26 15:59:16 +0800 | [diff] [blame] | 1898 | maybe_retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
| 1900 | /* we requeue for retry because the error was retryable, and |
| 1901 | * the request was not marked fast fail. Note that above, |
| 1902 | * even if the request is marked fast fail, we still requeue |
| 1903 | * for queue congestion conditions (QUEUE_FULL or BUSY) */ |
Brian King | 8884efa | 2006-02-24 17:10:04 -0600 | [diff] [blame] | 1904 | if ((++scmd->retries) <= scmd->allowed |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 1905 | && !scsi_noretry_cmd(scmd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | return NEEDS_RETRY; |
| 1907 | } else { |
| 1908 | /* |
| 1909 | * no more retries - report this one back to upper level. |
| 1910 | */ |
| 1911 | return SUCCESS; |
| 1912 | } |
| 1913 | } |
| 1914 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1915 | static void eh_lock_door_done(struct request *req, blk_status_t status) |
FUJITA Tomonori | f078727 | 2008-12-14 01:23:45 +0900 | [diff] [blame] | 1916 | { |
| 1917 | __blk_put_request(req->q, req); |
| 1918 | } |
| 1919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | * scsi_eh_lock_door - Prevent medium removal for the specified device |
| 1922 | * @sdev: SCSI device to prevent medium removal |
| 1923 | * |
| 1924 | * Locking: |
James Bottomley | 91bc31f | 2009-05-17 09:30:48 -0500 | [diff] [blame] | 1925 | * We must be called from process context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | * |
| 1927 | * Notes: |
| 1928 | * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the |
| 1929 | * head of the devices request queue, and continue. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1930 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | static void scsi_eh_lock_door(struct scsi_device *sdev) |
| 1932 | { |
FUJITA Tomonori | f078727 | 2008-12-14 01:23:45 +0900 | [diff] [blame] | 1933 | struct request *req; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 1934 | struct scsi_request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
James Bottomley | 91bc31f | 2009-05-17 09:30:48 -0500 | [diff] [blame] | 1936 | /* |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 1937 | * blk_get_request with GFP_KERNEL (__GFP_RECLAIM) sleeps until a |
James Bottomley | 91bc31f | 2009-05-17 09:30:48 -0500 | [diff] [blame] | 1938 | * request becomes available |
| 1939 | */ |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 1940 | req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, GFP_KERNEL); |
Joe Lawrence | a492f07 | 2014-08-28 08:15:21 -0600 | [diff] [blame] | 1941 | if (IS_ERR(req)) |
Joe Lawrence | eb571ee | 2014-07-02 15:35:16 -0400 | [diff] [blame] | 1942 | return; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 1943 | rq = scsi_req(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 1945 | rq->cmd[0] = ALLOW_MEDIUM_REMOVAL; |
| 1946 | rq->cmd[1] = 0; |
| 1947 | rq->cmd[2] = 0; |
| 1948 | rq->cmd[3] = 0; |
| 1949 | rq->cmd[4] = SCSI_REMOVAL_PREVENT; |
| 1950 | rq->cmd[5] = 0; |
| 1951 | rq->cmd_len = COMMAND_SIZE(rq->cmd[0]); |
FUJITA Tomonori | f078727 | 2008-12-14 01:23:45 +0900 | [diff] [blame] | 1952 | |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 1953 | req->rq_flags |= RQF_QUIET; |
FUJITA Tomonori | f078727 | 2008-12-14 01:23:45 +0900 | [diff] [blame] | 1954 | req->timeout = 10 * HZ; |
Christoph Hellwig | 64c7f1d | 2017-04-05 19:18:12 +0200 | [diff] [blame] | 1955 | rq->retries = 5; |
FUJITA Tomonori | f078727 | 2008-12-14 01:23:45 +0900 | [diff] [blame] | 1956 | |
| 1957 | blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | /** |
| 1961 | * scsi_restart_operations - restart io operations to the specified host. |
| 1962 | * @shost: Host we are restarting. |
| 1963 | * |
| 1964 | * Notes: |
| 1965 | * When we entered the error handler, we blocked all further i/o to |
| 1966 | * this device. we need to 'reverse' this process. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 1967 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | static void scsi_restart_operations(struct Scsi_Host *shost) |
| 1969 | { |
| 1970 | struct scsi_device *sdev; |
James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 1971 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
| 1973 | /* |
| 1974 | * If the door was locked, we need to insert a door lock request |
| 1975 | * onto the head of the SCSI request queue for the device. There |
| 1976 | * is no point trying to lock the door of an off-line device. |
| 1977 | */ |
| 1978 | shost_for_each_device(sdev, shost) { |
Christoph Hellwig | 4837927 | 2014-11-03 19:36:40 +0100 | [diff] [blame] | 1979 | if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | scsi_eh_lock_door(sdev); |
Christoph Hellwig | 4837927 | 2014-11-03 19:36:40 +0100 | [diff] [blame] | 1981 | sdev->was_reset = 0; |
| 1982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * next free up anything directly waiting upon the host. this |
| 1987 | * will be requests for character device operations, and also for |
| 1988 | * ioctls to queued block devices. |
| 1989 | */ |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 1990 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 1991 | shost_printk(KERN_INFO, shost, "waking up host to restart\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
James Bottomley | 939647e | 2005-09-18 15:05:20 -0500 | [diff] [blame] | 1993 | spin_lock_irqsave(shost->host_lock, flags); |
| 1994 | if (scsi_host_set_state(shost, SHOST_RUNNING)) |
| 1995 | if (scsi_host_set_state(shost, SHOST_CANCEL)) |
| 1996 | BUG_ON(scsi_host_set_state(shost, SHOST_DEL)); |
| 1997 | spin_unlock_irqrestore(shost->host_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
| 1999 | wake_up(&shost->host_wait); |
| 2000 | |
| 2001 | /* |
| 2002 | * finally we need to re-initiate requests that may be pending. we will |
| 2003 | * have had everything blocked while error handling is taking place, and |
| 2004 | * now that error recovery is done, we will need to ensure that these |
| 2005 | * requests are started. |
| 2006 | */ |
| 2007 | scsi_run_host_queues(shost); |
Dan Williams | 57fc2e3 | 2012-06-21 23:25:32 -0700 | [diff] [blame] | 2008 | |
| 2009 | /* |
| 2010 | * if eh is active and host_eh_scheduled is pending we need to re-run |
| 2011 | * recovery. we do this check after scsi_run_host_queues() to allow |
| 2012 | * everything pent up since the last eh run a chance to make forward |
| 2013 | * progress before we sync again. Either we'll immediately re-run |
| 2014 | * recovery or scsi_device_unbusy() will wake us again when these |
| 2015 | * pending commands complete. |
| 2016 | */ |
| 2017 | spin_lock_irqsave(shost->host_lock, flags); |
| 2018 | if (shost->host_eh_scheduled) |
| 2019 | if (scsi_host_set_state(shost, SHOST_RECOVERY)) |
| 2020 | WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)); |
| 2021 | spin_unlock_irqrestore(shost->host_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * scsi_eh_ready_devs - check device ready state and recover if not. |
Randy Dunlap | 74cf298 | 2014-08-16 14:15:11 -0700 | [diff] [blame] | 2026 | * @shost: host to be recovered. |
| 2027 | * @work_q: &list_head for pending commands. |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2028 | * @done_q: &list_head for processed commands. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 2029 | */ |
Darrick J. Wong | dca84e4 | 2007-01-26 14:08:49 -0800 | [diff] [blame] | 2030 | void scsi_eh_ready_devs(struct Scsi_Host *shost, |
| 2031 | struct list_head *work_q, |
| 2032 | struct list_head *done_q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | { |
| 2034 | if (!scsi_eh_stu(shost, work_q, done_q)) |
| 2035 | if (!scsi_eh_bus_device_reset(shost, work_q, done_q)) |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 2036 | if (!scsi_eh_target_reset(shost, work_q, done_q)) |
| 2037 | if (!scsi_eh_bus_reset(shost, work_q, done_q)) |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2038 | if (!scsi_eh_host_reset(shost, work_q, done_q)) |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 2039 | scsi_eh_offline_sdevs(work_q, |
| 2040 | done_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | } |
Darrick J. Wong | dca84e4 | 2007-01-26 14:08:49 -0800 | [diff] [blame] | 2042 | EXPORT_SYMBOL_GPL(scsi_eh_ready_devs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | |
| 2044 | /** |
| 2045 | * scsi_eh_flush_done_q - finish processed commands or retry them. |
| 2046 | * @done_q: list_head of processed commands. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 2047 | */ |
Tejun Heo | 041c5fc | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2048 | void scsi_eh_flush_done_q(struct list_head *done_q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | { |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 2050 | struct scsi_cmnd *scmd, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | |
Christoph Hellwig | 937abeaa | 2005-06-19 13:43:56 +0200 | [diff] [blame] | 2052 | list_for_each_entry_safe(scmd, next, done_q, eh_entry) { |
| 2053 | list_del_init(&scmd->eh_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | if (scsi_device_online(scmd->device) && |
Mike Christie | 4a27446 | 2008-08-19 18:45:31 -0500 | [diff] [blame] | 2055 | !scsi_noretry_cmd(scmd) && |
Brian King | 8884efa | 2006-02-24 17:10:04 -0600 | [diff] [blame] | 2056 | (++scmd->retries <= scmd->allowed)) { |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2057 | SCSI_LOG_ERROR_RECOVERY(3, |
| 2058 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 2059 | "%s: flush retry cmd\n", |
| 2060 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY); |
| 2062 | } else { |
Patrick Mansfield | 793698c | 2005-05-16 17:42:15 -0700 | [diff] [blame] | 2063 | /* |
| 2064 | * If just we got sense for the device (called |
| 2065 | * scsi_eh_get_sense), scmd->result is already |
| 2066 | * set, do not set DRIVER_TIMEOUT. |
| 2067 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | if (!scmd->result) |
| 2069 | scmd->result |= (DRIVER_TIMEOUT << 24); |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2070 | SCSI_LOG_ERROR_RECOVERY(3, |
| 2071 | scmd_printk(KERN_INFO, scmd, |
Hannes Reinecke | 470613b | 2015-01-08 07:43:50 +0100 | [diff] [blame] | 2072 | "%s: flush finish cmd\n", |
| 2073 | current->comm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | scsi_finish_command(scmd); |
| 2075 | } |
| 2076 | } |
| 2077 | } |
Tejun Heo | 041c5fc | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2078 | EXPORT_SYMBOL(scsi_eh_flush_done_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
| 2080 | /** |
| 2081 | * scsi_unjam_host - Attempt to fix a host which has a cmd that failed. |
| 2082 | * @shost: Host to unjam. |
| 2083 | * |
| 2084 | * Notes: |
| 2085 | * When we come in here, we *know* that all commands on the bus have |
| 2086 | * either completed, failed or timed out. we also know that no further |
| 2087 | * commands are being sent to the host, so things are relatively quiet |
| 2088 | * and we have freedom to fiddle with things as we wish. |
| 2089 | * |
| 2090 | * This is only the *default* implementation. it is possible for |
| 2091 | * individual drivers to supply their own version of this function, and |
| 2092 | * if the maintainer wishes to do this, it is strongly suggested that |
| 2093 | * this function be taken as a template and modified. this function |
| 2094 | * was designed to correctly handle problems for about 95% of the |
| 2095 | * different cases out there, and it should always provide at least a |
| 2096 | * reasonable amount of error recovery. |
| 2097 | * |
| 2098 | * Any command marked 'failed' or 'timeout' must eventually have |
| 2099 | * scsi_finish_cmd() called for it. we do all of the retry stuff |
| 2100 | * here, so when we restart the host after we return it should have an |
| 2101 | * empty queue. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 2102 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | static void scsi_unjam_host(struct Scsi_Host *shost) |
| 2104 | { |
| 2105 | unsigned long flags; |
| 2106 | LIST_HEAD(eh_work_q); |
| 2107 | LIST_HEAD(eh_done_q); |
| 2108 | |
| 2109 | spin_lock_irqsave(shost->host_lock, flags); |
| 2110 | list_splice_init(&shost->eh_cmd_q, &eh_work_q); |
| 2111 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2112 | |
| 2113 | SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q)); |
| 2114 | |
| 2115 | if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q)) |
Hannes Reinecke | a065863 | 2017-04-06 15:36:35 +0200 | [diff] [blame] | 2116 | scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 2118 | spin_lock_irqsave(shost->host_lock, flags); |
Ren Mingxin | bb3b621 | 2013-11-11 13:44:56 +0100 | [diff] [blame] | 2119 | if (shost->eh_deadline != -1) |
Hannes Reinecke | b456202 | 2013-10-23 10:51:21 +0200 | [diff] [blame] | 2120 | shost->last_reset = 0; |
| 2121 | spin_unlock_irqrestore(shost->host_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | scsi_eh_flush_done_q(&eh_done_q); |
| 2123 | } |
| 2124 | |
| 2125 | /** |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2126 | * scsi_error_handler - SCSI error handler thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | * @data: Host for which we are running. |
| 2128 | * |
| 2129 | * Notes: |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2130 | * This is the main error handling loop. This is run as a kernel thread |
| 2131 | * for every SCSI host and handles all error handling activity. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 2132 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | int scsi_error_handler(void *data) |
| 2134 | { |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2135 | struct Scsi_Host *shost = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | /* |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2138 | * We use TASK_INTERRUPTIBLE so that the thread is not |
| 2139 | * counted against the load average as a running process. |
| 2140 | * We never actually get interrupted because kthread_run |
Frederik Schwarzer | c03264a | 2008-12-23 00:54:00 +0100 | [diff] [blame] | 2141 | * disables signal delivery for the created thread. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | */ |
Michal Hocko | 537b604 | 2015-08-27 20:16:37 +0200 | [diff] [blame] | 2143 | while (true) { |
| 2144 | /* |
| 2145 | * The sequence in kthread_stop() sets the stop flag first |
| 2146 | * then wakes the process. To avoid missed wakeups, the task |
| 2147 | * should always be in a non running state before the stop |
| 2148 | * flag is checked |
| 2149 | */ |
Dan Williams | b9d5c6b | 2012-06-21 23:25:42 -0700 | [diff] [blame] | 2150 | set_current_state(TASK_INTERRUPTIBLE); |
Michal Hocko | 537b604 | 2015-08-27 20:16:37 +0200 | [diff] [blame] | 2151 | if (kthread_should_stop()) |
| 2152 | break; |
| 2153 | |
Tejun Heo | ee7863b | 2006-05-15 20:57:20 +0900 | [diff] [blame] | 2154 | if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) || |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 2155 | shost->host_failed != atomic_read(&shost->host_busy)) { |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2156 | SCSI_LOG_ERROR_RECOVERY(1, |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2157 | shost_printk(KERN_INFO, shost, |
| 2158 | "scsi_eh_%d: sleeping\n", |
| 2159 | shost->host_no)); |
James Bottomley | 3ed7a47 | 2005-09-19 09:50:04 -0500 | [diff] [blame] | 2160 | schedule(); |
James Bottomley | 3ed7a47 | 2005-09-19 09:50:04 -0500 | [diff] [blame] | 2161 | continue; |
| 2162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | |
James Bottomley | 3ed7a47 | 2005-09-19 09:50:04 -0500 | [diff] [blame] | 2164 | __set_current_state(TASK_RUNNING); |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2165 | SCSI_LOG_ERROR_RECOVERY(1, |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2166 | shost_printk(KERN_INFO, shost, |
| 2167 | "scsi_eh_%d: waking up %d/%d/%d\n", |
| 2168 | shost->host_no, shost->host_eh_scheduled, |
Christoph Hellwig | 7466501 | 2014-01-22 15:29:29 +0100 | [diff] [blame] | 2169 | shost->host_failed, |
| 2170 | atomic_read(&shost->host_busy))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | /* |
| 2173 | * We have a host that is failing for some reason. Figure out |
| 2174 | * what we need to do to get it up and online again (if we can). |
| 2175 | * If we fail, we end up taking the thing offline. |
| 2176 | */ |
Lin Ming | ae0751f | 2011-12-05 09:20:24 +0800 | [diff] [blame] | 2177 | if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) { |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 2178 | SCSI_LOG_ERROR_RECOVERY(1, |
Hannes Reinecke | a222b1e | 2014-10-24 14:27:02 +0200 | [diff] [blame] | 2179 | shost_printk(KERN_ERR, shost, |
| 2180 | "scsi_eh_%d: unable to autoresume\n", |
| 2181 | shost->host_no)); |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 2182 | continue; |
| 2183 | } |
| 2184 | |
Christoph Hellwig | 9227c33 | 2006-04-01 19:21:04 +0200 | [diff] [blame] | 2185 | if (shost->transportt->eh_strategy_handler) |
| 2186 | shost->transportt->eh_strategy_handler(shost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | else |
| 2188 | scsi_unjam_host(shost); |
| 2189 | |
Wei Fang | 72d8c36 | 2016-06-07 14:53:56 +0800 | [diff] [blame] | 2190 | /* All scmds have been handled */ |
| 2191 | shost->host_failed = 0; |
| 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | /* |
| 2194 | * Note - if the above fails completely, the action is to take |
| 2195 | * individual devices offline and flush the queue of any |
| 2196 | * outstanding requests that may have been pending. When we |
| 2197 | * restart, we restart any I/O to any other devices on the bus |
| 2198 | * which are still online. |
| 2199 | */ |
| 2200 | scsi_restart_operations(shost); |
Lin Ming | ae0751f | 2011-12-05 09:20:24 +0800 | [diff] [blame] | 2201 | if (!shost->eh_noresume) |
| 2202 | scsi_autopm_put_host(shost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | } |
Steven Rostedt | 461a0ff | 2005-10-19 08:22:13 -0400 | [diff] [blame] | 2204 | __set_current_state(TASK_RUNNING); |
| 2205 | |
Christoph Hellwig | ad42eb1 | 2005-10-29 01:01:55 +0200 | [diff] [blame] | 2206 | SCSI_LOG_ERROR_RECOVERY(1, |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2207 | shost_printk(KERN_INFO, shost, |
| 2208 | "Error handler scsi_eh_%d exiting\n", |
| 2209 | shost->host_no)); |
James Bottomley | 3ed7a47 | 2005-09-19 09:50:04 -0500 | [diff] [blame] | 2210 | shost->ehandler = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | return 0; |
| 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * Function: scsi_report_bus_reset() |
| 2216 | * |
| 2217 | * Purpose: Utility function used by low-level drivers to report that |
| 2218 | * they have observed a bus reset on the bus being handled. |
| 2219 | * |
| 2220 | * Arguments: shost - Host in question |
| 2221 | * channel - channel on which reset was observed. |
| 2222 | * |
| 2223 | * Returns: Nothing |
| 2224 | * |
| 2225 | * Lock status: Host lock must be held. |
| 2226 | * |
| 2227 | * Notes: This only needs to be called if the reset is one which |
| 2228 | * originates from an unknown location. Resets originated |
| 2229 | * by the mid-level itself don't need to call this, but there |
| 2230 | * should be no harm. |
| 2231 | * |
| 2232 | * The main purpose of this is to make sure that a CHECK_CONDITION |
| 2233 | * is properly treated. |
| 2234 | */ |
| 2235 | void scsi_report_bus_reset(struct Scsi_Host *shost, int channel) |
| 2236 | { |
| 2237 | struct scsi_device *sdev; |
| 2238 | |
| 2239 | __shost_for_each_device(sdev, shost) { |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 2240 | if (channel == sdev_channel(sdev)) |
| 2241 | __scsi_report_device_reset(sdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | EXPORT_SYMBOL(scsi_report_bus_reset); |
| 2245 | |
| 2246 | /* |
| 2247 | * Function: scsi_report_device_reset() |
| 2248 | * |
| 2249 | * Purpose: Utility function used by low-level drivers to report that |
| 2250 | * they have observed a device reset on the device being handled. |
| 2251 | * |
| 2252 | * Arguments: shost - Host in question |
| 2253 | * channel - channel on which reset was observed |
| 2254 | * target - target on which reset was observed |
| 2255 | * |
| 2256 | * Returns: Nothing |
| 2257 | * |
| 2258 | * Lock status: Host lock must be held |
| 2259 | * |
| 2260 | * Notes: This only needs to be called if the reset is one which |
| 2261 | * originates from an unknown location. Resets originated |
| 2262 | * by the mid-level itself don't need to call this, but there |
| 2263 | * should be no harm. |
| 2264 | * |
| 2265 | * The main purpose of this is to make sure that a CHECK_CONDITION |
| 2266 | * is properly treated. |
| 2267 | */ |
| 2268 | void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target) |
| 2269 | { |
| 2270 | struct scsi_device *sdev; |
| 2271 | |
| 2272 | __shost_for_each_device(sdev, shost) { |
Jeff Garzik | 422c0d6 | 2005-10-24 18:05:09 -0400 | [diff] [blame] | 2273 | if (channel == sdev_channel(sdev) && |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 2274 | target == sdev_id(sdev)) |
| 2275 | __scsi_report_device_reset(sdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | EXPORT_SYMBOL(scsi_report_device_reset); |
| 2279 | |
| 2280 | static void |
| 2281 | scsi_reset_provider_done_command(struct scsi_cmnd *scmd) |
| 2282 | { |
| 2283 | } |
| 2284 | |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2285 | /** |
| 2286 | * scsi_ioctl_reset: explicitly reset a host/bus/target/device |
| 2287 | * @dev: scsi_device to operate on |
| 2288 | * @arg: reset type (see sg.h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | */ |
| 2290 | int |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2291 | scsi_ioctl_reset(struct scsi_device *dev, int __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | { |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 2293 | struct scsi_cmnd *scmd; |
James Smart | d7a1bb0 | 2006-03-08 14:50:12 -0500 | [diff] [blame] | 2294 | struct Scsi_Host *shost = dev->host; |
Christoph Hellwig | e9c787e | 2017-01-02 21:55:26 +0300 | [diff] [blame] | 2295 | struct request *rq; |
James Smart | d7a1bb0 | 2006-03-08 14:50:12 -0500 | [diff] [blame] | 2296 | unsigned long flags; |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2297 | int error = 0, rtn, val; |
| 2298 | |
| 2299 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 2300 | return -EACCES; |
| 2301 | |
| 2302 | error = get_user(val, arg); |
| 2303 | if (error) |
| 2304 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 2306 | if (scsi_autopm_get_host(shost) < 0) |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2307 | return -EIO; |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 2308 | |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2309 | error = -EIO; |
Christoph Hellwig | e9c787e | 2017-01-02 21:55:26 +0300 | [diff] [blame] | 2310 | rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) + |
| 2311 | shost->hostt->cmd_size, GFP_KERNEL); |
| 2312 | if (!rq) |
Christoph Hellwig | 95eeb5f | 2014-05-01 16:51:05 +0200 | [diff] [blame] | 2313 | goto out_put_autopm_host; |
Christoph Hellwig | e9c787e | 2017-01-02 21:55:26 +0300 | [diff] [blame] | 2314 | blk_rq_init(NULL, rq); |
Christoph Hellwig | 95eeb5f | 2014-05-01 16:51:05 +0200 | [diff] [blame] | 2315 | |
Christoph Hellwig | e9c787e | 2017-01-02 21:55:26 +0300 | [diff] [blame] | 2316 | scmd = (struct scsi_cmnd *)(rq + 1); |
| 2317 | scsi_init_command(dev, scmd); |
| 2318 | scmd->request = rq; |
Christoph Hellwig | 82ed4db | 2017-01-27 09:46:29 +0100 | [diff] [blame] | 2319 | scmd->cmnd = scsi_req(rq)->cmd; |
Boaz Harrosh | 64a87b2 | 2008-04-30 11:19:47 +0300 | [diff] [blame] | 2320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | scmd->scsi_done = scsi_reset_provider_done_command; |
Boaz Harrosh | 30b0c37 | 2007-12-13 13:47:40 +0200 | [diff] [blame] | 2322 | memset(&scmd->sdb, 0, sizeof(scmd->sdb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
| 2324 | scmd->cmd_len = 0; |
| 2325 | |
| 2326 | scmd->sc_data_direction = DMA_BIDIRECTIONAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
James Smart | d7a1bb0 | 2006-03-08 14:50:12 -0500 | [diff] [blame] | 2328 | spin_lock_irqsave(shost->host_lock, flags); |
| 2329 | shost->tmf_in_progress = 1; |
| 2330 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2331 | |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2332 | switch (val & ~SG_SCSI_RESET_NO_ESCALATE) { |
| 2333 | case SG_SCSI_RESET_NOTHING: |
| 2334 | rtn = SUCCESS; |
| 2335 | break; |
| 2336 | case SG_SCSI_RESET_DEVICE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | rtn = scsi_try_bus_device_reset(scmd); |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2338 | if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | break; |
| 2340 | /* FALLTHROUGH */ |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2341 | case SG_SCSI_RESET_TARGET: |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 2342 | rtn = scsi_try_target_reset(scmd); |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2343 | if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) |
Mike Christie | 30bd7df | 2008-02-29 18:25:19 -0600 | [diff] [blame] | 2344 | break; |
| 2345 | /* FALLTHROUGH */ |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2346 | case SG_SCSI_RESET_BUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | rtn = scsi_try_bus_reset(scmd); |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2348 | if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | break; |
| 2350 | /* FALLTHROUGH */ |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2351 | case SG_SCSI_RESET_HOST: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | rtn = scsi_try_host_reset(scmd); |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2353 | if (rtn == SUCCESS) |
| 2354 | break; |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2355 | /* FALLTHROUGH */ |
Bart Van Assche | 3bf2ff6 | 2017-08-25 13:46:29 -0700 | [diff] [blame] | 2356 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | rtn = FAILED; |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2358 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2361 | error = (rtn == SUCCESS) ? 0 : -EIO; |
| 2362 | |
James Smart | d7a1bb0 | 2006-03-08 14:50:12 -0500 | [diff] [blame] | 2363 | spin_lock_irqsave(shost->host_lock, flags); |
| 2364 | shost->tmf_in_progress = 0; |
| 2365 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 2366 | |
| 2367 | /* |
| 2368 | * be sure to wake up anyone who was sleeping or had their queue |
| 2369 | * suspended while we performed the TMF. |
| 2370 | */ |
| 2371 | SCSI_LOG_ERROR_RECOVERY(3, |
Hannes Reinecke | 91921e0 | 2014-06-25 16:39:59 +0200 | [diff] [blame] | 2372 | shost_printk(KERN_INFO, shost, |
| 2373 | "waking up host to restart after TMF\n")); |
James Smart | d7a1bb0 | 2006-03-08 14:50:12 -0500 | [diff] [blame] | 2374 | |
| 2375 | wake_up(&shost->host_wait); |
James Smart | d7a1bb0 | 2006-03-08 14:50:12 -0500 | [diff] [blame] | 2376 | scsi_run_host_queues(shost); |
| 2377 | |
Christoph Hellwig | 0f121dd | 2014-09-05 18:00:05 -0700 | [diff] [blame] | 2378 | scsi_put_command(scmd); |
Christoph Hellwig | e9c787e | 2017-01-02 21:55:26 +0300 | [diff] [blame] | 2379 | kfree(rq); |
Christoph Hellwig | 0f121dd | 2014-09-05 18:00:05 -0700 | [diff] [blame] | 2380 | |
Christoph Hellwig | 0479633 | 2014-02-20 14:20:55 -0800 | [diff] [blame] | 2381 | out_put_autopm_host: |
Alan Stern | bc4f240 | 2010-06-17 10:41:42 -0400 | [diff] [blame] | 2382 | scsi_autopm_put_host(shost); |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2383 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | } |
Christoph Hellwig | 176aa9d | 2014-10-11 12:06:47 +0200 | [diff] [blame] | 2385 | EXPORT_SYMBOL(scsi_ioctl_reset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Hannes Reinecke | 4753cbc | 2014-10-24 14:26:52 +0200 | [diff] [blame] | 2387 | bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd, |
| 2388 | struct scsi_sense_hdr *sshdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | { |
| 2390 | return scsi_normalize_sense(cmd->sense_buffer, |
FUJITA Tomonori | b80ca4f | 2008-01-13 15:46:13 +0900 | [diff] [blame] | 2391 | SCSI_SENSE_BUFFERSIZE, sshdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | } |
| 2393 | EXPORT_SYMBOL(scsi_command_normalize_sense); |
| 2394 | |
| 2395 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 2396 | * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | * @sense_buffer: byte array of sense data |
| 2398 | * @sb_len: number of valid bytes in sense_buffer |
| 2399 | * @info_out: pointer to 64 integer where 8 or 4 byte information |
| 2400 | * field will be placed if found. |
| 2401 | * |
| 2402 | * Return value: |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 2403 | * true if information field found, false if not found. |
Randy Dunlap | dc8875e | 2007-11-15 15:42:30 -0800 | [diff] [blame] | 2404 | */ |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 2405 | bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len, |
| 2406 | u64 *info_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | const u8 * ucp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
| 2410 | if (sb_len < 7) |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 2411 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | switch (sense_buffer[0] & 0x7f) { |
| 2413 | case 0x70: |
| 2414 | case 0x71: |
| 2415 | if (sense_buffer[0] & 0x80) { |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 2416 | *info_out = get_unaligned_be32(&sense_buffer[3]); |
| 2417 | return true; |
| 2418 | } |
| 2419 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | case 0x72: |
| 2421 | case 0x73: |
| 2422 | ucp = scsi_sense_desc_find(sense_buffer, sb_len, |
| 2423 | 0 /* info desc */); |
| 2424 | if (ucp && (0xa == ucp[1])) { |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 2425 | *info_out = get_unaligned_be64(&ucp[4]); |
| 2426 | return true; |
| 2427 | } |
| 2428 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | default: |
Damien Le Moal | 2908769 | 2017-04-24 16:51:12 +0900 | [diff] [blame] | 2430 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | EXPORT_SYMBOL(scsi_get_sense_info_fld); |