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 - 2016
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;
49 struct work_struct scan_work; /* for async scanning */
53 SUBMIT_STATUS_URB = (1 << 1),
54 ALLOC_DATA_IN_URB = (1 << 2),
55 SUBMIT_DATA_IN_URB = (1 << 3),
56 ALLOC_DATA_OUT_URB = (1 << 4),
57 SUBMIT_DATA_OUT_URB = (1 << 5),
58 ALLOC_CMD_URB = (1 << 6),
59 SUBMIT_CMD_URB = (1 << 7),
60 COMMAND_INFLIGHT = (1 << 8),
61 DATA_IN_URB_INFLIGHT = (1 << 9),
62 DATA_OUT_URB_INFLIGHT = (1 << 10),
63 COMMAND_ABORTED = (1 << 11),
64 IS_IN_WORK_LIST = (1 << 12),
67 /* Overrides scsi_pointer */
72 struct urb *data_in_urb;
73 struct urb *data_out_urb;
76 /* I hate forward declarations, but I actually have a loop */
77 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
78 struct uas_dev_info *devinfo, gfp_t gfp);
79 static void uas_do_work(struct work_struct *work);
80 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
81 static void uas_free_streams(struct uas_dev_info *devinfo);
82 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
86 * This driver needs its own workqueue, as we need to control memory allocation.
88 * In the course of error handling and power management uas_wait_for_pending_cmnds()
89 * needs to flush pending work items. In these contexts we cannot allocate memory
90 * by doing block IO as we would deadlock. For the same reason we cannot wait
91 * for anything allocating memory not heeding these constraints.
93 * So we have to control all work items that can be on the workqueue we flush.
94 * Hence we cannot share a queue and need our own.
96 static struct workqueue_struct *workqueue;
98 static void uas_do_work(struct work_struct *work)
100 struct uas_dev_info *devinfo =
101 container_of(work, struct uas_dev_info, work);
102 struct uas_cmd_info *cmdinfo;
103 struct scsi_cmnd *cmnd;
107 spin_lock_irqsave(&devinfo->lock, flags);
109 if (devinfo->resetting)
112 for (i = 0; i < devinfo->qdepth; i++) {
113 if (!devinfo->cmnd[i])
116 cmnd = devinfo->cmnd[i];
117 cmdinfo = (void *)&cmnd->SCp;
119 if (!(cmdinfo->state & IS_IN_WORK_LIST))
122 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
124 cmdinfo->state &= ~IS_IN_WORK_LIST;
126 queue_work(workqueue, &devinfo->work);
129 spin_unlock_irqrestore(&devinfo->lock, flags);
132 static void uas_scan_work(struct work_struct *work)
134 struct uas_dev_info *devinfo =
135 container_of(work, struct uas_dev_info, scan_work);
136 struct Scsi_Host *shost = usb_get_intfdata(devinfo->intf);
138 dev_dbg(&devinfo->intf->dev, "starting scan\n");
139 scsi_scan_host(shost);
140 dev_dbg(&devinfo->intf->dev, "scan complete\n");
143 static void uas_add_work(struct uas_cmd_info *cmdinfo)
145 struct scsi_pointer *scp = (void *)cmdinfo;
146 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
147 struct uas_dev_info *devinfo = cmnd->device->hostdata;
149 lockdep_assert_held(&devinfo->lock);
150 cmdinfo->state |= IS_IN_WORK_LIST;
151 queue_work(workqueue, &devinfo->work);
154 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
156 struct uas_cmd_info *cmdinfo;
157 struct scsi_cmnd *cmnd;
161 spin_lock_irqsave(&devinfo->lock, flags);
162 for (i = 0; i < devinfo->qdepth; i++) {
163 if (!devinfo->cmnd[i])
166 cmnd = devinfo->cmnd[i];
167 cmdinfo = (void *)&cmnd->SCp;
168 uas_log_cmd_state(cmnd, __func__, 0);
169 /* Sense urbs were killed, clear COMMAND_INFLIGHT manually */
170 cmdinfo->state &= ~COMMAND_INFLIGHT;
171 cmnd->result = result << 16;
172 err = uas_try_complete(cmnd, __func__);
175 spin_unlock_irqrestore(&devinfo->lock, flags);
178 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
180 struct sense_iu *sense_iu = urb->transfer_buffer;
181 struct scsi_device *sdev = cmnd->device;
183 if (urb->actual_length > 16) {
184 unsigned len = be16_to_cpup(&sense_iu->len);
185 if (len + 16 != urb->actual_length) {
186 int newlen = min(len + 16, urb->actual_length) - 16;
189 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
190 "disagrees with IU sense data length %d, "
191 "using %d bytes of sense data\n", __func__,
192 urb->actual_length, len, newlen);
195 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
198 cmnd->result = sense_iu->status;
201 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
204 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
205 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
207 if (status == -ENODEV) /* too late */
210 scmd_printk(KERN_INFO, cmnd,
211 "%s %d uas-tag %d inflight:%s%s%s%s%s%s%s%s%s%s%s%s ",
212 prefix, status, cmdinfo->uas_tag,
213 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
214 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
215 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
216 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
217 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
218 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
219 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
220 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
221 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
222 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
223 (ci->state & COMMAND_ABORTED) ? " abort" : "",
224 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
225 scsi_print_command(cmnd);
228 static void uas_free_unsubmitted_urbs(struct scsi_cmnd *cmnd)
230 struct uas_cmd_info *cmdinfo;
235 cmdinfo = (void *)&cmnd->SCp;
237 if (cmdinfo->state & SUBMIT_CMD_URB)
238 usb_free_urb(cmdinfo->cmd_urb);
240 /* data urbs may have never gotten their submit flag set */
241 if (!(cmdinfo->state & DATA_IN_URB_INFLIGHT))
242 usb_free_urb(cmdinfo->data_in_urb);
243 if (!(cmdinfo->state & DATA_OUT_URB_INFLIGHT))
244 usb_free_urb(cmdinfo->data_out_urb);
247 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
249 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
250 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
252 lockdep_assert_held(&devinfo->lock);
253 if (cmdinfo->state & (COMMAND_INFLIGHT |
254 DATA_IN_URB_INFLIGHT |
255 DATA_OUT_URB_INFLIGHT |
258 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
259 uas_free_unsubmitted_urbs(cmnd);
260 cmnd->scsi_done(cmnd);
264 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
267 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
270 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
271 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
273 uas_add_work(cmdinfo);
277 static void uas_stat_cmplt(struct urb *urb)
279 struct iu *iu = urb->transfer_buffer;
280 struct Scsi_Host *shost = urb->context;
281 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
282 struct urb *data_in_urb = NULL;
283 struct urb *data_out_urb = NULL;
284 struct scsi_cmnd *cmnd;
285 struct uas_cmd_info *cmdinfo;
288 int status = urb->status;
290 spin_lock_irqsave(&devinfo->lock, flags);
292 if (devinfo->resetting)
296 if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
297 dev_err(&urb->dev->dev, "stat urb: status %d\n", status);
301 idx = be16_to_cpup(&iu->tag) - 1;
302 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
303 dev_err(&urb->dev->dev,
304 "stat urb: no pending cmd for uas-tag %d\n", idx + 1);
308 cmnd = devinfo->cmnd[idx];
309 cmdinfo = (void *)&cmnd->SCp;
311 if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
312 uas_log_cmd_state(cmnd, "unexpected status cmplt", 0);
318 uas_sense(urb, cmnd);
319 if (cmnd->result != 0) {
320 /* cancel data transfers on error */
321 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
322 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
324 cmdinfo->state &= ~COMMAND_INFLIGHT;
325 uas_try_complete(cmnd, __func__);
327 case IU_ID_READ_READY:
328 if (!cmdinfo->data_in_urb ||
329 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
330 uas_log_cmd_state(cmnd, "unexpected read rdy", 0);
333 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
335 case IU_ID_WRITE_READY:
336 if (!cmdinfo->data_out_urb ||
337 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
338 uas_log_cmd_state(cmnd, "unexpected write rdy", 0);
341 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
344 uas_log_cmd_state(cmnd, "unexpected response iu",
345 ((struct response_iu *)iu)->response_code);
346 /* Error, cancel data transfers */
347 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
348 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
349 cmdinfo->state &= ~COMMAND_INFLIGHT;
350 cmnd->result = DID_ERROR << 16;
351 uas_try_complete(cmnd, __func__);
354 uas_log_cmd_state(cmnd, "bogus IU", iu->iu_id);
358 spin_unlock_irqrestore(&devinfo->lock, flags);
360 /* Unlinking of data urbs must be done without holding the lock */
362 usb_unlink_urb(data_in_urb);
363 usb_put_urb(data_in_urb);
366 usb_unlink_urb(data_out_urb);
367 usb_put_urb(data_out_urb);
371 static void uas_data_cmplt(struct urb *urb)
373 struct scsi_cmnd *cmnd = urb->context;
374 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
375 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
376 struct scsi_data_buffer *sdb = NULL;
378 int status = urb->status;
380 spin_lock_irqsave(&devinfo->lock, flags);
382 if (cmdinfo->data_in_urb == urb) {
384 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
385 cmdinfo->data_in_urb = NULL;
386 } else if (cmdinfo->data_out_urb == urb) {
387 sdb = scsi_out(cmnd);
388 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
389 cmdinfo->data_out_urb = NULL;
396 if (devinfo->resetting)
399 /* Data urbs should not complete before the cmd urb is submitted */
400 if (cmdinfo->state & SUBMIT_CMD_URB) {
401 uas_log_cmd_state(cmnd, "unexpected data cmplt", 0);
406 if (status != -ENOENT && status != -ECONNRESET && status != -ESHUTDOWN)
407 uas_log_cmd_state(cmnd, "data cmplt err", status);
408 /* error: no data transfered */
409 sdb->resid = sdb->length;
411 sdb->resid = sdb->length - urb->actual_length;
413 uas_try_complete(cmnd, __func__);
416 spin_unlock_irqrestore(&devinfo->lock, flags);
419 static void uas_cmd_cmplt(struct urb *urb)
422 dev_err(&urb->dev->dev, "cmd cmplt err %d\n", urb->status);
427 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
428 struct scsi_cmnd *cmnd,
429 enum dma_data_direction dir)
431 struct usb_device *udev = devinfo->udev;
432 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
433 struct urb *urb = usb_alloc_urb(0, gfp);
434 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
435 ? scsi_in(cmnd) : scsi_out(cmnd);
436 unsigned int pipe = (dir == DMA_FROM_DEVICE)
437 ? devinfo->data_in_pipe : devinfo->data_out_pipe;
441 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
442 uas_data_cmplt, cmnd);
443 if (devinfo->use_streams)
444 urb->stream_id = cmdinfo->uas_tag;
445 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
446 urb->sg = sdb->table.sgl;
451 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
452 struct scsi_cmnd *cmnd)
454 struct usb_device *udev = devinfo->udev;
455 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
456 struct urb *urb = usb_alloc_urb(0, gfp);
462 iu = kzalloc(sizeof(*iu), gfp);
466 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
467 uas_stat_cmplt, cmnd->device->host);
468 if (devinfo->use_streams)
469 urb->stream_id = cmdinfo->uas_tag;
470 urb->transfer_flags |= URB_FREE_BUFFER;
478 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
479 struct scsi_cmnd *cmnd)
481 struct usb_device *udev = devinfo->udev;
482 struct scsi_device *sdev = cmnd->device;
483 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
484 struct urb *urb = usb_alloc_urb(0, gfp);
485 struct command_iu *iu;
491 len = cmnd->cmd_len - 16;
495 iu = kzalloc(sizeof(*iu) + len, gfp);
499 iu->iu_id = IU_ID_COMMAND;
500 iu->tag = cpu_to_be16(cmdinfo->uas_tag);
501 iu->prio_attr = UAS_SIMPLE_TAG;
503 int_to_scsilun(sdev->lun, &iu->lun);
504 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
506 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
507 uas_cmd_cmplt, NULL);
508 urb->transfer_flags |= URB_FREE_BUFFER;
517 * Why should I request the Status IU before sending the Command IU? Spec
518 * says to, but also says the device may receive them in any order. Seems
522 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd, gfp_t gfp)
524 struct uas_dev_info *devinfo = cmnd->device->hostdata;
528 urb = uas_alloc_sense_urb(devinfo, gfp, cmnd);
531 usb_anchor_urb(urb, &devinfo->sense_urbs);
532 err = usb_submit_urb(urb, gfp);
534 usb_unanchor_urb(urb);
535 uas_log_cmd_state(cmnd, "sense submit err", err);
542 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
543 struct uas_dev_info *devinfo, gfp_t gfp)
545 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
549 lockdep_assert_held(&devinfo->lock);
550 if (cmdinfo->state & SUBMIT_STATUS_URB) {
551 urb = uas_submit_sense_urb(cmnd, gfp);
553 return SCSI_MLQUEUE_DEVICE_BUSY;
554 cmdinfo->state &= ~SUBMIT_STATUS_URB;
557 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
558 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
559 cmnd, DMA_FROM_DEVICE);
560 if (!cmdinfo->data_in_urb)
561 return SCSI_MLQUEUE_DEVICE_BUSY;
562 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
565 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
566 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
567 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
569 usb_unanchor_urb(cmdinfo->data_in_urb);
570 uas_log_cmd_state(cmnd, "data in submit err", err);
571 return SCSI_MLQUEUE_DEVICE_BUSY;
573 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
574 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
577 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
578 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
579 cmnd, DMA_TO_DEVICE);
580 if (!cmdinfo->data_out_urb)
581 return SCSI_MLQUEUE_DEVICE_BUSY;
582 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
585 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
586 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
587 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
589 usb_unanchor_urb(cmdinfo->data_out_urb);
590 uas_log_cmd_state(cmnd, "data out submit err", err);
591 return SCSI_MLQUEUE_DEVICE_BUSY;
593 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
594 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
597 if (cmdinfo->state & ALLOC_CMD_URB) {
598 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
599 if (!cmdinfo->cmd_urb)
600 return SCSI_MLQUEUE_DEVICE_BUSY;
601 cmdinfo->state &= ~ALLOC_CMD_URB;
604 if (cmdinfo->state & SUBMIT_CMD_URB) {
605 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
606 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
608 usb_unanchor_urb(cmdinfo->cmd_urb);
609 uas_log_cmd_state(cmnd, "cmd submit err", err);
610 return SCSI_MLQUEUE_DEVICE_BUSY;
612 cmdinfo->cmd_urb = NULL;
613 cmdinfo->state &= ~SUBMIT_CMD_URB;
614 cmdinfo->state |= COMMAND_INFLIGHT;
620 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
621 void (*done)(struct scsi_cmnd *))
623 struct scsi_device *sdev = cmnd->device;
624 struct uas_dev_info *devinfo = sdev->hostdata;
625 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
629 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
631 /* Re-check scsi_block_requests now that we've the host-lock */
632 if (cmnd->device->host->host_self_blocked)
633 return SCSI_MLQUEUE_DEVICE_BUSY;
635 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
636 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
637 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
638 sizeof(usb_stor_sense_invalidCDB));
639 cmnd->result = SAM_STAT_CHECK_CONDITION;
640 cmnd->scsi_done(cmnd);
644 spin_lock_irqsave(&devinfo->lock, flags);
646 if (devinfo->resetting) {
647 cmnd->result = DID_ERROR << 16;
648 cmnd->scsi_done(cmnd);
652 /* Find a free uas-tag */
653 for (idx = 0; idx < devinfo->qdepth; idx++) {
654 if (!devinfo->cmnd[idx])
657 if (idx == devinfo->qdepth) {
658 spin_unlock_irqrestore(&devinfo->lock, flags);
659 return SCSI_MLQUEUE_DEVICE_BUSY;
662 cmnd->scsi_done = done;
664 memset(cmdinfo, 0, sizeof(*cmdinfo));
665 cmdinfo->uas_tag = idx + 1; /* uas-tag == usb-stream-id, so 1 based */
666 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
668 switch (cmnd->sc_data_direction) {
669 case DMA_FROM_DEVICE:
670 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
672 case DMA_BIDIRECTIONAL:
673 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
675 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
680 if (!devinfo->use_streams)
681 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
683 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
685 * in case of fatal errors the SCSI layer is peculiar
686 * a command that has finished is a success for the purpose
687 * of queueing, no matter how fatal the error
689 if (err == -ENODEV) {
690 cmnd->result = DID_ERROR << 16;
691 cmnd->scsi_done(cmnd);
695 /* If we did nothing, give up now */
696 if (cmdinfo->state & SUBMIT_STATUS_URB) {
697 spin_unlock_irqrestore(&devinfo->lock, flags);
698 return SCSI_MLQUEUE_DEVICE_BUSY;
700 uas_add_work(cmdinfo);
703 devinfo->cmnd[idx] = cmnd;
705 spin_unlock_irqrestore(&devinfo->lock, flags);
709 static DEF_SCSI_QCMD(uas_queuecommand)
712 * For now we do not support actually sending an abort to the device, so
713 * this eh always fails. Still we must define it to make sure that we've
714 * dropped all references to the cmnd in question once this function exits.
716 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
718 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
719 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
720 struct urb *data_in_urb = NULL;
721 struct urb *data_out_urb = NULL;
724 spin_lock_irqsave(&devinfo->lock, flags);
726 uas_log_cmd_state(cmnd, __func__, 0);
728 /* Ensure that try_complete does not call scsi_done */
729 cmdinfo->state |= COMMAND_ABORTED;
731 /* Drop all refs to this cmnd, kill data urbs to break their ref */
732 devinfo->cmnd[cmdinfo->uas_tag - 1] = NULL;
733 if (cmdinfo->state & DATA_IN_URB_INFLIGHT)
734 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
735 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT)
736 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
738 uas_free_unsubmitted_urbs(cmnd);
740 spin_unlock_irqrestore(&devinfo->lock, flags);
743 usb_kill_urb(data_in_urb);
744 usb_put_urb(data_in_urb);
747 usb_kill_urb(data_out_urb);
748 usb_put_urb(data_out_urb);
754 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
756 struct scsi_device *sdev = cmnd->device;
757 struct uas_dev_info *devinfo = sdev->hostdata;
758 struct usb_device *udev = devinfo->udev;
762 err = usb_lock_device_for_reset(udev, devinfo->intf);
764 shost_printk(KERN_ERR, sdev->host,
765 "%s FAILED to get lock err %d\n", __func__, err);
769 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
771 spin_lock_irqsave(&devinfo->lock, flags);
772 devinfo->resetting = 1;
773 spin_unlock_irqrestore(&devinfo->lock, flags);
775 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
776 usb_kill_anchored_urbs(&devinfo->sense_urbs);
777 usb_kill_anchored_urbs(&devinfo->data_urbs);
778 uas_zap_pending(devinfo, DID_RESET);
780 err = usb_reset_device(udev);
782 spin_lock_irqsave(&devinfo->lock, flags);
783 devinfo->resetting = 0;
784 spin_unlock_irqrestore(&devinfo->lock, flags);
786 usb_unlock_device(udev);
789 shost_printk(KERN_INFO, sdev->host, "%s FAILED err %d\n",
794 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
798 static int uas_target_alloc(struct scsi_target *starget)
800 struct uas_dev_info *devinfo = (struct uas_dev_info *)
801 dev_to_shost(starget->dev.parent)->hostdata;
803 if (devinfo->flags & US_FL_NO_REPORT_LUNS)
804 starget->no_report_luns = 1;
809 static int uas_slave_alloc(struct scsi_device *sdev)
811 struct uas_dev_info *devinfo =
812 (struct uas_dev_info *)sdev->host->hostdata;
814 sdev->hostdata = devinfo;
817 * The protocol has no requirements on alignment in the strict sense.
818 * Controllers may or may not have alignment restrictions.
819 * As this is not exported, we use an extremely conservative guess.
821 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
823 if (devinfo->flags & US_FL_MAX_SECTORS_64)
824 blk_queue_max_hw_sectors(sdev->request_queue, 64);
825 else if (devinfo->flags & US_FL_MAX_SECTORS_240)
826 blk_queue_max_hw_sectors(sdev->request_queue, 240);
831 static int uas_slave_configure(struct scsi_device *sdev)
833 struct uas_dev_info *devinfo = sdev->hostdata;
835 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
836 sdev->no_report_opcodes = 1;
838 /* A few buggy USB-ATA bridges don't understand FUA */
839 if (devinfo->flags & US_FL_BROKEN_FUA)
840 sdev->broken_fua = 1;
842 scsi_change_queue_depth(sdev, devinfo->qdepth - 2);
846 static struct scsi_host_template uas_host_template = {
847 .module = THIS_MODULE,
849 .queuecommand = uas_queuecommand,
850 .target_alloc = uas_target_alloc,
851 .slave_alloc = uas_slave_alloc,
852 .slave_configure = uas_slave_configure,
853 .eh_abort_handler = uas_eh_abort_handler,
854 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
855 .can_queue = MAX_CMNDS,
857 .sg_tablesize = SG_NONE,
858 .skip_settle_delay = 1,
861 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
862 vendorName, productName, useProtocol, useTransport, \
863 initFunction, flags) \
864 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
865 .driver_info = (flags) }
867 static struct usb_device_id uas_usb_ids[] = {
868 # include "unusual_uas.h"
869 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
870 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
873 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
877 static int uas_switch_interface(struct usb_device *udev,
878 struct usb_interface *intf)
880 struct usb_host_interface *alt;
882 alt = uas_find_uas_alt_setting(intf);
886 return usb_set_interface(udev, alt->desc.bInterfaceNumber,
887 alt->desc.bAlternateSetting);
890 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
892 struct usb_host_endpoint *eps[4] = { };
893 struct usb_device *udev = devinfo->udev;
896 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
900 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
901 usb_endpoint_num(&eps[0]->desc));
902 devinfo->status_pipe = usb_rcvbulkpipe(udev,
903 usb_endpoint_num(&eps[1]->desc));
904 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
905 usb_endpoint_num(&eps[2]->desc));
906 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
907 usb_endpoint_num(&eps[3]->desc));
909 if (udev->speed < USB_SPEED_SUPER) {
910 devinfo->qdepth = 32;
911 devinfo->use_streams = 0;
913 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
914 3, MAX_CMNDS, GFP_NOIO);
915 if (devinfo->qdepth < 0)
916 return devinfo->qdepth;
917 devinfo->use_streams = 1;
923 static void uas_free_streams(struct uas_dev_info *devinfo)
925 struct usb_device *udev = devinfo->udev;
926 struct usb_host_endpoint *eps[3];
928 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
929 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
930 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
931 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
934 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
936 int result = -ENOMEM;
937 struct Scsi_Host *shost = NULL;
938 struct uas_dev_info *devinfo;
939 struct usb_device *udev = interface_to_usbdev(intf);
940 unsigned long dev_flags;
942 if (!uas_use_uas_driver(intf, id, &dev_flags))
945 if (uas_switch_interface(udev, intf))
948 shost = scsi_host_alloc(&uas_host_template,
949 sizeof(struct uas_dev_info));
953 shost->max_cmd_len = 16 + 252;
955 shost->max_lun = 256;
956 shost->max_channel = 0;
957 shost->sg_tablesize = udev->bus->sg_tablesize;
959 devinfo = (struct uas_dev_info *)shost->hostdata;
960 devinfo->intf = intf;
961 devinfo->udev = udev;
962 devinfo->resetting = 0;
963 devinfo->shutdown = 0;
964 devinfo->flags = dev_flags;
965 init_usb_anchor(&devinfo->cmd_urbs);
966 init_usb_anchor(&devinfo->sense_urbs);
967 init_usb_anchor(&devinfo->data_urbs);
968 spin_lock_init(&devinfo->lock);
969 INIT_WORK(&devinfo->work, uas_do_work);
970 INIT_WORK(&devinfo->scan_work, uas_scan_work);
972 result = uas_configure_endpoints(devinfo);
977 * 1 tag is reserved for untagged commands +
978 * 1 tag to avoid off by one errors in some bridge firmwares
980 shost->can_queue = devinfo->qdepth - 2;
982 usb_set_intfdata(intf, shost);
983 result = scsi_add_host(shost, &intf->dev);
987 /* Submit the delayed_work for SCSI-device scanning */
988 schedule_work(&devinfo->scan_work);
993 uas_free_streams(devinfo);
994 usb_set_intfdata(intf, NULL);
996 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
998 scsi_host_put(shost);
1002 static int uas_cmnd_list_empty(struct uas_dev_info *devinfo)
1004 unsigned long flags;
1007 spin_lock_irqsave(&devinfo->lock, flags);
1009 for (i = 0; i < devinfo->qdepth; i++) {
1010 if (devinfo->cmnd[i]) {
1011 r = 0; /* Not empty */
1016 spin_unlock_irqrestore(&devinfo->lock, flags);
1022 * Wait for any pending cmnds to complete, on usb-2 sense_urbs may temporarily
1023 * get empty while there still is more work to do due to sense-urbs completing
1024 * with a READ/WRITE_READY iu code, so keep waiting until the list gets empty.
1026 static int uas_wait_for_pending_cmnds(struct uas_dev_info *devinfo)
1028 unsigned long start_time;
1031 start_time = jiffies;
1033 flush_work(&devinfo->work);
1035 r = usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000);
1039 r = usb_wait_anchor_empty_timeout(&devinfo->data_urbs, 500);
1043 if (time_after(jiffies, start_time + 5 * HZ))
1045 } while (!uas_cmnd_list_empty(devinfo));
1050 static int uas_pre_reset(struct usb_interface *intf)
1052 struct Scsi_Host *shost = usb_get_intfdata(intf);
1053 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1054 unsigned long flags;
1056 if (devinfo->shutdown)
1059 /* Block new requests */
1060 spin_lock_irqsave(shost->host_lock, flags);
1061 scsi_block_requests(shost);
1062 spin_unlock_irqrestore(shost->host_lock, flags);
1064 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1065 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1066 scsi_unblock_requests(shost);
1070 uas_free_streams(devinfo);
1075 static int uas_post_reset(struct usb_interface *intf)
1077 struct Scsi_Host *shost = usb_get_intfdata(intf);
1078 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1079 unsigned long flags;
1082 if (devinfo->shutdown)
1085 err = uas_configure_endpoints(devinfo);
1086 if (err && err != -ENODEV)
1087 shost_printk(KERN_ERR, shost,
1088 "%s: alloc streams error %d after reset",
1091 /* we must unblock the host in every case lest we deadlock */
1092 spin_lock_irqsave(shost->host_lock, flags);
1093 scsi_report_bus_reset(shost, 0);
1094 spin_unlock_irqrestore(shost->host_lock, flags);
1096 scsi_unblock_requests(shost);
1101 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1103 struct Scsi_Host *shost = usb_get_intfdata(intf);
1104 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1106 if (uas_wait_for_pending_cmnds(devinfo) != 0) {
1107 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1114 static int uas_resume(struct usb_interface *intf)
1119 static int uas_reset_resume(struct usb_interface *intf)
1121 struct Scsi_Host *shost = usb_get_intfdata(intf);
1122 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1123 unsigned long flags;
1126 err = uas_configure_endpoints(devinfo);
1128 shost_printk(KERN_ERR, shost,
1129 "%s: alloc streams error %d after reset",
1134 spin_lock_irqsave(shost->host_lock, flags);
1135 scsi_report_bus_reset(shost, 0);
1136 spin_unlock_irqrestore(shost->host_lock, flags);
1141 static void uas_disconnect(struct usb_interface *intf)
1143 struct Scsi_Host *shost = usb_get_intfdata(intf);
1144 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1145 unsigned long flags;
1147 spin_lock_irqsave(&devinfo->lock, flags);
1148 devinfo->resetting = 1;
1149 spin_unlock_irqrestore(&devinfo->lock, flags);
1151 cancel_work_sync(&devinfo->work);
1152 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1153 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1154 usb_kill_anchored_urbs(&devinfo->data_urbs);
1155 uas_zap_pending(devinfo, DID_NO_CONNECT);
1158 * Prevent SCSI scanning (if it hasn't started yet)
1159 * or wait for the SCSI-scanning routine to stop.
1161 cancel_work_sync(&devinfo->scan_work);
1163 scsi_remove_host(shost);
1164 uas_free_streams(devinfo);
1165 scsi_host_put(shost);
1169 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1170 * hang on reboot when the device is still in uas mode. Note the reset is
1171 * necessary as some devices won't revert to usb-storage mode without it.
1173 static void uas_shutdown(struct device *dev)
1175 struct usb_interface *intf = to_usb_interface(dev);
1176 struct usb_device *udev = interface_to_usbdev(intf);
1177 struct Scsi_Host *shost = usb_get_intfdata(intf);
1178 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1180 if (system_state != SYSTEM_RESTART)
1183 devinfo->shutdown = 1;
1184 uas_free_streams(devinfo);
1185 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1186 usb_reset_device(udev);
1189 static struct usb_driver uas_driver = {
1192 .disconnect = uas_disconnect,
1193 .pre_reset = uas_pre_reset,
1194 .post_reset = uas_post_reset,
1195 .suspend = uas_suspend,
1196 .resume = uas_resume,
1197 .reset_resume = uas_reset_resume,
1198 .drvwrap.driver.shutdown = uas_shutdown,
1199 .id_table = uas_usb_ids,
1202 static int __init uas_init(void)
1206 workqueue = alloc_workqueue("uas", WQ_MEM_RECLAIM, 0);
1210 rv = usb_register(&uas_driver);
1212 destroy_workqueue(workqueue);
1219 static void __exit uas_exit(void)
1221 usb_deregister(&uas_driver);
1222 destroy_workqueue(workqueue);
1225 module_init(uas_init);
1226 module_exit(uas_exit);
1228 MODULE_LICENSE("GPL");
1230 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");