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 *caller);
83 static void uas_do_work(struct work_struct *work)
85 struct uas_dev_info *devinfo =
86 container_of(work, struct uas_dev_info, work);
87 struct uas_cmd_info *cmdinfo;
88 struct scsi_cmnd *cmnd;
92 spin_lock_irqsave(&devinfo->lock, flags);
94 if (devinfo->resetting)
97 for (i = 0; i < devinfo->qdepth; i++) {
98 if (!devinfo->cmnd[i])
101 cmnd = devinfo->cmnd[i];
102 cmdinfo = (void *)&cmnd->SCp;
104 if (!(cmdinfo->state & IS_IN_WORK_LIST))
107 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
109 cmdinfo->state &= ~IS_IN_WORK_LIST;
111 schedule_work(&devinfo->work);
114 spin_unlock_irqrestore(&devinfo->lock, flags);
117 static void uas_add_work(struct uas_cmd_info *cmdinfo)
119 struct scsi_pointer *scp = (void *)cmdinfo;
120 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
121 struct uas_dev_info *devinfo = cmnd->device->hostdata;
123 lockdep_assert_held(&devinfo->lock);
124 cmdinfo->state |= IS_IN_WORK_LIST;
125 schedule_work(&devinfo->work);
128 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
130 struct uas_cmd_info *cmdinfo;
131 struct scsi_cmnd *cmnd;
135 spin_lock_irqsave(&devinfo->lock, flags);
136 for (i = 0; i < devinfo->qdepth; i++) {
137 if (!devinfo->cmnd[i])
140 cmnd = devinfo->cmnd[i];
141 cmdinfo = (void *)&cmnd->SCp;
142 uas_log_cmd_state(cmnd, __func__);
143 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
144 cmdinfo->state &= ~COMMAND_INFLIGHT;
145 cmnd->result = result << 16;
146 err = uas_try_complete(cmnd, __func__);
149 spin_unlock_irqrestore(&devinfo->lock, flags);
152 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
154 struct sense_iu *sense_iu = urb->transfer_buffer;
155 struct scsi_device *sdev = cmnd->device;
157 if (urb->actual_length > 16) {
158 unsigned len = be16_to_cpup(&sense_iu->len);
159 if (len + 16 != urb->actual_length) {
160 int newlen = min(len + 16, urb->actual_length) - 16;
163 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
164 "disagrees with IU sense data length %d, "
165 "using %d bytes of sense data\n", __func__,
166 urb->actual_length, len, newlen);
169 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
172 cmnd->result = sense_iu->status;
176 * scsi-tags go from 0 - (nr_tags - 1), uas tags need to match stream-ids,
177 * which go from 1 - nr_streams. And we use 1 for untagged commands.
179 static int uas_get_tag(struct scsi_cmnd *cmnd)
183 if (blk_rq_tagged(cmnd->request))
184 tag = cmnd->request->tag + 2;
191 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
193 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
195 scmd_printk(KERN_INFO, cmnd,
196 "%s tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
197 caller, uas_get_tag(cmnd),
198 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
199 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
200 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
201 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
202 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
203 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
204 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
205 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
206 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
207 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
208 (ci->state & COMMAND_ABORTED) ? " abort" : "",
209 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
210 scsi_print_command(cmnd);
213 static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
215 struct uas_cmd_info *cmdinfo;
220 cmdinfo = (void *)&cmnd->SCp;
222 if (cmdinfo->state & SUBMIT_CMD_URB)
223 usb_free_urb(cmdinfo->cmd_urb);
225 /* data urbs may have never gotten their submit flag set */
226 if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
227 usb_free_urb(cmdinfo->data_in_urb);
228 if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
229 usb_free_urb(cmdinfo->data_out_urb);
232 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
234 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
235 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
237 lockdep_assert_held(&devinfo->lock);
238 if (cmdinfo->state & (COMMAND_INFLIGHT |
239 DATA_IN_URB_INFLIGHT |
240 DATA_OUT_URB_INFLIGHT |
243 devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
244 uas_free_unsubmitted_urbs(cmnd);
245 cmnd->scsi_done(cmnd);
249 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
252 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
255 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
256 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
258 uas_add_work(cmdinfo);
262 static void uas_stat_cmplt(struct urb *urb)
264 struct iu *iu = urb->transfer_buffer;
265 struct Scsi_Host *shost = urb->context;
266 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
267 struct urb *data_in_urb = NULL;
268 struct urb *data_out_urb = NULL;
269 struct scsi_cmnd *cmnd;
270 struct uas_cmd_info *cmdinfo;
274 spin_lock_irqsave(&devinfo->lock, flags);
276 if (devinfo->resetting)
280 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
281 dev_err(&urb->dev->dev, "stat urb: status %d\n",
287 idx = be16_to_cpup(&iu->tag) - 1;
288 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
289 dev_err(&urb->dev->dev,
290 "stat urb: no pending cmd for tag %d\n", idx + 1);
294 cmnd = devinfo->cmnd[idx];
295 cmdinfo = (void *)&cmnd->SCp;
297 if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
298 scmd_printk(KERN_ERR, cmnd, "unexpected status cmplt\n");
304 uas_sense(urb, cmnd);
305 if (cmnd->result != 0) {
306 /* cancel data transfers on error */
307 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
308 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
310 cmdinfo->state &= ~COMMAND_INFLIGHT;
311 uas_try_complete(cmnd, __func__);
313 case IU_ID_READ_READY:
314 if (!cmdinfo->data_in_urb ||
315 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
316 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
319 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
321 case IU_ID_WRITE_READY:
322 if (!cmdinfo->data_out_urb ||
323 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
324 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
327 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
330 scmd_printk(KERN_ERR, cmnd,
331 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
335 spin_unlock_irqrestore(&devinfo->lock, flags);
337 /* Unlinking of data urbs must be done without holding the lock */
339 usb_unlink_urb(data_in_urb);
340 usb_put_urb(data_in_urb);
343 usb_unlink_urb(data_out_urb);
344 usb_put_urb(data_out_urb);
348 static void uas_data_cmplt(struct urb *urb)
350 struct scsi_cmnd *cmnd = urb->context;
351 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
352 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
353 struct scsi_data_buffer *sdb = NULL;
356 spin_lock_irqsave(&devinfo->lock, flags);
358 if (cmdinfo->data_in_urb == urb) {
360 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
361 cmdinfo->data_in_urb = NULL;
362 } else if (cmdinfo->data_out_urb == urb) {
363 sdb = scsi_out(cmnd);
364 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
365 cmdinfo->data_out_urb = NULL;
372 if (devinfo->resetting)
375 /* Data urbs should not complete before the cmd urb is submitted */
376 if (cmdinfo->state & SUBMIT_CMD_URB) {
377 scmd_printk(KERN_ERR, cmnd, "unexpected data cmplt\n");
382 if (urb->status != -ENOENT && urb->status != -ECONNRESET) {
383 uas_log_cmd_state(cmnd, __func__);
384 scmd_printk(KERN_ERR, cmnd,
385 "data cmplt err %d stream %d\n",
386 urb->status, urb->stream_id);
388 /* error: no data transfered */
389 sdb->resid = sdb->length;
391 sdb->resid = sdb->length - urb->actual_length;
393 uas_try_complete(cmnd, __func__);
396 spin_unlock_irqrestore(&devinfo->lock, flags);
399 static void uas_cmd_cmplt(struct urb *urb)
402 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
407 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
408 unsigned int pipe, u16 stream_id,
409 struct scsi_cmnd *cmnd,
410 enum dma_data_direction dir)
412 struct usb_device *udev = devinfo->udev;
413 struct urb *urb = usb_alloc_urb(0, gfp);
414 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
415 ? scsi_in(cmnd) : scsi_out(cmnd);
419 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
420 uas_data_cmplt, cmnd);
421 urb->stream_id = stream_id;
422 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
423 urb->sg = sdb->table.sgl;
428 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
429 struct Scsi_Host *shost, u16 stream_id)
431 struct usb_device *udev = devinfo->udev;
432 struct urb *urb = usb_alloc_urb(0, gfp);
438 iu = kzalloc(sizeof(*iu), gfp);
442 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
443 uas_stat_cmplt, shost);
444 urb->stream_id = stream_id;
445 urb->transfer_flags |= URB_FREE_BUFFER;
453 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
454 struct scsi_cmnd *cmnd)
456 struct usb_device *udev = devinfo->udev;
457 struct scsi_device *sdev = cmnd->device;
458 struct urb *urb = usb_alloc_urb(0, gfp);
459 struct command_iu *iu;
465 len = cmnd->cmd_len - 16;
469 iu = kzalloc(sizeof(*iu) + len, gfp);
473 iu->iu_id = IU_ID_COMMAND;
474 iu->tag = cpu_to_be16(uas_get_tag(cmnd));
475 iu->prio_attr = UAS_SIMPLE_TAG;
477 int_to_scsilun(sdev->lun, &iu->lun);
478 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
480 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
481 uas_cmd_cmplt, NULL);
482 urb->transfer_flags |= URB_FREE_BUFFER;
491 * Why should I request the Status IU before sending the Command IU? Spec
492 * says to, but also says the device may receive them in any order. Seems
496 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
497 gfp_t gfp, unsigned int stream)
499 struct Scsi_Host *shost = cmnd->device->host;
500 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
504 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
507 usb_anchor_urb(urb, &devinfo->sense_urbs);
508 err = usb_submit_urb(urb, gfp);
510 usb_unanchor_urb(urb);
511 uas_log_cmd_state(cmnd, __func__);
512 shost_printk(KERN_INFO, shost,
513 "sense urb submission error %d stream %d\n",
521 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
522 struct uas_dev_info *devinfo, gfp_t gfp)
524 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
528 lockdep_assert_held(&devinfo->lock);
529 if (cmdinfo->state & SUBMIT_STATUS_URB) {
530 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
532 return SCSI_MLQUEUE_DEVICE_BUSY;
533 cmdinfo->state &= ~SUBMIT_STATUS_URB;
536 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
537 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
538 devinfo->data_in_pipe, cmdinfo->stream,
539 cmnd, DMA_FROM_DEVICE);
540 if (!cmdinfo->data_in_urb)
541 return SCSI_MLQUEUE_DEVICE_BUSY;
542 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
545 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
546 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
547 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
549 usb_unanchor_urb(cmdinfo->data_in_urb);
550 uas_log_cmd_state(cmnd, __func__);
551 scmd_printk(KERN_INFO, cmnd,
552 "data in urb submission error %d stream %d\n",
553 err, cmdinfo->data_in_urb->stream_id);
554 return SCSI_MLQUEUE_DEVICE_BUSY;
556 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
557 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
560 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
561 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
562 devinfo->data_out_pipe, cmdinfo->stream,
563 cmnd, DMA_TO_DEVICE);
564 if (!cmdinfo->data_out_urb)
565 return SCSI_MLQUEUE_DEVICE_BUSY;
566 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
569 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
570 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
571 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
573 usb_unanchor_urb(cmdinfo->data_out_urb);
574 uas_log_cmd_state(cmnd, __func__);
575 scmd_printk(KERN_INFO, cmnd,
576 "data out urb submission error %d stream %d\n",
577 err, cmdinfo->data_out_urb->stream_id);
578 return SCSI_MLQUEUE_DEVICE_BUSY;
580 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
581 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
584 if (cmdinfo->state & ALLOC_CMD_URB) {
585 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
586 if (!cmdinfo->cmd_urb)
587 return SCSI_MLQUEUE_DEVICE_BUSY;
588 cmdinfo->state &= ~ALLOC_CMD_URB;
591 if (cmdinfo->state & SUBMIT_CMD_URB) {
592 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
593 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
595 usb_unanchor_urb(cmdinfo->cmd_urb);
596 uas_log_cmd_state(cmnd, __func__);
597 scmd_printk(KERN_INFO, cmnd,
598 "cmd urb submission error %d\n", err);
599 return SCSI_MLQUEUE_DEVICE_BUSY;
601 cmdinfo->cmd_urb = NULL;
602 cmdinfo->state &= ~SUBMIT_CMD_URB;
603 cmdinfo->state |= COMMAND_INFLIGHT;
609 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
610 void (*done)(struct scsi_cmnd *))
612 struct scsi_device *sdev = cmnd->device;
613 struct uas_dev_info *devinfo = sdev->hostdata;
614 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
619 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
621 /* Re-check scsi_block_requests now that we've the host-lock */
622 if (cmnd->device->host->host_self_blocked)
623 return SCSI_MLQUEUE_DEVICE_BUSY;
625 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
626 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
627 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
628 sizeof(usb_stor_sense_invalidCDB));
629 cmnd->result = SAM_STAT_CHECK_CONDITION;
630 cmnd->scsi_done(cmnd);
634 spin_lock_irqsave(&devinfo->lock, flags);
636 if (devinfo->resetting) {
637 cmnd->result = DID_ERROR << 16;
638 cmnd->scsi_done(cmnd);
639 spin_unlock_irqrestore(&devinfo->lock, flags);
643 stream = uas_get_tag(cmnd);
644 if (devinfo->cmnd[stream - 1]) {
645 spin_unlock_irqrestore(&devinfo->lock, flags);
646 return SCSI_MLQUEUE_DEVICE_BUSY;
649 cmnd->scsi_done = done;
651 memset(cmdinfo, 0, sizeof(*cmdinfo));
652 cmdinfo->stream = stream;
653 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
655 switch (cmnd->sc_data_direction) {
656 case DMA_FROM_DEVICE:
657 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
659 case DMA_BIDIRECTIONAL:
660 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
662 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
667 if (!devinfo->use_streams) {
668 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
672 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
674 /* If we did nothing, give up now */
675 if (cmdinfo->state & SUBMIT_STATUS_URB) {
676 spin_unlock_irqrestore(&devinfo->lock, flags);
677 return SCSI_MLQUEUE_DEVICE_BUSY;
679 uas_add_work(cmdinfo);
682 devinfo->cmnd[stream - 1] = cmnd;
683 spin_unlock_irqrestore(&devinfo->lock, flags);
687 static DEF_SCSI_QCMD(uas_queuecommand)
690 * For now we do not support actually sending an abort to the device, so
691 * this eh always fails. Still we must define it to make sure that we've
692 * dropped all references to the cmnd in question once this function exits.
694 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
696 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
697 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
698 struct urb *data_in_urb = NULL;
699 struct urb *data_out_urb = NULL;
702 spin_lock_irqsave(&devinfo->lock, flags);
704 uas_log_cmd_state(cmnd, __func__);
706 /* Ensure that try_complete does not call scsi_done */
707 cmdinfo->state |= COMMAND_ABORTED;
709 /* Drop all refs to this cmnd, kill data urbs to break their ref */
710 devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
711 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
712 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
713 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
714 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
716 uas_free_unsubmitted_urbs(cmnd);
718 spin_unlock_irqrestore(&devinfo->lock, flags);
721 usb_kill_urb(data_in_urb);
722 usb_put_urb(data_in_urb);
725 usb_kill_urb(data_out_urb);
726 usb_put_urb(data_out_urb);
732 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
734 struct scsi_device *sdev = cmnd->device;
735 struct uas_dev_info *devinfo = sdev->hostdata;
736 struct usb_device *udev = devinfo->udev;
740 err = usb_lock_device_for_reset(udev, devinfo->intf);
742 shost_printk(KERN_ERR, sdev->host,
743 "%s FAILED to get lock err %d\n", __func__, err);
747 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
749 spin_lock_irqsave(&devinfo->lock, flags);
750 devinfo->resetting = 1;
751 spin_unlock_irqrestore(&devinfo->lock, flags);
753 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
754 usb_kill_anchored_urbs(&devinfo->sense_urbs);
755 usb_kill_anchored_urbs(&devinfo->data_urbs);
756 uas_zap_pending(devinfo, DID_RESET);
758 err = usb_reset_device(udev);
760 spin_lock_irqsave(&devinfo->lock, flags);
761 devinfo->resetting = 0;
762 spin_unlock_irqrestore(&devinfo->lock, flags);
764 usb_unlock_device(udev);
767 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
771 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
775 static int uas_slave_alloc(struct scsi_device *sdev)
777 sdev->hostdata = (void *)sdev->host->hostdata;
779 /* USB has unusual DMA-alignment requirements: Although the
780 * starting address of each scatter-gather element doesn't matter,
781 * the length of each element except the last must be divisible
782 * by the Bulk maxpacket value. There's currently no way to
783 * express this by block-layer constraints, so we'll cop out
784 * and simply require addresses to be aligned at 512-byte
785 * boundaries. This is okay since most block I/O involves
786 * hardware sectors that are multiples of 512 bytes in length,
787 * and since host controllers up through USB 2.0 have maxpacket
788 * values no larger than 512.
790 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
791 * values can be as large as 2048. To make that work properly
792 * will require changes to the block layer.
794 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
799 static int uas_slave_configure(struct scsi_device *sdev)
801 struct uas_dev_info *devinfo = sdev->hostdata;
803 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
804 sdev->no_report_opcodes = 1;
806 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
807 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
811 static struct scsi_host_template uas_host_template = {
812 .module = THIS_MODULE,
814 .queuecommand = uas_queuecommand,
815 .slave_alloc = uas_slave_alloc,
816 .slave_configure = uas_slave_configure,
817 .eh_abort_handler = uas_eh_abort_handler,
818 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
819 .can_queue = 65536, /* Is there a limit on the _host_ ? */
821 .sg_tablesize = SG_NONE,
822 .cmd_per_lun = 1, /* until we override it */
823 .skip_settle_delay = 1,
827 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
828 vendorName, productName, useProtocol, useTransport, \
829 initFunction, flags) \
830 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
831 .driver_info = (flags) }
833 static struct usb_device_id uas_usb_ids[] = {
834 # include "unusual_uas.h"
835 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
836 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
837 /* 0xaa is a prototype device I happen to have access to */
838 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
841 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
845 static int uas_switch_interface(struct usb_device *udev,
846 struct usb_interface *intf)
850 alt = uas_find_uas_alt_setting(intf);
854 return usb_set_interface(udev,
855 intf->altsetting[0].desc.bInterfaceNumber, alt);
858 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
860 struct usb_host_endpoint *eps[4] = { };
861 struct usb_device *udev = devinfo->udev;
864 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
868 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
869 usb_endpoint_num(&eps[0]->desc));
870 devinfo->status_pipe = usb_rcvbulkpipe(udev,
871 usb_endpoint_num(&eps[1]->desc));
872 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
873 usb_endpoint_num(&eps[2]->desc));
874 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
875 usb_endpoint_num(&eps[3]->desc));
877 if (udev->speed < USB_SPEED_SUPER) {
878 devinfo->qdepth = 32;
879 devinfo->use_streams = 0;
881 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
882 3, MAX_CMNDS, GFP_NOIO);
883 if (devinfo->qdepth < 0)
884 return devinfo->qdepth;
885 devinfo->use_streams = 1;
891 static void uas_free_streams(struct uas_dev_info *devinfo)
893 struct usb_device *udev = devinfo->udev;
894 struct usb_host_endpoint *eps[3];
896 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
897 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
898 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
899 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
902 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
904 int result = -ENOMEM;
905 struct Scsi_Host *shost = NULL;
906 struct uas_dev_info *devinfo;
907 struct usb_device *udev = interface_to_usbdev(intf);
909 if (!uas_use_uas_driver(intf, id))
912 if (uas_switch_interface(udev, intf))
915 shost = scsi_host_alloc(&uas_host_template,
916 sizeof(struct uas_dev_info));
920 shost->max_cmd_len = 16 + 252;
922 shost->max_lun = 256;
923 shost->max_channel = 0;
924 shost->sg_tablesize = udev->bus->sg_tablesize;
926 devinfo = (struct uas_dev_info *)shost->hostdata;
927 devinfo->intf = intf;
928 devinfo->udev = udev;
929 devinfo->resetting = 0;
930 devinfo->shutdown = 0;
931 devinfo->flags = id->driver_info;
932 usb_stor_adjust_quirks(udev, &devinfo->flags);
933 init_usb_anchor(&devinfo->cmd_urbs);
934 init_usb_anchor(&devinfo->sense_urbs);
935 init_usb_anchor(&devinfo->data_urbs);
936 spin_lock_init(&devinfo->lock);
937 INIT_WORK(&devinfo->work, uas_do_work);
939 result = uas_configure_endpoints(devinfo);
943 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
947 usb_set_intfdata(intf, shost);
948 result = scsi_add_host(shost, &intf->dev);
952 scsi_scan_host(shost);
956 uas_free_streams(devinfo);
957 usb_set_intfdata(intf, NULL);
959 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
961 scsi_host_put(shost);
965 static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
970 spin_lock_irqsave(&devinfo->lock, flags);
972 for (i = 0; i < devinfo->qdepth; i++) {
973 if (devinfo->cmnd[i]) {
974 r = 0; /* Not empty */
979 spin_unlock_irqrestore(&devinfo->lock, flags);
985 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
986 * get empty while there still is more work to do due to sense-urbs completing
987 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
989 static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
991 unsigned long start_time;
994 start_time = jiffies;
996 flush_work(&devinfo->work);
998 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1002 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1006 if (time_after(jiffies, start_time + 5 * HZ))
1008 } while (!uas_cmnd_list_empty(devinfo));
1013 static int uas_pre_reset(struct usb_interface *intf)
1015 struct Scsi_Host *shost = usb_get_intfdata(intf);
1016 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1017 unsigned long flags;
1019 if (devinfo->shutdown)
1022 /* Block new requests */
1023 spin_lock_irqsave(shost->host_lock, flags);
1024 scsi_block_requests(shost);
1025 spin_unlock_irqrestore(shost->host_lock, flags);
1027 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1028 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1029 scsi_unblock_requests(shost);
1033 uas_free_streams(devinfo);
1038 static int uas_post_reset(struct usb_interface *intf)
1040 struct Scsi_Host *shost = usb_get_intfdata(intf);
1041 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1042 unsigned long flags;
1044 if (devinfo->shutdown)
1047 if (uas_configure_endpoints(devinfo) != 0) {
1048 shost_printk(KERN_ERR, shost,
1049 "%s: alloc streams error after reset", __func__);
1053 spin_lock_irqsave(shost->host_lock, flags);
1054 scsi_report_bus_reset(shost, 0);
1055 spin_unlock_irqrestore(shost->host_lock, flags);
1057 scsi_unblock_requests(shost);
1062 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1064 struct Scsi_Host *shost = usb_get_intfdata(intf);
1065 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1067 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1068 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1075 static int uas_resume(struct usb_interface *intf)
1080 static int uas_reset_resume(struct usb_interface *intf)
1082 struct Scsi_Host *shost = usb_get_intfdata(intf);
1083 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1084 unsigned long flags;
1086 if (uas_configure_endpoints(devinfo) != 0) {
1087 shost_printk(KERN_ERR, shost,
1088 "%s: alloc streams error after reset", __func__);
1092 spin_lock_irqsave(shost->host_lock, flags);
1093 scsi_report_bus_reset(shost, 0);
1094 spin_unlock_irqrestore(shost->host_lock, flags);
1099 static void uas_disconnect(struct usb_interface *intf)
1101 struct Scsi_Host *shost = usb_get_intfdata(intf);
1102 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1103 unsigned long flags;
1105 spin_lock_irqsave(&devinfo->lock, flags);
1106 devinfo->resetting = 1;
1107 spin_unlock_irqrestore(&devinfo->lock, flags);
1109 cancel_work_sync(&devinfo->work);
1110 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1111 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1112 usb_kill_anchored_urbs(&devinfo->data_urbs);
1113 uas_zap_pending(devinfo, DID_NO_CONNECT);
1115 scsi_remove_host(shost);
1116 uas_free_streams(devinfo);
1117 scsi_host_put(shost);
1121 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1122 * hang on reboot when the device is still in uas mode. Note the reset is
1123 * necessary as some devices won't revert to usb-storage mode without it.
1125 static void uas_shutdown(struct device *dev)
1127 struct usb_interface *intf = to_usb_interface(dev);
1128 struct usb_device *udev = interface_to_usbdev(intf);
1129 struct Scsi_Host *shost = usb_get_intfdata(intf);
1130 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1132 if (system_state != SYSTEM_RESTART)
1135 devinfo->shutdown = 1;
1136 uas_free_streams(devinfo);
1137 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1138 usb_reset_device(udev);
1141 static struct usb_driver uas_driver = {
1144 .disconnect = uas_disconnect,
1145 .pre_reset = uas_pre_reset,
1146 .post_reset = uas_post_reset,
1147 .suspend = uas_suspend,
1148 .resume = uas_resume,
1149 .reset_resume = uas_reset_resume,
1150 .drvwrap.driver.shutdown = uas_shutdown,
1151 .id_table = uas_usb_ids,
1154 module_usb_driver(uas_driver);
1156 MODULE_LICENSE("GPL");
1158 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");