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