blob: 3f021f2fafdf4e3c2dc586995273d6e257e07b5f [file] [log] [blame]
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001/*
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 Gortmaker6eb0de82011-07-03 16:09:31 -040014#include <linux/module.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020015#include <linux/usb.h>
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +010016#include <linux/usb/hcd.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020017#include <linux/usb/storage.h>
Sebastian Andrzej Siewior348748b2012-01-11 12:45:56 +010018#include <linux/usb/uas.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020019
20#include <scsi/scsi.h>
Hans de Goede4de7a3732013-10-22 16:10:44 +010021#include <scsi/scsi_eh.h>
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020022#include <scsi/scsi_dbg.h>
23#include <scsi/scsi_cmnd.h>
24#include <scsi/scsi_device.h>
25#include <scsi/scsi_host.h>
26#include <scsi/scsi_tcq.h>
27
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020028/*
29 * The r00-r01c specs define this version of the SENSE IU data structure.
30 * It's still in use by several different firmware releases.
31 */
32struct sense_iu_old {
33 __u8 iu_id;
34 __u8 rsvd1;
35 __be16 tag;
36 __be16 len;
37 __u8 status;
38 __u8 service_response;
39 __u8 sense[SCSI_SENSE_BUFFERSIZE];
40};
41
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020042struct uas_dev_info {
43 struct usb_interface *intf;
44 struct usb_device *udev;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +020045 struct usb_anchor cmd_urbs;
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +020046 struct usb_anchor sense_urbs;
47 struct usb_anchor data_urbs;
Gerd Hoffmann023b5152012-06-19 09:54:54 +020048 int qdepth, resetting;
Hans de Goedee52e0312013-10-23 14:27:09 +010049 struct response_iu response;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020050 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
51 unsigned use_streams:1;
52 unsigned uas_sense_old:1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +010053 struct scsi_cmnd *cmnd;
Gerd Hoffmanne0648522012-09-25 10:47:08 +020054 spinlock_t lock;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020055 struct work_struct work;
56 struct list_head work_list;
Gerd Hoffmann326349f2013-09-13 13:27:13 +020057 struct list_head dead_list;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020058};
59
60enum {
Matthew Wilcox92a3f762010-12-15 15:44:04 -050061 SUBMIT_STATUS_URB = (1 << 1),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020062 ALLOC_DATA_IN_URB = (1 << 2),
63 SUBMIT_DATA_IN_URB = (1 << 3),
64 ALLOC_DATA_OUT_URB = (1 << 4),
65 SUBMIT_DATA_OUT_URB = (1 << 5),
66 ALLOC_CMD_URB = (1 << 6),
67 SUBMIT_CMD_URB = (1 << 7),
Gerd Hoffmannb1d67692012-06-19 09:54:51 +020068 COMMAND_INFLIGHT = (1 << 8),
69 DATA_IN_URB_INFLIGHT = (1 << 9),
70 DATA_OUT_URB_INFLIGHT = (1 << 10),
71 COMMAND_COMPLETED = (1 << 11),
Gerd Hoffmannef018cc92012-09-25 10:47:06 +020072 COMMAND_ABORTED = (1 << 12),
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +010073 UNLINK_DATA_URBS = (1 << 13),
Gerd Hoffmannefefecf2012-11-30 11:54:42 +010074 IS_IN_WORK_LIST = (1 << 14),
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020075};
76
77/* Overrides scsi_pointer */
78struct uas_cmd_info {
79 unsigned int state;
80 unsigned int stream;
81 struct urb *cmd_urb;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020082 struct urb *data_in_urb;
83 struct urb *data_out_urb;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +020084 struct list_head work;
Gerd Hoffmann326349f2013-09-13 13:27:13 +020085 struct list_head dead;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020086};
87
88/* I hate forward declarations, but I actually have a loop */
89static int uas_submit_urbs(struct scsi_cmnd *cmnd,
90 struct uas_dev_info *devinfo, gfp_t gfp);
Sarah Sharpea9da1c2011-12-02 11:55:44 -080091static void uas_do_work(struct work_struct *work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +010092static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
Gerd Hoffmannd89bd832013-09-13 13:27:11 +020093static void uas_configure_endpoints(struct uas_dev_info *devinfo);
94static void uas_free_streams(struct uas_dev_info *devinfo);
Gerd Hoffmann326349f2013-09-13 13:27:13 +020095static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +020096
Hans de Goede7e50e0b2013-10-17 19:19:04 +020097/* Must be called with devinfo->lock held, will temporary unlock the lock */
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +010098static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
Hans de Goede7e50e0b2013-10-17 19:19:04 +020099 struct uas_cmd_info *cmdinfo,
100 unsigned long *lock_flags)
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100101{
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100102 /*
103 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
104 * (called by urb completion) doesn't release cmdinfo
105 * underneath us.
106 */
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100107 cmdinfo->state |= UNLINK_DATA_URBS;
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200108 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100109
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100110 if (cmdinfo->data_in_urb)
111 usb_unlink_urb(cmdinfo->data_in_urb);
112 if (cmdinfo->data_out_urb)
113 usb_unlink_urb(cmdinfo->data_out_urb);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100114
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200115 spin_lock_irqsave(&devinfo->lock, *lock_flags);
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100116 cmdinfo->state &= ~UNLINK_DATA_URBS;
Gerd Hoffmannaa8f6122012-11-30 11:54:40 +0100117}
118
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200119static void uas_do_work(struct work_struct *work)
120{
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200121 struct uas_dev_info *devinfo =
122 container_of(work, struct uas_dev_info, work);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200123 struct uas_cmd_info *cmdinfo;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800124 struct uas_cmd_info *temp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200125 unsigned long flags;
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800126 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200127
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200128 spin_lock_irqsave(&devinfo->lock, flags);
129 list_for_each_entry_safe(cmdinfo, temp, &devinfo->work_list, work) {
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200130 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200131 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
132 SCp);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200133 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200134 if (!err) {
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100135 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200136 list_del(&cmdinfo->work);
137 } else {
138 schedule_work(&devinfo->work);
Sarah Sharpea9da1c2011-12-02 11:55:44 -0800139 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200140 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200141 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200142}
143
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100144static void uas_abort_work(struct uas_dev_info *devinfo)
145{
146 struct uas_cmd_info *cmdinfo;
147 struct uas_cmd_info *temp;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100148 unsigned long flags;
149
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100150 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200151 list_for_each_entry_safe(cmdinfo, temp, &devinfo->work_list, work) {
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100152 struct scsi_pointer *scp = (void *)cmdinfo;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200153 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
154 SCp);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200155 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200156 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200157 cmdinfo->state |= COMMAND_ABORTED;
158 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200159 list_del(&cmdinfo->work);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200160 list_add_tail(&cmdinfo->dead, &devinfo->dead_list);
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100161 }
162 spin_unlock_irqrestore(&devinfo->lock, flags);
163}
164
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200165static void uas_add_work(struct uas_cmd_info *cmdinfo)
166{
167 struct scsi_pointer *scp = (void *)cmdinfo;
168 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
169 struct uas_dev_info *devinfo = cmnd->device->hostdata;
170
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200171 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200172 list_add_tail(&cmdinfo->work, &devinfo->work_list);
173 cmdinfo->state |= IS_IN_WORK_LIST;
174 schedule_work(&devinfo->work);
175}
176
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200177static void uas_zap_dead(struct uas_dev_info *devinfo)
178{
179 struct uas_cmd_info *cmdinfo;
180 struct uas_cmd_info *temp;
181 unsigned long flags;
182
183 spin_lock_irqsave(&devinfo->lock, flags);
184 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, dead) {
185 struct scsi_pointer *scp = (void *)cmdinfo;
186 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
187 SCp);
188 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200189 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200190 /* all urbs are killed, clear inflight bits */
191 cmdinfo->state &= ~(COMMAND_INFLIGHT |
192 DATA_IN_URB_INFLIGHT |
193 DATA_OUT_URB_INFLIGHT);
194 uas_try_complete(cmnd, __func__);
195 }
196 spin_unlock_irqrestore(&devinfo->lock, flags);
197}
198
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200199static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
200{
201 struct sense_iu *sense_iu = urb->transfer_buffer;
202 struct scsi_device *sdev = cmnd->device;
203
204 if (urb->actual_length > 16) {
205 unsigned len = be16_to_cpup(&sense_iu->len);
206 if (len + 16 != urb->actual_length) {
207 int newlen = min(len + 16, urb->actual_length) - 16;
208 if (newlen < 0)
209 newlen = 0;
210 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
211 "disagrees with IU sense data length %d, "
212 "using %d bytes of sense data\n", __func__,
213 urb->actual_length, len, newlen);
214 len = newlen;
215 }
216 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
217 }
218
219 cmnd->result = sense_iu->status;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200220}
221
222static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
223{
224 struct sense_iu_old *sense_iu = urb->transfer_buffer;
225 struct scsi_device *sdev = cmnd->device;
226
227 if (urb->actual_length > 8) {
228 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
229 if (len + 8 != urb->actual_length) {
230 int newlen = min(len + 8, urb->actual_length) - 8;
231 if (newlen < 0)
232 newlen = 0;
233 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
234 "disagrees with IU sense data length %d, "
235 "using %d bytes of sense data\n", __func__,
236 urb->actual_length, len, newlen);
237 len = newlen;
238 }
239 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
240 }
241
242 cmnd->result = sense_iu->status;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200243}
244
245static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
246{
247 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
248
249 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100250 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200251 caller, cmnd, cmnd->request->tag,
252 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
253 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
254 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
255 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
256 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
257 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
258 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
259 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
260 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
261 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200262 (ci->state & COMMAND_COMPLETED) ? " done" : "",
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100263 (ci->state & COMMAND_ABORTED) ? " abort" : "",
Gerd Hoffmannefefecf2012-11-30 11:54:42 +0100264 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
265 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200266}
267
268static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
269{
270 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200271 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200272
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200273 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200274 if (cmdinfo->state & (COMMAND_INFLIGHT |
275 DATA_IN_URB_INFLIGHT |
Gerd Hoffmannb06e48a2012-11-30 11:54:41 +0100276 DATA_OUT_URB_INFLIGHT |
277 UNLINK_DATA_URBS))
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200278 return -EBUSY;
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200279 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200280 cmdinfo->state |= COMMAND_COMPLETED;
281 usb_free_urb(cmdinfo->data_in_urb);
282 usb_free_urb(cmdinfo->data_out_urb);
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200283 if (cmdinfo->state & COMMAND_ABORTED) {
284 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
285 cmnd->result = DID_ABORT << 16;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200286 list_del(&cmdinfo->dead);
Gerd Hoffmann0871d7d2012-09-25 10:47:07 +0200287 }
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200288 cmnd->scsi_done(cmnd);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200289 return 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200290}
291
292static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200293 unsigned direction)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200294{
295 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
296 int err;
297
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200298 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200299 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
300 if (err) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200301 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200302 }
303}
304
305static void uas_stat_cmplt(struct urb *urb)
306{
307 struct iu *iu = urb->transfer_buffer;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100308 struct Scsi_Host *shost = urb->context;
309 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200310 struct scsi_cmnd *cmnd;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200311 struct uas_cmd_info *cmdinfo;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200312 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200313 u16 tag;
314
315 if (urb->status) {
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200316 if (urb->status == -ENOENT) {
317 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
318 urb->stream_id);
319 } else {
320 dev_err(&urb->dev->dev, "stat urb: status %d\n",
321 urb->status);
322 }
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200323 usb_free_urb(urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200324 return;
325 }
326
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200327 if (devinfo->resetting) {
328 usb_free_urb(urb);
329 return;
330 }
331
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200332 spin_lock_irqsave(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200333 tag = be16_to_cpup(&iu->tag) - 1;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100334 if (tag == 0)
335 cmnd = devinfo->cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200336 else
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100337 cmnd = scsi_host_find_tag(shost, tag - 1);
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200338
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800339 if (!cmnd) {
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200340 if (iu->iu_id == IU_ID_RESPONSE) {
341 /* store results for uas_eh_task_mgmt() */
342 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200343 }
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200344 usb_free_urb(urb);
345 spin_unlock_irqrestore(&devinfo->lock, flags);
346 return;
Sarah Sharp96c1eb92011-12-02 11:55:48 -0800347 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200348
Gerd Hoffmanne0423de2012-09-26 10:29:03 +0200349 cmdinfo = (void *)&cmnd->SCp;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200350 switch (iu->iu_id) {
351 case IU_ID_STATUS:
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100352 if (devinfo->cmnd == cmnd)
353 devinfo->cmnd = NULL;
354
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200355 if (urb->actual_length < 16)
356 devinfo->uas_sense_old = 1;
357 if (devinfo->uas_sense_old)
358 uas_sense_old(urb, cmnd);
359 else
360 uas_sense(urb, cmnd);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200361 if (cmnd->result != 0) {
362 /* cancel data transfers on error */
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200363 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200364 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200365 cmdinfo->state &= ~COMMAND_INFLIGHT;
366 uas_try_complete(cmnd, __func__);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200367 break;
368 case IU_ID_READ_READY:
369 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
370 break;
371 case IU_ID_WRITE_READY:
372 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
373 break;
374 default:
375 scmd_printk(KERN_ERR, cmnd,
376 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
377 }
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200378 usb_free_urb(urb);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200379 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200380}
381
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200382static void uas_data_cmplt(struct urb *urb)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200383{
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200384 struct scsi_cmnd *cmnd = urb->context;
385 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200386 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200387 struct scsi_data_buffer *sdb = NULL;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200388 unsigned long flags;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200389
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200390 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200391 if (cmdinfo->data_in_urb == urb) {
392 sdb = scsi_in(cmnd);
393 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
394 } else if (cmdinfo->data_out_urb == urb) {
395 sdb = scsi_out(cmnd);
396 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
397 }
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200398 if (sdb == NULL) {
399 WARN_ON_ONCE(1);
400 } else if (urb->status) {
Gerd Hoffmann8aac8632012-06-19 09:54:52 +0200401 /* error: no data transfered */
402 sdb->resid = sdb->length;
403 } else {
404 sdb->resid = sdb->length - urb->actual_length;
405 }
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200406 uas_try_complete(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200407 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200408}
409
410static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200411 unsigned int pipe, u16 stream_id,
412 struct scsi_cmnd *cmnd,
413 enum dma_data_direction dir)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200414{
415 struct usb_device *udev = devinfo->udev;
416 struct urb *urb = usb_alloc_urb(0, gfp);
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200417 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
418 ? scsi_in(cmnd) : scsi_out(cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200419
420 if (!urb)
421 goto out;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200422 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
423 uas_data_cmplt, cmnd);
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200424 if (devinfo->use_streams)
425 urb->stream_id = stream_id;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200426 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
427 urb->sg = sdb->table.sgl;
428 out:
429 return urb;
430}
431
432static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200433 struct Scsi_Host *shost, u16 stream_id)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200434{
435 struct usb_device *udev = devinfo->udev;
436 struct urb *urb = usb_alloc_urb(0, gfp);
437 struct sense_iu *iu;
438
439 if (!urb)
440 goto out;
441
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500442 iu = kzalloc(sizeof(*iu), gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200443 if (!iu)
444 goto free;
445
446 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200447 uas_stat_cmplt, shost);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200448 urb->stream_id = stream_id;
449 urb->transfer_flags |= URB_FREE_BUFFER;
450 out:
451 return urb;
452 free:
453 usb_free_urb(urb);
454 return NULL;
455}
456
457static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
Hans de Goedea887cd32013-10-17 19:47:28 +0200458 struct scsi_cmnd *cmnd)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200459{
460 struct usb_device *udev = devinfo->udev;
461 struct scsi_device *sdev = cmnd->device;
462 struct urb *urb = usb_alloc_urb(0, gfp);
463 struct command_iu *iu;
464 int len;
465
466 if (!urb)
467 goto out;
468
469 len = cmnd->cmd_len - 16;
470 if (len < 0)
471 len = 0;
472 len = ALIGN(len, 4);
Matthew Wilcoxac563cf2010-12-15 15:44:03 -0500473 iu = kzalloc(sizeof(*iu) + len, gfp);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200474 if (!iu)
475 goto free;
476
477 iu->iu_id = IU_ID_COMMAND;
Sarah Sharp9eb44542011-12-02 11:55:46 -0800478 if (blk_rq_tagged(cmnd->request))
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100479 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
Sarah Sharp9eb44542011-12-02 11:55:46 -0800480 else
481 iu->tag = cpu_to_be16(1);
Christoph Hellwig02e031c2010-11-10 14:54:09 +0100482 iu->prio_attr = UAS_SIMPLE_TAG;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200483 iu->len = len;
484 int_to_scsilun(sdev->lun, &iu->lun);
485 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
486
487 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
488 usb_free_urb, NULL);
489 urb->transfer_flags |= URB_FREE_BUFFER;
490 out:
491 return urb;
492 free:
493 usb_free_urb(urb);
494 return NULL;
495}
496
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200497static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
498 u8 function, u16 stream_id)
499{
500 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
501 struct usb_device *udev = devinfo->udev;
502 struct urb *urb = usb_alloc_urb(0, gfp);
503 struct task_mgmt_iu *iu;
504 int err = -ENOMEM;
505
506 if (!urb)
507 goto err;
508
509 iu = kzalloc(sizeof(*iu), gfp);
510 if (!iu)
511 goto err;
512
513 iu->iu_id = IU_ID_TASK_MGMT;
514 iu->tag = cpu_to_be16(stream_id);
515 int_to_scsilun(cmnd->device->lun, &iu->lun);
516
517 iu->function = function;
518 switch (function) {
519 case TMF_ABORT_TASK:
520 if (blk_rq_tagged(cmnd->request))
521 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
522 else
523 iu->task_tag = cpu_to_be16(1);
524 break;
525 }
526
527 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
528 usb_free_urb, NULL);
529 urb->transfer_flags |= URB_FREE_BUFFER;
530
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200531 usb_anchor_urb(urb, &devinfo->cmd_urbs);
Hans de Goeded5f808d2013-10-17 19:30:26 +0200532 err = usb_submit_urb(urb, gfp);
533 if (err) {
534 usb_unanchor_urb(urb);
535 goto err;
536 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200537
538 return 0;
539
540err:
541 usb_free_urb(urb);
542 return err;
543}
544
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200545/*
546 * Why should I request the Status IU before sending the Command IU? Spec
547 * says to, but also says the device may receive them in any order. Seems
548 * daft to me.
549 */
550
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200551static int uas_submit_sense_urb(struct Scsi_Host *shost,
552 gfp_t gfp, unsigned int stream)
553{
554 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
555 struct urb *urb;
556
557 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
558 if (!urb)
559 return SCSI_MLQUEUE_DEVICE_BUSY;
Hans de Goeded5f808d2013-10-17 19:30:26 +0200560 usb_anchor_urb(urb, &devinfo->sense_urbs);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200561 if (usb_submit_urb(urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200562 usb_unanchor_urb(urb);
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200563 shost_printk(KERN_INFO, shost,
564 "sense urb submission failure\n");
565 usb_free_urb(urb);
566 return SCSI_MLQUEUE_DEVICE_BUSY;
567 }
568 return 0;
569}
570
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200571static int uas_submit_urbs(struct scsi_cmnd *cmnd,
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200572 struct uas_dev_info *devinfo, gfp_t gfp)
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200573{
574 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200575 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200576
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200577 WARN_ON_ONCE(!spin_is_locked(&devinfo->lock));
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500578 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200579 err = uas_submit_sense_urb(cmnd->device->host, gfp,
580 cmdinfo->stream);
581 if (err) {
582 return err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200583 }
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500584 cmdinfo->state &= ~SUBMIT_STATUS_URB;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200585 }
586
587 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
588 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200589 devinfo->data_in_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200590 cmnd, DMA_FROM_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200591 if (!cmdinfo->data_in_urb)
592 return SCSI_MLQUEUE_DEVICE_BUSY;
593 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
594 }
595
596 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200597 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200598 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200599 usb_unanchor_urb(cmdinfo->data_in_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200600 scmd_printk(KERN_INFO, cmnd,
601 "data in urb submission failure\n");
602 return SCSI_MLQUEUE_DEVICE_BUSY;
603 }
604 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200605 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200606 }
607
608 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
609 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
Gerd Hoffmannc621a812012-06-19 09:54:48 +0200610 devinfo->data_out_pipe, cmdinfo->stream,
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200611 cmnd, DMA_TO_DEVICE);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200612 if (!cmdinfo->data_out_urb)
613 return SCSI_MLQUEUE_DEVICE_BUSY;
614 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
615 }
616
617 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200618 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200619 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200620 usb_unanchor_urb(cmdinfo->data_out_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200621 scmd_printk(KERN_INFO, cmnd,
622 "data out urb submission failure\n");
623 return SCSI_MLQUEUE_DEVICE_BUSY;
624 }
625 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200626 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200627 }
628
629 if (cmdinfo->state & ALLOC_CMD_URB) {
Hans de Goedea887cd32013-10-17 19:47:28 +0200630 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200631 if (!cmdinfo->cmd_urb)
632 return SCSI_MLQUEUE_DEVICE_BUSY;
633 cmdinfo->state &= ~ALLOC_CMD_URB;
634 }
635
636 if (cmdinfo->state & SUBMIT_CMD_URB) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200637 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200638 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
Hans de Goeded5f808d2013-10-17 19:30:26 +0200639 usb_unanchor_urb(cmdinfo->cmd_urb);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200640 scmd_printk(KERN_INFO, cmnd,
641 "cmd urb submission failure\n");
642 return SCSI_MLQUEUE_DEVICE_BUSY;
643 }
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200644 cmdinfo->cmd_urb = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200645 cmdinfo->state &= ~SUBMIT_CMD_URB;
Gerd Hoffmannb1d67692012-06-19 09:54:51 +0200646 cmdinfo->state |= COMMAND_INFLIGHT;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200647 }
648
649 return 0;
650}
651
Jeff Garzikf2812332010-11-16 02:10:29 -0500652static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200653 void (*done)(struct scsi_cmnd *))
654{
655 struct scsi_device *sdev = cmnd->device;
656 struct uas_dev_info *devinfo = sdev->hostdata;
657 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200658 unsigned long flags;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200659 int err;
660
661 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
662
Gerd Hoffmannf8be6bf2012-11-30 11:54:45 +0100663 if (devinfo->resetting) {
664 cmnd->result = DID_ERROR << 16;
665 cmnd->scsi_done(cmnd);
666 return 0;
667 }
668
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200669 spin_lock_irqsave(&devinfo->lock, flags);
670 if (devinfo->cmnd) {
671 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200672 return SCSI_MLQUEUE_DEVICE_BUSY;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200673 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200674
675 if (blk_rq_tagged(cmnd->request)) {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100676 cmdinfo->stream = cmnd->request->tag + 2;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200677 } else {
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100678 devinfo->cmnd = cmnd;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200679 cmdinfo->stream = 1;
680 }
681
682 cmnd->scsi_done = done;
683
Gerd Hoffmanne9bd7e12012-06-19 09:54:50 +0200684 cmdinfo->state = SUBMIT_STATUS_URB |
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200685 ALLOC_CMD_URB | SUBMIT_CMD_URB;
686
687 switch (cmnd->sc_data_direction) {
688 case DMA_FROM_DEVICE:
689 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
690 break;
691 case DMA_BIDIRECTIONAL:
692 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
693 case DMA_TO_DEVICE:
694 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
695 case DMA_NONE:
696 break;
697 }
698
699 if (!devinfo->use_streams) {
Gerd Hoffmanndb32de12012-06-19 09:54:49 +0200700 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200701 cmdinfo->stream = 0;
702 }
703
704 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
705 if (err) {
706 /* If we did nothing, give up now */
Matthew Wilcox92a3f762010-12-15 15:44:04 -0500707 if (cmdinfo->state & SUBMIT_STATUS_URB) {
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200708 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200709 return SCSI_MLQUEUE_DEVICE_BUSY;
710 }
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200711 uas_add_work(cmdinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200712 }
713
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200714 spin_unlock_irqrestore(&devinfo->lock, flags);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200715 return 0;
716}
717
Jeff Garzikf2812332010-11-16 02:10:29 -0500718static DEF_SCSI_QCMD(uas_queuecommand)
719
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200720static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
721 const char *fname, u8 function)
722{
723 struct Scsi_Host *shost = cmnd->device->host;
724 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
Hans de Goeded3f7c152013-10-23 17:46:17 +0100725 u16 tag = devinfo->qdepth;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200726 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200727
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200728 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200729 memset(&devinfo->response, 0, sizeof(devinfo->response));
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200730 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200731 shost_printk(KERN_INFO, shost,
732 "%s: %s: submit sense urb failed\n",
733 __func__, fname);
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200734 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200735 return FAILED;
736 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200737 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200738 shost_printk(KERN_INFO, shost,
739 "%s: %s: submit task mgmt urb failed\n",
740 __func__, fname);
Gerd Hoffmann1994ff42012-09-26 10:28:58 +0200741 spin_unlock_irqrestore(&devinfo->lock, flags);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200742 return FAILED;
743 }
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200744 spin_unlock_irqrestore(&devinfo->lock, flags);
745
746 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200747 shost_printk(KERN_INFO, shost,
748 "%s: %s timed out\n", __func__, fname);
749 return FAILED;
750 }
751 if (be16_to_cpu(devinfo->response.tag) != tag) {
752 shost_printk(KERN_INFO, shost,
753 "%s: %s failed (wrong tag %d/%d)\n", __func__,
754 fname, be16_to_cpu(devinfo->response.tag), tag);
755 return FAILED;
756 }
757 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
758 shost_printk(KERN_INFO, shost,
759 "%s: %s failed (rc 0x%x)\n", __func__,
760 fname, devinfo->response.response_code);
761 return FAILED;
762 }
763 return SUCCESS;
764}
765
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200766static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
767{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200768 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200769 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
770 unsigned long flags;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200771 int ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200772
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200773 uas_log_cmd_state(cmnd, __func__);
Gerd Hoffmanne0648522012-09-25 10:47:08 +0200774 spin_lock_irqsave(&devinfo->lock, flags);
Gerd Hoffmannf491ecb2013-09-13 13:27:14 +0200775 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
Gerd Hoffmannef018cc92012-09-25 10:47:06 +0200776 cmdinfo->state |= COMMAND_ABORTED;
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200777 list_add_tail(&cmdinfo->dead, &devinfo->dead_list);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100778 if (cmdinfo->state & IS_IN_WORK_LIST) {
Gerd Hoffmann1bf81982013-09-13 13:27:12 +0200779 list_del(&cmdinfo->work);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100780 cmdinfo->state &= ~IS_IN_WORK_LIST;
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100781 }
782 if (cmdinfo->state & COMMAND_INFLIGHT) {
783 spin_unlock_irqrestore(&devinfo->lock, flags);
784 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
785 } else {
Hans de Goede7e50e0b2013-10-17 19:19:04 +0200786 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
Gerd Hoffmann5d390402012-11-30 11:54:43 +0100787 uas_try_complete(cmnd, __func__);
788 spin_unlock_irqrestore(&devinfo->lock, flags);
789 ret = SUCCESS;
790 }
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200791 return ret;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200792}
793
794static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
795{
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200796 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
797 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
798 TMF_LOGICAL_UNIT_RESET);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200799}
800
801static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
802{
803 struct scsi_device *sdev = cmnd->device;
804 struct uas_dev_info *devinfo = sdev->hostdata;
805 struct usb_device *udev = devinfo->udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200806 int err;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200807
Hans de Goedebe326f42013-09-22 16:27:02 +0200808 err = usb_lock_device_for_reset(udev, devinfo->intf);
809 if (err) {
810 shost_printk(KERN_ERR, sdev->host,
811 "%s FAILED to get lock err %d\n", __func__, err);
812 return FAILED;
813 }
814
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200815 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200816 devinfo->resetting = 1;
Gerd Hoffmann4c456972012-11-30 11:54:44 +0100817 uas_abort_work(devinfo);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +0200818 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200819 usb_kill_anchored_urbs(&devinfo->sense_urbs);
820 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +0200821 uas_zap_dead(devinfo);
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200822 err = usb_reset_device(udev);
823 devinfo->resetting = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200824
Hans de Goedebe326f42013-09-22 16:27:02 +0200825 usb_unlock_device(udev);
826
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200827 if (err) {
828 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
829 return FAILED;
830 }
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200831
Gerd Hoffmann023b5152012-06-19 09:54:54 +0200832 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
833 return SUCCESS;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200834}
835
836static int uas_slave_alloc(struct scsi_device *sdev)
837{
838 sdev->hostdata = (void *)sdev->host->hostdata[0];
839 return 0;
840}
841
842static int uas_slave_configure(struct scsi_device *sdev)
843{
844 struct uas_dev_info *devinfo = sdev->hostdata;
845 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
Hans de Goeded3f7c152013-10-23 17:46:17 +0100846 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200847 return 0;
848}
849
850static struct scsi_host_template uas_host_template = {
851 .module = THIS_MODULE,
852 .name = "uas",
853 .queuecommand = uas_queuecommand,
854 .slave_alloc = uas_slave_alloc,
855 .slave_configure = uas_slave_configure,
856 .eh_abort_handler = uas_eh_abort_handler,
857 .eh_device_reset_handler = uas_eh_device_reset_handler,
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200858 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
859 .can_queue = 65536, /* Is there a limit on the _host_ ? */
860 .this_id = -1,
861 .sg_tablesize = SG_NONE,
862 .cmd_per_lun = 1, /* until we override it */
863 .skip_settle_delay = 1,
864 .ordered_tag = 1,
865};
866
867static struct usb_device_id uas_usb_ids[] = {
868 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
869 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
870 /* 0xaa is a prototype device I happen to have access to */
871 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
872 { }
873};
874MODULE_DEVICE_TABLE(usb, uas_usb_ids);
875
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500876static int uas_is_interface(struct usb_host_interface *intf)
877{
878 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
879 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
880 intf->desc.bInterfaceProtocol == USB_PR_UAS);
881}
882
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100883static int uas_isnt_supported(struct usb_device *udev)
884{
885 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
886
887 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
888 "support scatter-gather which is\n",
889 hcd->driver->description);
890 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
891 "alternative USB controller if you wish to use UAS.\n");
892 return -ENODEV;
893}
894
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500895static int uas_switch_interface(struct usb_device *udev,
896 struct usb_interface *intf)
897{
898 int i;
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100899 int sg_supported = udev->bus->sg_tablesize != 0;
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500900
901 for (i = 0; i < intf->num_altsetting; i++) {
902 struct usb_host_interface *alt = &intf->altsetting[i];
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100903
904 if (uas_is_interface(alt)) {
905 if (!sg_supported)
906 return uas_isnt_supported(udev);
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500907 return usb_set_interface(udev,
908 alt->desc.bInterfaceNumber,
909 alt->desc.bAlternateSetting);
Sebastian Andrzej Siewiorc898add2012-01-11 12:42:32 +0100910 }
Matthew Wilcox89dc2902010-12-15 15:44:05 -0500911 }
912
913 return -ENODEV;
914}
915
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200916static void uas_configure_endpoints(struct uas_dev_info *devinfo)
917{
918 struct usb_host_endpoint *eps[4] = { };
919 struct usb_interface *intf = devinfo->intf;
920 struct usb_device *udev = devinfo->udev;
921 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
922 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
923
924 devinfo->uas_sense_old = 0;
Sebastian Andrzej Siewior22188f42011-12-19 20:22:39 +0100925 devinfo->cmnd = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200926
927 for (i = 0; i < n_endpoints; i++) {
928 unsigned char *extra = endpoint[i].extra;
929 int len = endpoint[i].extralen;
930 while (len > 1) {
931 if (extra[1] == USB_DT_PIPE_USAGE) {
932 unsigned pipe_id = extra[2];
933 if (pipe_id > 0 && pipe_id < 5)
934 eps[pipe_id - 1] = &endpoint[i];
935 break;
936 }
937 len -= extra[0];
938 extra += extra[0];
939 }
940 }
941
942 /*
943 * Assume that if we didn't find a control pipe descriptor, we're
944 * using a device with old firmware that happens to be set up like
945 * this.
946 */
947 if (!eps[0]) {
948 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
949 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
950 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
951 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
952
953 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
954 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
955 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
956 } else {
957 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200958 usb_endpoint_num(&eps[0]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200959 devinfo->status_pipe = usb_rcvbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200960 usb_endpoint_num(&eps[1]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200961 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200962 usb_endpoint_num(&eps[2]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200963 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
Hans de Goede6c2334e2013-10-17 22:20:54 +0200964 usb_endpoint_num(&eps[3]->desc));
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200965 }
966
967 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
968 GFP_KERNEL);
969 if (devinfo->qdepth < 0) {
970 devinfo->qdepth = 256;
971 devinfo->use_streams = 0;
972 } else {
973 devinfo->use_streams = 1;
974 }
975}
976
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +0100977static void uas_free_streams(struct uas_dev_info *devinfo)
978{
979 struct usb_device *udev = devinfo->udev;
980 struct usb_host_endpoint *eps[3];
981
982 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
983 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
984 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
985 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
986}
987
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200988/*
989 * XXX: What I'd like to do here is register a SCSI host for each USB host in
990 * the system. Follow usb-storage's design of registering a SCSI host for
991 * each USB device for the moment. Can implement this by walking up the
992 * USB hierarchy until we find a USB host.
993 */
994static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
995{
Hans de Goede6ce82132013-10-17 19:00:45 +0200996 int result = -ENOMEM;
997 struct Scsi_Host *shost = NULL;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +0200998 struct uas_dev_info *devinfo;
999 struct usb_device *udev = interface_to_usbdev(intf);
1000
Matthew Wilcox89dc2902010-12-15 15:44:05 -05001001 if (uas_switch_interface(udev, intf))
1002 return -ENODEV;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001003
1004 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
1005 if (!devinfo)
Hans de Goede6ce82132013-10-17 19:00:45 +02001006 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001007
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001008 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
1009 if (!shost)
Hans de Goede6ce82132013-10-17 19:00:45 +02001010 goto set_alt0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001011
1012 shost->max_cmd_len = 16 + 252;
1013 shost->max_id = 1;
Gerd Hoffmannbde027b2013-01-25 15:03:36 +01001014 shost->max_lun = 256;
1015 shost->max_channel = 0;
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001016 shost->sg_tablesize = udev->bus->sg_tablesize;
1017
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001018 devinfo->intf = intf;
1019 devinfo->udev = udev;
Gerd Hoffmann023b5152012-06-19 09:54:54 +02001020 devinfo->resetting = 0;
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001021 init_usb_anchor(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001022 init_usb_anchor(&devinfo->sense_urbs);
1023 init_usb_anchor(&devinfo->data_urbs);
Gerd Hoffmanne0648522012-09-25 10:47:08 +02001024 spin_lock_init(&devinfo->lock);
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001025 INIT_WORK(&devinfo->work, uas_do_work);
1026 INIT_LIST_HEAD(&devinfo->work_list);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001027 INIT_LIST_HEAD(&devinfo->dead_list);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001028 uas_configure_endpoints(devinfo);
1029
Hans de Goeded3f7c152013-10-23 17:46:17 +01001030 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001031 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001032 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001033
1034 result = scsi_add_host(shost, &intf->dev);
1035 if (result)
Hans de Goede6ce82132013-10-17 19:00:45 +02001036 goto free_streams;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001037
1038 shost->hostdata[0] = (unsigned long)devinfo;
1039
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001040 scsi_scan_host(shost);
1041 usb_set_intfdata(intf, shost);
1042 return result;
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001043
Hans de Goede6ce82132013-10-17 19:00:45 +02001044free_streams:
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001045 uas_free_streams(devinfo);
Hans de Goede6ce82132013-10-17 19:00:45 +02001046set_alt0:
1047 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001048 kfree(devinfo);
1049 if (shost)
1050 scsi_host_put(shost);
1051 return result;
1052}
1053
1054static int uas_pre_reset(struct usb_interface *intf)
1055{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001056 struct Scsi_Host *shost = usb_get_intfdata(intf);
1057 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1058 unsigned long flags;
1059
1060 /* Block new requests */
1061 spin_lock_irqsave(shost->host_lock, flags);
1062 scsi_block_requests(shost);
1063 spin_unlock_irqrestore(shost->host_lock, flags);
1064
1065 /* Wait for any pending requests to complete */
1066 flush_work(&devinfo->work);
1067 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1068 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1069 return 1;
1070 }
1071
1072 uas_free_streams(devinfo);
1073
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001074 return 0;
1075}
1076
1077static int uas_post_reset(struct usb_interface *intf)
1078{
Hans de Goede4de7a3732013-10-22 16:10:44 +01001079 struct Scsi_Host *shost = usb_get_intfdata(intf);
1080 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1081 unsigned long flags;
1082
1083 uas_configure_endpoints(devinfo);
1084
1085 spin_lock_irqsave(shost->host_lock, flags);
1086 scsi_report_bus_reset(shost, 0);
1087 spin_unlock_irqrestore(shost->host_lock, flags);
1088
1089 scsi_unblock_requests(shost);
1090
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001091 return 0;
1092}
1093
1094static void uas_disconnect(struct usb_interface *intf)
1095{
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001096 struct Scsi_Host *shost = usb_get_intfdata(intf);
1097 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
1098
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001099 devinfo->resetting = 1;
Gerd Hoffmann1bf81982013-09-13 13:27:12 +02001100 cancel_work_sync(&devinfo->work);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001101 uas_abort_work(devinfo);
Gerd Hoffmanna0e39e32012-09-25 10:47:04 +02001102 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
Gerd Hoffmannbdd000f2012-06-19 09:54:53 +02001103 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1104 usb_kill_anchored_urbs(&devinfo->data_urbs);
Gerd Hoffmann326349f2013-09-13 13:27:13 +02001105 uas_zap_dead(devinfo);
Gerd Hoffmann4c456972012-11-30 11:54:44 +01001106 scsi_remove_host(shost);
Sebastian Andrzej Siewiordae51542011-12-19 17:06:08 +01001107 uas_free_streams(devinfo);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001108 kfree(devinfo);
1109}
1110
1111/*
1112 * XXX: Should this plug into libusual so we can auto-upgrade devices from
1113 * Bulk-Only to UAS?
1114 */
1115static struct usb_driver uas_driver = {
1116 .name = "uas",
1117 .probe = uas_probe,
1118 .disconnect = uas_disconnect,
1119 .pre_reset = uas_pre_reset,
1120 .post_reset = uas_post_reset,
1121 .id_table = uas_usb_ids,
1122};
1123
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001124module_usb_driver(uas_driver);
Matthew Wilcox115bb1f2010-10-07 13:05:23 +02001125
1126MODULE_LICENSE("GPL");
1127MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");