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 * The r00-r01c specs define this version of the SENSE IU data structure.
37 * It's still in use by several different firmware releases.
45 __u8 service_response;
46 __u8 sense[SCSI_SENSE_BUFFERSIZE];
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;
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;
61 struct scsi_cmnd *cmnd[MAX_CMNDS];
63 struct work_struct work;
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),
82 /* Overrides scsi_pointer */
87 struct urb *data_in_urb;
88 struct urb *data_out_urb;
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);
99 static void uas_do_work(struct work_struct *work)
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;
108 spin_lock_irqsave(&devinfo->lock, flags);
110 if (devinfo->resetting)
113 for (i = 0; i < devinfo->qdepth; i++) {
114 if (!devinfo->cmnd[i])
117 cmnd = devinfo->cmnd[i];
118 cmdinfo = (void *)&cmnd->SCp;
120 if (!(cmdinfo->state & IS_IN_WORK_LIST))
123 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
125 cmdinfo->state &= ~IS_IN_WORK_LIST;
127 schedule_work(&devinfo->work);
130 spin_unlock_irqrestore(&devinfo->lock, flags);
133 static void uas_add_work(struct uas_cmd_info *cmdinfo)
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;
139 lockdep_assert_held(&devinfo->lock);
140 cmdinfo->state |= IS_IN_WORK_LIST;
141 schedule_work(&devinfo->work);
144 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
146 struct uas_cmd_info *cmdinfo;
147 struct scsi_cmnd *cmnd;
151 spin_lock_irqsave(&devinfo->lock, flags);
152 for (i = 0; i < devinfo->qdepth; i++) {
153 if (!devinfo->cmnd[i])
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__);
165 spin_unlock_irqrestore(&devinfo->lock, flags);
168 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
170 struct sense_iu *sense_iu = urb->transfer_buffer;
171 struct scsi_device *sdev = cmnd->device;
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;
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);
185 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
188 cmnd->result = sense_iu->status;
191 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
193 struct sense_iu_old *sense_iu = urb->transfer_buffer;
194 struct scsi_device *sdev = cmnd->device;
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;
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);
208 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
211 cmnd->result = sense_iu->status;
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.
218 static int uas_get_tag(struct scsi_cmnd *cmnd)
222 if (blk_rq_tagged(cmnd->request))
223 tag = cmnd->request->tag + 2;
230 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
232 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
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" : "");
252 static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
254 struct uas_cmd_info *cmdinfo;
259 cmdinfo = (void *)&cmnd->SCp;
261 if (cmdinfo->state & SUBMIT_CMD_URB)
262 usb_free_urb(cmdinfo->cmd_urb);
264 /* data urbs may have never gotten their submit flag set */
265 if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
266 usb_free_urb(cmdinfo->data_in_urb);
267 if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
268 usb_free_urb(cmdinfo->data_out_urb);
271 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
273 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
274 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
276 lockdep_assert_held(&devinfo->lock);
277 if (cmdinfo->state & (COMMAND_INFLIGHT |
278 DATA_IN_URB_INFLIGHT |
279 DATA_OUT_URB_INFLIGHT |
282 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
283 cmdinfo->state |= COMMAND_COMPLETED;
284 devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
285 uas_free_unsubmitted_urbs(cmnd);
286 cmnd->scsi_done(cmnd);
290 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
293 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
296 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
297 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
299 uas_add_work(cmdinfo);
303 static void uas_stat_cmplt(struct urb *urb)
305 struct iu *iu = urb->transfer_buffer;
306 struct Scsi_Host *shost = urb->context;
307 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
308 struct urb *data_in_urb = NULL;
309 struct urb *data_out_urb = NULL;
310 struct scsi_cmnd *cmnd;
311 struct uas_cmd_info *cmdinfo;
315 spin_lock_irqsave(&devinfo->lock, flags);
317 if (devinfo->resetting)
321 if (urb->status == -ENOENT) {
322 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
325 dev_err(&urb->dev->dev, "stat urb: status %d\n",
331 idx = be16_to_cpup(&iu->tag) - 1;
332 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
333 dev_err(&urb->dev->dev,
334 "stat urb: no pending cmd for tag %d\n", idx + 1);
338 cmnd = devinfo->cmnd[idx];
339 cmdinfo = (void *)&cmnd->SCp;
341 if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
342 scmd_printk(KERN_ERR, cmnd, "unexpected status cmplt\n");
348 if (urb->actual_length < 16)
349 devinfo->uas_sense_old = 1;
350 if (devinfo->uas_sense_old)
351 uas_sense_old(urb, cmnd);
353 uas_sense(urb, cmnd);
354 if (cmnd->result != 0) {
355 /* cancel data transfers on error */
356 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
357 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
359 cmdinfo->state &= ~COMMAND_INFLIGHT;
360 uas_try_complete(cmnd, __func__);
362 case IU_ID_READ_READY:
363 if (!cmdinfo->data_in_urb ||
364 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
365 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
368 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
370 case IU_ID_WRITE_READY:
371 if (!cmdinfo->data_out_urb ||
372 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
373 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
376 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
379 scmd_printk(KERN_ERR, cmnd,
380 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
384 spin_unlock_irqrestore(&devinfo->lock, flags);
386 /* Unlinking of data urbs must be done without holding the lock */
388 usb_unlink_urb(data_in_urb);
389 usb_put_urb(data_in_urb);
392 usb_unlink_urb(data_out_urb);
393 usb_put_urb(data_out_urb);
397 static void uas_data_cmplt(struct urb *urb)
399 struct scsi_cmnd *cmnd = urb->context;
400 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
401 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
402 struct scsi_data_buffer *sdb = NULL;
405 spin_lock_irqsave(&devinfo->lock, flags);
407 if (cmdinfo->data_in_urb == urb) {
409 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
410 cmdinfo->data_in_urb = NULL;
411 } else if (cmdinfo->data_out_urb == urb) {
412 sdb = scsi_out(cmnd);
413 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
414 cmdinfo->data_out_urb = NULL;
421 if (devinfo->resetting)
424 /* Data urbs should not complete before the cmd urb is submitted */
425 if (cmdinfo->state & SUBMIT_CMD_URB) {
426 scmd_printk(KERN_ERR, cmnd, "unexpected data cmplt\n");
431 if (urb->status != -ECONNRESET) {
432 uas_log_cmd_state(cmnd, __func__);
433 scmd_printk(KERN_ERR, cmnd,
434 "data cmplt err %d stream %d\n",
435 urb->status, urb->stream_id);
437 /* error: no data transfered */
438 sdb->resid = sdb->length;
440 sdb->resid = sdb->length - urb->actual_length;
442 uas_try_complete(cmnd, __func__);
445 spin_unlock_irqrestore(&devinfo->lock, flags);
448 static void uas_cmd_cmplt(struct urb *urb)
451 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
456 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
457 unsigned int pipe, u16 stream_id,
458 struct scsi_cmnd *cmnd,
459 enum dma_data_direction dir)
461 struct usb_device *udev = devinfo->udev;
462 struct urb *urb = usb_alloc_urb(0, gfp);
463 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
464 ? scsi_in(cmnd) : scsi_out(cmnd);
468 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
469 uas_data_cmplt, cmnd);
470 urb->stream_id = stream_id;
471 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
472 urb->sg = sdb->table.sgl;
477 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
478 struct Scsi_Host *shost, u16 stream_id)
480 struct usb_device *udev = devinfo->udev;
481 struct urb *urb = usb_alloc_urb(0, gfp);
487 iu = kzalloc(sizeof(*iu), gfp);
491 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
492 uas_stat_cmplt, shost);
493 urb->stream_id = stream_id;
494 urb->transfer_flags |= URB_FREE_BUFFER;
502 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
503 struct scsi_cmnd *cmnd)
505 struct usb_device *udev = devinfo->udev;
506 struct scsi_device *sdev = cmnd->device;
507 struct urb *urb = usb_alloc_urb(0, gfp);
508 struct command_iu *iu;
514 len = cmnd->cmd_len - 16;
518 iu = kzalloc(sizeof(*iu) + len, gfp);
522 iu->iu_id = IU_ID_COMMAND;
523 iu->tag = cpu_to_be16(uas_get_tag(cmnd));
524 iu->prio_attr = UAS_SIMPLE_TAG;
526 int_to_scsilun(sdev->lun, &iu->lun);
527 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
529 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
530 uas_cmd_cmplt, NULL);
531 urb->transfer_flags |= URB_FREE_BUFFER;
540 * Why should I request the Status IU before sending the Command IU? Spec
541 * says to, but also says the device may receive them in any order. Seems
545 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
546 gfp_t gfp, unsigned int stream)
548 struct Scsi_Host *shost = cmnd->device->host;
549 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
553 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
556 usb_anchor_urb(urb, &devinfo->sense_urbs);
557 err = usb_submit_urb(urb, gfp);
559 usb_unanchor_urb(urb);
560 uas_log_cmd_state(cmnd, __func__);
561 shost_printk(KERN_INFO, shost,
562 "sense urb submission error %d stream %d\n",
570 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
571 struct uas_dev_info *devinfo, gfp_t gfp)
573 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
577 lockdep_assert_held(&devinfo->lock);
578 if (cmdinfo->state & SUBMIT_STATUS_URB) {
579 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
581 return SCSI_MLQUEUE_DEVICE_BUSY;
582 cmdinfo->state &= ~SUBMIT_STATUS_URB;
585 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
586 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
587 devinfo->data_in_pipe, cmdinfo->stream,
588 cmnd, DMA_FROM_DEVICE);
589 if (!cmdinfo->data_in_urb)
590 return SCSI_MLQUEUE_DEVICE_BUSY;
591 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
594 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
595 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
596 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
598 usb_unanchor_urb(cmdinfo->data_in_urb);
599 uas_log_cmd_state(cmnd, __func__);
600 scmd_printk(KERN_INFO, cmnd,
601 "data in urb submission error %d stream %d\n",
602 err, cmdinfo->data_in_urb->stream_id);
603 return SCSI_MLQUEUE_DEVICE_BUSY;
605 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
606 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
609 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
610 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
611 devinfo->data_out_pipe, cmdinfo->stream,
612 cmnd, DMA_TO_DEVICE);
613 if (!cmdinfo->data_out_urb)
614 return SCSI_MLQUEUE_DEVICE_BUSY;
615 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
618 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
619 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
620 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
622 usb_unanchor_urb(cmdinfo->data_out_urb);
623 uas_log_cmd_state(cmnd, __func__);
624 scmd_printk(KERN_INFO, cmnd,
625 "data out urb submission error %d stream %d\n",
626 err, cmdinfo->data_out_urb->stream_id);
627 return SCSI_MLQUEUE_DEVICE_BUSY;
629 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
630 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
633 if (cmdinfo->state & ALLOC_CMD_URB) {
634 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
635 if (!cmdinfo->cmd_urb)
636 return SCSI_MLQUEUE_DEVICE_BUSY;
637 cmdinfo->state &= ~ALLOC_CMD_URB;
640 if (cmdinfo->state & SUBMIT_CMD_URB) {
641 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
642 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
644 usb_unanchor_urb(cmdinfo->cmd_urb);
645 uas_log_cmd_state(cmnd, __func__);
646 scmd_printk(KERN_INFO, cmnd,
647 "cmd urb submission error %d\n", err);
648 return SCSI_MLQUEUE_DEVICE_BUSY;
650 cmdinfo->cmd_urb = NULL;
651 cmdinfo->state &= ~SUBMIT_CMD_URB;
652 cmdinfo->state |= COMMAND_INFLIGHT;
658 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
659 void (*done)(struct scsi_cmnd *))
661 struct scsi_device *sdev = cmnd->device;
662 struct uas_dev_info *devinfo = sdev->hostdata;
663 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
668 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
670 /* Re-check scsi_block_requests now that we've the host-lock */
671 if (cmnd->device->host->host_self_blocked)
672 return SCSI_MLQUEUE_DEVICE_BUSY;
674 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
675 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
676 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
677 sizeof(usb_stor_sense_invalidCDB));
678 cmnd->result = SAM_STAT_CHECK_CONDITION;
679 cmnd->scsi_done(cmnd);
683 spin_lock_irqsave(&devinfo->lock, flags);
685 if (devinfo->resetting) {
686 cmnd->result = DID_ERROR << 16;
687 cmnd->scsi_done(cmnd);
688 spin_unlock_irqrestore(&devinfo->lock, flags);
692 stream = uas_get_tag(cmnd);
693 if (devinfo->cmnd[stream - 1]) {
694 spin_unlock_irqrestore(&devinfo->lock, flags);
695 return SCSI_MLQUEUE_DEVICE_BUSY;
698 cmnd->scsi_done = done;
700 memset(cmdinfo, 0, sizeof(*cmdinfo));
701 cmdinfo->stream = stream;
702 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
704 switch (cmnd->sc_data_direction) {
705 case DMA_FROM_DEVICE:
706 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
708 case DMA_BIDIRECTIONAL:
709 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
711 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
716 if (!devinfo->use_streams) {
717 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
721 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
723 /* If we did nothing, give up now */
724 if (cmdinfo->state & SUBMIT_STATUS_URB) {
725 spin_unlock_irqrestore(&devinfo->lock, flags);
726 return SCSI_MLQUEUE_DEVICE_BUSY;
728 uas_add_work(cmdinfo);
731 devinfo->cmnd[stream - 1] = cmnd;
732 spin_unlock_irqrestore(&devinfo->lock, flags);
736 static DEF_SCSI_QCMD(uas_queuecommand)
739 * For now we do not support actually sending an abort to the device, so
740 * this eh always fails. Still we must define it to make sure that we've
741 * dropped all references to the cmnd in question once this function exits.
743 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
745 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
746 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
747 struct urb *data_in_urb = NULL;
748 struct urb *data_out_urb = NULL;
751 spin_lock_irqsave(&devinfo->lock, flags);
753 uas_log_cmd_state(cmnd, __func__);
755 /* Ensure that try_complete does not call scsi_done */
756 cmdinfo->state |= COMMAND_ABORTED;
758 /* Drop all refs to this cmnd, kill data urbs to break their ref */
759 devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
760 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
761 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
762 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
763 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
765 uas_free_unsubmitted_urbs(cmnd);
767 spin_unlock_irqrestore(&devinfo->lock, flags);
770 usb_kill_urb(data_in_urb);
771 usb_put_urb(data_in_urb);
774 usb_kill_urb(data_out_urb);
775 usb_put_urb(data_out_urb);
781 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
783 struct scsi_device *sdev = cmnd->device;
784 struct uas_dev_info *devinfo = sdev->hostdata;
785 struct usb_device *udev = devinfo->udev;
789 err = usb_lock_device_for_reset(udev, devinfo->intf);
791 shost_printk(KERN_ERR, sdev->host,
792 "%s FAILED to get lock err %d\n", __func__, err);
796 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
798 spin_lock_irqsave(&devinfo->lock, flags);
799 devinfo->resetting = 1;
800 spin_unlock_irqrestore(&devinfo->lock, flags);
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_zap_pending(devinfo, DID_RESET);
807 err = usb_reset_device(udev);
809 spin_lock_irqsave(&devinfo->lock, flags);
810 devinfo->resetting = 0;
811 spin_unlock_irqrestore(&devinfo->lock, flags);
813 usb_unlock_device(udev);
816 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
820 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
824 static int uas_slave_alloc(struct scsi_device *sdev)
826 sdev->hostdata = (void *)sdev->host->hostdata;
828 /* USB has unusual DMA-alignment requirements: Although the
829 * starting address of each scatter-gather element doesn't matter,
830 * the length of each element except the last must be divisible
831 * by the Bulk maxpacket value. There's currently no way to
832 * express this by block-layer constraints, so we'll cop out
833 * and simply require addresses to be aligned at 512-byte
834 * boundaries. This is okay since most block I/O involves
835 * hardware sectors that are multiples of 512 bytes in length,
836 * and since host controllers up through USB 2.0 have maxpacket
837 * values no larger than 512.
839 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
840 * values can be as large as 2048. To make that work properly
841 * will require changes to the block layer.
843 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
848 static int uas_slave_configure(struct scsi_device *sdev)
850 struct uas_dev_info *devinfo = sdev->hostdata;
852 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
853 sdev->no_report_opcodes = 1;
855 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
856 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
860 static struct scsi_host_template uas_host_template = {
861 .module = THIS_MODULE,
863 .queuecommand = uas_queuecommand,
864 .slave_alloc = uas_slave_alloc,
865 .slave_configure = uas_slave_configure,
866 .eh_abort_handler = uas_eh_abort_handler,
867 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
868 .can_queue = 65536, /* Is there a limit on the _host_ ? */
870 .sg_tablesize = SG_NONE,
871 .cmd_per_lun = 1, /* until we override it */
872 .skip_settle_delay = 1,
876 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
877 vendorName, productName, useProtocol, useTransport, \
878 initFunction, flags) \
879 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
880 .driver_info = (flags) }
882 static struct usb_device_id uas_usb_ids[] = {
883 # include "unusual_uas.h"
884 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
885 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
886 /* 0xaa is a prototype device I happen to have access to */
887 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
890 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
894 static int uas_switch_interface(struct usb_device *udev,
895 struct usb_interface *intf)
899 alt = uas_find_uas_alt_setting(intf);
903 return usb_set_interface(udev,
904 intf->altsetting[0].desc.bInterfaceNumber, alt);
907 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
909 struct usb_host_endpoint *eps[4] = { };
910 struct usb_device *udev = devinfo->udev;
913 devinfo->uas_sense_old = 0;
915 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
919 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
920 usb_endpoint_num(&eps[0]->desc));
921 devinfo->status_pipe = usb_rcvbulkpipe(udev,
922 usb_endpoint_num(&eps[1]->desc));
923 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
924 usb_endpoint_num(&eps[2]->desc));
925 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
926 usb_endpoint_num(&eps[3]->desc));
928 if (udev->speed != USB_SPEED_SUPER) {
929 devinfo->qdepth = 32;
930 devinfo->use_streams = 0;
932 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
933 3, MAX_CMNDS, GFP_NOIO);
934 if (devinfo->qdepth < 0)
935 return devinfo->qdepth;
936 devinfo->use_streams = 1;
942 static void uas_free_streams(struct uas_dev_info *devinfo)
944 struct usb_device *udev = devinfo->udev;
945 struct usb_host_endpoint *eps[3];
947 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
948 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
949 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
950 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
953 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
955 int result = -ENOMEM;
956 struct Scsi_Host *shost = NULL;
957 struct uas_dev_info *devinfo;
958 struct usb_device *udev = interface_to_usbdev(intf);
960 if (!uas_use_uas_driver(intf, id))
963 if (uas_switch_interface(udev, intf))
966 shost = scsi_host_alloc(&uas_host_template,
967 sizeof(struct uas_dev_info));
971 shost->max_cmd_len = 16 + 252;
973 shost->max_lun = 256;
974 shost->max_channel = 0;
975 shost->sg_tablesize = udev->bus->sg_tablesize;
977 devinfo = (struct uas_dev_info *)shost->hostdata;
978 devinfo->intf = intf;
979 devinfo->udev = udev;
980 devinfo->resetting = 0;
981 devinfo->shutdown = 0;
982 devinfo->flags = id->driver_info;
983 usb_stor_adjust_quirks(udev, &devinfo->flags);
984 init_usb_anchor(&devinfo->cmd_urbs);
985 init_usb_anchor(&devinfo->sense_urbs);
986 init_usb_anchor(&devinfo->data_urbs);
987 spin_lock_init(&devinfo->lock);
988 INIT_WORK(&devinfo->work, uas_do_work);
990 result = uas_configure_endpoints(devinfo);
994 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
998 usb_set_intfdata(intf, shost);
999 result = scsi_add_host(shost, &intf->dev);
1003 scsi_scan_host(shost);
1007 uas_free_streams(devinfo);
1008 usb_set_intfdata(intf, NULL);
1010 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1012 scsi_host_put(shost);
1016 static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
1018 unsigned long flags;
1021 spin_lock_irqsave(&devinfo->lock, flags);
1023 for (i = 0; i < devinfo->qdepth; i++) {
1024 if (devinfo->cmnd[i]) {
1025 r = 0; /* Not empty */
1030 spin_unlock_irqrestore(&devinfo->lock, flags);
1036 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
1037 * get empty while there still is more work to do due to sense-urbs completing
1038 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
1040 static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
1042 unsigned long start_time;
1045 start_time = jiffies;
1047 flush_work(&devinfo->work);
1049 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1053 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1057 if (time_after(jiffies, start_time + 5 * HZ))
1059 } while (!uas_cmnd_list_empty(devinfo));
1064 static int uas_pre_reset(struct usb_interface *intf)
1066 struct Scsi_Host *shost = usb_get_intfdata(intf);
1067 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1068 unsigned long flags;
1070 if (devinfo->shutdown)
1073 /* Block new requests */
1074 spin_lock_irqsave(shost->host_lock, flags);
1075 scsi_block_requests(shost);
1076 spin_unlock_irqrestore(shost->host_lock, flags);
1078 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1079 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1080 scsi_unblock_requests(shost);
1084 uas_free_streams(devinfo);
1089 static int uas_post_reset(struct usb_interface *intf)
1091 struct Scsi_Host *shost = usb_get_intfdata(intf);
1092 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1093 unsigned long flags;
1095 if (devinfo->shutdown)
1098 if (uas_configure_endpoints(devinfo) != 0) {
1099 shost_printk(KERN_ERR, shost,
1100 "%s: alloc streams error after reset", __func__);
1104 spin_lock_irqsave(shost->host_lock, flags);
1105 scsi_report_bus_reset(shost, 0);
1106 spin_unlock_irqrestore(shost->host_lock, flags);
1108 scsi_unblock_requests(shost);
1113 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1115 struct Scsi_Host *shost = usb_get_intfdata(intf);
1116 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1118 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1119 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1126 static int uas_resume(struct usb_interface *intf)
1131 static int uas_reset_resume(struct usb_interface *intf)
1133 struct Scsi_Host *shost = usb_get_intfdata(intf);
1134 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1135 unsigned long flags;
1137 if (uas_configure_endpoints(devinfo) != 0) {
1138 shost_printk(KERN_ERR, shost,
1139 "%s: alloc streams error after reset", __func__);
1143 spin_lock_irqsave(shost->host_lock, flags);
1144 scsi_report_bus_reset(shost, 0);
1145 spin_unlock_irqrestore(shost->host_lock, flags);
1150 static void uas_disconnect(struct usb_interface *intf)
1152 struct Scsi_Host *shost = usb_get_intfdata(intf);
1153 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1154 unsigned long flags;
1156 spin_lock_irqsave(&devinfo->lock, flags);
1157 devinfo->resetting = 1;
1158 spin_unlock_irqrestore(&devinfo->lock, flags);
1160 cancel_work_sync(&devinfo->work);
1161 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1162 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1163 usb_kill_anchored_urbs(&devinfo->data_urbs);
1164 uas_zap_pending(devinfo, DID_NO_CONNECT);
1166 scsi_remove_host(shost);
1167 uas_free_streams(devinfo);
1168 scsi_host_put(shost);
1172 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1173 * hang on reboot when the device is still in uas mode. Note the reset is
1174 * necessary as some devices won't revert to usb-storage mode without it.
1176 static void uas_shutdown(struct device *dev)
1178 struct usb_interface *intf = to_usb_interface(dev);
1179 struct usb_device *udev = interface_to_usbdev(intf);
1180 struct Scsi_Host *shost = usb_get_intfdata(intf);
1181 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1183 if (system_state != SYSTEM_RESTART)
1186 devinfo->shutdown = 1;
1187 uas_free_streams(devinfo);
1188 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1189 usb_reset_device(udev);
1192 static struct usb_driver uas_driver = {
1195 .disconnect = uas_disconnect,
1196 .pre_reset = uas_pre_reset,
1197 .post_reset = uas_post_reset,
1198 .suspend = uas_suspend,
1199 .resume = uas_resume,
1200 .reset_resume = uas_reset_resume,
1201 .drvwrap.driver.shutdown = uas_shutdown,
1202 .id_table = uas_usb_ids,
1205 module_usb_driver(uas_driver);
1207 MODULE_LICENSE("GPL");
1209 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");