OSDN Git Service

b1a1acb43461504d3e131a3c7f5781050c9e1d11
[android-x86/kernel.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 Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2014
6  * Copyright Matthew Wilcox for Intel Corp, 2010
7  * Copyright Sarah Sharp for Intel Corp, 2010
8  *
9  * Distributed under the terms of the GNU GPL, version two.
10  */
11
12 #include <linux/blkdev.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb_usual.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/storage.h>
20 #include <linux/usb/uas.h>
21
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_eh.h>
24 #include <scsi/scsi_dbg.h>
25 #include <scsi/scsi_cmnd.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_tcq.h>
29
30 #include "uas-detect.h"
31 #include "scsiglue.h"
32
33 #define MAX_CMNDS 256
34
35 /*
36  * The r00-r01c specs define this version of the SENSE IU data structure.
37  * It's still in use by several different firmware releases.
38  */
39 struct sense_iu_old {
40         __u8 iu_id;
41         __u8 rsvd1;
42         __be16 tag;
43         __be16 len;
44         __u8 status;
45         __u8 service_response;
46         __u8 sense[SCSI_SENSE_BUFFERSIZE];
47 };
48
49 struct uas_dev_info {
50         struct usb_interface *intf;
51         struct usb_device *udev;
52         struct usb_anchor cmd_urbs;
53         struct usb_anchor sense_urbs;
54         struct usb_anchor data_urbs;
55         unsigned long flags;
56         int qdepth, resetting;
57         unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
58         unsigned use_streams:1;
59         unsigned uas_sense_old:1;
60         unsigned shutdown:1;
61         struct scsi_cmnd *cmnd[MAX_CMNDS];
62         spinlock_t lock;
63         struct work_struct work;
64 };
65
66 enum {
67         SUBMIT_STATUS_URB       = (1 << 1),
68         ALLOC_DATA_IN_URB       = (1 << 2),
69         SUBMIT_DATA_IN_URB      = (1 << 3),
70         ALLOC_DATA_OUT_URB      = (1 << 4),
71         SUBMIT_DATA_OUT_URB     = (1 << 5),
72         ALLOC_CMD_URB           = (1 << 6),
73         SUBMIT_CMD_URB          = (1 << 7),
74         COMMAND_INFLIGHT        = (1 << 8),
75         DATA_IN_URB_INFLIGHT    = (1 << 9),
76         DATA_OUT_URB_INFLIGHT   = (1 << 10),
77         COMMAND_COMPLETED       = (1 << 11),
78         COMMAND_ABORTED         = (1 << 12),
79         IS_IN_WORK_LIST         = (1 << 13),
80 };
81
82 /* Overrides scsi_pointer */
83 struct uas_cmd_info {
84         unsigned int state;
85         unsigned int stream;
86         struct urb *cmd_urb;
87         struct urb *data_in_urb;
88         struct urb *data_out_urb;
89 };
90
91 /* I hate forward declarations, but I actually have a loop */
92 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
93                                 struct uas_dev_info *devinfo, gfp_t gfp);
94 static void uas_do_work(struct work_struct *work);
95 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
96 static void uas_free_streams(struct uas_dev_info *devinfo);
97 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
98
99 static void uas_do_work(struct work_struct *work)
100 {
101         struct uas_dev_info *devinfo =
102                 container_of(work, struct uas_dev_info, work);
103         struct uas_cmd_info *cmdinfo;
104         struct scsi_cmnd *cmnd;
105         unsigned long flags;
106         int i, err;
107
108         spin_lock_irqsave(&devinfo->lock, flags);
109
110         if (devinfo->resetting)
111                 goto out;
112
113         for (i = 0; i < devinfo->qdepth; i++) {
114                 if (!devinfo->cmnd[i])
115                         continue;
116
117                 cmnd = devinfo->cmnd[i];
118                 cmdinfo = (void *)&cmnd->SCp;
119
120                 if (!(cmdinfo->state & IS_IN_WORK_LIST))
121                         continue;
122
123                 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
124                 if (!err)
125                         cmdinfo->state &= ~IS_IN_WORK_LIST;
126                 else
127                         schedule_work(&devinfo->work);
128         }
129 out:
130         spin_unlock_irqrestore(&devinfo->lock, flags);
131 }
132
133 static void uas_add_work(struct uas_cmd_info *cmdinfo)
134 {
135         struct scsi_pointer *scp = (void *)cmdinfo;
136         struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
137         struct uas_dev_info *devinfo = cmnd->device->hostdata;
138
139         lockdep_assert_held(&devinfo->lock);
140         cmdinfo->state |= IS_IN_WORK_LIST;
141         schedule_work(&devinfo->work);
142 }
143
144 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
145 {
146         struct uas_cmd_info *cmdinfo;
147         struct scsi_cmnd *cmnd;
148         unsigned long flags;
149         int i, err;
150
151         spin_lock_irqsave(&devinfo->lock, flags);
152         for (i = 0; i < devinfo->qdepth; i++) {
153                 if (!devinfo->cmnd[i])
154                         continue;
155
156                 cmnd = devinfo->cmnd[i];
157                 cmdinfo = (void *)&cmnd->SCp;
158                 uas_log_cmd_state(cmnd, __func__);
159                 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
160                 cmdinfo->state &= ~COMMAND_INFLIGHT;
161                 cmnd->result = result << 16;
162                 err = uas_try_complete(cmnd, __func__);
163                 WARN_ON(err != 0);
164         }
165         spin_unlock_irqrestore(&devinfo->lock, flags);
166 }
167
168 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
169 {
170         struct sense_iu *sense_iu = urb->transfer_buffer;
171         struct scsi_device *sdev = cmnd->device;
172
173         if (urb->actual_length > 16) {
174                 unsigned len = be16_to_cpup(&sense_iu->len);
175                 if (len + 16 != urb->actual_length) {
176                         int newlen = min(len + 16, urb->actual_length) - 16;
177                         if (newlen < 0)
178                                 newlen = 0;
179                         sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
180                                 "disagrees with IU sense data length %d, "
181                                 "using %d bytes of sense data\n", __func__,
182                                         urb->actual_length, len, newlen);
183                         len = newlen;
184                 }
185                 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
186         }
187
188         cmnd->result = sense_iu->status;
189 }
190
191 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
192 {
193         struct sense_iu_old *sense_iu = urb->transfer_buffer;
194         struct scsi_device *sdev = cmnd->device;
195
196         if (urb->actual_length > 8) {
197                 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
198                 if (len + 8 != urb->actual_length) {
199                         int newlen = min(len + 8, urb->actual_length) - 8;
200                         if (newlen < 0)
201                                 newlen = 0;
202                         sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
203                                 "disagrees with IU sense data length %d, "
204                                 "using %d bytes of sense data\n", __func__,
205                                         urb->actual_length, len, newlen);
206                         len = newlen;
207                 }
208                 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
209         }
210
211         cmnd->result = sense_iu->status;
212 }
213
214 /*
215  * scsi-tags go from 0 - (nr_tags - 1), uas tags need to match stream-ids,
216  * which go from 1 - nr_streams. And we use 1 for untagged commands.
217  */
218 static int uas_get_tag(struct scsi_cmnd *cmnd)
219 {
220         int tag;
221
222         if (blk_rq_tagged(cmnd->request))
223                 tag = cmnd->request->tag + 2;
224         else
225                 tag = 1;
226
227         return tag;
228 }
229
230 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
231 {
232         struct uas_cmd_info *ci = (void *)&cmnd->SCp;
233
234         scmd_printk(KERN_INFO, cmnd,
235                     "%s %p tag %d, inflight:%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
236                     caller, cmnd, uas_get_tag(cmnd),
237                     (ci->state & SUBMIT_STATUS_URB)     ? " s-st"  : "",
238                     (ci->state & ALLOC_DATA_IN_URB)     ? " a-in"  : "",
239                     (ci->state & SUBMIT_DATA_IN_URB)    ? " s-in"  : "",
240                     (ci->state & ALLOC_DATA_OUT_URB)    ? " a-out" : "",
241                     (ci->state & SUBMIT_DATA_OUT_URB)   ? " s-out" : "",
242                     (ci->state & ALLOC_CMD_URB)         ? " a-cmd" : "",
243                     (ci->state & SUBMIT_CMD_URB)        ? " s-cmd" : "",
244                     (ci->state & COMMAND_INFLIGHT)      ? " CMD"   : "",
245                     (ci->state & DATA_IN_URB_INFLIGHT)  ? " IN"    : "",
246                     (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT"   : "",
247                     (ci->state & COMMAND_COMPLETED)     ? " done"  : "",
248                     (ci->state & COMMAND_ABORTED)       ? " abort" : "",
249                     (ci->state & IS_IN_WORK_LIST)       ? " work"  : "");
250 }
251
252 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
253 {
254         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
255         struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
256
257         lockdep_assert_held(&devinfo->lock);
258         if (cmdinfo->state & (COMMAND_INFLIGHT |
259                               DATA_IN_URB_INFLIGHT |
260                               DATA_OUT_URB_INFLIGHT |
261                               COMMAND_ABORTED))
262                 return -EBUSY;
263         WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
264         cmdinfo->state |= COMMAND_COMPLETED;
265         devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
266         cmnd->scsi_done(cmnd);
267         return 0;
268 }
269
270 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
271                           unsigned direction)
272 {
273         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
274         int err;
275
276         cmdinfo->state |= direction | SUBMIT_STATUS_URB;
277         err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
278         if (err) {
279                 uas_add_work(cmdinfo);
280         }
281 }
282
283 static void uas_stat_cmplt(struct urb *urb)
284 {
285         struct iu *iu = urb->transfer_buffer;
286         struct Scsi_Host *shost = urb->context;
287         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
288         struct urb *data_in_urb = NULL;
289         struct urb *data_out_urb = NULL;
290         struct scsi_cmnd *cmnd;
291         struct uas_cmd_info *cmdinfo;
292         unsigned long flags;
293         unsigned int idx;
294
295         spin_lock_irqsave(&devinfo->lock, flags);
296
297         if (devinfo->resetting)
298                 goto out;
299
300         if (urb->status) {
301                 if (urb->status == -ENOENT) {
302                         dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
303                                 urb->stream_id);
304                 } else {
305                         dev_err(&urb->dev->dev, "stat urb: status %d\n",
306                                 urb->status);
307                 }
308                 goto out;
309         }
310
311         idx = be16_to_cpup(&iu->tag) - 1;
312         if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
313                 dev_err(&urb->dev->dev,
314                         "stat urb: no pending cmd for tag %d\n", idx + 1);
315                 goto out;
316         }
317
318         cmnd = devinfo->cmnd[idx];
319         cmdinfo = (void *)&cmnd->SCp;
320
321         if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
322                 scmd_printk(KERN_ERR, cmnd, "unexpected status cmplt\n");
323                 goto out;
324         }
325
326         switch (iu->iu_id) {
327         case IU_ID_STATUS:
328                 if (urb->actual_length < 16)
329                         devinfo->uas_sense_old = 1;
330                 if (devinfo->uas_sense_old)
331                         uas_sense_old(urb, cmnd);
332                 else
333                         uas_sense(urb, cmnd);
334                 if (cmnd->result != 0) {
335                         /* cancel data transfers on error */
336                         data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
337                         data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
338                 }
339                 cmdinfo->state &= ~COMMAND_INFLIGHT;
340                 uas_try_complete(cmnd, __func__);
341                 break;
342         case IU_ID_READ_READY:
343                 if (!cmdinfo->data_in_urb ||
344                                 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
345                         scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
346                         break;
347                 }
348                 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
349                 break;
350         case IU_ID_WRITE_READY:
351                 if (!cmdinfo->data_out_urb ||
352                                 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
353                         scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
354                         break;
355                 }
356                 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
357                 break;
358         default:
359                 scmd_printk(KERN_ERR, cmnd,
360                         "Bogus IU (%d) received on status pipe\n", iu->iu_id);
361         }
362 out:
363         usb_free_urb(urb);
364         spin_unlock_irqrestore(&devinfo->lock, flags);
365
366         /* Unlinking of data urbs must be done without holding the lock */
367         if (data_in_urb) {
368                 usb_unlink_urb(data_in_urb);
369                 usb_put_urb(data_in_urb);
370         }
371         if (data_out_urb) {
372                 usb_unlink_urb(data_out_urb);
373                 usb_put_urb(data_out_urb);
374         }
375 }
376
377 static void uas_data_cmplt(struct urb *urb)
378 {
379         struct scsi_cmnd *cmnd = urb->context;
380         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
381         struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
382         struct scsi_data_buffer *sdb = NULL;
383         unsigned long flags;
384
385         spin_lock_irqsave(&devinfo->lock, flags);
386
387         if (cmdinfo->data_in_urb == urb) {
388                 sdb = scsi_in(cmnd);
389                 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
390                 cmdinfo->data_in_urb = NULL;
391         } else if (cmdinfo->data_out_urb == urb) {
392                 sdb = scsi_out(cmnd);
393                 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
394                 cmdinfo->data_out_urb = NULL;
395         }
396         if (sdb == NULL) {
397                 WARN_ON_ONCE(1);
398                 goto out;
399         }
400
401         if (devinfo->resetting)
402                 goto out;
403
404         /* Data urbs should not complete before the cmd urb is submitted */
405         if (cmdinfo->state & SUBMIT_CMD_URB) {
406                 scmd_printk(KERN_ERR, cmnd, "unexpected data cmplt\n");
407                 goto out;
408         }
409
410         if (urb->status) {
411                 if (urb->status != -ECONNRESET) {
412                         uas_log_cmd_state(cmnd, __func__);
413                         scmd_printk(KERN_ERR, cmnd,
414                                 "data cmplt err %d stream %d\n",
415                                 urb->status, urb->stream_id);
416                 }
417                 /* error: no data transfered */
418                 sdb->resid = sdb->length;
419         } else {
420                 sdb->resid = sdb->length - urb->actual_length;
421         }
422         uas_try_complete(cmnd, __func__);
423 out:
424         usb_free_urb(urb);
425         spin_unlock_irqrestore(&devinfo->lock, flags);
426 }
427
428 static void uas_cmd_cmplt(struct urb *urb)
429 {
430         if (urb->status)
431                 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
432
433         usb_free_urb(urb);
434 }
435
436 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
437                                       unsigned int pipe, u16 stream_id,
438                                       struct scsi_cmnd *cmnd,
439                                       enum dma_data_direction dir)
440 {
441         struct usb_device *udev = devinfo->udev;
442         struct urb *urb = usb_alloc_urb(0, gfp);
443         struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
444                 ? scsi_in(cmnd) : scsi_out(cmnd);
445
446         if (!urb)
447                 goto out;
448         usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
449                           uas_data_cmplt, cmnd);
450         urb->stream_id = stream_id;
451         urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
452         urb->sg = sdb->table.sgl;
453  out:
454         return urb;
455 }
456
457 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
458                                        struct Scsi_Host *shost, u16 stream_id)
459 {
460         struct usb_device *udev = devinfo->udev;
461         struct urb *urb = usb_alloc_urb(0, gfp);
462         struct sense_iu *iu;
463
464         if (!urb)
465                 goto out;
466
467         iu = kzalloc(sizeof(*iu), gfp);
468         if (!iu)
469                 goto free;
470
471         usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
472                                                 uas_stat_cmplt, shost);
473         urb->stream_id = stream_id;
474         urb->transfer_flags |= URB_FREE_BUFFER;
475  out:
476         return urb;
477  free:
478         usb_free_urb(urb);
479         return NULL;
480 }
481
482 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
483                                         struct scsi_cmnd *cmnd)
484 {
485         struct usb_device *udev = devinfo->udev;
486         struct scsi_device *sdev = cmnd->device;
487         struct urb *urb = usb_alloc_urb(0, gfp);
488         struct command_iu *iu;
489         int len;
490
491         if (!urb)
492                 goto out;
493
494         len = cmnd->cmd_len - 16;
495         if (len < 0)
496                 len = 0;
497         len = ALIGN(len, 4);
498         iu = kzalloc(sizeof(*iu) + len, gfp);
499         if (!iu)
500                 goto free;
501
502         iu->iu_id = IU_ID_COMMAND;
503         iu->tag = cpu_to_be16(uas_get_tag(cmnd));
504         iu->prio_attr = UAS_SIMPLE_TAG;
505         iu->len = len;
506         int_to_scsilun(sdev->lun, &iu->lun);
507         memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
508
509         usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
510                                                         uas_cmd_cmplt, NULL);
511         urb->transfer_flags |= URB_FREE_BUFFER;
512  out:
513         return urb;
514  free:
515         usb_free_urb(urb);
516         return NULL;
517 }
518
519 /*
520  * Why should I request the Status IU before sending the Command IU?  Spec
521  * says to, but also says the device may receive them in any order.  Seems
522  * daft to me.
523  */
524
525 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
526                                         gfp_t gfp, unsigned int stream)
527 {
528         struct Scsi_Host *shost = cmnd->device->host;
529         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
530         struct urb *urb;
531         int err;
532
533         urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
534         if (!urb)
535                 return NULL;
536         usb_anchor_urb(urb, &devinfo->sense_urbs);
537         err = usb_submit_urb(urb, gfp);
538         if (err) {
539                 usb_unanchor_urb(urb);
540                 uas_log_cmd_state(cmnd, __func__);
541                 shost_printk(KERN_INFO, shost,
542                              "sense urb submission error %d stream %d\n",
543                              err, stream);
544                 usb_free_urb(urb);
545                 return NULL;
546         }
547         return urb;
548 }
549
550 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
551                            struct uas_dev_info *devinfo, gfp_t gfp)
552 {
553         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
554         struct urb *urb;
555         int err;
556
557         lockdep_assert_held(&devinfo->lock);
558         if (cmdinfo->state & SUBMIT_STATUS_URB) {
559                 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
560                 if (!urb)
561                         return SCSI_MLQUEUE_DEVICE_BUSY;
562                 cmdinfo->state &= ~SUBMIT_STATUS_URB;
563         }
564
565         if (cmdinfo->state & ALLOC_DATA_IN_URB) {
566                 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
567                                         devinfo->data_in_pipe, cmdinfo->stream,
568                                         cmnd, DMA_FROM_DEVICE);
569                 if (!cmdinfo->data_in_urb)
570                         return SCSI_MLQUEUE_DEVICE_BUSY;
571                 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
572         }
573
574         if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
575                 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
576                 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
577                 if (err) {
578                         usb_unanchor_urb(cmdinfo->data_in_urb);
579                         uas_log_cmd_state(cmnd, __func__);
580                         scmd_printk(KERN_INFO, cmnd,
581                                 "data in urb submission error %d stream %d\n",
582                                 err, cmdinfo->data_in_urb->stream_id);
583                         return SCSI_MLQUEUE_DEVICE_BUSY;
584                 }
585                 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
586                 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
587         }
588
589         if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
590                 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
591                                         devinfo->data_out_pipe, cmdinfo->stream,
592                                         cmnd, DMA_TO_DEVICE);
593                 if (!cmdinfo->data_out_urb)
594                         return SCSI_MLQUEUE_DEVICE_BUSY;
595                 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
596         }
597
598         if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
599                 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
600                 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
601                 if (err) {
602                         usb_unanchor_urb(cmdinfo->data_out_urb);
603                         uas_log_cmd_state(cmnd, __func__);
604                         scmd_printk(KERN_INFO, cmnd,
605                                 "data out urb submission error %d stream %d\n",
606                                 err, cmdinfo->data_out_urb->stream_id);
607                         return SCSI_MLQUEUE_DEVICE_BUSY;
608                 }
609                 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
610                 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
611         }
612
613         if (cmdinfo->state & ALLOC_CMD_URB) {
614                 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
615                 if (!cmdinfo->cmd_urb)
616                         return SCSI_MLQUEUE_DEVICE_BUSY;
617                 cmdinfo->state &= ~ALLOC_CMD_URB;
618         }
619
620         if (cmdinfo->state & SUBMIT_CMD_URB) {
621                 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
622                 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
623                 if (err) {
624                         usb_unanchor_urb(cmdinfo->cmd_urb);
625                         uas_log_cmd_state(cmnd, __func__);
626                         scmd_printk(KERN_INFO, cmnd,
627                                     "cmd urb submission error %d\n", err);
628                         return SCSI_MLQUEUE_DEVICE_BUSY;
629                 }
630                 cmdinfo->cmd_urb = NULL;
631                 cmdinfo->state &= ~SUBMIT_CMD_URB;
632                 cmdinfo->state |= COMMAND_INFLIGHT;
633         }
634
635         return 0;
636 }
637
638 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
639                                         void (*done)(struct scsi_cmnd *))
640 {
641         struct scsi_device *sdev = cmnd->device;
642         struct uas_dev_info *devinfo = sdev->hostdata;
643         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
644         unsigned long flags;
645         unsigned int stream;
646         int err;
647
648         BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
649
650         if ((devinfo->flags & US_FL_NO_ATA_1X) &&
651                         (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
652                 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
653                        sizeof(usb_stor_sense_invalidCDB));
654                 cmnd->result = SAM_STAT_CHECK_CONDITION;
655                 cmnd->scsi_done(cmnd);
656                 return 0;
657         }
658
659         spin_lock_irqsave(&devinfo->lock, flags);
660
661         if (devinfo->resetting) {
662                 cmnd->result = DID_ERROR << 16;
663                 cmnd->scsi_done(cmnd);
664                 spin_unlock_irqrestore(&devinfo->lock, flags);
665                 return 0;
666         }
667
668         stream = uas_get_tag(cmnd);
669         if (devinfo->cmnd[stream - 1]) {
670                 spin_unlock_irqrestore(&devinfo->lock, flags);
671                 return SCSI_MLQUEUE_DEVICE_BUSY;
672         }
673
674         cmnd->scsi_done = done;
675
676         memset(cmdinfo, 0, sizeof(*cmdinfo));
677         cmdinfo->stream = stream;
678         cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
679
680         switch (cmnd->sc_data_direction) {
681         case DMA_FROM_DEVICE:
682                 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
683                 break;
684         case DMA_BIDIRECTIONAL:
685                 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
686         case DMA_TO_DEVICE:
687                 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
688         case DMA_NONE:
689                 break;
690         }
691
692         if (!devinfo->use_streams) {
693                 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
694                 cmdinfo->stream = 0;
695         }
696
697         err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
698         if (err) {
699                 /* If we did nothing, give up now */
700                 if (cmdinfo->state & SUBMIT_STATUS_URB) {
701                         spin_unlock_irqrestore(&devinfo->lock, flags);
702                         return SCSI_MLQUEUE_DEVICE_BUSY;
703                 }
704                 uas_add_work(cmdinfo);
705         }
706
707         devinfo->cmnd[stream - 1] = cmnd;
708         spin_unlock_irqrestore(&devinfo->lock, flags);
709         return 0;
710 }
711
712 static DEF_SCSI_QCMD(uas_queuecommand)
713
714 /*
715  * For now we do not support actually sending an abort to the device, so
716  * this eh always fails. Still we must define it to make sure that we've
717  * dropped all references to the cmnd in question once this function exits.
718  */
719 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
720 {
721         struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
722         struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
723         struct urb *data_in_urb = NULL;
724         struct urb *data_out_urb = NULL;
725         unsigned long flags;
726
727         spin_lock_irqsave(&devinfo->lock, flags);
728
729         uas_log_cmd_state(cmnd, __func__);
730
731         /* Ensure that try_complete does not call scsi_done */
732         cmdinfo->state |= COMMAND_ABORTED;
733
734         /* Drop all refs to this cmnd, kill data urbs to break their ref */
735         devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
736         if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
737                 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
738         if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
739                 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
740
741         spin_unlock_irqrestore(&devinfo->lock, flags);
742
743         if (data_in_urb) {
744                 usb_kill_urb(data_in_urb);
745                 usb_put_urb(data_in_urb);
746         }
747         if (data_out_urb) {
748                 usb_kill_urb(data_out_urb);
749                 usb_put_urb(data_out_urb);
750         }
751
752         return FAILED;
753 }
754
755 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
756 {
757         struct scsi_device *sdev = cmnd->device;
758         struct uas_dev_info *devinfo = sdev->hostdata;
759         struct usb_device *udev = devinfo->udev;
760         unsigned long flags;
761         int err;
762
763         err = usb_lock_device_for_reset(udev, devinfo->intf);
764         if (err) {
765                 shost_printk(KERN_ERR, sdev->host,
766                              "%s FAILED to get lock err %d\n", __func__, err);
767                 return FAILED;
768         }
769
770         shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
771
772         spin_lock_irqsave(&devinfo->lock, flags);
773         devinfo->resetting = 1;
774         spin_unlock_irqrestore(&devinfo->lock, flags);
775
776         usb_kill_anchored_urbs(&devinfo->cmd_urbs);
777         usb_kill_anchored_urbs(&devinfo->sense_urbs);
778         usb_kill_anchored_urbs(&devinfo->data_urbs);
779         uas_zap_pending(devinfo, DID_RESET);
780
781         err = usb_reset_device(udev);
782
783         spin_lock_irqsave(&devinfo->lock, flags);
784         devinfo->resetting = 0;
785         spin_unlock_irqrestore(&devinfo->lock, flags);
786
787         usb_unlock_device(udev);
788
789         if (err) {
790                 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
791                 return FAILED;
792         }
793
794         shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
795         return SUCCESS;
796 }
797
798 static int uas_slave_alloc(struct scsi_device *sdev)
799 {
800         sdev->hostdata = (void *)sdev->host->hostdata;
801
802         /* USB has unusual DMA-alignment requirements: Although the
803          * starting address of each scatter-gather element doesn't matter,
804          * the length of each element except the last must be divisible
805          * by the Bulk maxpacket value.  There's currently no way to
806          * express this by block-layer constraints, so we'll cop out
807          * and simply require addresses to be aligned at 512-byte
808          * boundaries.  This is okay since most block I/O involves
809          * hardware sectors that are multiples of 512 bytes in length,
810          * and since host controllers up through USB 2.0 have maxpacket
811          * values no larger than 512.
812          *
813          * But it doesn't suffice for Wireless USB, where Bulk maxpacket
814          * values can be as large as 2048.  To make that work properly
815          * will require changes to the block layer.
816          */
817         blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
818
819         return 0;
820 }
821
822 static int uas_slave_configure(struct scsi_device *sdev)
823 {
824         struct uas_dev_info *devinfo = sdev->hostdata;
825
826         if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
827                 sdev->no_report_opcodes = 1;
828
829         scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
830         scsi_activate_tcq(sdev, devinfo->qdepth - 2);
831         return 0;
832 }
833
834 static struct scsi_host_template uas_host_template = {
835         .module = THIS_MODULE,
836         .name = "uas",
837         .queuecommand = uas_queuecommand,
838         .slave_alloc = uas_slave_alloc,
839         .slave_configure = uas_slave_configure,
840         .eh_abort_handler = uas_eh_abort_handler,
841         .eh_bus_reset_handler = uas_eh_bus_reset_handler,
842         .can_queue = 65536,     /* Is there a limit on the _host_ ? */
843         .this_id = -1,
844         .sg_tablesize = SG_NONE,
845         .cmd_per_lun = 1,       /* until we override it */
846         .skip_settle_delay = 1,
847         .ordered_tag = 1,
848 };
849
850 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
851                     vendorName, productName, useProtocol, useTransport, \
852                     initFunction, flags) \
853 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
854         .driver_info = (flags) }
855
856 static struct usb_device_id uas_usb_ids[] = {
857 #       include "unusual_uas.h"
858         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
859         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
860         /* 0xaa is a prototype device I happen to have access to */
861         { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
862         { }
863 };
864 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
865
866 #undef UNUSUAL_DEV
867
868 static int uas_switch_interface(struct usb_device *udev,
869                                 struct usb_interface *intf)
870 {
871         int alt;
872
873         alt = uas_find_uas_alt_setting(intf);
874         if (alt < 0)
875                 return alt;
876
877         return usb_set_interface(udev,
878                         intf->altsetting[0].desc.bInterfaceNumber, alt);
879 }
880
881 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
882 {
883         struct usb_host_endpoint *eps[4] = { };
884         struct usb_device *udev = devinfo->udev;
885         int r;
886
887         devinfo->uas_sense_old = 0;
888
889         r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
890         if (r)
891                 return r;
892
893         devinfo->cmd_pipe = usb_sndbulkpipe(udev,
894                                             usb_endpoint_num(&eps[0]->desc));
895         devinfo->status_pipe = usb_rcvbulkpipe(udev,
896                                             usb_endpoint_num(&eps[1]->desc));
897         devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
898                                             usb_endpoint_num(&eps[2]->desc));
899         devinfo->data_out_pipe = usb_sndbulkpipe(udev,
900                                             usb_endpoint_num(&eps[3]->desc));
901
902         if (udev->speed != USB_SPEED_SUPER) {
903                 devinfo->qdepth = 32;
904                 devinfo->use_streams = 0;
905         } else {
906                 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
907                                                     3, MAX_CMNDS, GFP_NOIO);
908                 if (devinfo->qdepth < 0)
909                         return devinfo->qdepth;
910                 devinfo->use_streams = 1;
911         }
912
913         return 0;
914 }
915
916 static void uas_free_streams(struct uas_dev_info *devinfo)
917 {
918         struct usb_device *udev = devinfo->udev;
919         struct usb_host_endpoint *eps[3];
920
921         eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
922         eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
923         eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
924         usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
925 }
926
927 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
928 {
929         int result = -ENOMEM;
930         struct Scsi_Host *shost = NULL;
931         struct uas_dev_info *devinfo;
932         struct usb_device *udev = interface_to_usbdev(intf);
933
934         if (!uas_use_uas_driver(intf, id))
935                 return -ENODEV;
936
937         if (uas_switch_interface(udev, intf))
938                 return -ENODEV;
939
940         shost = scsi_host_alloc(&uas_host_template,
941                                 sizeof(struct uas_dev_info));
942         if (!shost)
943                 goto set_alt0;
944
945         shost->max_cmd_len = 16 + 252;
946         shost->max_id = 1;
947         shost->max_lun = 256;
948         shost->max_channel = 0;
949         shost->sg_tablesize = udev->bus->sg_tablesize;
950
951         devinfo = (struct uas_dev_info *)shost->hostdata;
952         devinfo->intf = intf;
953         devinfo->udev = udev;
954         devinfo->resetting = 0;
955         devinfo->shutdown = 0;
956         devinfo->flags = id->driver_info;
957         usb_stor_adjust_quirks(udev, &devinfo->flags);
958         init_usb_anchor(&devinfo->cmd_urbs);
959         init_usb_anchor(&devinfo->sense_urbs);
960         init_usb_anchor(&devinfo->data_urbs);
961         spin_lock_init(&devinfo->lock);
962         INIT_WORK(&devinfo->work, uas_do_work);
963
964         result = uas_configure_endpoints(devinfo);
965         if (result)
966                 goto set_alt0;
967
968         result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
969         if (result)
970                 goto free_streams;
971
972         usb_set_intfdata(intf, shost);
973         result = scsi_add_host(shost, &intf->dev);
974         if (result)
975                 goto free_streams;
976
977         scsi_scan_host(shost);
978         return result;
979
980 free_streams:
981         uas_free_streams(devinfo);
982         usb_set_intfdata(intf, NULL);
983 set_alt0:
984         usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
985         if (shost)
986                 scsi_host_put(shost);
987         return result;
988 }
989
990 static int uas_pre_reset(struct usb_interface *intf)
991 {
992         struct Scsi_Host *shost = usb_get_intfdata(intf);
993         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
994         unsigned long flags;
995
996         if (devinfo->shutdown)
997                 return 0;
998
999         /* Block new requests */
1000         spin_lock_irqsave(shost->host_lock, flags);
1001         scsi_block_requests(shost);
1002         spin_unlock_irqrestore(shost->host_lock, flags);
1003
1004         /* Wait for any pending requests to complete */
1005         flush_work(&devinfo->work);
1006         if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1007                 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1008                 return 1;
1009         }
1010
1011         uas_free_streams(devinfo);
1012
1013         return 0;
1014 }
1015
1016 static int uas_post_reset(struct usb_interface *intf)
1017 {
1018         struct Scsi_Host *shost = usb_get_intfdata(intf);
1019         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1020         unsigned long flags;
1021
1022         if (devinfo->shutdown)
1023                 return 0;
1024
1025         if (uas_configure_endpoints(devinfo) != 0) {
1026                 shost_printk(KERN_ERR, shost,
1027                              "%s: alloc streams error after reset", __func__);
1028                 return 1;
1029         }
1030
1031         spin_lock_irqsave(shost->host_lock, flags);
1032         scsi_report_bus_reset(shost, 0);
1033         spin_unlock_irqrestore(shost->host_lock, flags);
1034
1035         scsi_unblock_requests(shost);
1036
1037         return 0;
1038 }
1039
1040 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1041 {
1042         struct Scsi_Host *shost = usb_get_intfdata(intf);
1043         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1044
1045         /* Wait for any pending requests to complete */
1046         flush_work(&devinfo->work);
1047         if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1048                 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1049                 return -ETIME;
1050         }
1051
1052         return 0;
1053 }
1054
1055 static int uas_resume(struct usb_interface *intf)
1056 {
1057         return 0;
1058 }
1059
1060 static int uas_reset_resume(struct usb_interface *intf)
1061 {
1062         struct Scsi_Host *shost = usb_get_intfdata(intf);
1063         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1064         unsigned long flags;
1065
1066         if (uas_configure_endpoints(devinfo) != 0) {
1067                 shost_printk(KERN_ERR, shost,
1068                              "%s: alloc streams error after reset", __func__);
1069                 return -EIO;
1070         }
1071
1072         spin_lock_irqsave(shost->host_lock, flags);
1073         scsi_report_bus_reset(shost, 0);
1074         spin_unlock_irqrestore(shost->host_lock, flags);
1075
1076         return 0;
1077 }
1078
1079 static void uas_disconnect(struct usb_interface *intf)
1080 {
1081         struct Scsi_Host *shost = usb_get_intfdata(intf);
1082         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1083         unsigned long flags;
1084
1085         spin_lock_irqsave(&devinfo->lock, flags);
1086         devinfo->resetting = 1;
1087         spin_unlock_irqrestore(&devinfo->lock, flags);
1088
1089         cancel_work_sync(&devinfo->work);
1090         usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1091         usb_kill_anchored_urbs(&devinfo->sense_urbs);
1092         usb_kill_anchored_urbs(&devinfo->data_urbs);
1093         uas_zap_pending(devinfo, DID_NO_CONNECT);
1094
1095         scsi_remove_host(shost);
1096         uas_free_streams(devinfo);
1097         scsi_host_put(shost);
1098 }
1099
1100 /*
1101  * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1102  * hang on reboot when the device is still in uas mode. Note the reset is
1103  * necessary as some devices won't revert to usb-storage mode without it.
1104  */
1105 static void uas_shutdown(struct device *dev)
1106 {
1107         struct usb_interface *intf = to_usb_interface(dev);
1108         struct usb_device *udev = interface_to_usbdev(intf);
1109         struct Scsi_Host *shost = usb_get_intfdata(intf);
1110         struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1111
1112         if (system_state != SYSTEM_RESTART)
1113                 return;
1114
1115         devinfo->shutdown = 1;
1116         uas_free_streams(devinfo);
1117         usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1118         usb_reset_device(udev);
1119 }
1120
1121 static struct usb_driver uas_driver = {
1122         .name = "uas",
1123         .probe = uas_probe,
1124         .disconnect = uas_disconnect,
1125         .pre_reset = uas_pre_reset,
1126         .post_reset = uas_post_reset,
1127         .suspend = uas_suspend,
1128         .resume = uas_resume,
1129         .reset_resume = uas_reset_resume,
1130         .drvwrap.driver.shutdown = uas_shutdown,
1131         .id_table = uas_usb_ids,
1132 };
1133
1134 module_usb_driver(uas_driver);
1135
1136 MODULE_LICENSE("GPL");
1137 MODULE_AUTHOR(
1138         "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");