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