3 * Note that this is not the same as the USB Mass Storage driver
5 * Copyright Hans de Goede <hdegoede@redhat.com> for Red Hat, Inc. 2013 - 2014
6 * Copyright Matthew Wilcox for Intel Corp, 2010
7 * Copyright Sarah Sharp for Intel Corp, 2010
9 * Distributed under the terms of the GNU GPL, version two.
12 #include <linux/blkdev.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb_usual.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/storage.h>
20 #include <linux/usb/uas.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_eh.h>
24 #include <scsi/scsi_dbg.h>
25 #include <scsi/scsi_cmnd.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_tcq.h>
30 #include "uas-detect.h"
36 * The r00-r01c specs define this version of the SENSE IU data structure.
37 * It's still in use by several different firmware releases.
45 __u8 service_response;
46 __u8 sense[SCSI_SENSE_BUFFERSIZE];
50 struct usb_interface *intf;
51 struct usb_device *udev;
52 struct usb_anchor cmd_urbs;
53 struct usb_anchor sense_urbs;
54 struct usb_anchor data_urbs;
56 int qdepth, resetting;
57 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
58 unsigned use_streams:1;
59 unsigned uas_sense_old:1;
61 struct scsi_cmnd *cmnd[MAX_CMNDS];
63 struct work_struct work;
64 struct list_head inflight_list;
68 SUBMIT_STATUS_URB = (1 << 1),
69 ALLOC_DATA_IN_URB = (1 << 2),
70 SUBMIT_DATA_IN_URB = (1 << 3),
71 ALLOC_DATA_OUT_URB = (1 << 4),
72 SUBMIT_DATA_OUT_URB = (1 << 5),
73 ALLOC_CMD_URB = (1 << 6),
74 SUBMIT_CMD_URB = (1 << 7),
75 COMMAND_INFLIGHT = (1 << 8),
76 DATA_IN_URB_INFLIGHT = (1 << 9),
77 DATA_OUT_URB_INFLIGHT = (1 << 10),
78 COMMAND_COMPLETED = (1 << 11),
79 COMMAND_ABORTED = (1 << 12),
80 IS_IN_WORK_LIST = (1 << 13),
83 /* Overrides scsi_pointer */
88 struct urb *data_in_urb;
89 struct urb *data_out_urb;
90 struct list_head list;
93 /* I hate forward declarations, but I actually have a loop */
94 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
95 struct uas_dev_info *devinfo, gfp_t gfp);
96 static void uas_do_work(struct work_struct *work);
97 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller);
98 static void uas_free_streams(struct uas_dev_info *devinfo);
99 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller);
101 static void uas_do_work(struct work_struct *work)
103 struct uas_dev_info *devinfo =
104 container_of(work, struct uas_dev_info, work);
105 struct uas_cmd_info *cmdinfo;
109 spin_lock_irqsave(&devinfo->lock, flags);
111 if (devinfo->resetting)
114 list_for_each_entry(cmdinfo, &devinfo->inflight_list, list) {
115 struct scsi_pointer *scp = (void *)cmdinfo;
116 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
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 schedule_work(&devinfo->work);
129 spin_unlock_irqrestore(&devinfo->lock, flags);
132 static void uas_add_work(struct uas_cmd_info *cmdinfo)
134 struct scsi_pointer *scp = (void *)cmdinfo;
135 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd, SCp);
136 struct uas_dev_info *devinfo = cmnd->device->hostdata;
138 lockdep_assert_held(&devinfo->lock);
139 cmdinfo->state |= IS_IN_WORK_LIST;
140 schedule_work(&devinfo->work);
143 static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
145 struct uas_cmd_info *cmdinfo;
146 struct uas_cmd_info *temp;
149 spin_lock_irqsave(&devinfo->lock, flags);
150 list_for_each_entry_safe(cmdinfo, temp, &devinfo->dead_list, list) {
151 struct scsi_pointer *scp = (void *)cmdinfo;
152 struct scsi_cmnd *cmnd = container_of(scp, struct scsi_cmnd,
154 uas_log_cmd_state(cmnd, __func__);
155 /* all urbs are killed, clear inflight bits */
156 cmdinfo->state &= ~(COMMAND_INFLIGHT |
157 DATA_IN_URB_INFLIGHT |
158 DATA_OUT_URB_INFLIGHT);
159 cmnd->result = result << 16;
160 uas_try_complete(cmnd, __func__);
162 spin_unlock_irqrestore(&devinfo->lock, flags);
165 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
167 struct sense_iu *sense_iu = urb->transfer_buffer;
168 struct scsi_device *sdev = cmnd->device;
170 if (urb->actual_length > 16) {
171 unsigned len = be16_to_cpup(&sense_iu->len);
172 if (len + 16 != urb->actual_length) {
173 int newlen = min(len + 16, urb->actual_length) - 16;
176 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
177 "disagrees with IU sense data length %d, "
178 "using %d bytes of sense data\n", __func__,
179 urb->actual_length, len, newlen);
182 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
185 cmnd->result = sense_iu->status;
188 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
190 struct sense_iu_old *sense_iu = urb->transfer_buffer;
191 struct scsi_device *sdev = cmnd->device;
193 if (urb->actual_length > 8) {
194 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
195 if (len + 8 != urb->actual_length) {
196 int newlen = min(len + 8, urb->actual_length) - 8;
199 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
200 "disagrees with IU sense data length %d, "
201 "using %d bytes of sense data\n", __func__,
202 urb->actual_length, len, newlen);
205 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
208 cmnd->result = sense_iu->status;
212 * scsi-tags go from 0 - (nr_tags - 1), uas tags need to match stream-ids,
213 * which go from 1 - nr_streams. And we use 1 for untagged commands.
215 static int uas_get_tag(struct scsi_cmnd *cmnd)
219 if (blk_rq_tagged(cmnd->request))
220 tag = cmnd->request->tag + 2;
227 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
229 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
231 scmd_printk(KERN_INFO, cmnd,
232 "%s %p tag %d, inflight:%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
233 caller, cmnd, uas_get_tag(cmnd),
234 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
235 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
236 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
237 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
238 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
239 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
240 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
241 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
242 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
243 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
244 (ci->state & COMMAND_COMPLETED) ? " done" : "",
245 (ci->state & COMMAND_ABORTED) ? " abort" : "",
246 (ci->state & IS_IN_WORK_LIST) ? " work" : "");
249 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
251 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
252 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
254 lockdep_assert_held(&devinfo->lock);
255 if (cmdinfo->state & (COMMAND_INFLIGHT |
256 DATA_IN_URB_INFLIGHT |
257 DATA_OUT_URB_INFLIGHT))
259 WARN_ON_ONCE(cmdinfo->state & COMMAND_COMPLETED);
260 cmdinfo->state |= COMMAND_COMPLETED;
261 if (cmdinfo->state & COMMAND_ABORTED)
262 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
263 list_del(&cmdinfo->list);
264 devinfo->cmnd[uas_get_tag(cmnd) - 1] = NULL;
265 cmnd->scsi_done(cmnd);
269 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
272 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
275 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
276 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
278 uas_add_work(cmdinfo);
282 static void uas_stat_cmplt(struct urb *urb)
284 struct iu *iu = urb->transfer_buffer;
285 struct Scsi_Host *shost = urb->context;
286 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
287 struct urb *data_in_urb = NULL;
288 struct urb *data_out_urb = NULL;
289 struct scsi_cmnd *cmnd;
290 struct uas_cmd_info *cmdinfo;
294 spin_lock_irqsave(&devinfo->lock, flags);
296 if (devinfo->resetting)
300 if (urb->status == -ENOENT) {
301 dev_err(&urb->dev->dev, "stat urb: killed, stream %d\n",
304 dev_err(&urb->dev->dev, "stat urb: status %d\n",
310 idx = be16_to_cpup(&iu->tag) - 1;
311 if (idx >= MAX_CMNDS || !devinfo->cmnd[idx]) {
312 dev_err(&urb->dev->dev,
313 "stat urb: no pending cmd for tag %d\n", idx + 1);
317 cmnd = devinfo->cmnd[idx];
318 cmdinfo = (void *)&cmnd->SCp;
320 if (!(cmdinfo->state & COMMAND_INFLIGHT)) {
321 scmd_printk(KERN_ERR, cmnd, "unexpected status cmplt\n");
327 if (urb->actual_length < 16)
328 devinfo->uas_sense_old = 1;
329 if (devinfo->uas_sense_old)
330 uas_sense_old(urb, cmnd);
332 uas_sense(urb, cmnd);
333 if (cmnd->result != 0) {
334 /* cancel data transfers on error */
335 data_in_urb = usb_get_urb(cmdinfo->data_in_urb);
336 data_out_urb = usb_get_urb(cmdinfo->data_out_urb);
338 cmdinfo->state &= ~COMMAND_INFLIGHT;
339 uas_try_complete(cmnd, __func__);
341 case IU_ID_READ_READY:
342 if (!cmdinfo->data_in_urb ||
343 (cmdinfo->state & DATA_IN_URB_INFLIGHT)) {
344 scmd_printk(KERN_ERR, cmnd, "unexpected read rdy\n");
347 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
349 case IU_ID_WRITE_READY:
350 if (!cmdinfo->data_out_urb ||
351 (cmdinfo->state & DATA_OUT_URB_INFLIGHT)) {
352 scmd_printk(KERN_ERR, cmnd, "unexpected write rdy\n");
355 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
358 scmd_printk(KERN_ERR, cmnd,
359 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
363 spin_unlock_irqrestore(&devinfo->lock, flags);
365 /* Unlinking of data urbs must be done without holding the lock */
367 usb_unlink_urb(data_in_urb);
368 usb_put_urb(data_in_urb);
371 usb_unlink_urb(data_out_urb);
372 usb_put_urb(data_out_urb);
376 static void uas_data_cmplt(struct urb *urb)
378 struct scsi_cmnd *cmnd = urb->context;
379 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
380 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
381 struct scsi_data_buffer *sdb = NULL;
384 spin_lock_irqsave(&devinfo->lock, flags);
386 if (cmdinfo->data_in_urb == urb) {
388 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
389 cmdinfo->data_in_urb = NULL;
390 } else if (cmdinfo->data_out_urb == urb) {
391 sdb = scsi_out(cmnd);
392 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
393 cmdinfo->data_out_urb = NULL;
400 if (devinfo->resetting)
403 /* Data urbs should not complete before the cmd urb is submitted */
404 if (cmdinfo->state & SUBMIT_CMD_URB) {
405 scmd_printk(KERN_ERR, cmnd, "unexpected data cmplt\n");
410 if (urb->status != -ECONNRESET) {
411 uas_log_cmd_state(cmnd, __func__);
412 scmd_printk(KERN_ERR, cmnd,
413 "data cmplt err %d stream %d\n",
414 urb->status, urb->stream_id);
416 /* error: no data transfered */
417 sdb->resid = sdb->length;
419 sdb->resid = sdb->length - urb->actual_length;
421 uas_try_complete(cmnd, __func__);
424 spin_unlock_irqrestore(&devinfo->lock, flags);
427 static void uas_cmd_cmplt(struct urb *urb)
429 struct scsi_cmnd *cmnd = urb->context;
432 uas_log_cmd_state(cmnd, __func__);
433 scmd_printk(KERN_ERR, cmnd, "cmd cmplt err %d\n", urb->status);
438 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
439 unsigned int pipe, u16 stream_id,
440 struct scsi_cmnd *cmnd,
441 enum dma_data_direction dir)
443 struct usb_device *udev = devinfo->udev;
444 struct urb *urb = usb_alloc_urb(0, gfp);
445 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
446 ? scsi_in(cmnd) : scsi_out(cmnd);
450 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
451 uas_data_cmplt, cmnd);
452 urb->stream_id = stream_id;
453 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
454 urb->sg = sdb->table.sgl;
459 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
460 struct Scsi_Host *shost, u16 stream_id)
462 struct usb_device *udev = devinfo->udev;
463 struct urb *urb = usb_alloc_urb(0, gfp);
469 iu = kzalloc(sizeof(*iu), gfp);
473 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
474 uas_stat_cmplt, shost);
475 urb->stream_id = stream_id;
476 urb->transfer_flags |= URB_FREE_BUFFER;
484 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
485 struct scsi_cmnd *cmnd)
487 struct usb_device *udev = devinfo->udev;
488 struct scsi_device *sdev = cmnd->device;
489 struct urb *urb = usb_alloc_urb(0, gfp);
490 struct command_iu *iu;
496 len = cmnd->cmd_len - 16;
500 iu = kzalloc(sizeof(*iu) + len, gfp);
504 iu->iu_id = IU_ID_COMMAND;
505 iu->tag = cpu_to_be16(uas_get_tag(cmnd));
506 iu->prio_attr = UAS_SIMPLE_TAG;
508 int_to_scsilun(sdev->lun, &iu->lun);
509 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
511 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
512 uas_cmd_cmplt, cmnd);
513 urb->transfer_flags |= URB_FREE_BUFFER;
522 * Why should I request the Status IU before sending the Command IU? Spec
523 * says to, but also says the device may receive them in any order. Seems
527 static struct urb *uas_submit_sense_urb(struct scsi_cmnd *cmnd,
528 gfp_t gfp, unsigned int stream)
530 struct Scsi_Host *shost = cmnd->device->host;
531 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
535 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
538 usb_anchor_urb(urb, &devinfo->sense_urbs);
539 err = usb_submit_urb(urb, gfp);
541 usb_unanchor_urb(urb);
542 uas_log_cmd_state(cmnd, __func__);
543 shost_printk(KERN_INFO, shost,
544 "sense urb submission error %d stream %d\n",
552 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
553 struct uas_dev_info *devinfo, gfp_t gfp)
555 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
559 lockdep_assert_held(&devinfo->lock);
560 if (cmdinfo->state & SUBMIT_STATUS_URB) {
561 urb = uas_submit_sense_urb(cmnd, gfp, cmdinfo->stream);
563 return SCSI_MLQUEUE_DEVICE_BUSY;
564 cmdinfo->state &= ~SUBMIT_STATUS_URB;
567 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
568 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
569 devinfo->data_in_pipe, cmdinfo->stream,
570 cmnd, DMA_FROM_DEVICE);
571 if (!cmdinfo->data_in_urb)
572 return SCSI_MLQUEUE_DEVICE_BUSY;
573 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
576 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
577 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
578 err = usb_submit_urb(cmdinfo->data_in_urb, gfp);
580 usb_unanchor_urb(cmdinfo->data_in_urb);
581 uas_log_cmd_state(cmnd, __func__);
582 scmd_printk(KERN_INFO, cmnd,
583 "data in urb submission error %d stream %d\n",
584 err, cmdinfo->data_in_urb->stream_id);
585 return SCSI_MLQUEUE_DEVICE_BUSY;
587 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
588 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
591 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
592 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
593 devinfo->data_out_pipe, cmdinfo->stream,
594 cmnd, DMA_TO_DEVICE);
595 if (!cmdinfo->data_out_urb)
596 return SCSI_MLQUEUE_DEVICE_BUSY;
597 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
600 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
601 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
602 err = usb_submit_urb(cmdinfo->data_out_urb, gfp);
604 usb_unanchor_urb(cmdinfo->data_out_urb);
605 uas_log_cmd_state(cmnd, __func__);
606 scmd_printk(KERN_INFO, cmnd,
607 "data out urb submission error %d stream %d\n",
608 err, cmdinfo->data_out_urb->stream_id);
609 return SCSI_MLQUEUE_DEVICE_BUSY;
611 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
612 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
615 if (cmdinfo->state & ALLOC_CMD_URB) {
616 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd);
617 if (!cmdinfo->cmd_urb)
618 return SCSI_MLQUEUE_DEVICE_BUSY;
619 cmdinfo->state &= ~ALLOC_CMD_URB;
622 if (cmdinfo->state & SUBMIT_CMD_URB) {
623 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
624 err = usb_submit_urb(cmdinfo->cmd_urb, gfp);
626 usb_unanchor_urb(cmdinfo->cmd_urb);
627 uas_log_cmd_state(cmnd, __func__);
628 scmd_printk(KERN_INFO, cmnd,
629 "cmd urb submission error %d\n", err);
630 return SCSI_MLQUEUE_DEVICE_BUSY;
632 cmdinfo->cmd_urb = NULL;
633 cmdinfo->state &= ~SUBMIT_CMD_URB;
634 cmdinfo->state |= COMMAND_INFLIGHT;
640 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
641 void (*done)(struct scsi_cmnd *))
643 struct scsi_device *sdev = cmnd->device;
644 struct uas_dev_info *devinfo = sdev->hostdata;
645 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
650 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
652 if ((devinfo->flags & US_FL_NO_ATA_1X) &&
653 (cmnd->cmnd[0] == ATA_12 || cmnd->cmnd[0] == ATA_16)) {
654 memcpy(cmnd->sense_buffer, usb_stor_sense_invalidCDB,
655 sizeof(usb_stor_sense_invalidCDB));
656 cmnd->result = SAM_STAT_CHECK_CONDITION;
657 cmnd->scsi_done(cmnd);
661 spin_lock_irqsave(&devinfo->lock, flags);
663 if (devinfo->resetting) {
664 cmnd->result = DID_ERROR << 16;
665 cmnd->scsi_done(cmnd);
666 spin_unlock_irqrestore(&devinfo->lock, flags);
670 stream = uas_get_tag(cmnd);
671 if (devinfo->cmnd[stream - 1]) {
672 spin_unlock_irqrestore(&devinfo->lock, flags);
673 return SCSI_MLQUEUE_DEVICE_BUSY;
676 cmnd->scsi_done = done;
678 memset(cmdinfo, 0, sizeof(*cmdinfo));
679 cmdinfo->stream = stream;
680 cmdinfo->state = SUBMIT_STATUS_URB | ALLOC_CMD_URB | SUBMIT_CMD_URB;
682 switch (cmnd->sc_data_direction) {
683 case DMA_FROM_DEVICE:
684 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
686 case DMA_BIDIRECTIONAL:
687 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
689 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
694 if (!devinfo->use_streams) {
695 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
699 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
701 /* If we did nothing, give up now */
702 if (cmdinfo->state & SUBMIT_STATUS_URB) {
703 spin_unlock_irqrestore(&devinfo->lock, flags);
704 return SCSI_MLQUEUE_DEVICE_BUSY;
706 uas_add_work(cmdinfo);
709 devinfo->cmnd[stream - 1] = cmnd;
710 list_add_tail(&cmdinfo->list, &devinfo->inflight_list);
711 spin_unlock_irqrestore(&devinfo->lock, flags);
715 static DEF_SCSI_QCMD(uas_queuecommand)
717 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
719 struct scsi_device *sdev = cmnd->device;
720 struct uas_dev_info *devinfo = sdev->hostdata;
721 struct usb_device *udev = devinfo->udev;
725 err = usb_lock_device_for_reset(udev, devinfo->intf);
727 shost_printk(KERN_ERR, sdev->host,
728 "%s FAILED to get lock err %d\n", __func__, err);
732 shost_printk(KERN_INFO, sdev->host, "%s start\n", __func__);
734 spin_lock_irqsave(&devinfo->lock, flags);
735 devinfo->resetting = 1;
736 spin_unlock_irqrestore(&devinfo->lock, flags);
738 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
739 usb_kill_anchored_urbs(&devinfo->sense_urbs);
740 usb_kill_anchored_urbs(&devinfo->data_urbs);
741 uas_zap_pending(devinfo, DID_RESET);
743 err = usb_reset_device(udev);
745 spin_lock_irqsave(&devinfo->lock, flags);
746 devinfo->resetting = 0;
747 spin_unlock_irqrestore(&devinfo->lock, flags);
749 usb_unlock_device(udev);
752 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
756 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
760 static int uas_slave_alloc(struct scsi_device *sdev)
762 sdev->hostdata = (void *)sdev->host->hostdata;
764 /* USB has unusual DMA-alignment requirements: Although the
765 * starting address of each scatter-gather element doesn't matter,
766 * the length of each element except the last must be divisible
767 * by the Bulk maxpacket value. There's currently no way to
768 * express this by block-layer constraints, so we'll cop out
769 * and simply require addresses to be aligned at 512-byte
770 * boundaries. This is okay since most block I/O involves
771 * hardware sectors that are multiples of 512 bytes in length,
772 * and since host controllers up through USB 2.0 have maxpacket
773 * values no larger than 512.
775 * But it doesn't suffice for Wireless USB, where Bulk maxpacket
776 * values can be as large as 2048. To make that work properly
777 * will require changes to the block layer.
779 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
784 static int uas_slave_configure(struct scsi_device *sdev)
786 struct uas_dev_info *devinfo = sdev->hostdata;
788 if (devinfo->flags & US_FL_NO_REPORT_OPCODES)
789 sdev->no_report_opcodes = 1;
791 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
792 scsi_activate_tcq(sdev, devinfo->qdepth - 2);
796 static struct scsi_host_template uas_host_template = {
797 .module = THIS_MODULE,
799 .queuecommand = uas_queuecommand,
800 .slave_alloc = uas_slave_alloc,
801 .slave_configure = uas_slave_configure,
802 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
803 .can_queue = 65536, /* Is there a limit on the _host_ ? */
805 .sg_tablesize = SG_NONE,
806 .cmd_per_lun = 1, /* until we override it */
807 .skip_settle_delay = 1,
811 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
812 vendorName, productName, useProtocol, useTransport, \
813 initFunction, flags) \
814 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
815 .driver_info = (flags) }
817 static struct usb_device_id uas_usb_ids[] = {
818 # include "unusual_uas.h"
819 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
820 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
821 /* 0xaa is a prototype device I happen to have access to */
822 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
825 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
829 static int uas_switch_interface(struct usb_device *udev,
830 struct usb_interface *intf)
834 alt = uas_find_uas_alt_setting(intf);
838 return usb_set_interface(udev,
839 intf->altsetting[0].desc.bInterfaceNumber, alt);
842 static int uas_configure_endpoints(struct uas_dev_info *devinfo)
844 struct usb_host_endpoint *eps[4] = { };
845 struct usb_device *udev = devinfo->udev;
848 devinfo->uas_sense_old = 0;
850 r = uas_find_endpoints(devinfo->intf->cur_altsetting, eps);
854 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
855 usb_endpoint_num(&eps[0]->desc));
856 devinfo->status_pipe = usb_rcvbulkpipe(udev,
857 usb_endpoint_num(&eps[1]->desc));
858 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
859 usb_endpoint_num(&eps[2]->desc));
860 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
861 usb_endpoint_num(&eps[3]->desc));
863 if (udev->speed != USB_SPEED_SUPER) {
864 devinfo->qdepth = 32;
865 devinfo->use_streams = 0;
867 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1,
868 3, MAX_CMNDS, GFP_NOIO);
869 if (devinfo->qdepth < 0)
870 return devinfo->qdepth;
871 devinfo->use_streams = 1;
877 static void uas_free_streams(struct uas_dev_info *devinfo)
879 struct usb_device *udev = devinfo->udev;
880 struct usb_host_endpoint *eps[3];
882 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
883 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
884 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
885 usb_free_streams(devinfo->intf, eps, 3, GFP_NOIO);
888 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
890 int result = -ENOMEM;
891 struct Scsi_Host *shost = NULL;
892 struct uas_dev_info *devinfo;
893 struct usb_device *udev = interface_to_usbdev(intf);
895 if (!uas_use_uas_driver(intf, id))
898 if (uas_switch_interface(udev, intf))
901 shost = scsi_host_alloc(&uas_host_template,
902 sizeof(struct uas_dev_info));
906 shost->max_cmd_len = 16 + 252;
908 shost->max_lun = 256;
909 shost->max_channel = 0;
910 shost->sg_tablesize = udev->bus->sg_tablesize;
912 devinfo = (struct uas_dev_info *)shost->hostdata;
913 devinfo->intf = intf;
914 devinfo->udev = udev;
915 devinfo->resetting = 0;
916 devinfo->shutdown = 0;
917 devinfo->flags = id->driver_info;
918 usb_stor_adjust_quirks(udev, &devinfo->flags);
919 init_usb_anchor(&devinfo->cmd_urbs);
920 init_usb_anchor(&devinfo->sense_urbs);
921 init_usb_anchor(&devinfo->data_urbs);
922 spin_lock_init(&devinfo->lock);
923 INIT_WORK(&devinfo->work, uas_do_work);
924 INIT_LIST_HEAD(&devinfo->inflight_list);
926 result = uas_configure_endpoints(devinfo);
930 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 2);
934 usb_set_intfdata(intf, shost);
935 result = scsi_add_host(shost, &intf->dev);
939 scsi_scan_host(shost);
943 uas_free_streams(devinfo);
944 usb_set_intfdata(intf, NULL);
946 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
948 scsi_host_put(shost);
952 static int uas_pre_reset(struct usb_interface *intf)
954 struct Scsi_Host *shost = usb_get_intfdata(intf);
955 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
958 if (devinfo->shutdown)
961 /* Block new requests */
962 spin_lock_irqsave(shost->host_lock, flags);
963 scsi_block_requests(shost);
964 spin_unlock_irqrestore(shost->host_lock, flags);
966 /* Wait for any pending requests to complete */
967 flush_work(&devinfo->work);
968 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
969 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
973 uas_free_streams(devinfo);
978 static int uas_post_reset(struct usb_interface *intf)
980 struct Scsi_Host *shost = usb_get_intfdata(intf);
981 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
984 if (devinfo->shutdown)
987 if (uas_configure_endpoints(devinfo) != 0) {
988 shost_printk(KERN_ERR, shost,
989 "%s: alloc streams error after reset", __func__);
993 spin_lock_irqsave(shost->host_lock, flags);
994 scsi_report_bus_reset(shost, 0);
995 spin_unlock_irqrestore(shost->host_lock, flags);
997 scsi_unblock_requests(shost);
1002 static int uas_suspend(struct usb_interface *intf, pm_message_t message)
1004 struct Scsi_Host *shost = usb_get_intfdata(intf);
1005 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1007 /* Wait for any pending requests to complete */
1008 flush_work(&devinfo->work);
1009 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 5000) == 0) {
1010 shost_printk(KERN_ERR, shost, "%s: timed out\n", __func__);
1017 static int uas_resume(struct usb_interface *intf)
1022 static int uas_reset_resume(struct usb_interface *intf)
1024 struct Scsi_Host *shost = usb_get_intfdata(intf);
1025 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1026 unsigned long flags;
1028 if (uas_configure_endpoints(devinfo) != 0) {
1029 shost_printk(KERN_ERR, shost,
1030 "%s: alloc streams error after reset", __func__);
1034 spin_lock_irqsave(shost->host_lock, flags);
1035 scsi_report_bus_reset(shost, 0);
1036 spin_unlock_irqrestore(shost->host_lock, flags);
1041 static void uas_disconnect(struct usb_interface *intf)
1043 struct Scsi_Host *shost = usb_get_intfdata(intf);
1044 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1045 unsigned long flags;
1047 spin_lock_irqsave(&devinfo->lock, flags);
1048 devinfo->resetting = 1;
1049 spin_unlock_irqrestore(&devinfo->lock, flags);
1051 cancel_work_sync(&devinfo->work);
1052 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
1053 usb_kill_anchored_urbs(&devinfo->sense_urbs);
1054 usb_kill_anchored_urbs(&devinfo->data_urbs);
1055 uas_zap_pending(devinfo, DID_NO_CONNECT);
1057 scsi_remove_host(shost);
1058 uas_free_streams(devinfo);
1059 scsi_host_put(shost);
1063 * Put the device back in usb-storage mode on shutdown, as some BIOS-es
1064 * hang on reboot when the device is still in uas mode. Note the reset is
1065 * necessary as some devices won't revert to usb-storage mode without it.
1067 static void uas_shutdown(struct device *dev)
1069 struct usb_interface *intf = to_usb_interface(dev);
1070 struct usb_device *udev = interface_to_usbdev(intf);
1071 struct Scsi_Host *shost = usb_get_intfdata(intf);
1072 struct uas_dev_info *devinfo = (struct uas_dev_info *)shost->hostdata;
1074 if (system_state != SYSTEM_RESTART)
1077 devinfo->shutdown = 1;
1078 uas_free_streams(devinfo);
1079 usb_set_interface(udev, intf->altsetting[0].desc.bInterfaceNumber, 0);
1080 usb_reset_device(udev);
1083 static struct usb_driver uas_driver = {
1086 .disconnect = uas_disconnect,
1087 .pre_reset = uas_pre_reset,
1088 .post_reset = uas_post_reset,
1089 .suspend = uas_suspend,
1090 .resume = uas_resume,
1091 .reset_resume = uas_reset_resume,
1092 .drvwrap.driver.shutdown = uas_shutdown,
1093 .id_table = uas_usb_ids,
1096 module_usb_driver(uas_driver);
1098 MODULE_LICENSE("GPL");
1100 "Hans de Goede <hdegoede@redhat.com>, Matthew Wilcox and Sarah Sharp");