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