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