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