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