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