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