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