3 * Note that this is not the same as the USB Mass Storage driver
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
9 * Distributed under the terms of the GNU GPL, version two.
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>
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>
30 #include "uas-detect.h"
36 struct usb_interface
*intf
;
37 struct usb_device
*udev
;
38 struct usb_anchor cmd_urbs
;
39 struct usb_anchor sense_urbs
;
40 struct usb_anchor data_urbs
;
42 int qdepth
, resetting
;
43 unsigned cmd_pipe
, status_pipe
, data_in_pipe
, data_out_pipe
;
44 unsigned use_streams
:1;
46 struct scsi_cmnd
*cmnd
[MAX_CMNDS
];
48 struct work_struct work
;
52 SUBMIT_STATUS_URB
= (1 << 1),
53 ALLOC_DATA_IN_URB
= (1 << 2),
54 SUBMIT_DATA_IN_URB
= (1 << 3),
55 ALLOC_DATA_OUT_URB
= (1 << 4),
56 SUBMIT_DATA_OUT_URB
= (1 << 5),
57 ALLOC_CMD_URB
= (1 << 6),
58 SUBMIT_CMD_URB
= (1 << 7),
59 COMMAND_INFLIGHT
= (1 << 8),
60 DATA_IN_URB_INFLIGHT
= (1 << 9),
61 DATA_OUT_URB_INFLIGHT
= (1 << 10),
62 COMMAND_ABORTED
= (1 << 11),
63 IS_IN_WORK_LIST
= (1 << 12),
66 /* Overrides scsi_pointer */
71 struct urb
*data_in_urb
;
72 struct urb
*data_out_urb
;
75 /* I hate forward declarations, but I actually have a loop */
76 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
77 struct uas_dev_info
*devinfo
, gfp_t gfp
);
78 static void uas_do_work(struct work_struct
*work
);
79 static int uas_try_complete(struct scsi_cmnd
*cmnd
, const char *caller
);
80 static void uas_free_streams(struct uas_dev_info
*devinfo
);
81 static void uas_log_cmd_state(struct scsi_cmnd
*cmnd
, const char *prefix
,
84 static void uas_do_work(struct work_struct
*work
)
86 struct uas_dev_info
*devinfo
=
87 container_of(work
, struct uas_dev_info
, work
);
88 struct uas_cmd_info
*cmdinfo
;
89 struct scsi_cmnd
*cmnd
;
93 spin_lock_irqsave(&devinfo
->lock
, flags
);
95 if (devinfo
->resetting
)
98 for (i
= 0; i
< devinfo
->qdepth
; i
++) {
99 if (!devinfo
->cmnd
[i
])
102 cmnd
= devinfo
->cmnd
[i
];
103 cmdinfo
= (void *)&cmnd
->SCp
;
105 if (!(cmdinfo
->state
& IS_IN_WORK_LIST
))
108 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
110 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
112 schedule_work(&devinfo
->work
);
115 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
118 static void uas_add_work(struct uas_cmd_info
*cmdinfo
)
120 struct scsi_pointer
*scp
= (void *)cmdinfo
;
121 struct scsi_cmnd
*cmnd
= container_of(scp
, struct scsi_cmnd
, SCp
);
122 struct uas_dev_info
*devinfo
= cmnd
->device
->hostdata
;
124 lockdep_assert_held(&devinfo
->lock
);
125 cmdinfo
->state
|= IS_IN_WORK_LIST
;
126 schedule_work(&devinfo
->work
);
129 static void uas_zap_pending(struct uas_dev_info
*devinfo
, int result
)
131 struct uas_cmd_info
*cmdinfo
;
132 struct scsi_cmnd
*cmnd
;
136 spin_lock_irqsave(&devinfo
->lock
, flags
);
137 for (i
= 0; i
< devinfo
->qdepth
; i
++) {
138 if (!devinfo
->cmnd
[i
])
141 cmnd
= devinfo
->cmnd
[i
];
142 cmdinfo
= (void *)&cmnd
->SCp
;
143 uas_log_cmd_state(cmnd
, __func__
, 0);
144 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
145 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
146 cmnd
->result
= result
<< 16;
147 err
= uas_try_complete(cmnd
, __func__
);
150 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
153 static void uas_sense(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
155 struct sense_iu
*sense_iu
= urb
->transfer_buffer
;
156 struct scsi_device
*sdev
= cmnd
->device
;
158 if (urb
->actual_length
> 16) {
159 unsigned len
= be16_to_cpup(&sense_iu
->len
);
160 if (len
+ 16 != urb
->actual_length
) {
161 int newlen
= min(len
+ 16, urb
->actual_length
) - 16;
164 sdev_printk(KERN_INFO
, sdev
, "%s: urb length %d "
165 "disagrees with IU sense data length %d, "
166 "using %d bytes of sense data\n", __func__
,
167 urb
->actual_length
, len
, newlen
);
170 memcpy(cmnd
->sense_buffer
, sense_iu
->sense
, len
);
173 cmnd
->result
= sense_iu
->status
;
176 static void uas_log_cmd_state(struct scsi_cmnd
*cmnd
, const char *prefix
,
179 struct uas_cmd_info
*ci
= (void *)&cmnd
->SCp
;
180 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
182 scmd_printk(KERN_INFO
, cmnd
,
183 "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
184 prefix
, status
, cmdinfo
->uas_tag
,
185 (ci
->state
& SUBMIT_STATUS_URB
) ? " s-st" : "",
186 (ci
->state
& ALLOC_DATA_IN_URB
) ? " a-in" : "",
187 (ci
->state
& SUBMIT_DATA_IN_URB
) ? " s-in" : "",
188 (ci
->state
& ALLOC_DATA_OUT_URB
) ? " a-out" : "",
189 (ci
->state
& SUBMIT_DATA_OUT_URB
) ? " s-out" : "",
190 (ci
->state
& ALLOC_CMD_URB
) ? " a-cmd" : "",
191 (ci
->state
& SUBMIT_CMD_URB
) ? " s-cmd" : "",
192 (ci
->state
& COMMAND_INFLIGHT
) ? " CMD" : "",
193 (ci
->state
& DATA_IN_URB_INFLIGHT
) ? " IN" : "",
194 (ci
->state
& DATA_OUT_URB_INFLIGHT
) ? " OUT" : "",
195 (ci
->state
& COMMAND_ABORTED
) ? " abort" : "",
196 (ci
->state
& IS_IN_WORK_LIST
) ? " work" : "");
197 scsi_print_command(cmnd
);
200 static void uas_free_unsubmitted_urbs(struct scsi_cmnd
*cmnd
)
202 struct uas_cmd_info
*cmdinfo
;
207 cmdinfo
= (void *)&cmnd
->SCp
;
209 if (cmdinfo
->state
& SUBMIT_CMD_URB
)
210 usb_free_urb(cmdinfo
->cmd_urb
);
212 /* data urbs may have never gotten their submit flag set */
213 if (!(cmdinfo
->state
& DATA_IN_URB_INFLIGHT
))
214 usb_free_urb(cmdinfo
->data_in_urb
);
215 if (!(cmdinfo
->state
& DATA_OUT_URB_INFLIGHT
))
216 usb_free_urb(cmdinfo
->data_out_urb
);
219 static int uas_try_complete(struct scsi_cmnd
*cmnd
, const char *caller
)
221 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
222 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
224 lockdep_assert_held(&devinfo
->lock
);
225 if (cmdinfo
->state
& (COMMAND_INFLIGHT
|
226 DATA_IN_URB_INFLIGHT
|
227 DATA_OUT_URB_INFLIGHT
|
230 devinfo
->cmnd
[cmdinfo
->uas_tag
- 1] = NULL
;
231 uas_free_unsubmitted_urbs(cmnd
);
232 cmnd
->scsi_done(cmnd
);
236 static void uas_xfer_data(struct urb
*urb
, struct scsi_cmnd
*cmnd
,
239 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
242 cmdinfo
->state
|= direction
| SUBMIT_STATUS_URB
;
243 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
245 uas_add_work(cmdinfo
);
249 static void uas_stat_cmplt(struct urb
*urb
)
251 struct iu
*iu
= urb
->transfer_buffer
;
252 struct Scsi_Host
*shost
= urb
->context
;
253 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
254 struct urb
*data_in_urb
= NULL
;
255 struct urb
*data_out_urb
= NULL
;
256 struct scsi_cmnd
*cmnd
;
257 struct uas_cmd_info
*cmdinfo
;
261 spin_lock_irqsave(&devinfo
->lock
, flags
);
263 if (devinfo
->resetting
)
267 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
) {
268 dev_err(&urb
->dev
->dev
, "stat urb: status %d\n",
274 idx
= be16_to_cpup(&iu
->tag
) - 1;
275 if (idx
>= MAX_CMNDS
|| !devinfo
->cmnd
[idx
]) {
276 dev_err(&urb
->dev
->dev
,
277 "stat urb: no pending cmd for uas-tag %d\n", idx
+ 1);
281 cmnd
= devinfo
->cmnd
[idx
];
282 cmdinfo
= (void *)&cmnd
->SCp
;
284 if (!(cmdinfo
->state
& COMMAND_INFLIGHT
)) {
285 uas_log_cmd_state(cmnd
, "unexpected status cmplt", 0);
291 uas_sense(urb
, cmnd
);
292 if (cmnd
->result
!= 0) {
293 /* cancel data transfers on error */
294 data_in_urb
= usb_get_urb(cmdinfo
->data_in_urb
);
295 data_out_urb
= usb_get_urb(cmdinfo
->data_out_urb
);
297 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
298 uas_try_complete(cmnd
, __func__
);
300 case IU_ID_READ_READY
:
301 if (!cmdinfo
->data_in_urb
||
302 (cmdinfo
->state
& DATA_IN_URB_INFLIGHT
)) {
303 uas_log_cmd_state(cmnd
, "unexpected read rdy", 0);
306 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_IN_URB
);
308 case IU_ID_WRITE_READY
:
309 if (!cmdinfo
->data_out_urb
||
310 (cmdinfo
->state
& DATA_OUT_URB_INFLIGHT
)) {
311 uas_log_cmd_state(cmnd
, "unexpected write rdy", 0);
314 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_OUT_URB
);
317 uas_log_cmd_state(cmnd
, "unexpected response iu",
318 ((struct response_iu
*)iu
)->response_code
);
319 /* Error, cancel data transfers */
320 data_in_urb
= usb_get_urb(cmdinfo
->data_in_urb
);
321 data_out_urb
= usb_get_urb(cmdinfo
->data_out_urb
);
322 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
323 cmnd
->result
= DID_ERROR
<< 16;
324 uas_try_complete(cmnd
, __func__
);
327 uas_log_cmd_state(cmnd
, "bogus IU", iu
->iu_id
);
331 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
333 /* Unlinking of data urbs must be done without holding the lock */
335 usb_unlink_urb(data_in_urb
);
336 usb_put_urb(data_in_urb
);
339 usb_unlink_urb(data_out_urb
);
340 usb_put_urb(data_out_urb
);
344 static void uas_data_cmplt(struct urb
*urb
)
346 struct scsi_cmnd
*cmnd
= urb
->context
;
347 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
348 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
349 struct scsi_data_buffer
*sdb
= NULL
;
352 spin_lock_irqsave(&devinfo
->lock
, flags
);
354 if (cmdinfo
->data_in_urb
== urb
) {
356 cmdinfo
->state
&= ~DATA_IN_URB_INFLIGHT
;
357 cmdinfo
->data_in_urb
= NULL
;
358 } else if (cmdinfo
->data_out_urb
== urb
) {
359 sdb
= scsi_out(cmnd
);
360 cmdinfo
->state
&= ~DATA_OUT_URB_INFLIGHT
;
361 cmdinfo
->data_out_urb
= NULL
;
368 if (devinfo
->resetting
)
371 /* Data urbs should not complete before the cmd urb is submitted */
372 if (cmdinfo
->state
& SUBMIT_CMD_URB
) {
373 uas_log_cmd_state(cmnd
, "unexpected data cmplt", 0);
378 if (urb
->status
!= -ENOENT
&& urb
->status
!= -ECONNRESET
)
379 uas_log_cmd_state(cmnd
, "data cmplt err", urb
->status
);
380 /* error: no data transfered */
381 sdb
->resid
= sdb
->length
;
383 sdb
->resid
= sdb
->length
- urb
->actual_length
;
385 uas_try_complete(cmnd
, __func__
);
388 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
391 static void uas_cmd_cmplt(struct urb
*urb
)
394 dev_err(&urb
->dev
->dev
, "cmd cmplt err %d\n", urb
->status
);
399 static struct urb
*uas_alloc_data_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
400 struct scsi_cmnd
*cmnd
,
401 enum dma_data_direction dir
)
403 struct usb_device
*udev
= devinfo
->udev
;
404 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
405 struct urb
*urb
= usb_alloc_urb(0, gfp
);
406 struct scsi_data_buffer
*sdb
= (dir
== DMA_FROM_DEVICE
)
407 ? scsi_in(cmnd
) : scsi_out(cmnd
);
408 unsigned int pipe
= (dir
== DMA_FROM_DEVICE
)
409 ? devinfo
->data_in_pipe
: devinfo
->data_out_pipe
;
413 usb_fill_bulk_urb(urb
, udev
, pipe
, NULL
, sdb
->length
,
414 uas_data_cmplt
, cmnd
);
415 if (devinfo
->use_streams
)
416 urb
->stream_id
= cmdinfo
->uas_tag
;
417 urb
->num_sgs
= udev
->bus
->sg_tablesize
? sdb
->table
.nents
: 0;
418 urb
->sg
= sdb
->table
.sgl
;
423 static struct urb
*uas_alloc_sense_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
424 struct scsi_cmnd
*cmnd
)
426 struct usb_device
*udev
= devinfo
->udev
;
427 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
428 struct urb
*urb
= usb_alloc_urb(0, gfp
);
434 iu
= kzalloc(sizeof(*iu
), gfp
);
438 usb_fill_bulk_urb(urb
, udev
, devinfo
->status_pipe
, iu
, sizeof(*iu
),
439 uas_stat_cmplt
, cmnd
->device
->host
);
440 if (devinfo
->use_streams
)
441 urb
->stream_id
= cmdinfo
->uas_tag
;
442 urb
->transfer_flags
|= URB_FREE_BUFFER
;
450 static struct urb
*uas_alloc_cmd_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
451 struct scsi_cmnd
*cmnd
)
453 struct usb_device
*udev
= devinfo
->udev
;
454 struct scsi_device
*sdev
= cmnd
->device
;
455 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
456 struct urb
*urb
= usb_alloc_urb(0, gfp
);
457 struct command_iu
*iu
;
463 len
= cmnd
->cmd_len
- 16;
467 iu
= kzalloc(sizeof(*iu
) + len
, gfp
);
471 iu
->iu_id
= IU_ID_COMMAND
;
472 iu
->tag
= cpu_to_be16(cmdinfo
->uas_tag
);
473 iu
->prio_attr
= UAS_SIMPLE_TAG
;
475 int_to_scsilun(sdev
->lun
, &iu
->lun
);
476 memcpy(iu
->cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
478 usb_fill_bulk_urb(urb
, udev
, devinfo
->cmd_pipe
, iu
, sizeof(*iu
) + len
,
479 uas_cmd_cmplt
, NULL
);
480 urb
->transfer_flags
|= URB_FREE_BUFFER
;
489 * Why should I request the Status IU before sending the Command IU? Spec
490 * says to, but also says the device may receive them in any order. Seems
494 static struct urb
*uas_submit_sense_urb(struct scsi_cmnd
*cmnd
, gfp_t gfp
)
496 struct uas_dev_info
*devinfo
= cmnd
->device
->hostdata
;
500 urb
= uas_alloc_sense_urb(devinfo
, gfp
, cmnd
);
503 usb_anchor_urb(urb
, &devinfo
->sense_urbs
);
504 err
= usb_submit_urb(urb
, gfp
);
506 usb_unanchor_urb(urb
);
507 uas_log_cmd_state(cmnd
, "sense submit err", err
);
514 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
515 struct uas_dev_info
*devinfo
, gfp_t gfp
)
517 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
521 lockdep_assert_held(&devinfo
->lock
);
522 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
523 urb
= uas_submit_sense_urb(cmnd
, gfp
);
525 return SCSI_MLQUEUE_DEVICE_BUSY
;
526 cmdinfo
->state
&= ~SUBMIT_STATUS_URB
;
529 if (cmdinfo
->state
& ALLOC_DATA_IN_URB
) {
530 cmdinfo
->data_in_urb
= uas_alloc_data_urb(devinfo
, gfp
,
531 cmnd
, DMA_FROM_DEVICE
);
532 if (!cmdinfo
->data_in_urb
)
533 return SCSI_MLQUEUE_DEVICE_BUSY
;
534 cmdinfo
->state
&= ~ALLOC_DATA_IN_URB
;
537 if (cmdinfo
->state
& SUBMIT_DATA_IN_URB
) {
538 usb_anchor_urb(cmdinfo
->data_in_urb
, &devinfo
->data_urbs
);
539 err
= usb_submit_urb(cmdinfo
->data_in_urb
, gfp
);
541 usb_unanchor_urb(cmdinfo
->data_in_urb
);
542 uas_log_cmd_state(cmnd
, "data in submit err", err
);
543 return SCSI_MLQUEUE_DEVICE_BUSY
;
545 cmdinfo
->state
&= ~SUBMIT_DATA_IN_URB
;
546 cmdinfo
->state
|= DATA_IN_URB_INFLIGHT
;
549 if (cmdinfo
->state
& ALLOC_DATA_OUT_URB
) {
550 cmdinfo
->data_out_urb
= uas_alloc_data_urb(devinfo
, gfp
,
551 cmnd
, DMA_TO_DEVICE
);
552 if (!cmdinfo
->data_out_urb
)
553 return SCSI_MLQUEUE_DEVICE_BUSY
;
554 cmdinfo
->state
&= ~ALLOC_DATA_OUT_URB
;
557 if (cmdinfo
->state
& SUBMIT_DATA_OUT_URB
) {
558 usb_anchor_urb(cmdinfo
->data_out_urb
, &devinfo
->data_urbs
);
559 err
= usb_submit_urb(cmdinfo
->data_out_urb
, gfp
);
561 usb_unanchor_urb(cmdinfo
->data_out_urb
);
562 uas_log_cmd_state(cmnd
, "data out submit err", err
);
563 return SCSI_MLQUEUE_DEVICE_BUSY
;
565 cmdinfo
->state
&= ~SUBMIT_DATA_OUT_URB
;
566 cmdinfo
->state
|= DATA_OUT_URB_INFLIGHT
;
569 if (cmdinfo
->state
& ALLOC_CMD_URB
) {
570 cmdinfo
->cmd_urb
= uas_alloc_cmd_urb(devinfo
, gfp
, cmnd
);
571 if (!cmdinfo
->cmd_urb
)
572 return SCSI_MLQUEUE_DEVICE_BUSY
;
573 cmdinfo
->state
&= ~ALLOC_CMD_URB
;
576 if (cmdinfo
->state
& SUBMIT_CMD_URB
) {
577 usb_anchor_urb(cmdinfo
->cmd_urb
, &devinfo
->cmd_urbs
);
578 err
= usb_submit_urb(cmdinfo
->cmd_urb
, gfp
);
580 usb_unanchor_urb(cmdinfo
->cmd_urb
);
581 uas_log_cmd_state(cmnd
, "cmd submit err", err
);
582 return SCSI_MLQUEUE_DEVICE_BUSY
;
584 cmdinfo
->cmd_urb
= NULL
;
585 cmdinfo
->state
&= ~SUBMIT_CMD_URB
;
586 cmdinfo
->state
|= COMMAND_INFLIGHT
;
592 static int uas_queuecommand_lck(struct scsi_cmnd
*cmnd
,
593 void (*done
)(struct scsi_cmnd
*))
595 struct scsi_device
*sdev
= cmnd
->device
;
596 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
597 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
601 BUILD_BUG_ON(sizeof(struct uas_cmd_info
) > sizeof(struct scsi_pointer
));
603 /* Re-check scsi_block_requests now that we've the host-lock */
604 if (cmnd
->device
->host
->host_self_blocked
)
605 return SCSI_MLQUEUE_DEVICE_BUSY
;
607 if ((devinfo
->flags
& US_FL_NO_ATA_1X
) &&
608 (cmnd
->cmnd
[0] == ATA_12
|| cmnd
->cmnd
[0] == ATA_16
)) {
609 memcpy(cmnd
->sense_buffer
, usb_stor_sense_invalidCDB
,
610 sizeof(usb_stor_sense_invalidCDB
));
611 cmnd
->result
= SAM_STAT_CHECK_CONDITION
;
612 cmnd
->scsi_done(cmnd
);
616 spin_lock_irqsave(&devinfo
->lock
, flags
);
618 if (devinfo
->resetting
) {
619 cmnd
->result
= DID_ERROR
<< 16;
620 cmnd
->scsi_done(cmnd
);
621 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
625 /* Find a free uas-tag */
626 for (idx
= 0; idx
< devinfo
->qdepth
; idx
++) {
627 if (!devinfo
->cmnd
[idx
])
630 if (idx
== devinfo
->qdepth
) {
631 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
632 return SCSI_MLQUEUE_DEVICE_BUSY
;
635 cmnd
->scsi_done
= done
;
637 memset(cmdinfo
, 0, sizeof(*cmdinfo
));
638 cmdinfo
->uas_tag
= idx
+ 1; /* uas-tag == usb-stream-id, so 1 based */
639 cmdinfo
->state
= SUBMIT_STATUS_URB
| ALLOC_CMD_URB
| SUBMIT_CMD_URB
;
641 switch (cmnd
->sc_data_direction
) {
642 case DMA_FROM_DEVICE
:
643 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
645 case DMA_BIDIRECTIONAL
:
646 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
648 cmdinfo
->state
|= ALLOC_DATA_OUT_URB
| SUBMIT_DATA_OUT_URB
;
653 if (!devinfo
->use_streams
)
654 cmdinfo
->state
&= ~(SUBMIT_DATA_IN_URB
| SUBMIT_DATA_OUT_URB
);
656 err
= uas_submit_urbs(cmnd
, devinfo
, GFP_ATOMIC
);
658 /* If we did nothing, give up now */
659 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
660 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
661 return SCSI_MLQUEUE_DEVICE_BUSY
;
663 uas_add_work(cmdinfo
);
666 devinfo
->cmnd
[idx
] = cmnd
;
667 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
671 static DEF_SCSI_QCMD(uas_queuecommand
)
674 * For now we do not support actually sending an abort to the device, so
675 * this eh always fails. Still we must define it to make sure that we've
676 * dropped all references to the cmnd in question once this function exits.
678 static int uas_eh_abort_handler(struct scsi_cmnd
*cmnd
)
680 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
681 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
682 struct urb
*data_in_urb
= NULL
;
683 struct urb
*data_out_urb
= NULL
;
686 spin_lock_irqsave(&devinfo
->lock
, flags
);
688 uas_log_cmd_state(cmnd
, __func__
, 0);
690 /* Ensure that try_complete does not call scsi_done */
691 cmdinfo
->state
|= COMMAND_ABORTED
;
693 /* Drop all refs to this cmnd, kill data urbs to break their ref */
694 devinfo
->cmnd
[cmdinfo
->uas_tag
- 1] = NULL
;
695 if (cmdinfo
->state
& DATA_IN_URB_INFLIGHT
)
696 data_in_urb
= usb_get_urb(cmdinfo
->data_in_urb
);
697 if (cmdinfo
->state
& DATA_OUT_URB_INFLIGHT
)
698 data_out_urb
= usb_get_urb(cmdinfo
->data_out_urb
);
700 uas_free_unsubmitted_urbs(cmnd
);
702 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
705 usb_kill_urb(data_in_urb
);
706 usb_put_urb(data_in_urb
);
709 usb_kill_urb(data_out_urb
);
710 usb_put_urb(data_out_urb
);
716 static int uas_eh_bus_reset_handler(struct scsi_cmnd
*cmnd
)
718 struct scsi_device
*sdev
= cmnd
->device
;
719 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
720 struct usb_device
*udev
= devinfo
->udev
;
724 err
= usb_lock_device_for_reset(udev
, devinfo
->intf
);
726 shost_printk(KERN_ERR
, sdev
->host
,
727 "%s FAILED to get lock err %d\n", __func__
, err
);
731 shost_printk(KERN_INFO
, sdev
->host
, "%s start\n", __func__
);
733 spin_lock_irqsave(&devinfo
->lock
, flags
);
734 devinfo
->resetting
= 1;
735 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
737 usb_kill_anchored_urbs(&devinfo
->cmd_urbs
);
738 usb_kill_anchored_urbs(&devinfo
->sense_urbs
);
739 usb_kill_anchored_urbs(&devinfo
->data_urbs
);
740 uas_zap_pending(devinfo
, DID_RESET
);
742 err
= usb_reset_device(udev
);
744 spin_lock_irqsave(&devinfo
->lock
, flags
);
745 devinfo
->resetting
= 0;
746 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
748 usb_unlock_device(udev
);
751 shost_printk(KERN_INFO
, sdev
->host
, "%s FAILED err %d\n",
756 shost_printk(KERN_INFO
, sdev
->host
, "%s success\n", __func__
);
760 static int uas_slave_alloc(struct scsi_device
*sdev
)
762 struct uas_dev_info
*devinfo
=
763 (struct uas_dev_info
*)sdev
->host
->hostdata
;
765 sdev
->hostdata
= devinfo
;
767 /* USB has unusual DMA-alignment requirements: Although the
768 * starting address of each scatter-gather element doesn't matter,
769 * the length of each element except the last must be divisible
770 * by the Bulk maxpacket value. There's currently no way to
771 * express this by block-layer constraints, so we'll cop out
772 * and simply require addresses to be aligned at 512-byte
773 * boundaries. This is okay since most block I/O involves
774 * hardware sectors that are multiples of 512 bytes in length,
775 * and since host controllers up through USB 2.0 have maxpacket
776 * values no larger than 512.
778 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
779 * values can be as large as 2048. To make that work properly
780 * will require changes to the block layer.
782 blk_queue_update_dma_alignment(sdev
->request_queue
, (512 - 1));
784 if (devinfo
->flags
& US_FL_MAX_SECTORS_64
)
785 blk_queue_max_hw_sectors(sdev
->request_queue
, 64);
786 else if (devinfo
->flags
& US_FL_MAX_SECTORS_240
)
787 blk_queue_max_hw_sectors(sdev
->request_queue
, 240);
792 static int uas_slave_configure(struct scsi_device
*sdev
)
794 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
796 if (devinfo
->flags
& US_FL_NO_REPORT_OPCODES
)
797 sdev
->no_report_opcodes
= 1;
799 scsi_change_queue_depth(sdev
, devinfo
->qdepth
- 2);
803 static struct scsi_host_template uas_host_template
= {
804 .module
= THIS_MODULE
,
806 .queuecommand
= uas_queuecommand
,
807 .slave_alloc
= uas_slave_alloc
,
808 .slave_configure
= uas_slave_configure
,
809 .eh_abort_handler
= uas_eh_abort_handler
,
810 .eh_bus_reset_handler
= uas_eh_bus_reset_handler
,
811 .can_queue
= 65536, /* Is there a limit on the _host_ ? */
813 .sg_tablesize
= SG_NONE
,
814 .skip_settle_delay
= 1,
818 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
819 vendorName, productName, useProtocol, useTransport, \
820 initFunction, flags) \
821 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
822 .driver_info = (flags) }
824 static struct usb_device_id uas_usb_ids
[] = {
825 # include "unusual_uas.h"
826 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_BULK
) },
827 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_UAS
) },
830 MODULE_DEVICE_TABLE(usb
, uas_usb_ids
);
834 static int uas_switch_interface(struct usb_device
*udev
,
835 struct usb_interface
*intf
)
839 alt
= uas_find_uas_alt_setting(intf
);
843 return usb_set_interface(udev
,
844 intf
->altsetting
[0].desc
.bInterfaceNumber
, alt
);
847 static int uas_configure_endpoints(struct uas_dev_info
*devinfo
)
849 struct usb_host_endpoint
*eps
[4] = { };
850 struct usb_device
*udev
= devinfo
->udev
;
853 r
= uas_find_endpoints(devinfo
->intf
->cur_altsetting
, eps
);
857 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
,
858 usb_endpoint_num(&eps
[0]->desc
));
859 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
,
860 usb_endpoint_num(&eps
[1]->desc
));
861 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
,
862 usb_endpoint_num(&eps
[2]->desc
));
863 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
,
864 usb_endpoint_num(&eps
[3]->desc
));
866 if (udev
->speed
< USB_SPEED_SUPER
) {
867 devinfo
->qdepth
= 32;
868 devinfo
->use_streams
= 0;
870 devinfo
->qdepth
= usb_alloc_streams(devinfo
->intf
, eps
+ 1,
871 3, MAX_CMNDS
, GFP_NOIO
);
872 if (devinfo
->qdepth
< 0)
873 return devinfo
->qdepth
;
874 devinfo
->use_streams
= 1;
880 static void uas_free_streams(struct uas_dev_info
*devinfo
)
882 struct usb_device
*udev
= devinfo
->udev
;
883 struct usb_host_endpoint
*eps
[3];
885 eps
[0] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
886 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
887 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
888 usb_free_streams(devinfo
->intf
, eps
, 3, GFP_NOIO
);
891 static int uas_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
893 int result
= -ENOMEM
;
894 struct Scsi_Host
*shost
= NULL
;
895 struct uas_dev_info
*devinfo
;
896 struct usb_device
*udev
= interface_to_usbdev(intf
);
897 unsigned long dev_flags
;
899 if (!uas_use_uas_driver(intf
, id
, &dev_flags
))
902 if (uas_switch_interface(udev
, intf
))
905 shost
= scsi_host_alloc(&uas_host_template
,
906 sizeof(struct uas_dev_info
));
910 shost
->max_cmd_len
= 16 + 252;
912 shost
->max_lun
= 256;
913 shost
->max_channel
= 0;
914 shost
->sg_tablesize
= udev
->bus
->sg_tablesize
;
916 devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
917 devinfo
->intf
= intf
;
918 devinfo
->udev
= udev
;
919 devinfo
->resetting
= 0;
920 devinfo
->shutdown
= 0;
921 devinfo
->flags
= dev_flags
;
922 init_usb_anchor(&devinfo
->cmd_urbs
);
923 init_usb_anchor(&devinfo
->sense_urbs
);
924 init_usb_anchor(&devinfo
->data_urbs
);
925 spin_lock_init(&devinfo
->lock
);
926 INIT_WORK(&devinfo
->work
, uas_do_work
);
928 result
= uas_configure_endpoints(devinfo
);
932 result
= scsi_init_shared_tag_map(shost
, devinfo
->qdepth
- 2);
936 usb_set_intfdata(intf
, shost
);
937 result
= scsi_add_host(shost
, &intf
->dev
);
941 scsi_scan_host(shost
);
945 uas_free_streams(devinfo
);
946 usb_set_intfdata(intf
, NULL
);
948 usb_set_interface(udev
, intf
->altsetting
[0].desc
.bInterfaceNumber
, 0);
950 scsi_host_put(shost
);
954 static int uas_cmnd_list_empty(struct uas_dev_info
*devinfo
)
959 spin_lock_irqsave(&devinfo
->lock
, flags
);
961 for (i
= 0; i
< devinfo
->qdepth
; i
++) {
962 if (devinfo
->cmnd
[i
]) {
963 r
= 0; /* Not empty */
968 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
974 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
975 * get empty while there still is more work to do due to sense-urbs completing
976 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
978 static int uas_wait_for_pending_cmnds(struct uas_dev_info
*devinfo
)
980 unsigned long start_time
;
983 start_time
= jiffies
;
985 flush_work(&devinfo
->work
);
987 r
= usb_wait_anchor_empty_timeout(&devinfo
->sense_urbs
, 5000);
991 r
= usb_wait_anchor_empty_timeout(&devinfo
->data_urbs
, 500);
995 if (time_after(jiffies
, start_time
+ 5 * HZ
))
997 } while (!uas_cmnd_list_empty(devinfo
));
1002 static int uas_pre_reset(struct usb_interface
*intf
)
1004 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1005 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
1006 unsigned long flags
;
1008 if (devinfo
->shutdown
)
1011 /* Block new requests */
1012 spin_lock_irqsave(shost
->host_lock
, flags
);
1013 scsi_block_requests(shost
);
1014 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1016 if (uas_wait_for_pending_cmnds(devinfo
) != 0) {
1017 shost_printk(KERN_ERR
, shost
, "%s: timed out\n", __func__
);
1018 scsi_unblock_requests(shost
);
1022 uas_free_streams(devinfo
);
1027 static int uas_post_reset(struct usb_interface
*intf
)
1029 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1030 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
1031 unsigned long flags
;
1034 if (devinfo
->shutdown
)
1037 err
= uas_configure_endpoints(devinfo
);
1039 shost_printk(KERN_ERR
, shost
,
1040 "%s: alloc streams error %d after reset",
1045 spin_lock_irqsave(shost
->host_lock
, flags
);
1046 scsi_report_bus_reset(shost
, 0);
1047 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1049 scsi_unblock_requests(shost
);
1054 static int uas_suspend(struct usb_interface
*intf
, pm_message_t message
)
1056 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1057 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
1059 if (uas_wait_for_pending_cmnds(devinfo
) != 0) {
1060 shost_printk(KERN_ERR
, shost
, "%s: timed out\n", __func__
);
1067 static int uas_resume(struct usb_interface
*intf
)
1072 static int uas_reset_resume(struct usb_interface
*intf
)
1074 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1075 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
1076 unsigned long flags
;
1079 err
= uas_configure_endpoints(devinfo
);
1081 shost_printk(KERN_ERR
, shost
,
1082 "%s: alloc streams error %d after reset",
1087 spin_lock_irqsave(shost
->host_lock
, flags
);
1088 scsi_report_bus_reset(shost
, 0);
1089 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1094 static void uas_disconnect(struct usb_interface
*intf
)
1096 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1097 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
1098 unsigned long flags
;
1100 spin_lock_irqsave(&devinfo
->lock
, flags
);
1101 devinfo
->resetting
= 1;
1102 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
1104 cancel_work_sync(&devinfo
->work
);
1105 usb_kill_anchored_urbs(&devinfo
->cmd_urbs
);
1106 usb_kill_anchored_urbs(&devinfo
->sense_urbs
);
1107 usb_kill_anchored_urbs(&devinfo
->data_urbs
);
1108 uas_zap_pending(devinfo
, DID_NO_CONNECT
);
1110 scsi_remove_host(shost
);
1111 uas_free_streams(devinfo
);
1112 scsi_host_put(shost
);
1116 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1117 * hang on reboot when the device is still in uas mode. Note the reset is
1118 * necessary as some devices won't revert to usb-storage mode without it.
1120 static void uas_shutdown(struct device
*dev
)
1122 struct usb_interface
*intf
= to_usb_interface(dev
);
1123 struct usb_device
*udev
= interface_to_usbdev(intf
);
1124 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1125 struct uas_dev_info
*devinfo
= (struct uas_dev_info
*)shost
->hostdata
;
1127 if (system_state
!= SYSTEM_RESTART
)
1130 devinfo
->shutdown
= 1;
1131 uas_free_streams(devinfo
);
1132 usb_set_interface(udev
, intf
->altsetting
[0].desc
.bInterfaceNumber
, 0);
1133 usb_reset_device(udev
);
1136 static struct usb_driver uas_driver
= {
1139 .disconnect
= uas_disconnect
,
1140 .pre_reset
= uas_pre_reset
,
1141 .post_reset
= uas_post_reset
,
1142 .suspend
= uas_suspend
,
1143 .resume
= uas_resume
,
1144 .reset_resume
= uas_reset_resume
,
1145 .drvwrap
.driver
.shutdown
= uas_shutdown
,
1146 .id_table
= uas_usb_ids
,
1149 module_usb_driver(uas_driver
);
1151 MODULE_LICENSE("GPL");
1153 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");