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