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