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