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