Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * USB Attached SCSI |
| 3 | * Note that this is not the same as the USB Mass Storage driver |
| 4 | * |
| 5 | * Copyright Matthew Wilcox for Intel Corp, 2010 |
| 6 | * Copyright Sarah Sharp for Intel Corp, 2010 |
| 7 | * |
| 8 | * Distributed under the terms of the GNU GPL, version two. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/types.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 15 | #include <linux/usb.h> |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 16 | #include <linux/usb_usual.h> |
Sebastian Andrzej Siewior | c898add | 2012-01-11 12:42:32 +0100 | [diff] [blame] | 17 | #include <linux/usb/hcd.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 18 | #include <linux/usb/storage.h> |
Sebastian Andrzej Siewior | 348748b | 2012-01-11 12:45:56 +0100 | [diff] [blame] | 19 | #include <linux/usb/uas.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 20 | |
| 21 | #include <scsi/scsi.h> |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 22 | #include <scsi/scsi_eh.h> |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 23 | #include <scsi/scsi_dbg.h> |
| 24 | #include <scsi/scsi_cmnd.h> |
| 25 | #include <scsi/scsi_device.h> |
| 26 | #include <scsi/scsi_host.h> |
| 27 | #include <scsi/scsi_tcq.h> |
| 28 | |
Hans de Goede | 82aa038 | 2013-10-21 08:53:31 +0100 | [diff] [blame] | 29 | #include "uas-detect.h" |
| 30 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 31 | /* |
| 32 | * The r00-r01c specs define this version of the SENSE IU data structure. |
| 33 | * It's still in use by several different firmware releases. |
| 34 | */ |
| 35 | struct sense_iu_old { |
| 36 | __u8 iu_id; |
| 37 | __u8 rsvd1; |
| 38 | __be16 tag; |
| 39 | __be16 len; |
| 40 | __u8 status; |
| 41 | __u8 service_response; |
| 42 | __u8 sense[SCSI_SENSE_BUFFERSIZE]; |
| 43 | }; |
| 44 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 45 | struct uas_dev_info { |
| 46 | struct usb_interface *intf; |
| 47 | struct usb_device *udev; |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 48 | struct usb_anchor cmd_urbs; |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 49 | struct usb_anchor sense_urbs; |
| 50 | struct usb_anchor data_urbs; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 51 | int qdepth, resetting; |
Hans de Goede | e52e031 | 2013-10-23 14:27:09 +0100 | [diff] [blame] | 52 | struct response_iu response; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 53 | unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe; |
| 54 | unsigned use_streams:1; |
| 55 | unsigned uas_sense_old:1; |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 56 | unsigned running_task:1; |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 57 | unsigned shutdown:1; |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 58 | struct scsi_cmnd *cmnd; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 59 | spinlock_t lock; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 60 | struct work_struct work; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 61 | struct list_head inflight_list; |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 62 | struct list_head dead_list; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | enum { |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 66 | SUBMIT_STATUS_URB = (1 << 1), |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 67 | ALLOC_DATA_IN_URB = (1 << 2), |
| 68 | SUBMIT_DATA_IN_URB = (1 << 3), |
| 69 | ALLOC_DATA_OUT_URB = (1 << 4), |
| 70 | SUBMIT_DATA_OUT_URB = (1 << 5), |
| 71 | ALLOC_CMD_URB = (1 << 6), |
| 72 | SUBMIT_CMD_URB = (1 << 7), |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 73 | COMMAND_INFLIGHT = (1 << 8), |
| 74 | DATA_IN_URB_INFLIGHT = (1 << 9), |
| 75 | DATA_OUT_URB_INFLIGHT = (1 << 10), |
| 76 | COMMAND_COMPLETED = (1 << 11), |
Gerd Hoffmann | ef018cc9 | 2012-09-25 10:47:06 +0200 | [diff] [blame] | 77 | COMMAND_ABORTED = (1 << 12), |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 78 | UNLINK_DATA_URBS = (1 << 13), |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 79 | IS_IN_WORK_LIST = (1 << 14), |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* Overrides scsi_pointer */ |
| 83 | struct uas_cmd_info { |
| 84 | unsigned int state; |
| 85 | unsigned int stream; |
| 86 | struct urb *cmd_urb; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 87 | struct urb *data_in_urb; |
| 88 | struct urb *data_out_urb; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 89 | struct list_head inflight; |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 90 | struct list_head dead; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /* I hate forward declarations, but I actually have a loop */ |
| 94 | static int uas_submit_urbs(struct scsi_cmnd *cmnd, |
| 95 | struct uas_dev_info *devinfo, gfp_t gfp); |
Sarah Sharp | ea9da1c | 2011-12-02 11:55:44 -0800 | [diff] [blame] | 96 | static void uas_do_work(struct work_struct *work); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 97 | static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller); |
Gerd Hoffmann | d89bd83 | 2013-09-13 13:27:11 +0200 | [diff] [blame] | 98 | static void uas_free_streams(struct uas_dev_info *devinfo); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 99 | static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 100 | |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 101 | /* Must be called with devinfo->lock held, will temporary unlock the lock */ |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 102 | static void uas_unlink_data_urbs(struct uas_dev_info *devinfo, |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 103 | struct uas_cmd_info *cmdinfo, |
| 104 | unsigned long *lock_flags) |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 105 | { |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 106 | /* |
| 107 | * The UNLINK_DATA_URBS flag makes sure uas_try_complete |
| 108 | * (called by urb completion) doesn't release cmdinfo |
| 109 | * underneath us. |
| 110 | */ |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 111 | cmdinfo->state |= UNLINK_DATA_URBS; |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 112 | spin_unlock_irqrestore(&devinfo->lock, *lock_flags); |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 113 | |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 114 | if (cmdinfo->data_in_urb) |
| 115 | usb_unlink_urb(cmdinfo->data_in_urb); |
| 116 | if (cmdinfo->data_out_urb) |
| 117 | usb_unlink_urb(cmdinfo->data_out_urb); |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 118 | |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 119 | spin_lock_irqsave(&devinfo->lock, *lock_flags); |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 120 | cmdinfo->state &= ~UNLINK_DATA_URBS; |
Gerd Hoffmann | aa8f612 | 2012-11-30 11:54:40 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 123 | static void uas_do_work(struct work_struct *work) |
| 124 | { |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 125 | struct uas_dev_info *devinfo = |
| 126 | container_of(work, struct uas_dev_info, work); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 127 | struct uas_cmd_info *cmdinfo; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 128 | unsigned long flags; |
Sarah Sharp | ea9da1c | 2011-12-02 11:55:44 -0800 | [diff] [blame] | 129 | int err; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 130 | |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 131 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 132 | list_for_each_entry(cmdinfo, &devinfo->inflight_list, inflight) { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 133 | struct scsi_pointer *scp = (void *)cmdinfo; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 134 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, |
| 135 | SCp); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 136 | |
| 137 | if (!(cmdinfo->state & IS_IN_WORK_LIST)) |
| 138 | continue; |
| 139 | |
Hans de Goede | e36e649 | 2013-11-07 08:35:55 +0100 | [diff] [blame] | 140 | err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 141 | if (!err) |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 142 | cmdinfo->state &= ~IS_IN_WORK_LIST; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 143 | else |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 144 | schedule_work(&devinfo->work); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 145 | } |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 146 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 149 | static void uas_abort_inflight(struct uas_dev_info *devinfo) |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 150 | { |
| 151 | struct uas_cmd_info *cmdinfo; |
| 152 | struct uas_cmd_info *temp; |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 153 | unsigned long flags; |
| 154 | |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 155 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 156 | list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, |
| 157 | inflight) { |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 158 | struct scsi_pointer *scp = (void *)cmdinfo; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 159 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, |
| 160 | SCp); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 161 | uas_log_cmd_state(cmnd, __func__); |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 162 | WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED); |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 163 | cmdinfo->state |= COMMAND_ABORTED; |
| 164 | cmdinfo->state &= ~IS_IN_WORK_LIST; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 165 | list_del(&cmdinfo->inflight); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 166 | list_add_tail(&cmdinfo->dead, &devinfo->dead_list); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 167 | } |
| 168 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 169 | } |
| 170 | |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 171 | static void uas_add_work(struct uas_cmd_info *cmdinfo) |
| 172 | { |
| 173 | struct scsi_pointer *scp = (void *)cmdinfo; |
| 174 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp); |
| 175 | struct uas_dev_info *devinfo = cmnd->device->hostdata; |
| 176 | |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 177 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 178 | cmdinfo->state |= IS_IN_WORK_LIST; |
| 179 | schedule_work(&devinfo->work); |
| 180 | } |
| 181 | |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 182 | static void uas_zap_dead(struct uas_dev_info *devinfo) |
| 183 | { |
| 184 | struct uas_cmd_info *cmdinfo; |
| 185 | struct uas_cmd_info *temp; |
| 186 | unsigned long flags; |
| 187 | |
| 188 | spin_lock_irqsave(&devinfo->lock, flags); |
| 189 | list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, dead) { |
| 190 | struct scsi_pointer *scp = (void *)cmdinfo; |
| 191 | struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, |
| 192 | SCp); |
| 193 | uas_log_cmd_state(cmnd, __func__); |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 194 | WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED)); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 195 | /* all urbs are killed, clear inflight bits */ |
| 196 | cmdinfo->state &= ~(COMMAND_INFLIGHT | |
| 197 | DATA_IN_URB_INFLIGHT | |
| 198 | DATA_OUT_URB_INFLIGHT); |
| 199 | uas_try_complete(cmnd, __func__); |
| 200 | } |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 201 | devinfo->running_task = 0; |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 202 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 203 | } |
| 204 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 205 | static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd) |
| 206 | { |
| 207 | struct sense_iu *sense_iu = urb->transfer_buffer; |
| 208 | struct scsi_device *sdev = cmnd->device; |
| 209 | |
| 210 | if (urb->actual_length > 16) { |
| 211 | unsigned len = be16_to_cpup(&sense_iu->len); |
| 212 | if (len + 16 != urb->actual_length) { |
| 213 | int newlen = min(len + 16, urb->actual_length) - 16; |
| 214 | if (newlen < 0) |
| 215 | newlen = 0; |
| 216 | sdev_printk(KERN_INFO, sdev, "%s: urb length %d " |
| 217 | "disagrees with IU sense data length %d, " |
| 218 | "using %d bytes of sense data\n", __func__, |
| 219 | urb->actual_length, len, newlen); |
| 220 | len = newlen; |
| 221 | } |
| 222 | memcpy(cmnd->sense_buffer, sense_iu->sense, len); |
| 223 | } |
| 224 | |
| 225 | cmnd->result = sense_iu->status; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd) |
| 229 | { |
| 230 | struct sense_iu_old *sense_iu = urb->transfer_buffer; |
| 231 | struct scsi_device *sdev = cmnd->device; |
| 232 | |
| 233 | if (urb->actual_length > 8) { |
| 234 | unsigned len = be16_to_cpup(&sense_iu->len) - 2; |
| 235 | if (len + 8 != urb->actual_length) { |
| 236 | int newlen = min(len + 8, urb->actual_length) - 8; |
| 237 | if (newlen < 0) |
| 238 | newlen = 0; |
| 239 | sdev_printk(KERN_INFO, sdev, "%s: urb length %d " |
| 240 | "disagrees with IU sense data length %d, " |
| 241 | "using %d bytes of sense data\n", __func__, |
| 242 | urb->actual_length, len, newlen); |
| 243 | len = newlen; |
| 244 | } |
| 245 | memcpy(cmnd->sense_buffer, sense_iu->sense, len); |
| 246 | } |
| 247 | |
| 248 | cmnd->result = sense_iu->status; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller) |
| 252 | { |
| 253 | struct uas_cmd_info *ci = (void *)&cmnd->SCp; |
| 254 | |
| 255 | scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:" |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 256 | "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 257 | caller, cmnd, cmnd->request->tag, |
| 258 | (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "", |
| 259 | (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "", |
| 260 | (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "", |
| 261 | (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "", |
| 262 | (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "", |
| 263 | (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "", |
| 264 | (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "", |
| 265 | (ci->state & COMMAND_INFLIGHT) ? " CMD" : "", |
| 266 | (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "", |
| 267 | (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "", |
Gerd Hoffmann | ef018cc9 | 2012-09-25 10:47:06 +0200 | [diff] [blame] | 268 | (ci->state & COMMAND_COMPLETED) ? " done" : "", |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 269 | (ci->state & COMMAND_ABORTED) ? " abort" : "", |
Gerd Hoffmann | efefecf | 2012-11-30 11:54:42 +0100 | [diff] [blame] | 270 | (ci->state & UNLINK_DATA_URBS) ? " unlink": "", |
| 271 | (ci->state & IS_IN_WORK_LIST) ? " work" : ""); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller) |
| 275 | { |
| 276 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 277 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 278 | |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 279 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 280 | if (cmdinfo->state & (COMMAND_INFLIGHT | |
| 281 | DATA_IN_URB_INFLIGHT | |
Gerd Hoffmann | b06e48a | 2012-11-30 11:54:41 +0100 | [diff] [blame] | 282 | DATA_OUT_URB_INFLIGHT | |
| 283 | UNLINK_DATA_URBS)) |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 284 | return -EBUSY; |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 285 | WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 286 | cmdinfo->state |= COMMAND_COMPLETED; |
| 287 | usb_free_urb(cmdinfo->data_in_urb); |
| 288 | usb_free_urb(cmdinfo->data_out_urb); |
Gerd Hoffmann | 0871d7d | 2012-09-25 10:47:07 +0200 | [diff] [blame] | 289 | if (cmdinfo->state & COMMAND_ABORTED) { |
| 290 | scmd_printk(KERN_INFO, cmnd, "abort completed\n"); |
| 291 | cmnd->result = DID_ABORT << 16; |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 292 | list_del(&cmdinfo->dead); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 293 | } else |
| 294 | list_del(&cmdinfo->inflight); |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 295 | cmnd->scsi_done(cmnd); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 296 | return 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 300 | unsigned direction) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 301 | { |
| 302 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
| 303 | int err; |
| 304 | |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 305 | cmdinfo->state |= direction | SUBMIT_STATUS_URB; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 306 | err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC); |
| 307 | if (err) { |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 308 | uas_add_work(cmdinfo); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | static void uas_stat_cmplt(struct urb *urb) |
| 313 | { |
| 314 | struct iu *iu = urb->transfer_buffer; |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 315 | struct Scsi_Host *shost = urb->context; |
| 316 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 317 | struct scsi_cmnd *cmnd; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 318 | struct uas_cmd_info *cmdinfo; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 319 | unsigned long flags; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 320 | u16 tag; |
| 321 | |
| 322 | if (urb->status) { |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 323 | if (urb->status == -ENOENT) { |
| 324 | dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n", |
| 325 | urb->stream_id); |
| 326 | } else { |
| 327 | dev_err(&urb->dev->dev, "stat urb: status %d\n", |
| 328 | urb->status); |
| 329 | } |
Gerd Hoffmann | db32de1 | 2012-06-19 09:54:49 +0200 | [diff] [blame] | 330 | usb_free_urb(urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 334 | if (devinfo->resetting) { |
| 335 | usb_free_urb(urb); |
| 336 | return; |
| 337 | } |
| 338 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 339 | spin_lock_irqsave(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 340 | tag = be16_to_cpup(&iu->tag) - 1; |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 341 | if (tag == 0) |
| 342 | cmnd = devinfo->cmnd; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 343 | else |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 344 | cmnd = scsi_host_find_tag(shost, tag - 1); |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 345 | |
Sarah Sharp | 96c1eb9 | 2011-12-02 11:55:48 -0800 | [diff] [blame] | 346 | if (!cmnd) { |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 347 | if (iu->iu_id == IU_ID_RESPONSE) { |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 348 | if (!devinfo->running_task) |
| 349 | dev_warn(&urb->dev->dev, |
| 350 | "stat urb: recv unexpected response iu\n"); |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 351 | /* store results for uas_eh_task_mgmt() */ |
| 352 | memcpy(&devinfo->response, iu, sizeof(devinfo->response)); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 353 | } |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 354 | usb_free_urb(urb); |
| 355 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 356 | return; |
Sarah Sharp | 96c1eb9 | 2011-12-02 11:55:48 -0800 | [diff] [blame] | 357 | } |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 358 | |
Gerd Hoffmann | e0423de | 2012-09-26 10:29:03 +0200 | [diff] [blame] | 359 | cmdinfo = (void *)&cmnd->SCp; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 360 | switch (iu->iu_id) { |
| 361 | case IU_ID_STATUS: |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 362 | if (devinfo->cmnd == cmnd) |
| 363 | devinfo->cmnd = NULL; |
| 364 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 365 | if (urb->actual_length < 16) |
| 366 | devinfo->uas_sense_old = 1; |
| 367 | if (devinfo->uas_sense_old) |
| 368 | uas_sense_old(urb, cmnd); |
| 369 | else |
| 370 | uas_sense(urb, cmnd); |
Gerd Hoffmann | 8aac863 | 2012-06-19 09:54:52 +0200 | [diff] [blame] | 371 | if (cmnd->result != 0) { |
| 372 | /* cancel data transfers on error */ |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 373 | uas_unlink_data_urbs(devinfo, cmdinfo, &flags); |
Gerd Hoffmann | 8aac863 | 2012-06-19 09:54:52 +0200 | [diff] [blame] | 374 | } |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 375 | cmdinfo->state &= ~COMMAND_INFLIGHT; |
| 376 | uas_try_complete(cmnd, __func__); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 377 | break; |
| 378 | case IU_ID_READ_READY: |
| 379 | uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB); |
| 380 | break; |
| 381 | case IU_ID_WRITE_READY: |
| 382 | uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB); |
| 383 | break; |
| 384 | default: |
| 385 | scmd_printk(KERN_ERR, cmnd, |
| 386 | "Bogus IU (%d) received on status pipe\n", iu->iu_id); |
| 387 | } |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 388 | usb_free_urb(urb); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 389 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 392 | static void uas_data_cmplt(struct urb *urb) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 393 | { |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 394 | struct scsi_cmnd *cmnd = urb->context; |
| 395 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 396 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 397 | struct scsi_data_buffer *sdb = NULL; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 398 | unsigned long flags; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 399 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 400 | spin_lock_irqsave(&devinfo->lock, flags); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 401 | if (cmdinfo->data_in_urb == urb) { |
| 402 | sdb = scsi_in(cmnd); |
| 403 | cmdinfo->state &= ~DATA_IN_URB_INFLIGHT; |
| 404 | } else if (cmdinfo->data_out_urb == urb) { |
| 405 | sdb = scsi_out(cmnd); |
| 406 | cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT; |
| 407 | } |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 408 | if (sdb == NULL) { |
| 409 | WARN_ON_ONCE(1); |
| 410 | } else if (urb->status) { |
Gerd Hoffmann | 8aac863 | 2012-06-19 09:54:52 +0200 | [diff] [blame] | 411 | /* error: no data transfered */ |
| 412 | sdb->resid = sdb->length; |
| 413 | } else { |
| 414 | sdb->resid = sdb->length - urb->actual_length; |
| 415 | } |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 416 | uas_try_complete(cmnd, __func__); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 417 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 421 | unsigned int pipe, u16 stream_id, |
| 422 | struct scsi_cmnd *cmnd, |
| 423 | enum dma_data_direction dir) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 424 | { |
| 425 | struct usb_device *udev = devinfo->udev; |
| 426 | struct urb *urb = usb_alloc_urb(0, gfp); |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 427 | struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE) |
| 428 | ? scsi_in(cmnd) : scsi_out(cmnd); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 429 | |
| 430 | if (!urb) |
| 431 | goto out; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 432 | usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length, |
| 433 | uas_data_cmplt, cmnd); |
Hans de Goede | c6d4579 | 2013-11-12 10:53:57 +0100 | [diff] [blame] | 434 | urb->stream_id = stream_id; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 435 | urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0; |
| 436 | urb->sg = sdb->table.sgl; |
| 437 | out: |
| 438 | return urb; |
| 439 | } |
| 440 | |
| 441 | static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp, |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 442 | struct Scsi_Host *shost, u16 stream_id) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 443 | { |
| 444 | struct usb_device *udev = devinfo->udev; |
| 445 | struct urb *urb = usb_alloc_urb(0, gfp); |
| 446 | struct sense_iu *iu; |
| 447 | |
| 448 | if (!urb) |
| 449 | goto out; |
| 450 | |
Matthew Wilcox | ac563cf | 2010-12-15 15:44:03 -0500 | [diff] [blame] | 451 | iu = kzalloc(sizeof(*iu), gfp); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 452 | if (!iu) |
| 453 | goto free; |
| 454 | |
| 455 | usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu), |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 456 | uas_stat_cmplt, shost); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 457 | urb->stream_id = stream_id; |
| 458 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 459 | out: |
| 460 | return urb; |
| 461 | free: |
| 462 | usb_free_urb(urb); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp, |
Hans de Goede | a887cd3 | 2013-10-17 19:47:28 +0200 | [diff] [blame] | 467 | struct scsi_cmnd *cmnd) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 468 | { |
| 469 | struct usb_device *udev = devinfo->udev; |
| 470 | struct scsi_device *sdev = cmnd->device; |
| 471 | struct urb *urb = usb_alloc_urb(0, gfp); |
| 472 | struct command_iu *iu; |
| 473 | int len; |
| 474 | |
| 475 | if (!urb) |
| 476 | goto out; |
| 477 | |
| 478 | len = cmnd->cmd_len - 16; |
| 479 | if (len < 0) |
| 480 | len = 0; |
| 481 | len = ALIGN(len, 4); |
Matthew Wilcox | ac563cf | 2010-12-15 15:44:03 -0500 | [diff] [blame] | 482 | iu = kzalloc(sizeof(*iu) + len, gfp); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 483 | if (!iu) |
| 484 | goto free; |
| 485 | |
| 486 | iu->iu_id = IU_ID_COMMAND; |
Sarah Sharp | 9eb4454 | 2011-12-02 11:55:46 -0800 | [diff] [blame] | 487 | if (blk_rq_tagged(cmnd->request)) |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 488 | iu->tag = cpu_to_be16(cmnd->request->tag + 2); |
Sarah Sharp | 9eb4454 | 2011-12-02 11:55:46 -0800 | [diff] [blame] | 489 | else |
| 490 | iu->tag = cpu_to_be16(1); |
Christoph Hellwig | 02e031c | 2010-11-10 14:54:09 +0100 | [diff] [blame] | 491 | iu->prio_attr = UAS_SIMPLE_TAG; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 492 | iu->len = len; |
| 493 | int_to_scsilun(sdev->lun, &iu->lun); |
| 494 | memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len); |
| 495 | |
| 496 | usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len, |
| 497 | usb_free_urb, NULL); |
| 498 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 499 | out: |
| 500 | return urb; |
| 501 | free: |
| 502 | usb_free_urb(urb); |
| 503 | return NULL; |
| 504 | } |
| 505 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 506 | static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp, |
| 507 | u8 function, u16 stream_id) |
| 508 | { |
| 509 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
| 510 | struct usb_device *udev = devinfo->udev; |
| 511 | struct urb *urb = usb_alloc_urb(0, gfp); |
| 512 | struct task_mgmt_iu *iu; |
| 513 | int err = -ENOMEM; |
| 514 | |
| 515 | if (!urb) |
| 516 | goto err; |
| 517 | |
| 518 | iu = kzalloc(sizeof(*iu), gfp); |
| 519 | if (!iu) |
| 520 | goto err; |
| 521 | |
| 522 | iu->iu_id = IU_ID_TASK_MGMT; |
| 523 | iu->tag = cpu_to_be16(stream_id); |
| 524 | int_to_scsilun(cmnd->device->lun, &iu->lun); |
| 525 | |
| 526 | iu->function = function; |
| 527 | switch (function) { |
| 528 | case TMF_ABORT_TASK: |
| 529 | if (blk_rq_tagged(cmnd->request)) |
| 530 | iu->task_tag = cpu_to_be16(cmnd->request->tag + 2); |
| 531 | else |
| 532 | iu->task_tag = cpu_to_be16(1); |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu), |
| 537 | usb_free_urb, NULL); |
| 538 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 539 | |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 540 | usb_anchor_urb(urb, &devinfo->cmd_urbs); |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 541 | err = usb_submit_urb(urb, gfp); |
| 542 | if (err) { |
| 543 | usb_unanchor_urb(urb); |
| 544 | goto err; |
| 545 | } |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 546 | |
| 547 | return 0; |
| 548 | |
| 549 | err: |
| 550 | usb_free_urb(urb); |
| 551 | return err; |
| 552 | } |
| 553 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 554 | /* |
| 555 | * Why should I request the Status IU before sending the Command IU? Spec |
| 556 | * says to, but also says the device may receive them in any order. Seems |
| 557 | * daft to me. |
| 558 | */ |
| 559 | |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 560 | static struct urb *uas_submit_sense_urb(struct Scsi_Host *shost, |
| 561 | gfp_t gfp, unsigned int stream) |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 562 | { |
| 563 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 564 | struct urb *urb; |
| 565 | |
| 566 | urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream); |
| 567 | if (!urb) |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 568 | return NULL; |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 569 | usb_anchor_urb(urb, &devinfo->sense_urbs); |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 570 | if (usb_submit_urb(urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 571 | usb_unanchor_urb(urb); |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 572 | shost_printk(KERN_INFO, shost, |
| 573 | "sense urb submission failure\n"); |
| 574 | usb_free_urb(urb); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 575 | return NULL; |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 576 | } |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 577 | return urb; |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 580 | static int uas_submit_urbs(struct scsi_cmnd *cmnd, |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 581 | struct uas_dev_info *devinfo, gfp_t gfp) |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 582 | { |
| 583 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 584 | struct urb *urb; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 585 | |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 586 | WARN_ON_ONCE(!spin_is_locked(&devinfo->lock)); |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 587 | if (cmdinfo->state & SUBMIT_STATUS_URB) { |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 588 | urb = uas_submit_sense_urb(cmnd->device->host, gfp, |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 589 | cmdinfo->stream); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 590 | if (!urb) |
| 591 | return SCSI_MLQUEUE_DEVICE_BUSY; |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 592 | cmdinfo->state &= ~SUBMIT_STATUS_URB; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | if (cmdinfo->state & ALLOC_DATA_IN_URB) { |
| 596 | cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp, |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 597 | devinfo->data_in_pipe, cmdinfo->stream, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 598 | cmnd, DMA_FROM_DEVICE); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 599 | if (!cmdinfo->data_in_urb) |
| 600 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 601 | cmdinfo->state &= ~ALLOC_DATA_IN_URB; |
| 602 | } |
| 603 | |
| 604 | if (cmdinfo->state & SUBMIT_DATA_IN_URB) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 605 | usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 606 | if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 607 | usb_unanchor_urb(cmdinfo->data_in_urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 608 | scmd_printk(KERN_INFO, cmnd, |
| 609 | "data in urb submission failure\n"); |
| 610 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 611 | } |
| 612 | cmdinfo->state &= ~SUBMIT_DATA_IN_URB; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 613 | cmdinfo->state |= DATA_IN_URB_INFLIGHT; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (cmdinfo->state & ALLOC_DATA_OUT_URB) { |
| 617 | cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp, |
Gerd Hoffmann | c621a81 | 2012-06-19 09:54:48 +0200 | [diff] [blame] | 618 | devinfo->data_out_pipe, cmdinfo->stream, |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 619 | cmnd, DMA_TO_DEVICE); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 620 | if (!cmdinfo->data_out_urb) |
| 621 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 622 | cmdinfo->state &= ~ALLOC_DATA_OUT_URB; |
| 623 | } |
| 624 | |
| 625 | if (cmdinfo->state & SUBMIT_DATA_OUT_URB) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 626 | usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 627 | if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 628 | usb_unanchor_urb(cmdinfo->data_out_urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 629 | scmd_printk(KERN_INFO, cmnd, |
| 630 | "data out urb submission failure\n"); |
| 631 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 632 | } |
| 633 | cmdinfo->state &= ~SUBMIT_DATA_OUT_URB; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 634 | cmdinfo->state |= DATA_OUT_URB_INFLIGHT; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (cmdinfo->state & ALLOC_CMD_URB) { |
Hans de Goede | a887cd3 | 2013-10-17 19:47:28 +0200 | [diff] [blame] | 638 | cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 639 | if (!cmdinfo->cmd_urb) |
| 640 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 641 | cmdinfo->state &= ~ALLOC_CMD_URB; |
| 642 | } |
| 643 | |
| 644 | if (cmdinfo->state & SUBMIT_CMD_URB) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 645 | usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 646 | if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) { |
Hans de Goede | d5f808d | 2013-10-17 19:30:26 +0200 | [diff] [blame] | 647 | usb_unanchor_urb(cmdinfo->cmd_urb); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 648 | scmd_printk(KERN_INFO, cmnd, |
| 649 | "cmd urb submission failure\n"); |
| 650 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 651 | } |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 652 | cmdinfo->cmd_urb = NULL; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 653 | cmdinfo->state &= ~SUBMIT_CMD_URB; |
Gerd Hoffmann | b1d6769 | 2012-06-19 09:54:51 +0200 | [diff] [blame] | 654 | cmdinfo->state |= COMMAND_INFLIGHT; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 660 | static int uas_queuecommand_lck(struct scsi_cmnd *cmnd, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 661 | void (*done)(struct scsi_cmnd *)) |
| 662 | { |
| 663 | struct scsi_device *sdev = cmnd->device; |
| 664 | struct uas_dev_info *devinfo = sdev->hostdata; |
| 665 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 666 | unsigned long flags; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 667 | int err; |
| 668 | |
| 669 | BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer)); |
| 670 | |
Gerd Hoffmann | f8be6bf | 2012-11-30 11:54:45 +0100 | [diff] [blame] | 671 | if (devinfo->resetting) { |
| 672 | cmnd->result = DID_ERROR << 16; |
| 673 | cmnd->scsi_done(cmnd); |
| 674 | return 0; |
| 675 | } |
| 676 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 677 | spin_lock_irqsave(&devinfo->lock, flags); |
| 678 | if (devinfo->cmnd) { |
| 679 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 680 | return SCSI_MLQUEUE_DEVICE_BUSY; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 681 | } |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 682 | |
| 683 | if (blk_rq_tagged(cmnd->request)) { |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 684 | cmdinfo->stream = cmnd->request->tag + 2; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 685 | } else { |
Sebastian Andrzej Siewior | 22188f4 | 2011-12-19 20:22:39 +0100 | [diff] [blame] | 686 | devinfo->cmnd = cmnd; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 687 | cmdinfo->stream = 1; |
| 688 | } |
| 689 | |
| 690 | cmnd->scsi_done = done; |
| 691 | |
Gerd Hoffmann | e9bd7e1 | 2012-06-19 09:54:50 +0200 | [diff] [blame] | 692 | cmdinfo->state = SUBMIT_STATUS_URB | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 693 | ALLOC_CMD_URB | SUBMIT_CMD_URB; |
| 694 | |
| 695 | switch (cmnd->sc_data_direction) { |
| 696 | case DMA_FROM_DEVICE: |
| 697 | cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; |
| 698 | break; |
| 699 | case DMA_BIDIRECTIONAL: |
| 700 | cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB; |
| 701 | case DMA_TO_DEVICE: |
| 702 | cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB; |
| 703 | case DMA_NONE: |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | if (!devinfo->use_streams) { |
Gerd Hoffmann | db32de1 | 2012-06-19 09:54:49 +0200 | [diff] [blame] | 708 | cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 709 | cmdinfo->stream = 0; |
| 710 | } |
| 711 | |
| 712 | err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC); |
| 713 | if (err) { |
| 714 | /* If we did nothing, give up now */ |
Matthew Wilcox | 92a3f76 | 2010-12-15 15:44:04 -0500 | [diff] [blame] | 715 | if (cmdinfo->state & SUBMIT_STATUS_URB) { |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 716 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 717 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 718 | } |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 719 | uas_add_work(cmdinfo); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 722 | list_add_tail(&cmdinfo->inflight, &devinfo->inflight_list); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 723 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | |
Jeff Garzik | f281233 | 2010-11-16 02:10:29 -0500 | [diff] [blame] | 727 | static DEF_SCSI_QCMD(uas_queuecommand) |
| 728 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 729 | static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd, |
| 730 | const char *fname, u8 function) |
| 731 | { |
| 732 | struct Scsi_Host *shost = cmnd->device->host; |
| 733 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
Hans de Goede | d3f7c15 | 2013-10-23 17:46:17 +0100 | [diff] [blame] | 734 | u16 tag = devinfo->qdepth; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 735 | unsigned long flags; |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 736 | struct urb *sense_urb; |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 737 | int result = SUCCESS; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 738 | |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 739 | spin_lock_irqsave(&devinfo->lock, flags); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 740 | |
| 741 | if (devinfo->running_task) { |
| 742 | shost_printk(KERN_INFO, shost, |
| 743 | "%s: %s: error already running a task\n", |
| 744 | __func__, fname); |
| 745 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 746 | return FAILED; |
| 747 | } |
| 748 | |
| 749 | devinfo->running_task = 1; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 750 | memset(&devinfo->response, 0, sizeof(devinfo->response)); |
Hans de Goede | f323abc | 2013-11-12 10:51:33 +0100 | [diff] [blame] | 751 | sense_urb = uas_submit_sense_urb(shost, GFP_NOIO, |
| 752 | devinfo->use_streams ? tag : 0); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 753 | if (!sense_urb) { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 754 | shost_printk(KERN_INFO, shost, |
| 755 | "%s: %s: submit sense urb failed\n", |
| 756 | __func__, fname); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 757 | devinfo->running_task = 0; |
Gerd Hoffmann | 1994ff4 | 2012-09-26 10:28:58 +0200 | [diff] [blame] | 758 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 759 | return FAILED; |
| 760 | } |
Hans de Goede | e36e649 | 2013-11-07 08:35:55 +0100 | [diff] [blame] | 761 | if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 762 | shost_printk(KERN_INFO, shost, |
| 763 | "%s: %s: submit task mgmt urb failed\n", |
| 764 | __func__, fname); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 765 | devinfo->running_task = 0; |
Gerd Hoffmann | 1994ff4 | 2012-09-26 10:28:58 +0200 | [diff] [blame] | 766 | spin_unlock_irqrestore(&devinfo->lock, flags); |
Hans de Goede | 70cf0fb | 2013-10-29 10:37:23 +0100 | [diff] [blame] | 767 | usb_kill_urb(sense_urb); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 768 | return FAILED; |
| 769 | } |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 770 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 771 | |
| 772 | if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) { |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 773 | /* |
| 774 | * Note we deliberately do not clear running_task here. If we |
| 775 | * allow new tasks to be submitted, there is no way to figure |
| 776 | * out if a received response_iu is for the failed task or for |
| 777 | * the new one. A bus-reset will eventually clear running_task. |
| 778 | */ |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 779 | shost_printk(KERN_INFO, shost, |
| 780 | "%s: %s timed out\n", __func__, fname); |
| 781 | return FAILED; |
| 782 | } |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 783 | |
| 784 | spin_lock_irqsave(&devinfo->lock, flags); |
| 785 | devinfo->running_task = 0; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 786 | if (be16_to_cpu(devinfo->response.tag) != tag) { |
| 787 | shost_printk(KERN_INFO, shost, |
| 788 | "%s: %s failed (wrong tag %d/%d)\n", __func__, |
| 789 | fname, be16_to_cpu(devinfo->response.tag), tag); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 790 | result = FAILED; |
| 791 | } else if (devinfo->response.response_code != RC_TMF_COMPLETE) { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 792 | shost_printk(KERN_INFO, shost, |
| 793 | "%s: %s failed (rc 0x%x)\n", __func__, |
| 794 | fname, devinfo->response.response_code); |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 795 | result = FAILED; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 796 | } |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 797 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 798 | |
| 799 | return result; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 802 | static int uas_eh_abort_handler(struct scsi_cmnd *cmnd) |
| 803 | { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 804 | struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp; |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 805 | struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata; |
| 806 | unsigned long flags; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 807 | int ret; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 808 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 809 | uas_log_cmd_state(cmnd, __func__); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 810 | spin_lock_irqsave(&devinfo->lock, flags); |
Gerd Hoffmann | f491ecb | 2013-09-13 13:27:14 +0200 | [diff] [blame] | 811 | WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED); |
Gerd Hoffmann | ef018cc9 | 2012-09-25 10:47:06 +0200 | [diff] [blame] | 812 | cmdinfo->state |= COMMAND_ABORTED; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 813 | cmdinfo->state &= ~IS_IN_WORK_LIST; |
| 814 | list_del(&cmdinfo->inflight); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 815 | list_add_tail(&cmdinfo->dead, &devinfo->dead_list); |
Gerd Hoffmann | 5d39040 | 2012-11-30 11:54:43 +0100 | [diff] [blame] | 816 | if (cmdinfo->state & COMMAND_INFLIGHT) { |
| 817 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 818 | ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK); |
| 819 | } else { |
Hans de Goede | 7e50e0b | 2013-10-17 19:19:04 +0200 | [diff] [blame] | 820 | uas_unlink_data_urbs(devinfo, cmdinfo, &flags); |
Gerd Hoffmann | 5d39040 | 2012-11-30 11:54:43 +0100 | [diff] [blame] | 821 | uas_try_complete(cmnd, __func__); |
| 822 | spin_unlock_irqrestore(&devinfo->lock, flags); |
| 823 | ret = SUCCESS; |
| 824 | } |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 825 | return ret; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd) |
| 829 | { |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 830 | sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__); |
| 831 | return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET", |
| 832 | TMF_LOGICAL_UNIT_RESET); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd) |
| 836 | { |
| 837 | struct scsi_device *sdev = cmnd->device; |
| 838 | struct uas_dev_info *devinfo = sdev->hostdata; |
| 839 | struct usb_device *udev = devinfo->udev; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 840 | int err; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 841 | |
Hans de Goede | be326f4 | 2013-09-22 16:27:02 +0200 | [diff] [blame] | 842 | err = usb_lock_device_for_reset(udev, devinfo->intf); |
| 843 | if (err) { |
| 844 | shost_printk(KERN_ERR, sdev->host, |
| 845 | "%s FAILED to get lock err %d\n", __func__, err); |
| 846 | return FAILED; |
| 847 | } |
| 848 | |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 849 | shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 850 | devinfo->resetting = 1; |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 851 | uas_abort_inflight(devinfo); |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 852 | usb_kill_anchored_urbs(&devinfo->cmd_urbs); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 853 | usb_kill_anchored_urbs(&devinfo->sense_urbs); |
| 854 | usb_kill_anchored_urbs(&devinfo->data_urbs); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 855 | uas_zap_dead(devinfo); |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 856 | err = usb_reset_device(udev); |
| 857 | devinfo->resetting = 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 858 | |
Hans de Goede | be326f4 | 2013-09-22 16:27:02 +0200 | [diff] [blame] | 859 | usb_unlock_device(udev); |
| 860 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 861 | if (err) { |
| 862 | shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__); |
| 863 | return FAILED; |
| 864 | } |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 865 | |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 866 | shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__); |
| 867 | return SUCCESS; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static int uas_slave_alloc(struct scsi_device *sdev) |
| 871 | { |
| 872 | sdev->hostdata = (void *)sdev->host->hostdata[0]; |
| 873 | return 0; |
| 874 | } |
| 875 | |
| 876 | static int uas_slave_configure(struct scsi_device *sdev) |
| 877 | { |
| 878 | struct uas_dev_info *devinfo = sdev->hostdata; |
| 879 | scsi_set_tag_type(sdev, MSG_ORDERED_TAG); |
Hans de Goede | d3f7c15 | 2013-10-23 17:46:17 +0100 | [diff] [blame] | 880 | scsi_activate_tcq(sdev, devinfo->qdepth - 2); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static struct scsi_host_template uas_host_template = { |
| 885 | .module = THIS_MODULE, |
| 886 | .name = "uas", |
| 887 | .queuecommand = uas_queuecommand, |
| 888 | .slave_alloc = uas_slave_alloc, |
| 889 | .slave_configure = uas_slave_configure, |
| 890 | .eh_abort_handler = uas_eh_abort_handler, |
| 891 | .eh_device_reset_handler = uas_eh_device_reset_handler, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 892 | .eh_bus_reset_handler = uas_eh_bus_reset_handler, |
| 893 | .can_queue = 65536, /* Is there a limit on the _host_ ? */ |
| 894 | .this_id = -1, |
| 895 | .sg_tablesize = SG_NONE, |
| 896 | .cmd_per_lun = 1, /* until we override it */ |
| 897 | .skip_settle_delay = 1, |
| 898 | .ordered_tag = 1, |
| 899 | }; |
| 900 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 901 | #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ |
| 902 | vendorName, productName, useProtocol, useTransport, \ |
| 903 | initFunction, flags) \ |
| 904 | { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ |
| 905 | .driver_info = (flags) } |
| 906 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 907 | static struct usb_device_id uas_usb_ids[] = { |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 908 | # include "unusual_uas.h" |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 909 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, |
| 910 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) }, |
| 911 | /* 0xaa is a prototype device I happen to have access to */ |
| 912 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) }, |
| 913 | { } |
| 914 | }; |
| 915 | MODULE_DEVICE_TABLE(usb, uas_usb_ids); |
| 916 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 917 | #undef UNUSUAL_DEV |
| 918 | |
Hans de Goede | e1be067 | 2013-10-21 08:00:58 +0100 | [diff] [blame] | 919 | static int uas_switch_interface(struct usb_device *udev, |
| 920 | struct usb_interface *intf) |
| 921 | { |
| 922 | int alt; |
| 923 | |
| 924 | alt = uas_find_uas_alt_setting(intf); |
| 925 | if (alt < 0) |
| 926 | return alt; |
| 927 | |
| 928 | return usb_set_interface(udev, |
| 929 | intf->altsetting[0].desc.bInterfaceNumber, alt); |
| 930 | } |
| 931 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 932 | static int uas_configure_endpoints(struct uas_dev_info *devinfo) |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 933 | { |
| 934 | struct usb_host_endpoint *eps[4] = { }; |
| 935 | struct usb_device *udev = devinfo->udev; |
| 936 | int r; |
| 937 | |
| 938 | devinfo->uas_sense_old = 0; |
| 939 | devinfo->cmnd = NULL; |
| 940 | |
| 941 | r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps); |
Hans de Goede | 74d71ae | 2013-10-29 10:10:36 +0100 | [diff] [blame] | 942 | if (r) |
| 943 | return r; |
Hans de Goede | 34f11e5 | 2013-10-29 08:54:48 +0100 | [diff] [blame] | 944 | |
Hans de Goede | 74d71ae | 2013-10-29 10:10:36 +0100 | [diff] [blame] | 945 | devinfo->cmd_pipe = usb_sndbulkpipe(udev, |
| 946 | usb_endpoint_num(&eps[0]->desc)); |
| 947 | devinfo->status_pipe = usb_rcvbulkpipe(udev, |
| 948 | usb_endpoint_num(&eps[1]->desc)); |
| 949 | devinfo->data_in_pipe = usb_rcvbulkpipe(udev, |
| 950 | usb_endpoint_num(&eps[2]->desc)); |
| 951 | devinfo->data_out_pipe = usb_sndbulkpipe(udev, |
| 952 | usb_endpoint_num(&eps[3]->desc)); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 953 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 954 | if (udev->speed != USB_SPEED_SUPER) { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 955 | devinfo->qdepth = 256; |
| 956 | devinfo->use_streams = 0; |
| 957 | } else { |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 958 | devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, |
| 959 | 3, 256, GFP_KERNEL); |
| 960 | if (devinfo->qdepth < 0) |
| 961 | return devinfo->qdepth; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 962 | devinfo->use_streams = 1; |
| 963 | } |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 964 | |
| 965 | return 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 968 | static void uas_free_streams(struct uas_dev_info *devinfo) |
| 969 | { |
| 970 | struct usb_device *udev = devinfo->udev; |
| 971 | struct usb_host_endpoint *eps[3]; |
| 972 | |
| 973 | eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe); |
| 974 | eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe); |
| 975 | eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe); |
| 976 | usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL); |
| 977 | } |
| 978 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 979 | /* |
| 980 | * XXX: What I'd like to do here is register a SCSI host for each USB host in |
| 981 | * the system. Follow usb-storage's design of registering a SCSI host for |
| 982 | * each USB device for the moment. Can implement this by walking up the |
| 983 | * USB hierarchy until we find a USB host. |
| 984 | */ |
| 985 | static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 986 | { |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 987 | int result = -ENOMEM; |
| 988 | struct Scsi_Host *shost = NULL; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 989 | struct uas_dev_info *devinfo; |
| 990 | struct usb_device *udev = interface_to_usbdev(intf); |
| 991 | |
Hans de Goede | 79b4c06 | 2013-10-25 17:04:33 +0100 | [diff] [blame] | 992 | if (!uas_use_uas_driver(intf, id)) |
| 993 | return -ENODEV; |
| 994 | |
Matthew Wilcox | 89dc290 | 2010-12-15 15:44:05 -0500 | [diff] [blame] | 995 | if (uas_switch_interface(udev, intf)) |
| 996 | return -ENODEV; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 997 | |
| 998 | devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL); |
| 999 | if (!devinfo) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1000 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1001 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1002 | shost = scsi_host_alloc(&uas_host_template, sizeof(void *)); |
| 1003 | if (!shost) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1004 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1005 | |
| 1006 | shost->max_cmd_len = 16 + 252; |
| 1007 | shost->max_id = 1; |
Gerd Hoffmann | bde027b | 2013-01-25 15:03:36 +0100 | [diff] [blame] | 1008 | shost->max_lun = 256; |
| 1009 | shost->max_channel = 0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1010 | shost->sg_tablesize = udev->bus->sg_tablesize; |
| 1011 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1012 | devinfo->intf = intf; |
| 1013 | devinfo->udev = udev; |
Gerd Hoffmann | 023b515 | 2012-06-19 09:54:54 +0200 | [diff] [blame] | 1014 | devinfo->resetting = 0; |
Hans de Goede | b83b86a | 2013-10-29 10:51:00 +0100 | [diff] [blame] | 1015 | devinfo->running_task = 0; |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1016 | devinfo->shutdown = 0; |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 1017 | init_usb_anchor(&devinfo->cmd_urbs); |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 1018 | init_usb_anchor(&devinfo->sense_urbs); |
| 1019 | init_usb_anchor(&devinfo->data_urbs); |
Gerd Hoffmann | e064852 | 2012-09-25 10:47:08 +0200 | [diff] [blame] | 1020 | spin_lock_init(&devinfo->lock); |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 1021 | INIT_WORK(&devinfo->work, uas_do_work); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 1022 | INIT_LIST_HEAD(&devinfo->inflight_list); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 1023 | INIT_LIST_HEAD(&devinfo->dead_list); |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 1024 | |
| 1025 | result = uas_configure_endpoints(devinfo); |
| 1026 | if (result) |
| 1027 | goto set_alt0; |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1028 | |
Hans de Goede | d3f7c15 | 2013-10-23 17:46:17 +0100 | [diff] [blame] | 1029 | result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1030 | if (result) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1031 | goto free_streams; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1032 | |
| 1033 | result = scsi_add_host(shost, &intf->dev); |
| 1034 | if (result) |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1035 | goto free_streams; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1036 | |
| 1037 | shost->hostdata[0] = (unsigned long)devinfo; |
| 1038 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1039 | scsi_scan_host(shost); |
| 1040 | usb_set_intfdata(intf, shost); |
| 1041 | return result; |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1042 | |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1043 | free_streams: |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1044 | uas_free_streams(devinfo); |
Hans de Goede | 6ce8213 | 2013-10-17 19:00:45 +0200 | [diff] [blame] | 1045 | set_alt0: |
| 1046 | usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1047 | kfree(devinfo); |
| 1048 | if (shost) |
| 1049 | scsi_host_put(shost); |
| 1050 | return result; |
| 1051 | } |
| 1052 | |
| 1053 | static int uas_pre_reset(struct usb_interface *intf) |
| 1054 | { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1055 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1056 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1057 | unsigned long flags; |
| 1058 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1059 | if (devinfo->shutdown) |
| 1060 | return 0; |
| 1061 | |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1062 | /* Block new requests */ |
| 1063 | spin_lock_irqsave(shost->host_lock, flags); |
| 1064 | scsi_block_requests(shost); |
| 1065 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1066 | |
| 1067 | /* Wait for any pending requests to complete */ |
| 1068 | flush_work(&devinfo->work); |
| 1069 | if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) { |
| 1070 | shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); |
| 1071 | return 1; |
| 1072 | } |
| 1073 | |
| 1074 | uas_free_streams(devinfo); |
| 1075 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static int uas_post_reset(struct usb_interface *intf) |
| 1080 | { |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1081 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1082 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1083 | unsigned long flags; |
| 1084 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1085 | if (devinfo->shutdown) |
| 1086 | return 0; |
| 1087 | |
Hans de Goede | 58d5144 | 2013-10-29 10:23:26 +0100 | [diff] [blame] | 1088 | if (uas_configure_endpoints(devinfo) != 0) { |
| 1089 | shost_printk(KERN_ERR, shost, |
| 1090 | "%s: alloc streams error after reset", __func__); |
| 1091 | return 1; |
| 1092 | } |
Hans de Goede | 4de7a373 | 2013-10-22 16:10:44 +0100 | [diff] [blame] | 1093 | |
| 1094 | spin_lock_irqsave(shost->host_lock, flags); |
| 1095 | scsi_report_bus_reset(shost, 0); |
| 1096 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1097 | |
| 1098 | scsi_unblock_requests(shost); |
| 1099 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1103 | static int uas_suspend(struct usb_interface *intf, pm_message_t message) |
| 1104 | { |
| 1105 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1106 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1107 | |
| 1108 | /* Wait for any pending requests to complete */ |
| 1109 | flush_work(&devinfo->work); |
| 1110 | if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) { |
| 1111 | shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__); |
| 1112 | return -ETIME; |
| 1113 | } |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static int uas_resume(struct usb_interface *intf) |
| 1119 | { |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | static int uas_reset_resume(struct usb_interface *intf) |
| 1124 | { |
| 1125 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1126 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1127 | unsigned long flags; |
| 1128 | |
| 1129 | if (uas_configure_endpoints(devinfo) != 0) { |
| 1130 | shost_printk(KERN_ERR, shost, |
| 1131 | "%s: alloc streams error after reset", __func__); |
| 1132 | return -EIO; |
| 1133 | } |
| 1134 | |
| 1135 | spin_lock_irqsave(shost->host_lock, flags); |
| 1136 | scsi_report_bus_reset(shost, 0); |
| 1137 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1142 | static void uas_disconnect(struct usb_interface *intf) |
| 1143 | { |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1144 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1145 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1146 | |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 1147 | devinfo->resetting = 1; |
Gerd Hoffmann | 1bf8198 | 2013-09-13 13:27:12 +0200 | [diff] [blame] | 1148 | cancel_work_sync(&devinfo->work); |
Hans de Goede | 61c09ce | 2013-11-12 13:44:20 +0100 | [diff] [blame^] | 1149 | uas_abort_inflight(devinfo); |
Gerd Hoffmann | a0e39e3 | 2012-09-25 10:47:04 +0200 | [diff] [blame] | 1150 | usb_kill_anchored_urbs(&devinfo->cmd_urbs); |
Gerd Hoffmann | bdd000f | 2012-06-19 09:54:53 +0200 | [diff] [blame] | 1151 | usb_kill_anchored_urbs(&devinfo->sense_urbs); |
| 1152 | usb_kill_anchored_urbs(&devinfo->data_urbs); |
Gerd Hoffmann | 326349f | 2013-09-13 13:27:13 +0200 | [diff] [blame] | 1153 | uas_zap_dead(devinfo); |
Gerd Hoffmann | 4c45697 | 2012-11-30 11:54:44 +0100 | [diff] [blame] | 1154 | scsi_remove_host(shost); |
Sebastian Andrzej Siewior | dae5154 | 2011-12-19 17:06:08 +0100 | [diff] [blame] | 1155 | uas_free_streams(devinfo); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1156 | kfree(devinfo); |
| 1157 | } |
| 1158 | |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1159 | /* |
| 1160 | * Put the device back in usb-storage mode on shutdown, as some BIOS-es |
| 1161 | * hang on reboot when the device is still in uas mode. Note the reset is |
| 1162 | * necessary as some devices won't revert to usb-storage mode without it. |
| 1163 | */ |
| 1164 | static void uas_shutdown(struct device *dev) |
| 1165 | { |
| 1166 | struct usb_interface *intf = to_usb_interface(dev); |
| 1167 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1168 | struct Scsi_Host *shost = usb_get_intfdata(intf); |
| 1169 | struct uas_dev_info *devinfo = (void *)shost->hostdata[0]; |
| 1170 | |
| 1171 | if (system_state != SYSTEM_RESTART) |
| 1172 | return; |
| 1173 | |
| 1174 | devinfo->shutdown = 1; |
| 1175 | uas_free_streams(devinfo); |
| 1176 | usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0); |
| 1177 | usb_reset_device(udev); |
| 1178 | } |
| 1179 | |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1180 | static struct usb_driver uas_driver = { |
| 1181 | .name = "uas", |
| 1182 | .probe = uas_probe, |
| 1183 | .disconnect = uas_disconnect, |
| 1184 | .pre_reset = uas_pre_reset, |
| 1185 | .post_reset = uas_post_reset, |
Hans de Goede | 0df1f66 | 2013-11-07 08:47:05 +0100 | [diff] [blame] | 1186 | .suspend = uas_suspend, |
| 1187 | .resume = uas_resume, |
| 1188 | .reset_resume = uas_reset_resume, |
Hans de Goede | da65c2b | 2013-11-11 11:51:42 +0100 | [diff] [blame] | 1189 | .drvwrap.driver.shutdown = uas_shutdown, |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1190 | .id_table = uas_usb_ids, |
| 1191 | }; |
| 1192 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1193 | module_usb_driver(uas_driver); |
Matthew Wilcox | 115bb1f | 2010-10-07 13:05:23 +0200 | [diff] [blame] | 1194 | |
| 1195 | MODULE_LICENSE("GPL"); |
| 1196 | MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp"); |