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