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
);
88 static void uas_configure_endpoints(struct uas_dev_info
*devinfo
);
89 static void uas_free_streams(struct uas_dev_info
*devinfo
);
91 static DECLARE_WORK(uas_work
, uas_do_work
);
92 static DEFINE_SPINLOCK(uas_work_lock
);
93 static LIST_HEAD(uas_work_list
);
95 static void uas_unlink_data_urbs(struct uas_dev_info
*devinfo
,
96 struct uas_cmd_info
*cmdinfo
)
101 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
102 * (called by urb completion) doesn't release cmdinfo
105 spin_lock_irqsave(&devinfo
->lock
, flags
);
106 cmdinfo
->state
|= UNLINK_DATA_URBS
;
107 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
109 if (cmdinfo
->data_in_urb
)
110 usb_unlink_urb(cmdinfo
->data_in_urb
);
111 if (cmdinfo
->data_out_urb
)
112 usb_unlink_urb(cmdinfo
->data_out_urb
);
114 spin_lock_irqsave(&devinfo
->lock
, flags
);
115 cmdinfo
->state
&= ~UNLINK_DATA_URBS
;
116 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
119 static void uas_do_work(struct work_struct
*work
)
121 struct uas_cmd_info
*cmdinfo
;
122 struct uas_cmd_info
*temp
;
123 struct list_head list
;
127 spin_lock_irq(&uas_work_lock
);
128 list_replace_init(&uas_work_list
, &list
);
129 spin_unlock_irq(&uas_work_lock
);
131 list_for_each_entry_safe(cmdinfo
, temp
, &list
, list
) {
132 struct scsi_pointer
*scp
= (void *)cmdinfo
;
133 struct scsi_cmnd
*cmnd
= container_of(scp
,
134 struct scsi_cmnd
, SCp
);
135 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
136 spin_lock_irqsave(&devinfo
->lock
, flags
);
137 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
139 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
140 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
142 list_del(&cmdinfo
->list
);
143 spin_lock_irq(&uas_work_lock
);
144 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
145 spin_unlock_irq(&uas_work_lock
);
146 schedule_work(&uas_work
);
151 static void uas_abort_work(struct uas_dev_info
*devinfo
)
153 struct uas_cmd_info
*cmdinfo
;
154 struct uas_cmd_info
*temp
;
155 struct list_head list
;
158 spin_lock_irq(&uas_work_lock
);
159 list_replace_init(&uas_work_list
, &list
);
160 spin_unlock_irq(&uas_work_lock
);
162 spin_lock_irqsave(&devinfo
->lock
, flags
);
163 list_for_each_entry_safe(cmdinfo
, temp
, &list
, list
) {
164 struct scsi_pointer
*scp
= (void *)cmdinfo
;
165 struct scsi_cmnd
*cmnd
= container_of(scp
,
166 struct scsi_cmnd
, SCp
);
167 struct uas_dev_info
*di
= (void *)cmnd
->device
->hostdata
;
170 cmdinfo
->state
|= COMMAND_ABORTED
;
171 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
172 if (devinfo
->resetting
) {
173 /* uas_stat_cmplt() will not do that
174 * when a device reset is in
176 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
178 uas_try_complete(cmnd
, __func__
);
180 /* not our uas device, relink into list */
181 list_del(&cmdinfo
->list
);
182 spin_lock_irq(&uas_work_lock
);
183 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
184 spin_unlock_irq(&uas_work_lock
);
187 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
190 static void uas_sense(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
192 struct sense_iu
*sense_iu
= urb
->transfer_buffer
;
193 struct scsi_device
*sdev
= cmnd
->device
;
195 if (urb
->actual_length
> 16) {
196 unsigned len
= be16_to_cpup(&sense_iu
->len
);
197 if (len
+ 16 != urb
->actual_length
) {
198 int newlen
= min(len
+ 16, urb
->actual_length
) - 16;
201 sdev_printk(KERN_INFO
, sdev
, "%s: urb length %d "
202 "disagrees with IU sense data length %d, "
203 "using %d bytes of sense data\n", __func__
,
204 urb
->actual_length
, len
, newlen
);
207 memcpy(cmnd
->sense_buffer
, sense_iu
->sense
, len
);
210 cmnd
->result
= sense_iu
->status
;
213 static void uas_sense_old(struct urb
*urb
, struct scsi_cmnd
*cmnd
)
215 struct sense_iu_old
*sense_iu
= urb
->transfer_buffer
;
216 struct scsi_device
*sdev
= cmnd
->device
;
218 if (urb
->actual_length
> 8) {
219 unsigned len
= be16_to_cpup(&sense_iu
->len
) - 2;
220 if (len
+ 8 != urb
->actual_length
) {
221 int newlen
= min(len
+ 8, urb
->actual_length
) - 8;
224 sdev_printk(KERN_INFO
, sdev
, "%s: urb length %d "
225 "disagrees with IU sense data length %d, "
226 "using %d bytes of sense data\n", __func__
,
227 urb
->actual_length
, len
, newlen
);
230 memcpy(cmnd
->sense_buffer
, sense_iu
->sense
, len
);
233 cmnd
->result
= sense_iu
->status
;
236 static void uas_log_cmd_state(struct scsi_cmnd
*cmnd
, const char *caller
)
238 struct uas_cmd_info
*ci
= (void *)&cmnd
->SCp
;
240 scmd_printk(KERN_INFO
, cmnd
, "%s %p tag %d, inflight:"
241 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
242 caller
, cmnd
, cmnd
->request
->tag
,
243 (ci
->state
& SUBMIT_STATUS_URB
) ? " s-st" : "",
244 (ci
->state
& ALLOC_DATA_IN_URB
) ? " a-in" : "",
245 (ci
->state
& SUBMIT_DATA_IN_URB
) ? " s-in" : "",
246 (ci
->state
& ALLOC_DATA_OUT_URB
) ? " a-out" : "",
247 (ci
->state
& SUBMIT_DATA_OUT_URB
) ? " s-out" : "",
248 (ci
->state
& ALLOC_CMD_URB
) ? " a-cmd" : "",
249 (ci
->state
& SUBMIT_CMD_URB
) ? " s-cmd" : "",
250 (ci
->state
& COMMAND_INFLIGHT
) ? " CMD" : "",
251 (ci
->state
& DATA_IN_URB_INFLIGHT
) ? " IN" : "",
252 (ci
->state
& DATA_OUT_URB_INFLIGHT
) ? " OUT" : "",
253 (ci
->state
& COMMAND_COMPLETED
) ? " done" : "",
254 (ci
->state
& COMMAND_ABORTED
) ? " abort" : "",
255 (ci
->state
& UNLINK_DATA_URBS
) ? " unlink": "",
256 (ci
->state
& IS_IN_WORK_LIST
) ? " work" : "");
259 static int uas_try_complete(struct scsi_cmnd
*cmnd
, const char *caller
)
261 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
262 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
264 WARN_ON(!spin_is_locked(&devinfo
->lock
));
265 if (cmdinfo
->state
& (COMMAND_INFLIGHT
|
266 DATA_IN_URB_INFLIGHT
|
267 DATA_OUT_URB_INFLIGHT
|
270 BUG_ON(cmdinfo
->state
& COMMAND_COMPLETED
);
271 cmdinfo
->state
|= COMMAND_COMPLETED
;
272 usb_free_urb(cmdinfo
->data_in_urb
);
273 usb_free_urb(cmdinfo
->data_out_urb
);
274 if (cmdinfo
->state
& COMMAND_ABORTED
) {
275 scmd_printk(KERN_INFO
, cmnd
, "abort completed\n");
276 cmnd
->result
= DID_ABORT
<< 16;
278 cmnd
->scsi_done(cmnd
);
282 static void uas_xfer_data(struct urb
*urb
, struct scsi_cmnd
*cmnd
,
285 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
288 cmdinfo
->state
|= direction
| SUBMIT_STATUS_URB
;
289 err
= uas_submit_urbs(cmnd
, cmnd
->device
->hostdata
, GFP_ATOMIC
);
291 spin_lock(&uas_work_lock
);
292 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
293 cmdinfo
->state
|= IS_IN_WORK_LIST
;
294 spin_unlock(&uas_work_lock
);
295 schedule_work(&uas_work
);
299 static void uas_stat_cmplt(struct urb
*urb
)
301 struct iu
*iu
= urb
->transfer_buffer
;
302 struct Scsi_Host
*shost
= urb
->context
;
303 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
304 struct scsi_cmnd
*cmnd
;
305 struct uas_cmd_info
*cmdinfo
;
310 dev_err(&urb
->dev
->dev
, "URB BAD STATUS %d\n", urb
->status
);
315 if (devinfo
->resetting
) {
320 spin_lock_irqsave(&devinfo
->lock
, flags
);
321 tag
= be16_to_cpup(&iu
->tag
) - 1;
323 cmnd
= devinfo
->cmnd
;
325 cmnd
= scsi_host_find_tag(shost
, tag
- 1);
328 if (iu
->iu_id
== IU_ID_RESPONSE
) {
329 /* store results for uas_eh_task_mgmt() */
330 memcpy(&devinfo
->response
, iu
, sizeof(devinfo
->response
));
333 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
337 cmdinfo
= (void *)&cmnd
->SCp
;
340 if (devinfo
->cmnd
== cmnd
)
341 devinfo
->cmnd
= NULL
;
343 if (urb
->actual_length
< 16)
344 devinfo
->uas_sense_old
= 1;
345 if (devinfo
->uas_sense_old
)
346 uas_sense_old(urb
, cmnd
);
348 uas_sense(urb
, cmnd
);
349 if (cmnd
->result
!= 0) {
350 /* cancel data transfers on error */
351 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
352 uas_unlink_data_urbs(devinfo
, cmdinfo
);
353 spin_lock_irqsave(&devinfo
->lock
, flags
);
355 cmdinfo
->state
&= ~COMMAND_INFLIGHT
;
356 uas_try_complete(cmnd
, __func__
);
358 case IU_ID_READ_READY
:
359 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_IN_URB
);
361 case IU_ID_WRITE_READY
:
362 uas_xfer_data(urb
, cmnd
, SUBMIT_DATA_OUT_URB
);
365 scmd_printk(KERN_ERR
, cmnd
,
366 "Bogus IU (%d) received on status pipe\n", iu
->iu_id
);
369 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
372 static void uas_data_cmplt(struct urb
*urb
)
374 struct scsi_cmnd
*cmnd
= urb
->context
;
375 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
376 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
377 struct scsi_data_buffer
*sdb
= NULL
;
380 spin_lock_irqsave(&devinfo
->lock
, flags
);
381 if (cmdinfo
->data_in_urb
== urb
) {
383 cmdinfo
->state
&= ~DATA_IN_URB_INFLIGHT
;
384 } else if (cmdinfo
->data_out_urb
== urb
) {
385 sdb
= scsi_out(cmnd
);
386 cmdinfo
->state
&= ~DATA_OUT_URB_INFLIGHT
;
390 /* error: no data transfered */
391 sdb
->resid
= sdb
->length
;
393 sdb
->resid
= sdb
->length
- urb
->actual_length
;
395 uas_try_complete(cmnd
, __func__
);
396 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
399 static struct urb
*uas_alloc_data_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
400 unsigned int pipe
, u16 stream_id
,
401 struct scsi_cmnd
*cmnd
,
402 enum dma_data_direction dir
)
404 struct usb_device
*udev
= devinfo
->udev
;
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
);
411 usb_fill_bulk_urb(urb
, udev
, pipe
, NULL
, sdb
->length
,
412 uas_data_cmplt
, cmnd
);
413 if (devinfo
->use_streams
)
414 urb
->stream_id
= stream_id
;
415 urb
->num_sgs
= udev
->bus
->sg_tablesize
? sdb
->table
.nents
: 0;
416 urb
->sg
= sdb
->table
.sgl
;
421 static struct urb
*uas_alloc_sense_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
422 struct Scsi_Host
*shost
, u16 stream_id
)
424 struct usb_device
*udev
= devinfo
->udev
;
425 struct urb
*urb
= usb_alloc_urb(0, gfp
);
431 iu
= kzalloc(sizeof(*iu
), gfp
);
435 usb_fill_bulk_urb(urb
, udev
, devinfo
->status_pipe
, iu
, sizeof(*iu
),
436 uas_stat_cmplt
, shost
);
437 urb
->stream_id
= stream_id
;
438 urb
->transfer_flags
|= URB_FREE_BUFFER
;
446 static struct urb
*uas_alloc_cmd_urb(struct uas_dev_info
*devinfo
, gfp_t gfp
,
447 struct scsi_cmnd
*cmnd
, u16 stream_id
)
449 struct usb_device
*udev
= devinfo
->udev
;
450 struct scsi_device
*sdev
= cmnd
->device
;
451 struct urb
*urb
= usb_alloc_urb(0, gfp
);
452 struct command_iu
*iu
;
458 len
= cmnd
->cmd_len
- 16;
462 iu
= kzalloc(sizeof(*iu
) + len
, gfp
);
466 iu
->iu_id
= IU_ID_COMMAND
;
467 if (blk_rq_tagged(cmnd
->request
))
468 iu
->tag
= cpu_to_be16(cmnd
->request
->tag
+ 2);
470 iu
->tag
= cpu_to_be16(1);
471 iu
->prio_attr
= UAS_SIMPLE_TAG
;
473 int_to_scsilun(sdev
->lun
, &iu
->lun
);
474 memcpy(iu
->cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
476 usb_fill_bulk_urb(urb
, udev
, devinfo
->cmd_pipe
, iu
, sizeof(*iu
) + len
,
478 urb
->transfer_flags
|= URB_FREE_BUFFER
;
486 static int uas_submit_task_urb(struct scsi_cmnd
*cmnd
, gfp_t gfp
,
487 u8 function
, u16 stream_id
)
489 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
490 struct usb_device
*udev
= devinfo
->udev
;
491 struct urb
*urb
= usb_alloc_urb(0, gfp
);
492 struct task_mgmt_iu
*iu
;
498 iu
= kzalloc(sizeof(*iu
), gfp
);
502 iu
->iu_id
= IU_ID_TASK_MGMT
;
503 iu
->tag
= cpu_to_be16(stream_id
);
504 int_to_scsilun(cmnd
->device
->lun
, &iu
->lun
);
506 iu
->function
= function
;
509 if (blk_rq_tagged(cmnd
->request
))
510 iu
->task_tag
= cpu_to_be16(cmnd
->request
->tag
+ 2);
512 iu
->task_tag
= cpu_to_be16(1);
516 usb_fill_bulk_urb(urb
, udev
, devinfo
->cmd_pipe
, iu
, sizeof(*iu
),
518 urb
->transfer_flags
|= URB_FREE_BUFFER
;
520 err
= usb_submit_urb(urb
, gfp
);
523 usb_anchor_urb(urb
, &devinfo
->cmd_urbs
);
533 * Why should I request the Status IU before sending the Command IU? Spec
534 * says to, but also says the device may receive them in any order. Seems
538 static int uas_submit_sense_urb(struct Scsi_Host
*shost
,
539 gfp_t gfp
, unsigned int stream
)
541 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
544 urb
= uas_alloc_sense_urb(devinfo
, gfp
, shost
, stream
);
546 return SCSI_MLQUEUE_DEVICE_BUSY
;
547 if (usb_submit_urb(urb
, gfp
)) {
548 shost_printk(KERN_INFO
, shost
,
549 "sense urb submission failure\n");
551 return SCSI_MLQUEUE_DEVICE_BUSY
;
553 usb_anchor_urb(urb
, &devinfo
->sense_urbs
);
557 static int uas_submit_urbs(struct scsi_cmnd
*cmnd
,
558 struct uas_dev_info
*devinfo
, gfp_t gfp
)
560 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
563 WARN_ON(!spin_is_locked(&devinfo
->lock
));
564 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
565 err
= uas_submit_sense_urb(cmnd
->device
->host
, gfp
,
570 cmdinfo
->state
&= ~SUBMIT_STATUS_URB
;
573 if (cmdinfo
->state
& ALLOC_DATA_IN_URB
) {
574 cmdinfo
->data_in_urb
= uas_alloc_data_urb(devinfo
, gfp
,
575 devinfo
->data_in_pipe
, cmdinfo
->stream
,
576 cmnd
, DMA_FROM_DEVICE
);
577 if (!cmdinfo
->data_in_urb
)
578 return SCSI_MLQUEUE_DEVICE_BUSY
;
579 cmdinfo
->state
&= ~ALLOC_DATA_IN_URB
;
582 if (cmdinfo
->state
& SUBMIT_DATA_IN_URB
) {
583 if (usb_submit_urb(cmdinfo
->data_in_urb
, gfp
)) {
584 scmd_printk(KERN_INFO
, cmnd
,
585 "data in urb submission failure\n");
586 return SCSI_MLQUEUE_DEVICE_BUSY
;
588 cmdinfo
->state
&= ~SUBMIT_DATA_IN_URB
;
589 cmdinfo
->state
|= DATA_IN_URB_INFLIGHT
;
590 usb_anchor_urb(cmdinfo
->data_in_urb
, &devinfo
->data_urbs
);
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
;
602 if (cmdinfo
->state
& SUBMIT_DATA_OUT_URB
) {
603 if (usb_submit_urb(cmdinfo
->data_out_urb
, gfp
)) {
604 scmd_printk(KERN_INFO
, cmnd
,
605 "data out urb submission failure\n");
606 return SCSI_MLQUEUE_DEVICE_BUSY
;
608 cmdinfo
->state
&= ~SUBMIT_DATA_OUT_URB
;
609 cmdinfo
->state
|= DATA_OUT_URB_INFLIGHT
;
610 usb_anchor_urb(cmdinfo
->data_out_urb
, &devinfo
->data_urbs
);
613 if (cmdinfo
->state
& ALLOC_CMD_URB
) {
614 cmdinfo
->cmd_urb
= uas_alloc_cmd_urb(devinfo
, gfp
, cmnd
,
616 if (!cmdinfo
->cmd_urb
)
617 return SCSI_MLQUEUE_DEVICE_BUSY
;
618 cmdinfo
->state
&= ~ALLOC_CMD_URB
;
621 if (cmdinfo
->state
& SUBMIT_CMD_URB
) {
622 usb_get_urb(cmdinfo
->cmd_urb
);
623 if (usb_submit_urb(cmdinfo
->cmd_urb
, gfp
)) {
624 scmd_printk(KERN_INFO
, cmnd
,
625 "cmd urb submission failure\n");
626 return SCSI_MLQUEUE_DEVICE_BUSY
;
628 usb_anchor_urb(cmdinfo
->cmd_urb
, &devinfo
->cmd_urbs
);
629 usb_put_urb(cmdinfo
->cmd_urb
);
630 cmdinfo
->cmd_urb
= NULL
;
631 cmdinfo
->state
&= ~SUBMIT_CMD_URB
;
632 cmdinfo
->state
|= COMMAND_INFLIGHT
;
638 static int uas_queuecommand_lck(struct scsi_cmnd
*cmnd
,
639 void (*done
)(struct scsi_cmnd
*))
641 struct scsi_device
*sdev
= cmnd
->device
;
642 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
643 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
647 BUILD_BUG_ON(sizeof(struct uas_cmd_info
) > sizeof(struct scsi_pointer
));
649 if (devinfo
->resetting
) {
650 cmnd
->result
= DID_ERROR
<< 16;
651 cmnd
->scsi_done(cmnd
);
655 spin_lock_irqsave(&devinfo
->lock
, flags
);
657 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
658 return SCSI_MLQUEUE_DEVICE_BUSY
;
661 if (blk_rq_tagged(cmnd
->request
)) {
662 cmdinfo
->stream
= cmnd
->request
->tag
+ 2;
664 devinfo
->cmnd
= cmnd
;
668 cmnd
->scsi_done
= done
;
670 cmdinfo
->state
= SUBMIT_STATUS_URB
|
671 ALLOC_CMD_URB
| SUBMIT_CMD_URB
;
673 switch (cmnd
->sc_data_direction
) {
674 case DMA_FROM_DEVICE
:
675 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
677 case DMA_BIDIRECTIONAL
:
678 cmdinfo
->state
|= ALLOC_DATA_IN_URB
| SUBMIT_DATA_IN_URB
;
680 cmdinfo
->state
|= ALLOC_DATA_OUT_URB
| SUBMIT_DATA_OUT_URB
;
685 if (!devinfo
->use_streams
) {
686 cmdinfo
->state
&= ~(SUBMIT_DATA_IN_URB
| SUBMIT_DATA_OUT_URB
);
690 err
= uas_submit_urbs(cmnd
, devinfo
, GFP_ATOMIC
);
692 /* If we did nothing, give up now */
693 if (cmdinfo
->state
& SUBMIT_STATUS_URB
) {
694 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
695 return SCSI_MLQUEUE_DEVICE_BUSY
;
697 spin_lock(&uas_work_lock
);
698 list_add_tail(&cmdinfo
->list
, &uas_work_list
);
699 cmdinfo
->state
|= IS_IN_WORK_LIST
;
700 spin_unlock(&uas_work_lock
);
701 schedule_work(&uas_work
);
704 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
708 static DEF_SCSI_QCMD(uas_queuecommand
)
710 static int uas_eh_task_mgmt(struct scsi_cmnd
*cmnd
,
711 const char *fname
, u8 function
)
713 struct Scsi_Host
*shost
= cmnd
->device
->host
;
714 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
715 u16 tag
= devinfo
->qdepth
- 1;
718 spin_lock_irqsave(&devinfo
->lock
, flags
);
719 memset(&devinfo
->response
, 0, sizeof(devinfo
->response
));
720 if (uas_submit_sense_urb(shost
, GFP_ATOMIC
, tag
)) {
721 shost_printk(KERN_INFO
, shost
,
722 "%s: %s: submit sense urb failed\n",
724 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
727 if (uas_submit_task_urb(cmnd
, GFP_ATOMIC
, function
, tag
)) {
728 shost_printk(KERN_INFO
, shost
,
729 "%s: %s: submit task mgmt urb failed\n",
731 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
734 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
736 if (usb_wait_anchor_empty_timeout(&devinfo
->sense_urbs
, 3000) == 0) {
737 shost_printk(KERN_INFO
, shost
,
738 "%s: %s timed out\n", __func__
, fname
);
741 if (be16_to_cpu(devinfo
->response
.tag
) != tag
) {
742 shost_printk(KERN_INFO
, shost
,
743 "%s: %s failed (wrong tag %d/%d)\n", __func__
,
744 fname
, be16_to_cpu(devinfo
->response
.tag
), tag
);
747 if (devinfo
->response
.response_code
!= RC_TMF_COMPLETE
) {
748 shost_printk(KERN_INFO
, shost
,
749 "%s: %s failed (rc 0x%x)\n", __func__
,
750 fname
, devinfo
->response
.response_code
);
756 static int uas_eh_abort_handler(struct scsi_cmnd
*cmnd
)
758 struct uas_cmd_info
*cmdinfo
= (void *)&cmnd
->SCp
;
759 struct uas_dev_info
*devinfo
= (void *)cmnd
->device
->hostdata
;
763 uas_log_cmd_state(cmnd
, __func__
);
764 spin_lock_irqsave(&devinfo
->lock
, flags
);
765 cmdinfo
->state
|= COMMAND_ABORTED
;
766 if (cmdinfo
->state
& IS_IN_WORK_LIST
) {
767 spin_lock(&uas_work_lock
);
768 list_del(&cmdinfo
->list
);
769 cmdinfo
->state
&= ~IS_IN_WORK_LIST
;
770 spin_unlock(&uas_work_lock
);
772 if (cmdinfo
->state
& COMMAND_INFLIGHT
) {
773 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
774 ret
= uas_eh_task_mgmt(cmnd
, "ABORT TASK", TMF_ABORT_TASK
);
776 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
777 uas_unlink_data_urbs(devinfo
, cmdinfo
);
778 spin_lock_irqsave(&devinfo
->lock
, flags
);
779 uas_try_complete(cmnd
, __func__
);
780 spin_unlock_irqrestore(&devinfo
->lock
, flags
);
786 static int uas_eh_device_reset_handler(struct scsi_cmnd
*cmnd
)
788 sdev_printk(KERN_INFO
, cmnd
->device
, "%s\n", __func__
);
789 return uas_eh_task_mgmt(cmnd
, "LOGICAL UNIT RESET",
790 TMF_LOGICAL_UNIT_RESET
);
793 static int uas_eh_bus_reset_handler(struct scsi_cmnd
*cmnd
)
795 struct scsi_device
*sdev
= cmnd
->device
;
796 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
797 struct usb_device
*udev
= devinfo
->udev
;
800 devinfo
->resetting
= 1;
801 uas_abort_work(devinfo
);
802 usb_kill_anchored_urbs(&devinfo
->cmd_urbs
);
803 usb_kill_anchored_urbs(&devinfo
->sense_urbs
);
804 usb_kill_anchored_urbs(&devinfo
->data_urbs
);
805 uas_free_streams(devinfo
);
806 err
= usb_reset_device(udev
);
808 uas_configure_endpoints(devinfo
);
809 devinfo
->resetting
= 0;
812 shost_printk(KERN_INFO
, sdev
->host
, "%s FAILED\n", __func__
);
816 shost_printk(KERN_INFO
, sdev
->host
, "%s success\n", __func__
);
820 static int uas_slave_alloc(struct scsi_device
*sdev
)
822 sdev
->hostdata
= (void *)sdev
->host
->hostdata
[0];
826 static int uas_slave_configure(struct scsi_device
*sdev
)
828 struct uas_dev_info
*devinfo
= sdev
->hostdata
;
829 scsi_set_tag_type(sdev
, MSG_ORDERED_TAG
);
830 scsi_activate_tcq(sdev
, devinfo
->qdepth
- 3);
834 static struct scsi_host_template uas_host_template
= {
835 .module
= THIS_MODULE
,
837 .queuecommand
= uas_queuecommand
,
838 .slave_alloc
= uas_slave_alloc
,
839 .slave_configure
= uas_slave_configure
,
840 .eh_abort_handler
= uas_eh_abort_handler
,
841 .eh_device_reset_handler
= uas_eh_device_reset_handler
,
842 .eh_bus_reset_handler
= uas_eh_bus_reset_handler
,
843 .can_queue
= 65536, /* Is there a limit on the _host_ ? */
845 .sg_tablesize
= SG_NONE
,
846 .cmd_per_lun
= 1, /* until we override it */
847 .skip_settle_delay
= 1,
851 static struct usb_device_id uas_usb_ids
[] = {
852 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_BULK
) },
853 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, USB_PR_UAS
) },
854 /* 0xaa is a prototype device I happen to have access to */
855 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE
, USB_SC_SCSI
, 0xaa) },
858 MODULE_DEVICE_TABLE(usb
, uas_usb_ids
);
860 static int uas_is_interface(struct usb_host_interface
*intf
)
862 return (intf
->desc
.bInterfaceClass
== USB_CLASS_MASS_STORAGE
&&
863 intf
->desc
.bInterfaceSubClass
== USB_SC_SCSI
&&
864 intf
->desc
.bInterfaceProtocol
== USB_PR_UAS
);
867 static int uas_isnt_supported(struct usb_device
*udev
)
869 struct usb_hcd
*hcd
= bus_to_hcd(udev
->bus
);
871 dev_warn(&udev
->dev
, "The driver for the USB controller %s does not "
872 "support scatter-gather which is\n",
873 hcd
->driver
->description
);
874 dev_warn(&udev
->dev
, "required by the UAS driver. Please try an"
875 "alternative USB controller if you wish to use UAS.\n");
879 static int uas_switch_interface(struct usb_device
*udev
,
880 struct usb_interface
*intf
)
883 int sg_supported
= udev
->bus
->sg_tablesize
!= 0;
885 for (i
= 0; i
< intf
->num_altsetting
; i
++) {
886 struct usb_host_interface
*alt
= &intf
->altsetting
[i
];
888 if (uas_is_interface(alt
)) {
890 return uas_isnt_supported(udev
);
891 return usb_set_interface(udev
,
892 alt
->desc
.bInterfaceNumber
,
893 alt
->desc
.bAlternateSetting
);
900 static void uas_configure_endpoints(struct uas_dev_info
*devinfo
)
902 struct usb_host_endpoint
*eps
[4] = { };
903 struct usb_interface
*intf
= devinfo
->intf
;
904 struct usb_device
*udev
= devinfo
->udev
;
905 struct usb_host_endpoint
*endpoint
= intf
->cur_altsetting
->endpoint
;
906 unsigned i
, n_endpoints
= intf
->cur_altsetting
->desc
.bNumEndpoints
;
908 devinfo
->uas_sense_old
= 0;
909 devinfo
->cmnd
= NULL
;
911 for (i
= 0; i
< n_endpoints
; i
++) {
912 unsigned char *extra
= endpoint
[i
].extra
;
913 int len
= endpoint
[i
].extralen
;
915 if (extra
[1] == USB_DT_PIPE_USAGE
) {
916 unsigned pipe_id
= extra
[2];
917 if (pipe_id
> 0 && pipe_id
< 5)
918 eps
[pipe_id
- 1] = &endpoint
[i
];
927 * Assume that if we didn't find a control pipe descriptor, we're
928 * using a device with old firmware that happens to be set up like
932 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
, 1);
933 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
, 1);
934 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
, 2);
935 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
, 2);
937 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
938 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
939 eps
[3] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
941 devinfo
->cmd_pipe
= usb_sndbulkpipe(udev
,
942 eps
[0]->desc
.bEndpointAddress
);
943 devinfo
->status_pipe
= usb_rcvbulkpipe(udev
,
944 eps
[1]->desc
.bEndpointAddress
);
945 devinfo
->data_in_pipe
= usb_rcvbulkpipe(udev
,
946 eps
[2]->desc
.bEndpointAddress
);
947 devinfo
->data_out_pipe
= usb_sndbulkpipe(udev
,
948 eps
[3]->desc
.bEndpointAddress
);
951 devinfo
->qdepth
= usb_alloc_streams(devinfo
->intf
, eps
+ 1, 3, 256,
953 if (devinfo
->qdepth
< 0) {
954 devinfo
->qdepth
= 256;
955 devinfo
->use_streams
= 0;
957 devinfo
->use_streams
= 1;
961 static void uas_free_streams(struct uas_dev_info
*devinfo
)
963 struct usb_device
*udev
= devinfo
->udev
;
964 struct usb_host_endpoint
*eps
[3];
966 eps
[0] = usb_pipe_endpoint(udev
, devinfo
->status_pipe
);
967 eps
[1] = usb_pipe_endpoint(udev
, devinfo
->data_in_pipe
);
968 eps
[2] = usb_pipe_endpoint(udev
, devinfo
->data_out_pipe
);
969 usb_free_streams(devinfo
->intf
, eps
, 3, GFP_KERNEL
);
973 * XXX: What I'd like to do here is register a SCSI host for each USB host in
974 * the system. Follow usb-storage's design of registering a SCSI host for
975 * each USB device for the moment. Can implement this by walking up the
976 * USB hierarchy until we find a USB host.
978 static int uas_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
981 struct Scsi_Host
*shost
;
982 struct uas_dev_info
*devinfo
;
983 struct usb_device
*udev
= interface_to_usbdev(intf
);
985 if (uas_switch_interface(udev
, intf
))
988 devinfo
= kmalloc(sizeof(struct uas_dev_info
), GFP_KERNEL
);
993 shost
= scsi_host_alloc(&uas_host_template
, sizeof(void *));
997 shost
->max_cmd_len
= 16 + 252;
999 shost
->max_lun
= 256;
1000 shost
->max_channel
= 0;
1001 shost
->sg_tablesize
= udev
->bus
->sg_tablesize
;
1003 devinfo
->intf
= intf
;
1004 devinfo
->udev
= udev
;
1005 devinfo
->resetting
= 0;
1006 init_usb_anchor(&devinfo
->cmd_urbs
);
1007 init_usb_anchor(&devinfo
->sense_urbs
);
1008 init_usb_anchor(&devinfo
->data_urbs
);
1009 spin_lock_init(&devinfo
->lock
);
1010 uas_configure_endpoints(devinfo
);
1012 result
= scsi_init_shared_tag_map(shost
, devinfo
->qdepth
- 3);
1016 result
= scsi_add_host(shost
, &intf
->dev
);
1020 shost
->hostdata
[0] = (unsigned long)devinfo
;
1022 scsi_scan_host(shost
);
1023 usb_set_intfdata(intf
, shost
);
1027 uas_free_streams(devinfo
);
1031 scsi_host_put(shost
);
1035 static int uas_pre_reset(struct usb_interface
*intf
)
1037 /* XXX: Need to return 1 if it's not our device in error handling */
1041 static int uas_post_reset(struct usb_interface
*intf
)
1043 /* XXX: Need to return 1 if it's not our device in error handling */
1047 static void uas_disconnect(struct usb_interface
*intf
)
1049 struct Scsi_Host
*shost
= usb_get_intfdata(intf
);
1050 struct uas_dev_info
*devinfo
= (void *)shost
->hostdata
[0];
1052 devinfo
->resetting
= 1;
1053 uas_abort_work(devinfo
);
1054 usb_kill_anchored_urbs(&devinfo
->cmd_urbs
);
1055 usb_kill_anchored_urbs(&devinfo
->sense_urbs
);
1056 usb_kill_anchored_urbs(&devinfo
->data_urbs
);
1057 scsi_remove_host(shost
);
1058 uas_free_streams(devinfo
);
1063 * XXX: Should this plug into libusual so we can auto-upgrade devices from
1066 static struct usb_driver uas_driver
= {
1069 .disconnect
= uas_disconnect
,
1070 .pre_reset
= uas_pre_reset
,
1071 .post_reset
= uas_post_reset
,
1072 .id_table
= uas_usb_ids
,
1075 module_usb_driver(uas_driver
);
1077 MODULE_LICENSE("GPL");
1078 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");