3 * Note that this is not the same as the USB Mass Storage driver
5 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010
8 * Distributed under the terms of the GNU GPL, version two.
11 #include <linux/blkdev.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/usb/storage.h>
18 #include <linux/usb/uas.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi_tcq.h>
28 * The r00-r01c specs define this version of the SENSE IU data structure.
29 * It's still in use by several different firmware releases.
37 __u8 service_response
;
38 __u8 sense
[SCSI_SENSE_BUFFERSIZE
];
42 struct usb_interface
*intf
;
43 struct usb_device
*udev
;
44 struct usb_anchor cmd_urbs
;
45 struct usb_anchor sense_urbs
;
46 struct usb_anchor data_urbs
;
47 int qdepth
, resetting
;
48 struct response_ui response
;
49 unsigned cmd_pipe
, status_pipe
, data_in_pipe
, data_out_pipe
;
50 unsigned use_streams
:1;
51 unsigned uas_sense_old
:1;
52 struct scsi_cmnd
*cmnd
;
57 SUBMIT_STATUS_URB
= (1 << 1),
58 ALLOC_DATA_IN_URB
= (1 << 2),
59 SUBMIT_DATA_IN_URB
= (1 << 3),
60 ALLOC_DATA_OUT_URB
= (1 << 4),
61 SUBMIT_DATA_OUT_URB
= (1 << 5),
62 ALLOC_CMD_URB
= (1 << 6),
63 SUBMIT_CMD_URB
= (1 << 7),
64 COMMAND_INFLIGHT
= (1 << 8),
65 DATA_IN_URB_INFLIGHT
= (1 << 9),
66 DATA_OUT_URB_INFLIGHT
= (1 << 10),
67 COMMAND_COMPLETED
= (1 << 11),
68 COMMAND_ABORTED
= (1 << 12),
69 UNLINK_DATA_URBS
= (1 << 13),
70 IS_IN_WORK_LIST
= (1 << 14),
73 /* Overrides scsi_pointer */
78 struct urb
*data_in_urb
;
79 struct urb
*data_out_urb
;
80 struct list_head list
;
83 /* I hate forward declarations, but I actually have a loop */
84 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
85 struct uas_dev_info
*devinfo
, gfp_t gfp
);
86 static void uas_do_work(struct work_struct
*work
);
87 static int uas_try_complete(struct scsi_cmnd
*cmnd
, const char *caller
);
89 static DECLARE_WORK(uas_work
, uas_do_work
);
90 static DEFINE_SPINLOCK(uas_work_lock
);
91 static LIST_HEAD(uas_work_list
);
93 static void uas_unlink_data_urbs(struct uas_dev_info
*devinfo
,
94 struct uas_cmd_info
*cmdinfo
)
99 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
100 * (called by urb completion) doesn't release cmdinfo
103 spin_lock_irqsave(&devinfo
->lock
, flags
);
104 cmdinfo
->state
|= UNLINK_DATA_URBS
;
105 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
107 if (cmdinfo
->data_in_urb
)
108 usb_unlink_urb(cmdinfo
->data_in_urb
);
109 if (cmdinfo
->data_out_urb
)
110 usb_unlink_urb(cmdinfo
->data_out_urb
);
112 spin_lock_irqsave(&devinfo
->lock
, flags
);
113 cmdinfo
->state
&= ~UNLINK_DATA_URBS
;
114 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
117 static void uas_do_work(struct work_struct
*work
)
119 struct uas_cmd_info
*cmdinfo
;
120 struct uas_cmd_info
*temp
;
121 struct list_head list
;
125 spin_lock_irq(&uas_work_lock
);
126 list_replace_init(&uas_work_list
, &list
);
127 spin_unlock_irq(&uas_work_lock
);
129 list_for_each_entry_safe(cmdinfo
, temp
, &list
, list
) {
130 struct scsi_pointer
*scp
= (void *)cmdinfo
;
131 struct scsi_cmnd
*cmnd
= container_of(scp
,
132 struct scsi_cmnd
, SCp
);
133 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
134 spin_lock_irqsave(&devinfo
->lock
, flags
);
135 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
137 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
138 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
140 list_del(&cmdinfo
->list
);
141 spin_lock_irq(&uas_work_lock
);
142 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
143 spin_unlock_irq(&uas_work_lock
);
144 schedule_work(&uas_work
);
149 static void uas_abort_work(struct uas_dev_info
*devinfo
)
151 struct uas_cmd_info
*cmdinfo
;
152 struct uas_cmd_info
*temp
;
153 struct list_head list
;
156 spin_lock_irq(&uas_work_lock
);
157 list_replace_init(&uas_work_list
, &list
);
158 spin_unlock_irq(&uas_work_lock
);
160 spin_lock_irqsave(&devinfo
->lock
, flags
);
161 list_for_each_entry_safe(cmdinfo
, temp
, &list
, list
) {
162 struct scsi_pointer
*scp
= (void *)cmdinfo
;
163 struct scsi_cmnd
*cmnd
= container_of(scp
,
164 struct scsi_cmnd
, SCp
);
165 struct uas_dev_info
*di
= (void *)cmnd
->device
->hostdata
;
168 cmdinfo
->state
|= COMMAND_ABORTED
;
169 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
170 if (devinfo
->resetting
) {
171 /* uas_stat_cmplt() will not do that
172 * when a device reset is in
174 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
176 uas_try_complete(cmnd
, __func__
);
178 /* not our uas device, relink into list */
179 list_del(&cmdinfo
->list
);
180 spin_lock_irq(&uas_work_lock
);
181 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
182 spin_unlock_irq(&uas_work_lock
);
185 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
188 static void uas_sense(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
190 struct sense_iu
*sense_iu
= urb
->transfer_buffer
;
191 struct scsi_device
*sdev
= cmnd
->device
;
193 if (urb
->actual_length
> 16) {
194 unsigned len
= be16_to_cpup(&sense_iu
->len
);
195 if (len
+ 16 != urb
->actual_length
) {
196 int newlen
= min(len
+ 16, urb
->actual_length
) - 16;
199 sdev_printk(KERN_INFO
, sdev
, "%s: urb length %d "
200 "disagrees with IU sense data length %d, "
201 "using %d bytes of sense data\n", __func__
,
202 urb
->actual_length
, len
, newlen
);
205 memcpy(cmnd
->sense_buffer
, sense_iu
->sense
, len
);
208 cmnd
->result
= sense_iu
->status
;
211 static void uas_sense_old(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
213 struct sense_iu_old
*sense_iu
= urb
->transfer_buffer
;
214 struct scsi_device
*sdev
= cmnd
->device
;
216 if (urb
->actual_length
> 8) {
217 unsigned len
= be16_to_cpup(&sense_iu
->len
) - 2;
218 if (len
+ 8 != urb
->actual_length
) {
219 int newlen
= min(len
+ 8, urb
->actual_length
) - 8;
222 sdev_printk(KERN_INFO
, sdev
, "%s: urb length %d "
223 "disagrees with IU sense data length %d, "
224 "using %d bytes of sense data\n", __func__
,
225 urb
->actual_length
, len
, newlen
);
228 memcpy(cmnd
->sense_buffer
, sense_iu
->sense
, len
);
231 cmnd
->result
= sense_iu
->status
;
234 static void uas_log_cmd_state(struct scsi_cmnd
*cmnd
, const char *caller
)
236 struct uas_cmd_info
*ci
= (void *)&cmnd
->SCp
;
238 scmd_printk(KERN_INFO
, cmnd
, "%s %p tag %d, inflight:"
239 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
240 caller
, cmnd
, cmnd
->request
->tag
,
241 (ci
->state
& SUBMIT_STATUS_URB
) ? " s-st" : "",
242 (ci
->state
& ALLOC_DATA_IN_URB
) ? " a-in" : "",
243 (ci
->state
& SUBMIT_DATA_IN_URB
) ? " s-in" : "",
244 (ci
->state
& ALLOC_DATA_OUT_URB
) ? " a-out" : "",
245 (ci
->state
& SUBMIT_DATA_OUT_URB
) ? " s-out" : "",
246 (ci
->state
& ALLOC_CMD_URB
) ? " a-cmd" : "",
247 (ci
->state
& SUBMIT_CMD_URB
) ? " s-cmd" : "",
248 (ci
->state
& COMMAND_INFLIGHT
) ? " CMD" : "",
249 (ci
->state
& DATA_IN_URB_INFLIGHT
) ? " IN" : "",
250 (ci
->state
& DATA_OUT_URB_INFLIGHT
) ? " OUT" : "",
251 (ci
->state
& COMMAND_COMPLETED
) ? " done" : "",
252 (ci
->state
& COMMAND_ABORTED
) ? " abort" : "",
253 (ci
->state
& UNLINK_DATA_URBS
) ? " unlink": "",
254 (ci
->state
& IS_IN_WORK_LIST
) ? " work" : "");
257 static int uas_try_complete(struct scsi_cmnd
*cmnd
, const char *caller
)
259 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
260 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
262 WARN_ON(!spin_is_locked(&devinfo
->lock
));
263 if (cmdinfo
->state
& (COMMAND_INFLIGHT
|
264 DATA_IN_URB_INFLIGHT
|
265 DATA_OUT_URB_INFLIGHT
|
268 BUG_ON(cmdinfo
->state
& COMMAND_COMPLETED
);
269 cmdinfo
->state
|= COMMAND_COMPLETED
;
270 usb_free_urb(cmdinfo
->data_in_urb
);
271 usb_free_urb(cmdinfo
->data_out_urb
);
272 if (cmdinfo
->state
& COMMAND_ABORTED
) {
273 scmd_printk(KERN_INFO
, cmnd
, "abort completed\n");
274 cmnd
->result
= DID_ABORT
<< 16;
276 cmnd
->scsi_done(cmnd
);
280 static void uas_xfer_data(struct urb
*urb
, struct scsi_cmnd
*cmnd
,
283 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
286 cmdinfo
->state
|= direction
| SUBMIT_STATUS_URB
;
287 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
289 spin_lock(&uas_work_lock
);
290 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
291 cmdinfo
->state
|= IS_IN_WORK_LIST
;
292 spin_unlock(&uas_work_lock
);
293 schedule_work(&uas_work
);
297 static void uas_stat_cmplt(struct urb
*urb
)
299 struct iu
*iu
= urb
->transfer_buffer
;
300 struct Scsi_Host
*shost
= urb
->context
;
301 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
302 struct scsi_cmnd
*cmnd
;
303 struct uas_cmd_info
*cmdinfo
;
308 dev_err(&urb
->dev
->dev
, "URB BAD STATUS %d\n", urb
->status
);
313 if (devinfo
->resetting
) {
318 spin_lock_irqsave(&devinfo
->lock
, flags
);
319 tag
= be16_to_cpup(&iu
->tag
) - 1;
321 cmnd
= devinfo
->cmnd
;
323 cmnd
= scsi_host_find_tag(shost
, tag
- 1);
326 if (iu
->iu_id
== IU_ID_RESPONSE
) {
327 /* store results for uas_eh_task_mgmt() */
328 memcpy(&devinfo
->response
, iu
, sizeof(devinfo
->response
));
331 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
335 cmdinfo
= (void *)&cmnd
->SCp
;
338 if (devinfo
->cmnd
== cmnd
)
339 devinfo
->cmnd
= NULL
;
341 if (urb
->actual_length
< 16)
342 devinfo
->uas_sense_old
= 1;
343 if (devinfo
->uas_sense_old
)
344 uas_sense_old(urb
, cmnd
);
346 uas_sense(urb
, cmnd
);
347 if (cmnd
->result
!= 0) {
348 /* cancel data transfers on error */
349 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
350 uas_unlink_data_urbs(devinfo
, cmdinfo
);
351 spin_lock_irqsave(&devinfo
->lock
, flags
);
353 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
354 uas_try_complete(cmnd
, __func__
);
356 case IU_ID_READ_READY
:
357 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_IN_URB
);
359 case IU_ID_WRITE_READY
:
360 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_OUT_URB
);
363 scmd_printk(KERN_ERR
, cmnd
,
364 "Bogus IU (%d) received on status pipe\n", iu
->iu_id
);
367 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
370 static void uas_data_cmplt(struct urb
*urb
)
372 struct scsi_cmnd
*cmnd
= urb
->context
;
373 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
374 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
375 struct scsi_data_buffer
*sdb
= NULL
;
378 spin_lock_irqsave(&devinfo
->lock
, flags
);
379 if (cmdinfo
->data_in_urb
== urb
) {
381 cmdinfo
->state
&= ~DATA_IN_URB_INFLIGHT
;
382 } else if (cmdinfo
->data_out_urb
== urb
) {
383 sdb
= scsi_out(cmnd
);
384 cmdinfo
->state
&= ~DATA_OUT_URB_INFLIGHT
;
388 /* error: no data transfered */
389 sdb
->resid
= sdb
->length
;
391 sdb
->resid
= sdb
->length
- urb
->actual_length
;
393 uas_try_complete(cmnd
, __func__
);
394 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
397 static struct urb
*uas_alloc_data_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
398 unsigned int pipe
, u16 stream_id
,
399 struct scsi_cmnd
*cmnd
,
400 enum dma_data_direction dir
)
402 struct usb_device
*udev
= devinfo
->udev
;
403 struct urb
*urb
= usb_alloc_urb(0, gfp
);
404 struct scsi_data_buffer
*sdb
= (dir
== DMA_FROM_DEVICE
)
405 ? scsi_in(cmnd
) : scsi_out(cmnd
);
409 usb_fill_bulk_urb(urb
, udev
, pipe
, NULL
, sdb
->length
,
410 uas_data_cmplt
, cmnd
);
411 if (devinfo
->use_streams
)
412 urb
->stream_id
= stream_id
;
413 urb
->num_sgs
= udev
->bus
->sg_tablesize
? sdb
->table
.nents
: 0;
414 urb
->sg
= sdb
->table
.sgl
;
419 static struct urb
*uas_alloc_sense_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
420 struct Scsi_Host
*shost
, u16 stream_id
)
422 struct usb_device
*udev
= devinfo
->udev
;
423 struct urb
*urb
= usb_alloc_urb(0, gfp
);
429 iu
= kzalloc(sizeof(*iu
), gfp
);
433 usb_fill_bulk_urb(urb
, udev
, devinfo
->status_pipe
, iu
, sizeof(*iu
),
434 uas_stat_cmplt
, shost
);
435 urb
->stream_id
= stream_id
;
436 urb
->transfer_flags
|= URB_FREE_BUFFER
;
444 static struct urb
*uas_alloc_cmd_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
445 struct scsi_cmnd
*cmnd
, u16 stream_id
)
447 struct usb_device
*udev
= devinfo
->udev
;
448 struct scsi_device
*sdev
= cmnd
->device
;
449 struct urb
*urb
= usb_alloc_urb(0, gfp
);
450 struct command_iu
*iu
;
456 len
= cmnd
->cmd_len
- 16;
460 iu
= kzalloc(sizeof(*iu
) + len
, gfp
);
464 iu
->iu_id
= IU_ID_COMMAND
;
465 if (blk_rq_tagged(cmnd
->request
))
466 iu
->tag
= cpu_to_be16(cmnd
->request
->tag
+ 2);
468 iu
->tag
= cpu_to_be16(1);
469 iu
->prio_attr
= UAS_SIMPLE_TAG
;
471 int_to_scsilun(sdev
->lun
, &iu
->lun
);
472 memcpy(iu
->cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
474 usb_fill_bulk_urb(urb
, udev
, devinfo
->cmd_pipe
, iu
, sizeof(*iu
) + len
,
476 urb
->transfer_flags
|= URB_FREE_BUFFER
;
484 static int uas_submit_task_urb(struct scsi_cmnd
*cmnd
, gfp_t gfp
,
485 u8 function
, u16 stream_id
)
487 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
488 struct usb_device
*udev
= devinfo
->udev
;
489 struct urb
*urb
= usb_alloc_urb(0, gfp
);
490 struct task_mgmt_iu
*iu
;
496 iu
= kzalloc(sizeof(*iu
), gfp
);
500 iu
->iu_id
= IU_ID_TASK_MGMT
;
501 iu
->tag
= cpu_to_be16(stream_id
);
502 int_to_scsilun(cmnd
->device
->lun
, &iu
->lun
);
504 iu
->function
= function
;
507 if (blk_rq_tagged(cmnd
->request
))
508 iu
->task_tag
= cpu_to_be16(cmnd
->request
->tag
+ 2);
510 iu
->task_tag
= cpu_to_be16(1);
514 usb_fill_bulk_urb(urb
, udev
, devinfo
->cmd_pipe
, iu
, sizeof(*iu
),
516 urb
->transfer_flags
|= URB_FREE_BUFFER
;
518 err
= usb_submit_urb(urb
, gfp
);
521 usb_anchor_urb(urb
, &devinfo
->cmd_urbs
);
531 * Why should I request the Status IU before sending the Command IU? Spec
532 * says to, but also says the device may receive them in any order. Seems
536 static int uas_submit_sense_urb(struct Scsi_Host
*shost
,
537 gfp_t gfp
, unsigned int stream
)
539 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
542 urb
= uas_alloc_sense_urb(devinfo
, gfp
, shost
, stream
);
544 return SCSI_MLQUEUE_DEVICE_BUSY
;
545 if (usb_submit_urb(urb
, gfp
)) {
546 shost_printk(KERN_INFO
, shost
,
547 "sense urb submission failure\n");
549 return SCSI_MLQUEUE_DEVICE_BUSY
;
551 usb_anchor_urb(urb
, &devinfo
->sense_urbs
);
555 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
556 struct uas_dev_info
*devinfo
, gfp_t gfp
)
558 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
561 WARN_ON(!spin_is_locked(&devinfo
->lock
));
562 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
563 err
= uas_submit_sense_urb(cmnd
->device
->host
, gfp
,
568 cmdinfo
->state
&= ~SUBMIT_STATUS_URB
;
571 if (cmdinfo
->state
& ALLOC_DATA_IN_URB
) {
572 cmdinfo
->data_in_urb
= uas_alloc_data_urb(devinfo
, gfp
,
573 devinfo
->data_in_pipe
, cmdinfo
->stream
,
574 cmnd
, DMA_FROM_DEVICE
);
575 if (!cmdinfo
->data_in_urb
)
576 return SCSI_MLQUEUE_DEVICE_BUSY
;
577 cmdinfo
->state
&= ~ALLOC_DATA_IN_URB
;
580 if (cmdinfo
->state
& SUBMIT_DATA_IN_URB
) {
581 if (usb_submit_urb(cmdinfo
->data_in_urb
, gfp
)) {
582 scmd_printk(KERN_INFO
, cmnd
,
583 "data in urb submission failure\n");
584 return SCSI_MLQUEUE_DEVICE_BUSY
;
586 cmdinfo
->state
&= ~SUBMIT_DATA_IN_URB
;
587 cmdinfo
->state
|= DATA_IN_URB_INFLIGHT
;
588 usb_anchor_urb(cmdinfo
->data_in_urb
, &devinfo
->data_urbs
);
591 if (cmdinfo
->state
& ALLOC_DATA_OUT_URB
) {
592 cmdinfo
->data_out_urb
= uas_alloc_data_urb(devinfo
, gfp
,
593 devinfo
->data_out_pipe
, cmdinfo
->stream
,
594 cmnd
, DMA_TO_DEVICE
);
595 if (!cmdinfo
->data_out_urb
)
596 return SCSI_MLQUEUE_DEVICE_BUSY
;
597 cmdinfo
->state
&= ~ALLOC_DATA_OUT_URB
;
600 if (cmdinfo
->state
& SUBMIT_DATA_OUT_URB
) {
601 if (usb_submit_urb(cmdinfo
->data_out_urb
, gfp
)) {
602 scmd_printk(KERN_INFO
, cmnd
,
603 "data out urb submission failure\n");
604 return SCSI_MLQUEUE_DEVICE_BUSY
;
606 cmdinfo
->state
&= ~SUBMIT_DATA_OUT_URB
;
607 cmdinfo
->state
|= DATA_OUT_URB_INFLIGHT
;
608 usb_anchor_urb(cmdinfo
->data_out_urb
, &devinfo
->data_urbs
);
611 if (cmdinfo
->state
& ALLOC_CMD_URB
) {
612 cmdinfo
->cmd_urb
= uas_alloc_cmd_urb(devinfo
, gfp
, cmnd
,
614 if (!cmdinfo
->cmd_urb
)
615 return SCSI_MLQUEUE_DEVICE_BUSY
;
616 cmdinfo
->state
&= ~ALLOC_CMD_URB
;
619 if (cmdinfo
->state
& SUBMIT_CMD_URB
) {
620 usb_get_urb(cmdinfo
->cmd_urb
);
621 if (usb_submit_urb(cmdinfo
->cmd_urb
, gfp
)) {
622 scmd_printk(KERN_INFO
, cmnd
,
623 "cmd urb submission failure\n");
624 return SCSI_MLQUEUE_DEVICE_BUSY
;
626 usb_anchor_urb(cmdinfo
->cmd_urb
, &devinfo
->cmd_urbs
);
627 usb_put_urb(cmdinfo
->cmd_urb
);
628 cmdinfo
->cmd_urb
= NULL
;
629 cmdinfo
->state
&= ~SUBMIT_CMD_URB
;
630 cmdinfo
->state
|= COMMAND_INFLIGHT
;
636 static int uas_queuecommand_lck(struct scsi_cmnd
*cmnd
,
637 void (*done
)(struct scsi_cmnd
*))
639 struct scsi_device
*sdev
= cmnd
->device
;
640 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
641 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
645 BUILD_BUG_ON(sizeof(struct uas_cmd_info
) > sizeof(struct scsi_pointer
));
647 if (devinfo
->resetting
) {
648 cmnd
->result
= DID_ERROR
<< 16;
649 cmnd
->scsi_done(cmnd
);
653 spin_lock_irqsave(&devinfo
->lock
, flags
);
655 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
656 return SCSI_MLQUEUE_DEVICE_BUSY
;
659 if (blk_rq_tagged(cmnd
->request
)) {
660 cmdinfo
->stream
= cmnd
->request
->tag
+ 2;
662 devinfo
->cmnd
= cmnd
;
666 cmnd
->scsi_done
= done
;
668 cmdinfo
->state
= SUBMIT_STATUS_URB
|
669 ALLOC_CMD_URB
| SUBMIT_CMD_URB
;
671 switch (cmnd
->sc_data_direction
) {
672 case DMA_FROM_DEVICE
:
673 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
675 case DMA_BIDIRECTIONAL
:
676 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
678 cmdinfo
->state
|= ALLOC_DATA_OUT_URB
| SUBMIT_DATA_OUT_URB
;
683 if (!devinfo
->use_streams
) {
684 cmdinfo
->state
&= ~(SUBMIT_DATA_IN_URB
| SUBMIT_DATA_OUT_URB
);
688 err
= uas_submit_urbs(cmnd
, devinfo
, GFP_ATOMIC
);
690 /* If we did nothing, give up now */
691 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
692 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
693 return SCSI_MLQUEUE_DEVICE_BUSY
;
695 spin_lock(&uas_work_lock
);
696 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
697 cmdinfo
->state
|= IS_IN_WORK_LIST
;
698 spin_unlock(&uas_work_lock
);
699 schedule_work(&uas_work
);
702 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
706 static DEF_SCSI_QCMD(uas_queuecommand
)
708 static int uas_eh_task_mgmt(struct scsi_cmnd
*cmnd
,
709 const char *fname
, u8 function
)
711 struct Scsi_Host
*shost
= cmnd
->device
->host
;
712 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
713 u16 tag
= devinfo
->qdepth
- 1;
716 spin_lock_irqsave(&devinfo
->lock
, flags
);
717 memset(&devinfo
->response
, 0, sizeof(devinfo
->response
));
718 if (uas_submit_sense_urb(shost
, GFP_ATOMIC
, tag
)) {
719 shost_printk(KERN_INFO
, shost
,
720 "%s: %s: submit sense urb failed\n",
722 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
725 if (uas_submit_task_urb(cmnd
, GFP_ATOMIC
, function
, tag
)) {
726 shost_printk(KERN_INFO
, shost
,
727 "%s: %s: submit task mgmt urb failed\n",
729 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
732 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
734 if (usb_wait_anchor_empty_timeout(&devinfo
->sense_urbs
, 3000) == 0) {
735 shost_printk(KERN_INFO
, shost
,
736 "%s: %s timed out\n", __func__
, fname
);
739 if (be16_to_cpu(devinfo
->response
.tag
) != tag
) {
740 shost_printk(KERN_INFO
, shost
,
741 "%s: %s failed (wrong tag %d/%d)\n", __func__
,
742 fname
, be16_to_cpu(devinfo
->response
.tag
), tag
);
745 if (devinfo
->response
.response_code
!= RC_TMF_COMPLETE
) {
746 shost_printk(KERN_INFO
, shost
,
747 "%s: %s failed (rc 0x%x)\n", __func__
,
748 fname
, devinfo
->response
.response_code
);
754 static int uas_eh_abort_handler(struct scsi_cmnd
*cmnd
)
756 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
757 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
761 uas_log_cmd_state(cmnd
, __func__
);
762 spin_lock_irqsave(&devinfo
->lock
, flags
);
763 cmdinfo
->state
|= COMMAND_ABORTED
;
764 if (cmdinfo
->state
& IS_IN_WORK_LIST
) {
765 spin_lock(&uas_work_lock
);
766 list_del(&cmdinfo
->list
);
767 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
768 spin_unlock(&uas_work_lock
);
770 if (cmdinfo
->state
& COMMAND_INFLIGHT
) {
771 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
772 ret
= uas_eh_task_mgmt(cmnd
, "ABORT TASK", TMF_ABORT_TASK
);
774 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
775 uas_unlink_data_urbs(devinfo
, cmdinfo
);
776 spin_lock_irqsave(&devinfo
->lock
, flags
);
777 uas_try_complete(cmnd
, __func__
);
778 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
784 static int uas_eh_device_reset_handler(struct scsi_cmnd
*cmnd
)
786 sdev_printk(KERN_INFO
, cmnd
->device
, "%s\n", __func__
);
787 return uas_eh_task_mgmt(cmnd
, "LOGICAL UNIT RESET",
788 TMF_LOGICAL_UNIT_RESET
);
791 static int uas_eh_bus_reset_handler(struct scsi_cmnd
*cmnd
)
793 struct scsi_device
*sdev
= cmnd
->device
;
794 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
795 struct usb_device
*udev
= devinfo
->udev
;
798 devinfo
->resetting
= 1;
799 uas_abort_work(devinfo
);
800 usb_kill_anchored_urbs(&devinfo
->cmd_urbs
);
801 usb_kill_anchored_urbs(&devinfo
->sense_urbs
);
802 usb_kill_anchored_urbs(&devinfo
->data_urbs
);
803 err
= usb_reset_device(udev
);
804 devinfo
->resetting
= 0;
807 shost_printk(KERN_INFO
, sdev
->host
, "%s FAILED\n", __func__
);
811 shost_printk(KERN_INFO
, sdev
->host
, "%s success\n", __func__
);
815 static int uas_slave_alloc(struct scsi_device
*sdev
)
817 sdev
->hostdata
= (void *)sdev
->host
->hostdata
[0];
821 static int uas_slave_configure(struct scsi_device
*sdev
)
823 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
824 scsi_set_tag_type(sdev
, MSG_ORDERED_TAG
);
825 scsi_activate_tcq(sdev
, devinfo
->qdepth
- 3);
829 static struct scsi_host_template uas_host_template
= {
830 .module
= THIS_MODULE
,
832 .queuecommand
= uas_queuecommand
,
833 .slave_alloc
= uas_slave_alloc
,
834 .slave_configure
= uas_slave_configure
,
835 .eh_abort_handler
= uas_eh_abort_handler
,
836 .eh_device_reset_handler
= uas_eh_device_reset_handler
,
837 .eh_bus_reset_handler
= uas_eh_bus_reset_handler
,
838 .can_queue
= 65536, /* Is there a limit on the _host_ ? */
840 .sg_tablesize
= SG_NONE
,
841 .cmd_per_lun
= 1, /* until we override it */
842 .skip_settle_delay
= 1,
846 static struct usb_device_id uas_usb_ids
[] = {
847 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_BULK
) },
848 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_UAS
) },
849 /* 0xaa is a prototype device I happen to have access to */
850 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, 0xaa) },
853 MODULE_DEVICE_TABLE(usb
, uas_usb_ids
);
855 static int uas_is_interface(struct usb_host_interface
*intf
)
857 return (intf
->desc
.bInterfaceClass
== USB_CLASS_MASS_STORAGE
&&
858 intf
->desc
.bInterfaceSubClass
== USB_SC_SCSI
&&
859 intf
->desc
.bInterfaceProtocol
== USB_PR_UAS
);
862 static int uas_isnt_supported(struct usb_device
*udev
)
864 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
866 dev_warn(&udev
->dev
, "The driver for the USB controller %s does not "
867 "support scatter-gather which is\n",
868 hcd
->driver
->description
);
869 dev_warn(&udev
->dev
, "required by the UAS driver. Please try an"
870 "alternative USB controller if you wish to use UAS.\n");
874 static int uas_switch_interface(struct usb_device
*udev
,
875 struct usb_interface
*intf
)
878 int sg_supported
= udev
->bus
->sg_tablesize
!= 0;
880 for (i
= 0; i
< intf
->num_altsetting
; i
++) {
881 struct usb_host_interface
*alt
= &intf
->altsetting
[i
];
883 if (uas_is_interface(alt
)) {
885 return uas_isnt_supported(udev
);
886 return usb_set_interface(udev
,
887 alt
->desc
.bInterfaceNumber
,
888 alt
->desc
.bAlternateSetting
);
895 static void uas_configure_endpoints(struct uas_dev_info
*devinfo
)
897 struct usb_host_endpoint
*eps
[4] = { };
898 struct usb_interface
*intf
= devinfo
->intf
;
899 struct usb_device
*udev
= devinfo
->udev
;
900 struct usb_host_endpoint
*endpoint
= intf
->cur_altsetting
->endpoint
;
901 unsigned i
, n_endpoints
= intf
->cur_altsetting
->desc
.bNumEndpoints
;
903 devinfo
->uas_sense_old
= 0;
904 devinfo
->cmnd
= NULL
;
906 for (i
= 0; i
< n_endpoints
; i
++) {
907 unsigned char *extra
= endpoint
[i
].extra
;
908 int len
= endpoint
[i
].extralen
;
910 if (extra
[1] == USB_DT_PIPE_USAGE
) {
911 unsigned pipe_id
= extra
[2];
912 if (pipe_id
> 0 && pipe_id
< 5)
913 eps
[pipe_id
- 1] = &endpoint
[i
];
922 * Assume that if we didn't find a control pipe descriptor, we're
923 * using a device with old firmware that happens to be set up like
927 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
, 1);
928 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
, 1);
929 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
, 2);
930 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
, 2);
932 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
933 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
934 eps
[3] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
936 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
,
937 eps
[0]->desc
.bEndpointAddress
);
938 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
,
939 eps
[1]->desc
.bEndpointAddress
);
940 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
,
941 eps
[2]->desc
.bEndpointAddress
);
942 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
,
943 eps
[3]->desc
.bEndpointAddress
);
946 devinfo
->qdepth
= usb_alloc_streams(devinfo
->intf
, eps
+ 1, 3, 256,
948 if (devinfo
->qdepth
< 0) {
949 devinfo
->qdepth
= 256;
950 devinfo
->use_streams
= 0;
952 devinfo
->use_streams
= 1;
956 static void uas_free_streams(struct uas_dev_info
*devinfo
)
958 struct usb_device
*udev
= devinfo
->udev
;
959 struct usb_host_endpoint
*eps
[3];
961 eps
[0] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
962 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
963 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
964 usb_free_streams(devinfo
->intf
, eps
, 3, GFP_KERNEL
);
968 * XXX: What I'd like to do here is register a SCSI host for each USB host in
969 * the system. Follow usb-storage's design of registering a SCSI host for
970 * each USB device for the moment. Can implement this by walking up the
971 * USB hierarchy until we find a USB host.
973 static int uas_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
976 struct Scsi_Host
*shost
;
977 struct uas_dev_info
*devinfo
;
978 struct usb_device
*udev
= interface_to_usbdev(intf
);
980 if (uas_switch_interface(udev
, intf
))
983 devinfo
= kmalloc(sizeof(struct uas_dev_info
), GFP_KERNEL
);
988 shost
= scsi_host_alloc(&uas_host_template
, sizeof(void *));
992 shost
->max_cmd_len
= 16 + 252;
994 shost
->max_lun
= 256;
995 shost
->max_channel
= 0;
996 shost
->sg_tablesize
= udev
->bus
->sg_tablesize
;
998 devinfo
->intf
= intf
;
999 devinfo
->udev
= udev
;
1000 devinfo
->resetting
= 0;
1001 init_usb_anchor(&devinfo
->cmd_urbs
);
1002 init_usb_anchor(&devinfo
->sense_urbs
);
1003 init_usb_anchor(&devinfo
->data_urbs
);
1004 spin_lock_init(&devinfo
->lock
);
1005 uas_configure_endpoints(devinfo
);
1007 result
= scsi_init_shared_tag_map(shost
, devinfo
->qdepth
- 3);
1011 result
= scsi_add_host(shost
, &intf
->dev
);
1015 shost
->hostdata
[0] = (unsigned long)devinfo
;
1017 scsi_scan_host(shost
);
1018 usb_set_intfdata(intf
, shost
);
1022 uas_free_streams(devinfo
);
1026 scsi_host_put(shost
);
1030 static int uas_pre_reset(struct usb_interface
*intf
)
1032 /* XXX: Need to return 1 if it's not our device in error handling */
1036 static int uas_post_reset(struct usb_interface
*intf
)
1038 /* XXX: Need to return 1 if it's not our device in error handling */
1042 static void uas_disconnect(struct usb_interface
*intf
)
1044 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1045 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
1047 devinfo
->resetting
= 1;
1048 uas_abort_work(devinfo
);
1049 usb_kill_anchored_urbs(&devinfo
->cmd_urbs
);
1050 usb_kill_anchored_urbs(&devinfo
->sense_urbs
);
1051 usb_kill_anchored_urbs(&devinfo
->data_urbs
);
1052 scsi_remove_host(shost
);
1053 uas_free_streams(devinfo
);
1058 * XXX: Should this plug into libusual so we can auto-upgrade devices from
1061 static struct usb_driver uas_driver
= {
1064 .disconnect
= uas_disconnect
,
1065 .pre_reset
= uas_pre_reset
,
1066 .post_reset
= uas_post_reset
,
1067 .id_table
= uas_usb_ids
,
1070 module_usb_driver(uas_driver
);
1072 MODULE_LICENSE("GPL");
1073 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");