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