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