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
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"
34 * The r00-r01c specs define this version of the SENSE IU data structure.
35 * It's still in use by several different firmware releases.
43 __u8 service_response;
44 __u8 sense[SCSI_SENSE_BUFFERSIZE];
48 struct usb_interface *intf;
49 struct usb_device *udev;
50 struct usb_anchor cmd_urbs;
51 struct usb_anchor sense_urbs;
52 struct usb_anchor data_urbs;
54 int qdepth, resetting;
55 struct response_iu response;
56 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
57 unsigned use_streams:1;
58 unsigned uas_sense_old:1;
59 unsigned running_task:1;
61 struct scsi_cmnd *cmnd;
63 struct work_struct work;
64 struct list_head inflight_list;
65 struct list_head dead_list;
69 SUBMIT_STATUS_URB = (1 << 1),
70 ALLOC_DATA_IN_URB = (1 << 2),
71 SUBMIT_DATA_IN_URB = (1 << 3),
72 ALLOC_DATA_OUT_URB = (1 << 4),
73 SUBMIT_DATA_OUT_URB = (1 << 5),
74 ALLOC_CMD_URB = (1 << 6),
75 SUBMIT_CMD_URB = (1 << 7),
76 COMMAND_INFLIGHT = (1 << 8),
77 DATA_IN_URB_INFLIGHT = (1 << 9),
78 DATA_OUT_URB_INFLIGHT = (1 << 10),
79 COMMAND_COMPLETED = (1 << 11),
80 COMMAND_ABORTED = (1 << 12),
81 UNLINK_DATA_URBS = (1 << 13),
82 IS_IN_WORK_LIST = (1 << 14),
85 /* Overrides scsi_pointer */
90 struct urb *data_in_urb;
91 struct urb *data_out_urb;
92 struct list_head list;
95 /* I hate forward declarations, but I actually have a loop */
96 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
97 struct uas_dev_info *devinfo, gfp_t gfp);
98 static void uas_do_work(struct work_struct *work);
99 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
100 static void uas_free_streams(struct uas_dev_info *devinfo);
101 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
103 /* Must be called with devinfo->lock held, will temporary unlock the lock */
104 static void uas_unlink_data_urbs(struct uas_dev_info *devinfo,
105 struct uas_cmd_info *cmdinfo,
106 unsigned long *lock_flags)
109 * The UNLINK_DATA_URBS flag makes sure uas_try_complete
110 * (called by urb completion) doesn't release cmdinfo
113 cmdinfo->state |= UNLINK_DATA_URBS;
114 spin_unlock_irqrestore(&devinfo->lock, *lock_flags);
116 if (cmdinfo->data_in_urb)
117 usb_unlink_urb(cmdinfo->data_in_urb);
118 if (cmdinfo->data_out_urb)
119 usb_unlink_urb(cmdinfo->data_out_urb);
121 spin_lock_irqsave(&devinfo->lock, *lock_flags);
122 cmdinfo->state &= ~UNLINK_DATA_URBS;
125 static void uas_do_work(struct work_struct *work)
127 struct uas_dev_info *devinfo =
128 container_of(work, struct uas_dev_info, work);
129 struct uas_cmd_info *cmdinfo;
133 spin_lock_irqsave(&devinfo->lock, flags);
134 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
135 struct scsi_pointer *scp = (void *)cmdinfo;
136 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
139 if (!(cmdinfo->state & IS_IN_WORK_LIST))
142 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
144 cmdinfo->state &= ~IS_IN_WORK_LIST;
146 schedule_work(&devinfo->work);
148 spin_unlock_irqrestore(&devinfo->lock, flags);
151 static void uas_mark_cmd_dead(struct uas_dev_info *devinfo,
152 struct uas_cmd_info *cmdinfo,
153 int result, const char *caller)
155 struct scsi_pointer *scp = (void *)cmdinfo;
156 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
158 uas_log_cmd_state(cmnd, caller);
159 lockdep_assert_held(&devinfo->lock);
160 WARN_ON_ONCE(cmdinfo->state & COMMAND_ABORTED);
161 cmdinfo->state |= COMMAND_ABORTED;
162 cmdinfo->state &= ~IS_IN_WORK_LIST;
163 cmnd->result = result << 16;
164 list_move_tail(&cmdinfo->list, &devinfo->dead_list);
167 static void uas_abort_inflight(struct uas_dev_info *devinfo, int result,
170 struct uas_cmd_info *cmdinfo;
171 struct uas_cmd_info *temp;
174 spin_lock_irqsave(&devinfo->lock, flags);
175 list_for_each_entry_safe(cmdinfo, temp, &devinfo->inflight_list, list)
176 uas_mark_cmd_dead(devinfo, cmdinfo, result, caller);
177 spin_unlock_irqrestore(&devinfo->lock, flags);
180 static void uas_add_work(struct uas_cmd_info *cmdinfo)
182 struct scsi_pointer *scp = (void *)cmdinfo;
183 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
184 struct uas_dev_info *devinfo = cmnd->device->hostdata;
186 lockdep_assert_held(&devinfo->lock);
187 cmdinfo->state |= IS_IN_WORK_LIST;
188 schedule_work(&devinfo->work);
191 static void uas_zap_dead(struct uas_dev_info *devinfo)
193 struct uas_cmd_info *cmdinfo;
194 struct uas_cmd_info *temp;
197 spin_lock_irqsave(&devinfo->lock, flags);
198 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
199 struct scsi_pointer *scp = (void *)cmdinfo;
200 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
202 uas_log_cmd_state(cmnd, __func__);
203 WARN_ON_ONCE(!(cmdinfo->state & COMMAND_ABORTED));
204 /* all urbs are killed, clear inflight bits */
205 cmdinfo->state &= ~(COMMAND_INFLIGHT |
206 DATA_IN_URB_INFLIGHT |
207 DATA_OUT_URB_INFLIGHT);
208 uas_try_complete(cmnd, __func__);
210 devinfo->running_task = 0;
211 spin_unlock_irqrestore(&devinfo->lock, flags);
214 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
216 struct sense_iu *sense_iu = urb->transfer_buffer;
217 struct scsi_device *sdev = cmnd->device;
219 if (urb->actual_length > 16) {
220 unsigned len = be16_to_cpup(&sense_iu->len);
221 if (len + 16 != urb->actual_length) {
222 int newlen = min(len + 16, urb->actual_length) - 16;
225 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
226 "disagrees with IU sense data length %d, "
227 "using %d bytes of sense data\n", __func__,
228 urb->actual_length, len, newlen);
231 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
234 cmnd->result = sense_iu->status;
237 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
239 struct sense_iu_old *sense_iu = urb->transfer_buffer;
240 struct scsi_device *sdev = cmnd->device;
242 if (urb->actual_length > 8) {
243 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
244 if (len + 8 != urb->actual_length) {
245 int newlen = min(len + 8, urb->actual_length) - 8;
248 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
249 "disagrees with IU sense data length %d, "
250 "using %d bytes of sense data\n", __func__,
251 urb->actual_length, len, newlen);
254 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
257 cmnd->result = sense_iu->status;
260 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
262 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
264 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
265 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
266 caller, cmnd, cmnd->request->tag,
267 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
268 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
269 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
270 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
271 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
272 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
273 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
274 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
275 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
276 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
277 (ci->state & COMMAND_COMPLETED) ? " done" : "",
278 (ci->state & COMMAND_ABORTED) ? " abort" : "",
279 (ci->state & UNLINK_DATA_URBS) ? " unlink": "",
280 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
283 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
285 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
286 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
288 lockdep_assert_held(&devinfo->lock);
289 if (cmdinfo->state & (COMMAND_INFLIGHT |
290 DATA_IN_URB_INFLIGHT |
291 DATA_OUT_URB_INFLIGHT |
294 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
295 cmdinfo->state |= COMMAND_COMPLETED;
296 usb_free_urb(cmdinfo->data_in_urb);
297 usb_free_urb(cmdinfo->data_out_urb);
298 if (cmdinfo->state & COMMAND_ABORTED)
299 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
300 list_del(&cmdinfo->list);
301 cmnd->scsi_done(cmnd);
305 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
308 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
311 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
312 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
314 uas_add_work(cmdinfo);
318 static void uas_stat_cmplt(struct urb *urb)
320 struct iu *iu = urb->transfer_buffer;
321 struct Scsi_Host *shost = urb->context;
322 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
323 struct scsi_cmnd *cmnd;
324 struct uas_cmd_info *cmdinfo;
329 if (urb->status == -ENOENT) {
330 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
333 dev_err(&urb->dev->dev, "stat urb: status %d\n",
340 if (devinfo->resetting) {
345 spin_lock_irqsave(&devinfo->lock, flags);
346 tag = be16_to_cpup(&iu->tag) - 1;
348 cmnd = devinfo->cmnd;
350 cmnd = scsi_host_find_tag(shost, tag - 1);
353 if (iu->iu_id == IU_ID_RESPONSE) {
354 if (!devinfo->running_task)
355 dev_warn(&urb->dev->dev,
356 "stat urb: recv unexpected response iu\n");
357 /* store results for uas_eh_task_mgmt() */
358 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
361 spin_unlock_irqrestore(&devinfo->lock, flags);
365 cmdinfo = (void *)&cmnd->SCp;
368 if (devinfo->cmnd == cmnd)
369 devinfo->cmnd = NULL;
371 if (urb->actual_length < 16)
372 devinfo->uas_sense_old = 1;
373 if (devinfo->uas_sense_old)
374 uas_sense_old(urb, cmnd);
376 uas_sense(urb, cmnd);
377 if (cmnd->result != 0) {
378 /* cancel data transfers on error */
379 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
381 cmdinfo->state &= ~COMMAND_INFLIGHT;
382 uas_try_complete(cmnd, __func__);
384 case IU_ID_READ_READY:
385 if (!cmdinfo->data_in_urb ||
386 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
387 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
390 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
392 case IU_ID_WRITE_READY:
393 if (!cmdinfo->data_out_urb ||
394 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
395 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
398 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
401 scmd_printk(KERN_ERR, cmnd,
402 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
405 spin_unlock_irqrestore(&devinfo->lock, flags);
408 static void uas_data_cmplt(struct urb *urb)
410 struct scsi_cmnd *cmnd = urb->context;
411 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
412 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
413 struct scsi_data_buffer *sdb = NULL;
416 spin_lock_irqsave(&devinfo->lock, flags);
417 if (cmdinfo->data_in_urb == urb) {
419 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
420 } else if (cmdinfo->data_out_urb == urb) {
421 sdb = scsi_out(cmnd);
422 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
426 } else if (urb->status) {
427 if (urb->status != -ECONNRESET) {
428 uas_log_cmd_state(cmnd, __func__);
429 scmd_printk(KERN_ERR, cmnd,
430 "data cmplt err %d stream %d\n",
431 urb->status, urb->stream_id);
433 /* error: no data transfered */
434 sdb->resid = sdb->length;
436 sdb->resid = sdb->length - urb->actual_length;
438 uas_try_complete(cmnd, __func__);
439 spin_unlock_irqrestore(&devinfo->lock, flags);
442 static void uas_cmd_cmplt(struct urb *urb)
444 struct scsi_cmnd *cmnd = urb->context;
447 uas_log_cmd_state(cmnd, __func__);
448 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
453 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
454 unsigned int pipe, u16 stream_id,
455 struct scsi_cmnd *cmnd,
456 enum dma_data_direction dir)
458 struct usb_device *udev = devinfo->udev;
459 struct urb *urb = usb_alloc_urb(0, gfp);
460 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
461 ? scsi_in(cmnd) : scsi_out(cmnd);
465 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
466 uas_data_cmplt, cmnd);
467 urb->stream_id = stream_id;
468 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
469 urb->sg = sdb->table.sgl;
474 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
475 struct Scsi_Host *shost, u16 stream_id)
477 struct usb_device *udev = devinfo->udev;
478 struct urb *urb = usb_alloc_urb(0, gfp);
484 iu = kzalloc(sizeof(*iu), gfp);
488 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
489 uas_stat_cmplt, shost);
490 urb->stream_id = stream_id;
491 urb->transfer_flags |= URB_FREE_BUFFER;
499 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
500 struct scsi_cmnd *cmnd)
502 struct usb_device *udev = devinfo->udev;
503 struct scsi_device *sdev = cmnd->device;
504 struct urb *urb = usb_alloc_urb(0, gfp);
505 struct command_iu *iu;
511 len = cmnd->cmd_len - 16;
515 iu = kzalloc(sizeof(*iu) + len, gfp);
519 iu->iu_id = IU_ID_COMMAND;
520 if (blk_rq_tagged(cmnd->request))
521 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
523 iu->tag = cpu_to_be16(1);
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, cmnd);
531 urb->transfer_flags |= URB_FREE_BUFFER;
539 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
540 u8 function, u16 stream_id)
542 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
543 struct usb_device *udev = devinfo->udev;
544 struct urb *urb = usb_alloc_urb(0, gfp);
545 struct task_mgmt_iu *iu;
551 iu = kzalloc(sizeof(*iu), gfp);
555 iu->iu_id = IU_ID_TASK_MGMT;
556 iu->tag = cpu_to_be16(stream_id);
557 int_to_scsilun(cmnd->device->lun, &iu->lun);
559 iu->function = function;
562 if (blk_rq_tagged(cmnd->request))
563 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
565 iu->task_tag = cpu_to_be16(1);
569 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
570 uas_cmd_cmplt, cmnd);
571 urb->transfer_flags |= URB_FREE_BUFFER;
573 usb_anchor_urb(urb, &devinfo->cmd_urbs);
574 err = usb_submit_urb(urb, gfp);
576 usb_unanchor_urb(urb);
577 uas_log_cmd_state(cmnd, __func__);
578 scmd_printk(KERN_ERR, cmnd, "task submission err %d\n", err);
590 * Why should I request the Status IU before sending the Command IU? Spec
591 * says to, but also says the device may receive them in any order. Seems
595 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
596 gfp_t gfp, unsigned int stream)
598 struct Scsi_Host *shost = cmnd->device->host;
599 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
603 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
606 usb_anchor_urb(urb, &devinfo->sense_urbs);
607 err = usb_submit_urb(urb, gfp);
609 usb_unanchor_urb(urb);
610 uas_log_cmd_state(cmnd, __func__);
611 shost_printk(KERN_INFO, shost,
612 "sense urb submission error %d stream %d\n",
620 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
621 struct uas_dev_info *devinfo, gfp_t gfp)
623 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
627 lockdep_assert_held(&devinfo->lock);
628 if (cmdinfo->state & SUBMIT_STATUS_URB) {
629 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
631 return SCSI_MLQUEUE_DEVICE_BUSY;
632 cmdinfo->state &= ~SUBMIT_STATUS_URB;
635 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
636 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
637 devinfo->data_in_pipe, cmdinfo->stream,
638 cmnd, DMA_FROM_DEVICE);
639 if (!cmdinfo->data_in_urb)
640 return SCSI_MLQUEUE_DEVICE_BUSY;
641 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
644 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
645 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
646 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
648 usb_unanchor_urb(cmdinfo->data_in_urb);
649 uas_log_cmd_state(cmnd, __func__);
650 scmd_printk(KERN_INFO, cmnd,
651 "data in urb submission error %d stream %d\n",
652 err, cmdinfo->data_in_urb->stream_id);
653 return SCSI_MLQUEUE_DEVICE_BUSY;
655 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
656 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
659 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
660 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
661 devinfo->data_out_pipe, cmdinfo->stream,
662 cmnd, DMA_TO_DEVICE);
663 if (!cmdinfo->data_out_urb)
664 return SCSI_MLQUEUE_DEVICE_BUSY;
665 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
668 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
669 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
670 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
672 usb_unanchor_urb(cmdinfo->data_out_urb);
673 uas_log_cmd_state(cmnd, __func__);
674 scmd_printk(KERN_INFO, cmnd,
675 "data out urb submission error %d stream %d\n",
676 err, cmdinfo->data_out_urb->stream_id);
677 return SCSI_MLQUEUE_DEVICE_BUSY;
679 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
680 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
683 if (cmdinfo->state & ALLOC_CMD_URB) {
684 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
685 if (!cmdinfo->cmd_urb)
686 return SCSI_MLQUEUE_DEVICE_BUSY;
687 cmdinfo->state &= ~ALLOC_CMD_URB;
690 if (cmdinfo->state & SUBMIT_CMD_URB) {
691 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
692 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
694 usb_unanchor_urb(cmdinfo->cmd_urb);
695 uas_log_cmd_state(cmnd, __func__);
696 scmd_printk(KERN_INFO, cmnd,
697 "cmd urb submission error %d\n", err);
698 return SCSI_MLQUEUE_DEVICE_BUSY;
700 cmdinfo->cmd_urb = NULL;
701 cmdinfo->state &= ~SUBMIT_CMD_URB;
702 cmdinfo->state |= COMMAND_INFLIGHT;
708 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
709 void (*done)(struct scsi_cmnd *))
711 struct scsi_device *sdev = cmnd->device;
712 struct uas_dev_info *devinfo = sdev->hostdata;
713 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
717 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
719 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
720 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
721 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
722 sizeof(usb_stor_sense_invalidCDB));
723 cmnd->result = SAM_STAT_CHECK_CONDITION;
724 cmnd->scsi_done(cmnd);
728 spin_lock_irqsave(&devinfo->lock, flags);
730 if (devinfo->resetting) {
731 cmnd->result = DID_ERROR << 16;
732 cmnd->scsi_done(cmnd);
733 spin_unlock_irqrestore(&devinfo->lock, flags);
738 spin_unlock_irqrestore(&devinfo->lock, flags);
739 return SCSI_MLQUEUE_DEVICE_BUSY;
742 memset(cmdinfo, 0, sizeof(*cmdinfo));
744 if (blk_rq_tagged(cmnd->request)) {
745 cmdinfo->stream = cmnd->request->tag + 2;
747 devinfo->cmnd = cmnd;
751 cmnd->scsi_done = done;
753 cmdinfo->state = SUBMIT_STATUS_URB |
754 ALLOC_CMD_URB | SUBMIT_CMD_URB;
756 switch (cmnd->sc_data_direction) {
757 case DMA_FROM_DEVICE:
758 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
760 case DMA_BIDIRECTIONAL:
761 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
763 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
768 if (!devinfo->use_streams) {
769 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
773 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
775 /* If we did nothing, give up now */
776 if (cmdinfo->state & SUBMIT_STATUS_URB) {
777 spin_unlock_irqrestore(&devinfo->lock, flags);
778 return SCSI_MLQUEUE_DEVICE_BUSY;
780 uas_add_work(cmdinfo);
783 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
784 spin_unlock_irqrestore(&devinfo->lock, flags);
788 static DEF_SCSI_QCMD(uas_queuecommand)
790 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
791 const char *fname, u8 function)
793 struct Scsi_Host *shost = cmnd->device->host;
794 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
795 u16 tag = devinfo->qdepth;
797 struct urb *sense_urb;
798 int result = SUCCESS;
800 spin_lock_irqsave(&devinfo->lock, flags);
802 if (devinfo->resetting) {
803 spin_unlock_irqrestore(&devinfo->lock, flags);
807 if (devinfo->running_task) {
808 shost_printk(KERN_INFO, shost,
809 "%s: %s: error already running a task\n",
811 spin_unlock_irqrestore(&devinfo->lock, flags);
815 devinfo->running_task = 1;
816 memset(&devinfo->response, 0, sizeof(devinfo->response));
817 sense_urb = uas_submit_sense_urb(cmnd, GFP_ATOMIC,
818 devinfo->use_streams ? tag : 0);
820 shost_printk(KERN_INFO, shost,
821 "%s: %s: submit sense urb failed\n",
823 devinfo->running_task = 0;
824 spin_unlock_irqrestore(&devinfo->lock, flags);
827 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
828 shost_printk(KERN_INFO, shost,
829 "%s: %s: submit task mgmt urb failed\n",
831 devinfo->running_task = 0;
832 spin_unlock_irqrestore(&devinfo->lock, flags);
833 usb_kill_urb(sense_urb);
836 spin_unlock_irqrestore(&devinfo->lock, flags);
838 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
840 * Note we deliberately do not clear running_task here. If we
841 * allow new tasks to be submitted, there is no way to figure
842 * out if a received response_iu is for the failed task or for
843 * the new one. A bus-reset will eventually clear running_task.
845 shost_printk(KERN_INFO, shost,
846 "%s: %s timed out\n", __func__, fname);
850 spin_lock_irqsave(&devinfo->lock, flags);
851 devinfo->running_task = 0;
852 if (be16_to_cpu(devinfo->response.tag) != tag) {
853 shost_printk(KERN_INFO, shost,
854 "%s: %s failed (wrong tag %d/%d)\n", __func__,
855 fname, be16_to_cpu(devinfo->response.tag), tag);
857 } else if (devinfo->response.response_code != RC_TMF_COMPLETE) {
858 shost_printk(KERN_INFO, shost,
859 "%s: %s failed (rc 0x%x)\n", __func__,
860 fname, devinfo->response.response_code);
863 spin_unlock_irqrestore(&devinfo->lock, flags);
868 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
870 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
871 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
875 spin_lock_irqsave(&devinfo->lock, flags);
877 if (devinfo->resetting) {
878 spin_unlock_irqrestore(&devinfo->lock, flags);
882 uas_mark_cmd_dead(devinfo, cmdinfo, DID_ABORT, __func__);
883 if (cmdinfo->state & COMMAND_INFLIGHT) {
884 spin_unlock_irqrestore(&devinfo->lock, flags);
885 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
887 uas_unlink_data_urbs(devinfo, cmdinfo, &flags);
888 uas_try_complete(cmnd, __func__);
889 spin_unlock_irqrestore(&devinfo->lock, flags);
895 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
897 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
898 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
899 TMF_LOGICAL_UNIT_RESET);
902 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
904 struct scsi_device *sdev = cmnd->device;
905 struct uas_dev_info *devinfo = sdev->hostdata;
906 struct usb_device *udev = devinfo->udev;
909 err = usb_lock_device_for_reset(udev, devinfo->intf);
911 shost_printk(KERN_ERR, sdev->host,
912 "%s FAILED to get lock err %d\n", __func__, err);
916 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
917 devinfo->resetting = 1;
918 uas_abort_inflight(devinfo, DID_RESET, __func__);
919 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
920 usb_kill_anchored_urbs(&devinfo->sense_urbs);
921 usb_kill_anchored_urbs(&devinfo->data_urbs);
922 uas_zap_dead(devinfo);
923 err = usb_reset_device(udev);
924 devinfo->resetting = 0;
926 usb_unlock_device(udev);
929 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
933 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
937 static int uas_slave_alloc(struct scsi_device *sdev)
939 sdev->hostdata = (void *)sdev->host->hostdata;
941 /* USB has unusual DMA-alignment requirements: Although the
942 * starting address of each scatter-gather element doesn't matter,
943 * the length of each element except the last must be divisible
944 * by the Bulk maxpacket value. There's currently no way to
945 * express this by block-layer constraints, so we'll cop out
946 * and simply require addresses to be aligned at 512-byte
947 * boundaries. This is okay since most block I/O involves
948 * hardware sectors that are multiples of 512 bytes in length,
949 * and since host controllers up through USB 2.0 have maxpacket
950 * values no larger than 512.
952 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
953 * values can be as large as 2048. To make that work properly
954 * will require changes to the block layer.
956 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
961 static int uas_slave_configure(struct scsi_device *sdev)
963 struct uas_dev_info *devinfo = sdev->hostdata;
964 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
965 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
969 static struct scsi_host_template uas_host_template = {
970 .module = THIS_MODULE,
972 .queuecommand = uas_queuecommand,
973 .slave_alloc = uas_slave_alloc,
974 .slave_configure = uas_slave_configure,
975 .eh_abort_handler = uas_eh_abort_handler,
976 .eh_device_reset_handler = uas_eh_device_reset_handler,
977 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
978 .can_queue = 65536, /* Is there a limit on the _host_ ? */
980 .sg_tablesize = SG_NONE,
981 .cmd_per_lun = 1, /* until we override it */
982 .skip_settle_delay = 1,
986 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
987 vendorName, productName, useProtocol, useTransport, \
988 initFunction, flags) \
989 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
990 .driver_info = (flags) }
992 static struct usb_device_id uas_usb_ids[] = {
993 # include "unusual_uas.h"
994 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
995 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
996 /* 0xaa is a prototype device I happen to have access to */
997 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
1000 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
1004 static int uas_switch_interface(struct usb_device *udev,
1005 struct usb_interface *intf)
1009 alt = uas_find_uas_alt_setting(intf);
1013 return usb_set_interface(udev,
1014 intf->altsetting[0].desc.bInterfaceNumber, alt);
1017 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
1019 struct usb_host_endpoint *eps[4] = { };
1020 struct usb_device *udev = devinfo->udev;
1023 devinfo->uas_sense_old = 0;
1024 devinfo->cmnd = NULL;
1026 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
1030 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
1031 usb_endpoint_num(&eps[0]->desc));
1032 devinfo->status_pipe = usb_rcvbulkpipe(udev,
1033 usb_endpoint_num(&eps[1]->desc));
1034 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
1035 usb_endpoint_num(&eps[2]->desc));
1036 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
1037 usb_endpoint_num(&eps[3]->desc));
1039 if (udev->speed != USB_SPEED_SUPER) {
1040 devinfo->qdepth = 32;
1041 devinfo->use_streams = 0;
1043 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
1045 if (devinfo->qdepth < 0)
1046 return devinfo->qdepth;
1047 devinfo->use_streams = 1;
1053 static void uas_free_streams(struct uas_dev_info *devinfo)
1055 struct usb_device *udev = devinfo->udev;
1056 struct usb_host_endpoint *eps[3];
1058 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
1059 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
1060 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
1061 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
1064 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
1066 int result = -ENOMEM;
1067 struct Scsi_Host *shost = NULL;
1068 struct uas_dev_info *devinfo;
1069 struct usb_device *udev = interface_to_usbdev(intf);
1071 if (!uas_use_uas_driver(intf, id))
1074 if (uas_switch_interface(udev, intf))
1077 shost = scsi_host_alloc(&uas_host_template,
1078 sizeof(struct uas_dev_info));
1082 shost->max_cmd_len = 16 + 252;
1084 shost->max_lun = 256;
1085 shost->max_channel = 0;
1086 shost->sg_tablesize = udev->bus->sg_tablesize;
1088 devinfo = (struct uas_dev_info *)shost->hostdata;
1089 devinfo->intf = intf;
1090 devinfo->udev = udev;
1091 devinfo->resetting = 0;
1092 devinfo->running_task = 0;
1093 devinfo->shutdown = 0;
1094 devinfo->flags = id->driver_info;
1095 usb_stor_adjust_quirks(udev, &devinfo->flags);
1096 init_usb_anchor(&devinfo->cmd_urbs);
1097 init_usb_anchor(&devinfo->sense_urbs);
1098 init_usb_anchor(&devinfo->data_urbs);
1099 spin_lock_init(&devinfo->lock);
1100 INIT_WORK(&devinfo->work, uas_do_work);
1101 INIT_LIST_HEAD(&devinfo->inflight_list);
1102 INIT_LIST_HEAD(&devinfo->dead_list);
1104 result = uas_configure_endpoints(devinfo);
1108 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
1112 usb_set_intfdata(intf, shost);
1113 result = scsi_add_host(shost, &intf->dev);
1117 scsi_scan_host(shost);
1121 uas_free_streams(devinfo);
1122 usb_set_intfdata(intf, NULL);
1124 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1126 scsi_host_put(shost);
1130 static int uas_pre_reset(struct usb_interface *intf)
1132 struct Scsi_Host *shost = usb_get_intfdata(intf);
1133 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1134 unsigned long flags;
1136 if (devinfo->shutdown)
1139 /* Block new requests */
1140 spin_lock_irqsave(shost->host_lock, flags);
1141 scsi_block_requests(shost);
1142 spin_unlock_irqrestore(shost->host_lock, flags);
1144 /* Wait for any pending requests to complete */
1145 flush_work(&devinfo->work);
1146 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1147 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1151 uas_free_streams(devinfo);
1156 static int uas_post_reset(struct usb_interface *intf)
1158 struct Scsi_Host *shost = usb_get_intfdata(intf);
1159 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1160 unsigned long flags;
1162 if (devinfo->shutdown)
1165 if (uas_configure_endpoints(devinfo) != 0) {
1166 shost_printk(KERN_ERR, shost,
1167 "%s: alloc streams error after reset", __func__);
1171 spin_lock_irqsave(shost->host_lock, flags);
1172 scsi_report_bus_reset(shost, 0);
1173 spin_unlock_irqrestore(shost->host_lock, flags);
1175 scsi_unblock_requests(shost);
1180 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1182 struct Scsi_Host *shost = usb_get_intfdata(intf);
1183 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1185 /* Wait for any pending requests to complete */
1186 flush_work(&devinfo->work);
1187 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1188 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1195 static int uas_resume(struct usb_interface *intf)
1200 static int uas_reset_resume(struct usb_interface *intf)
1202 struct Scsi_Host *shost = usb_get_intfdata(intf);
1203 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1204 unsigned long flags;
1206 if (uas_configure_endpoints(devinfo) != 0) {
1207 shost_printk(KERN_ERR, shost,
1208 "%s: alloc streams error after reset", __func__);
1212 spin_lock_irqsave(shost->host_lock, flags);
1213 scsi_report_bus_reset(shost, 0);
1214 spin_unlock_irqrestore(shost->host_lock, flags);
1219 static void uas_disconnect(struct usb_interface *intf)
1221 struct Scsi_Host *shost = usb_get_intfdata(intf);
1222 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1224 devinfo->resetting = 1;
1225 cancel_work_sync(&devinfo->work);
1226 uas_abort_inflight(devinfo, DID_NO_CONNECT, __func__);
1227 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1228 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1229 usb_kill_anchored_urbs(&devinfo->data_urbs);
1230 uas_zap_dead(devinfo);
1231 scsi_remove_host(shost);
1232 uas_free_streams(devinfo);
1233 scsi_host_put(shost);
1237 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1238 * hang on reboot when the device is still in uas mode. Note the reset is
1239 * necessary as some devices won't revert to usb-storage mode without it.
1241 static void uas_shutdown(struct device *dev)
1243 struct usb_interface *intf = to_usb_interface(dev);
1244 struct usb_device *udev = interface_to_usbdev(intf);
1245 struct Scsi_Host *shost = usb_get_intfdata(intf);
1246 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1248 if (system_state != SYSTEM_RESTART)
1251 devinfo->shutdown = 1;
1252 uas_free_streams(devinfo);
1253 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1254 usb_reset_device(udev);
1257 static struct usb_driver uas_driver = {
1260 .disconnect = uas_disconnect,
1261 .pre_reset = uas_pre_reset,
1262 .post_reset = uas_post_reset,
1263 .suspend = uas_suspend,
1264 .resume = uas_resume,
1265 .reset_resume = uas_reset_resume,
1266 .drvwrap.driver.shutdown = uas_shutdown,
1267 .id_table = uas_usb_ids,
1270 module_usb_driver(uas_driver);
1272 MODULE_LICENSE("GPL");
1274 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");