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