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