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