]> Pileus Git - ~andy/linux/blob - drivers/usb/storage/uas.c
USB: uas: fix abort
[~andy/linux] / drivers / usb / storage / uas.c
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>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/usb/storage.h>
18 #include <linux/usb/uas.h>
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
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
41 struct uas_dev_info {
42         struct usb_interface *intf;
43         struct usb_device *udev;
44         struct usb_anchor cmd_urbs;
45         struct usb_anchor sense_urbs;
46         struct usb_anchor data_urbs;
47         int qdepth, resetting;
48         struct response_ui response;
49         unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
50         unsigned use_streams:1;
51         unsigned uas_sense_old:1;
52         struct scsi_cmnd *cmnd;
53 };
54
55 enum {
56         SUBMIT_STATUS_URB       = (1 << 1),
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),
63         COMMAND_INFLIGHT        = (1 << 8),
64         DATA_IN_URB_INFLIGHT    = (1 << 9),
65         DATA_OUT_URB_INFLIGHT   = (1 << 10),
66         COMMAND_COMPLETED       = (1 << 11),
67         COMMAND_ABORTED         = (1 << 12),
68 };
69
70 /* Overrides scsi_pointer */
71 struct uas_cmd_info {
72         unsigned int state;
73         unsigned int stream;
74         struct urb *cmd_urb;
75         struct urb *data_in_urb;
76         struct urb *data_out_urb;
77         struct list_head list;
78 };
79
80 /* I hate forward declarations, but I actually have a loop */
81 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
82                                 struct uas_dev_info *devinfo, gfp_t gfp);
83 static void uas_do_work(struct work_struct *work);
84
85 static DECLARE_WORK(uas_work, uas_do_work);
86 static DEFINE_SPINLOCK(uas_work_lock);
87 static LIST_HEAD(uas_work_list);
88
89 static void uas_do_work(struct work_struct *work)
90 {
91         struct uas_cmd_info *cmdinfo;
92         struct uas_cmd_info *temp;
93         struct list_head list;
94         int err;
95
96         spin_lock_irq(&uas_work_lock);
97         list_replace_init(&uas_work_list, &list);
98         spin_unlock_irq(&uas_work_lock);
99
100         list_for_each_entry_safe(cmdinfo, temp, &list, list) {
101                 struct scsi_pointer *scp = (void *)cmdinfo;
102                 struct scsi_cmnd *cmnd = container_of(scp,
103                                                         struct scsi_cmnd, SCp);
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                 }
112         }
113 }
114
115 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
116 {
117         struct sense_iu *sense_iu = urb->transfer_buffer;
118         struct scsi_device *sdev = cmnd->device;
119
120         if (urb->actual_length > 16) {
121                 unsigned len = be16_to_cpup(&sense_iu->len);
122                 if (len + 16 != urb->actual_length) {
123                         int newlen = min(len + 16, urb->actual_length) - 16;
124                         if (newlen < 0)
125                                 newlen = 0;
126                         sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
127                                 "disagrees with IU sense data length %d, "
128                                 "using %d bytes of sense data\n", __func__,
129                                         urb->actual_length, len, newlen);
130                         len = newlen;
131                 }
132                 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
133         }
134
135         cmnd->result = sense_iu->status;
136 }
137
138 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
139 {
140         struct sense_iu_old *sense_iu = urb->transfer_buffer;
141         struct scsi_device *sdev = cmnd->device;
142
143         if (urb->actual_length > 8) {
144                 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
145                 if (len + 8 != urb->actual_length) {
146                         int newlen = min(len + 8, urb->actual_length) - 8;
147                         if (newlen < 0)
148                                 newlen = 0;
149                         sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
150                                 "disagrees with IU sense data length %d, "
151                                 "using %d bytes of sense data\n", __func__,
152                                         urb->actual_length, len, newlen);
153                         len = newlen;
154                 }
155                 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
156         }
157
158         cmnd->result = sense_iu->status;
159 }
160
161 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
162 {
163         struct uas_cmd_info *ci = (void *)&cmnd->SCp;
164
165         scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
166                     "%s%s%s%s%s%s%s%s%s%s%s%s\n",
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"   : "",
178                     (ci->state & COMMAND_COMPLETED)     ? " done"  : "",
179                     (ci->state & COMMAND_ABORTED)       ? " abort" : "");
180 }
181
182 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
183 {
184         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
185
186         if (cmdinfo->state & (COMMAND_INFLIGHT |
187                               DATA_IN_URB_INFLIGHT |
188                               DATA_OUT_URB_INFLIGHT))
189                 return -EBUSY;
190         BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
191         cmdinfo->state |= COMMAND_COMPLETED;
192         usb_free_urb(cmdinfo->data_in_urb);
193         usb_free_urb(cmdinfo->data_out_urb);
194         if (cmdinfo->state & COMMAND_ABORTED) {
195                 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
196                 cmnd->result = DID_ABORT << 16;
197         }
198         cmnd->scsi_done(cmnd);
199         return 0;
200 }
201
202 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
203                           unsigned direction)
204 {
205         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
206         int err;
207
208         cmdinfo->state |= direction | SUBMIT_STATUS_URB;
209         err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
210         if (err) {
211                 spin_lock(&uas_work_lock);
212                 list_add_tail(&cmdinfo->list, &uas_work_list);
213                 spin_unlock(&uas_work_lock);
214                 schedule_work(&uas_work);
215         }
216 }
217
218 static void uas_stat_cmplt(struct urb *urb)
219 {
220         struct iu *iu = urb->transfer_buffer;
221         struct Scsi_Host *shost = urb->context;
222         struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
223         struct scsi_cmnd *cmnd;
224         struct uas_cmd_info *cmdinfo;
225         u16 tag;
226
227         if (urb->status) {
228                 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
229                 usb_free_urb(urb);
230                 return;
231         }
232
233         if (devinfo->resetting) {
234                 usb_free_urb(urb);
235                 return;
236         }
237
238         tag = be16_to_cpup(&iu->tag) - 1;
239         if (tag == 0)
240                 cmnd = devinfo->cmnd;
241         else
242                 cmnd = scsi_host_find_tag(shost, tag - 1);
243         if (!cmnd) {
244                 if (iu->iu_id != IU_ID_RESPONSE) {
245                         usb_free_urb(urb);
246                         return;
247                 }
248         } else {
249                 cmdinfo = (void *)&cmnd->SCp;
250         }
251
252         switch (iu->iu_id) {
253         case IU_ID_STATUS:
254                 if (devinfo->cmnd == cmnd)
255                         devinfo->cmnd = NULL;
256
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);
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                 }
270                 cmdinfo->state &= ~COMMAND_INFLIGHT;
271                 uas_try_complete(cmnd, __func__);
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;
279         case IU_ID_RESPONSE:
280                 /* store results for uas_eh_task_mgmt() */
281                 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
282                 break;
283         default:
284                 scmd_printk(KERN_ERR, cmnd,
285                         "Bogus IU (%d) received on status pipe\n", iu->iu_id);
286         }
287         usb_free_urb(urb);
288 }
289
290 static void uas_data_cmplt(struct urb *urb)
291 {
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);
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         }
310         uas_try_complete(cmnd, __func__);
311 }
312
313 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
314                                       unsigned int pipe, u16 stream_id,
315                                       struct scsi_cmnd *cmnd,
316                                       enum dma_data_direction dir)
317 {
318         struct usb_device *udev = devinfo->udev;
319         struct urb *urb = usb_alloc_urb(0, gfp);
320         struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
321                 ? scsi_in(cmnd) : scsi_out(cmnd);
322
323         if (!urb)
324                 goto out;
325         usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
326                           uas_data_cmplt, cmnd);
327         if (devinfo->use_streams)
328                 urb->stream_id = stream_id;
329         urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
330         urb->sg = sdb->table.sgl;
331  out:
332         return urb;
333 }
334
335 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
336                                        struct Scsi_Host *shost, u16 stream_id)
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
345         iu = kzalloc(sizeof(*iu), gfp);
346         if (!iu)
347                 goto free;
348
349         usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
350                                                 uas_stat_cmplt, shost);
351         urb->stream_id = stream_id;
352         urb->transfer_flags |= URB_FREE_BUFFER;
353  out:
354         return urb;
355  free:
356         usb_free_urb(urb);
357         return NULL;
358 }
359
360 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
361                                         struct scsi_cmnd *cmnd, u16 stream_id)
362 {
363         struct usb_device *udev = devinfo->udev;
364         struct scsi_device *sdev = cmnd->device;
365         struct urb *urb = usb_alloc_urb(0, gfp);
366         struct command_iu *iu;
367         int len;
368
369         if (!urb)
370                 goto out;
371
372         len = cmnd->cmd_len - 16;
373         if (len < 0)
374                 len = 0;
375         len = ALIGN(len, 4);
376         iu = kzalloc(sizeof(*iu) + len, gfp);
377         if (!iu)
378                 goto free;
379
380         iu->iu_id = IU_ID_COMMAND;
381         if (blk_rq_tagged(cmnd->request))
382                 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
383         else
384                 iu->tag = cpu_to_be16(1);
385         iu->prio_attr = UAS_SIMPLE_TAG;
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
400 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
401                                u8 function, u16 stream_id)
402 {
403         struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
404         struct usb_device *udev = devinfo->udev;
405         struct urb *urb = usb_alloc_urb(0, gfp);
406         struct task_mgmt_iu *iu;
407         int err = -ENOMEM;
408
409         if (!urb)
410                 goto err;
411
412         iu = kzalloc(sizeof(*iu), gfp);
413         if (!iu)
414                 goto err;
415
416         iu->iu_id = IU_ID_TASK_MGMT;
417         iu->tag = cpu_to_be16(stream_id);
418         int_to_scsilun(cmnd->device->lun, &iu->lun);
419
420         iu->function = function;
421         switch (function) {
422         case TMF_ABORT_TASK:
423                 if (blk_rq_tagged(cmnd->request))
424                         iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
425                 else
426                         iu->task_tag = cpu_to_be16(1);
427                 break;
428         }
429
430         usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
431                           usb_free_urb, NULL);
432         urb->transfer_flags |= URB_FREE_BUFFER;
433
434         err = usb_submit_urb(urb, gfp);
435         if (err)
436                 goto err;
437         usb_anchor_urb(urb, &devinfo->cmd_urbs);
438
439         return 0;
440
441 err:
442         usb_free_urb(urb);
443         return err;
444 }
445
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
452 static int uas_submit_sense_urb(struct Scsi_Host *shost,
453                                 gfp_t gfp, unsigned int stream)
454 {
455         struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
456         struct urb *urb;
457
458         urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
459         if (!urb)
460                 return SCSI_MLQUEUE_DEVICE_BUSY;
461         if (usb_submit_urb(urb, gfp)) {
462                 shost_printk(KERN_INFO, shost,
463                              "sense urb submission failure\n");
464                 usb_free_urb(urb);
465                 return SCSI_MLQUEUE_DEVICE_BUSY;
466         }
467         usb_anchor_urb(urb, &devinfo->sense_urbs);
468         return 0;
469 }
470
471 static 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;
476
477         if (cmdinfo->state & SUBMIT_STATUS_URB) {
478                 err = uas_submit_sense_urb(cmnd->device->host, gfp,
479                                            cmdinfo->stream);
480                 if (err) {
481                         return err;
482                 }
483                 cmdinfo->state &= ~SUBMIT_STATUS_URB;
484         }
485
486         if (cmdinfo->state & ALLOC_DATA_IN_URB) {
487                 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
488                                         devinfo->data_in_pipe, cmdinfo->stream,
489                                         cmnd, DMA_FROM_DEVICE);
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;
502                 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
503                 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
504         }
505
506         if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
507                 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
508                                         devinfo->data_out_pipe, cmdinfo->stream,
509                                         cmnd, DMA_TO_DEVICE);
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;
522                 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
523                 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
524         }
525
526         if (cmdinfo->state & ALLOC_CMD_URB) {
527                 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
528                                                      cmdinfo->stream);
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) {
535                 usb_get_urb(cmdinfo->cmd_urb);
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                 }
541                 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
542                 usb_put_urb(cmdinfo->cmd_urb);
543                 cmdinfo->cmd_urb = NULL;
544                 cmdinfo->state &= ~SUBMIT_CMD_URB;
545                 cmdinfo->state |= COMMAND_INFLIGHT;
546         }
547
548         return 0;
549 }
550
551 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
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
561         if (devinfo->cmnd)
562                 return SCSI_MLQUEUE_DEVICE_BUSY;
563
564         if (blk_rq_tagged(cmnd->request)) {
565                 cmdinfo->stream = cmnd->request->tag + 2;
566         } else {
567                 devinfo->cmnd = cmnd;
568                 cmdinfo->stream = 1;
569         }
570
571         cmnd->scsi_done = done;
572
573         cmdinfo->state = SUBMIT_STATUS_URB |
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) {
589                 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
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 */
596                 if (cmdinfo->state & SUBMIT_STATUS_URB) {
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
608 static DEF_SCSI_QCMD(uas_queuecommand)
609
610 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
611                             const char *fname, u8 function)
612 {
613         struct Scsi_Host *shost = cmnd->device->host;
614         struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
615         u16 tag = devinfo->qdepth - 1;
616
617         memset(&devinfo->response, 0, sizeof(devinfo->response));
618         if (uas_submit_sense_urb(shost, GFP_NOIO, tag)) {
619                 shost_printk(KERN_INFO, shost,
620                              "%s: %s: submit sense urb failed\n",
621                              __func__, fname);
622                 return FAILED;
623         }
624         if (uas_submit_task_urb(cmnd, GFP_NOIO, function, tag)) {
625                 shost_printk(KERN_INFO, shost,
626                              "%s: %s: submit task mgmt urb failed\n",
627                              __func__, fname);
628                 return FAILED;
629         }
630         if (0 == usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000)) {
631                 shost_printk(KERN_INFO, shost,
632                              "%s: %s timed out\n", __func__, fname);
633                 return FAILED;
634         }
635         if (be16_to_cpu(devinfo->response.tag) != tag) {
636                 shost_printk(KERN_INFO, shost,
637                              "%s: %s failed (wrong tag %d/%d)\n", __func__,
638                              fname, be16_to_cpu(devinfo->response.tag), tag);
639                 return FAILED;
640         }
641         if (devinfo->response.response_code != RC_TMF_COMPLETE) {
642                 shost_printk(KERN_INFO, shost,
643                              "%s: %s failed (rc 0x%x)\n", __func__,
644                              fname, devinfo->response.response_code);
645                 return FAILED;
646         }
647         return SUCCESS;
648 }
649
650 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
651 {
652         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
653         int ret;
654
655         uas_log_cmd_state(cmnd, __func__);
656         cmdinfo->state |= COMMAND_ABORTED;
657         ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
658         return ret;
659 }
660
661 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
662 {
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);
666 }
667
668 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
669 {
670         struct scsi_device *sdev = cmnd->device;
671         struct uas_dev_info *devinfo = sdev->hostdata;
672         struct usb_device *udev = devinfo->udev;
673         int err;
674
675         devinfo->resetting = 1;
676         usb_kill_anchored_urbs(&devinfo->cmd_urbs);
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;
681
682         if (err) {
683                 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
684                 return FAILED;
685         }
686
687         shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
688         return SUCCESS;
689 }
690
691 static int uas_slave_alloc(struct scsi_device *sdev)
692 {
693         sdev->hostdata = (void *)sdev->host->hostdata[0];
694         return 0;
695 }
696
697 static int uas_slave_configure(struct scsi_device *sdev)
698 {
699         struct uas_dev_info *devinfo = sdev->hostdata;
700         scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
701         scsi_activate_tcq(sdev, devinfo->qdepth - 3);
702         return 0;
703 }
704
705 static struct scsi_host_template uas_host_template = {
706         .module = THIS_MODULE,
707         .name = "uas",
708         .queuecommand = uas_queuecommand,
709         .slave_alloc = uas_slave_alloc,
710         .slave_configure = uas_slave_configure,
711         .eh_abort_handler = uas_eh_abort_handler,
712         .eh_device_reset_handler = uas_eh_device_reset_handler,
713         .eh_bus_reset_handler = uas_eh_bus_reset_handler,
714         .can_queue = 65536,     /* Is there a limit on the _host_ ? */
715         .this_id = -1,
716         .sg_tablesize = SG_NONE,
717         .cmd_per_lun = 1,       /* until we override it */
718         .skip_settle_delay = 1,
719         .ordered_tag = 1,
720 };
721
722 static struct usb_device_id uas_usb_ids[] = {
723         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
724         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
725         /* 0xaa is a prototype device I happen to have access to */
726         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
727         { }
728 };
729 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
730
731 static int uas_is_interface(struct usb_host_interface *intf)
732 {
733         return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
734                 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
735                 intf->desc.bInterfaceProtocol == USB_PR_UAS);
736 }
737
738 static int uas_isnt_supported(struct usb_device *udev)
739 {
740         struct usb_hcd *hcd = bus_to_hcd(udev->bus);
741
742         dev_warn(&udev->dev, "The driver for the USB controller %s does not "
743                         "support scatter-gather which is\n",
744                         hcd->driver->description);
745         dev_warn(&udev->dev, "required by the UAS driver. Please try an"
746                         "alternative USB controller if you wish to use UAS.\n");
747         return -ENODEV;
748 }
749
750 static int uas_switch_interface(struct usb_device *udev,
751                                                 struct usb_interface *intf)
752 {
753         int i;
754         int sg_supported = udev->bus->sg_tablesize != 0;
755
756         for (i = 0; i < intf->num_altsetting; i++) {
757                 struct usb_host_interface *alt = &intf->altsetting[i];
758
759                 if (uas_is_interface(alt)) {
760                         if (!sg_supported)
761                                 return uas_isnt_supported(udev);
762                         return usb_set_interface(udev,
763                                                 alt->desc.bInterfaceNumber,
764                                                 alt->desc.bAlternateSetting);
765                 }
766         }
767
768         return -ENODEV;
769 }
770
771 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
772 {
773         struct usb_host_endpoint *eps[4] = { };
774         struct usb_interface *intf = devinfo->intf;
775         struct usb_device *udev = devinfo->udev;
776         struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
777         unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
778
779         devinfo->uas_sense_old = 0;
780         devinfo->cmnd = NULL;
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
832 static void uas_free_streams(struct uas_dev_info *devinfo)
833 {
834         struct usb_device *udev = devinfo->udev;
835         struct usb_host_endpoint *eps[3];
836
837         eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
838         eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
839         eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
840         usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
841 }
842
843 /*
844  * XXX: What I'd like to do here is register a SCSI host for each USB host in
845  * the system.  Follow usb-storage's design of registering a SCSI host for
846  * each USB device for the moment.  Can implement this by walking up the
847  * USB hierarchy until we find a USB host.
848  */
849 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
850 {
851         int result;
852         struct Scsi_Host *shost;
853         struct uas_dev_info *devinfo;
854         struct usb_device *udev = interface_to_usbdev(intf);
855
856         if (uas_switch_interface(udev, intf))
857                 return -ENODEV;
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
872         devinfo->intf = intf;
873         devinfo->udev = udev;
874         devinfo->resetting = 0;
875         init_usb_anchor(&devinfo->cmd_urbs);
876         init_usb_anchor(&devinfo->sense_urbs);
877         init_usb_anchor(&devinfo->data_urbs);
878         uas_configure_endpoints(devinfo);
879
880         result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3);
881         if (result)
882                 goto free;
883
884         result = scsi_add_host(shost, &intf->dev);
885         if (result)
886                 goto deconfig_eps;
887
888         shost->hostdata[0] = (unsigned long)devinfo;
889
890         scsi_scan_host(shost);
891         usb_set_intfdata(intf, shost);
892         return result;
893
894 deconfig_eps:
895         uas_free_streams(devinfo);
896  free:
897         kfree(devinfo);
898         if (shost)
899                 scsi_host_put(shost);
900         return result;
901 }
902
903 static int uas_pre_reset(struct usb_interface *intf)
904 {
905 /* XXX: Need to return 1 if it's not our device in error handling */
906         return 0;
907 }
908
909 static int uas_post_reset(struct usb_interface *intf)
910 {
911 /* XXX: Need to return 1 if it's not our device in error handling */
912         return 0;
913 }
914
915 static void uas_disconnect(struct usb_interface *intf)
916 {
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);
921         usb_kill_anchored_urbs(&devinfo->cmd_urbs);
922         usb_kill_anchored_urbs(&devinfo->sense_urbs);
923         usb_kill_anchored_urbs(&devinfo->data_urbs);
924         uas_free_streams(devinfo);
925         kfree(devinfo);
926 }
927
928 /*
929  * XXX: Should this plug into libusual so we can auto-upgrade devices from
930  * Bulk-Only to UAS?
931  */
932 static struct usb_driver uas_driver = {
933         .name = "uas",
934         .probe = uas_probe,
935         .disconnect = uas_disconnect,
936         .pre_reset = uas_pre_reset,
937         .post_reset = uas_post_reset,
938         .id_table = uas_usb_ids,
939 };
940
941 module_usb_driver(uas_driver);
942
943 MODULE_LICENSE("GPL");
944 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");