3 * Note that this is not the same as the USB Mass Storage driver
5 * Copyright Matthew Wilcox for Intel Corp, 2010
6 * Copyright Sarah Sharp for Intel Corp, 2010
8 * Distributed under the terms of the GNU GPL, version two.
11 #include <linux/blkdev.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/usb/hcd.h>
17 #include <linux/usb/storage.h>
18 #include <linux/usb/uas.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_dbg.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_device.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi_tcq.h>
28 * The r00-r01c specs define this version of the SENSE IU data structure.
29 * It's still in use by several different firmware releases.
37 __u8 service_response;
38 __u8 sense[SCSI_SENSE_BUFFERSIZE];
42 struct usb_interface *intf;
43 struct usb_device *udev;
44 struct usb_anchor cmd_urbs;
45 struct usb_anchor sense_urbs;
46 struct usb_anchor data_urbs;
47 int qdepth, resetting;
48 struct response_ui response;
49 unsigned cmd_pipe, status_pipe, data_in_pipe, data_out_pipe;
50 unsigned use_streams:1;
51 unsigned uas_sense_old:1;
52 struct scsi_cmnd *cmnd;
57 SUBMIT_STATUS_URB = (1 << 1),
58 ALLOC_DATA_IN_URB = (1 << 2),
59 SUBMIT_DATA_IN_URB = (1 << 3),
60 ALLOC_DATA_OUT_URB = (1 << 4),
61 SUBMIT_DATA_OUT_URB = (1 << 5),
62 ALLOC_CMD_URB = (1 << 6),
63 SUBMIT_CMD_URB = (1 << 7),
64 COMMAND_INFLIGHT = (1 << 8),
65 DATA_IN_URB_INFLIGHT = (1 << 9),
66 DATA_OUT_URB_INFLIGHT = (1 << 10),
67 COMMAND_COMPLETED = (1 << 11),
68 COMMAND_ABORTED = (1 << 12),
71 /* Overrides scsi_pointer */
76 struct urb *data_in_urb;
77 struct urb *data_out_urb;
78 struct list_head list;
81 /* I hate forward declarations, but I actually have a loop */
82 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
83 struct uas_dev_info *devinfo, gfp_t gfp);
84 static void uas_do_work(struct work_struct *work);
86 static DECLARE_WORK(uas_work, uas_do_work);
87 static DEFINE_SPINLOCK(uas_work_lock);
88 static LIST_HEAD(uas_work_list);
90 static void uas_do_work(struct work_struct *work)
92 struct uas_cmd_info *cmdinfo;
93 struct uas_cmd_info *temp;
94 struct list_head list;
98 spin_lock_irq(&uas_work_lock);
99 list_replace_init(&uas_work_list, &list);
100 spin_unlock_irq(&uas_work_lock);
102 list_for_each_entry_safe(cmdinfo, temp, &list, list) {
103 struct scsi_pointer *scp = (void *)cmdinfo;
104 struct scsi_cmnd *cmnd = container_of(scp,
105 struct scsi_cmnd, SCp);
106 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
107 spin_lock_irqsave(&devinfo->lock, flags);
108 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
109 spin_unlock_irqrestore(&devinfo->lock, flags);
111 list_del(&cmdinfo->list);
112 spin_lock_irq(&uas_work_lock);
113 list_add_tail(&cmdinfo->list, &uas_work_list);
114 spin_unlock_irq(&uas_work_lock);
115 schedule_work(&uas_work);
120 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
122 struct sense_iu *sense_iu = urb->transfer_buffer;
123 struct scsi_device *sdev = cmnd->device;
125 if (urb->actual_length > 16) {
126 unsigned len = be16_to_cpup(&sense_iu->len);
127 if (len + 16 != urb->actual_length) {
128 int newlen = min(len + 16, urb->actual_length) - 16;
131 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
132 "disagrees with IU sense data length %d, "
133 "using %d bytes of sense data\n", __func__,
134 urb->actual_length, len, newlen);
137 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
140 cmnd->result = sense_iu->status;
143 static void uas_sense_old(struct urb *urb, struct scsi_cmnd *cmnd)
145 struct sense_iu_old *sense_iu = urb->transfer_buffer;
146 struct scsi_device *sdev = cmnd->device;
148 if (urb->actual_length > 8) {
149 unsigned len = be16_to_cpup(&sense_iu->len) - 2;
150 if (len + 8 != urb->actual_length) {
151 int newlen = min(len + 8, urb->actual_length) - 8;
154 sdev_printk(KERN_INFO, sdev, "%s: urb length %d "
155 "disagrees with IU sense data length %d, "
156 "using %d bytes of sense data\n", __func__,
157 urb->actual_length, len, newlen);
160 memcpy(cmnd->sense_buffer, sense_iu->sense, len);
163 cmnd->result = sense_iu->status;
166 static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *caller)
168 struct uas_cmd_info *ci = (void *)&cmnd->SCp;
170 scmd_printk(KERN_INFO, cmnd, "%s %p tag %d, inflight:"
171 "%s%s%s%s%s%s%s%s%s%s%s%s\n",
172 caller, cmnd, cmnd->request->tag,
173 (ci->state & SUBMIT_STATUS_URB) ? " s-st" : "",
174 (ci->state & ALLOC_DATA_IN_URB) ? " a-in" : "",
175 (ci->state & SUBMIT_DATA_IN_URB) ? " s-in" : "",
176 (ci->state & ALLOC_DATA_OUT_URB) ? " a-out" : "",
177 (ci->state & SUBMIT_DATA_OUT_URB) ? " s-out" : "",
178 (ci->state & ALLOC_CMD_URB) ? " a-cmd" : "",
179 (ci->state & SUBMIT_CMD_URB) ? " s-cmd" : "",
180 (ci->state & COMMAND_INFLIGHT) ? " CMD" : "",
181 (ci->state & DATA_IN_URB_INFLIGHT) ? " IN" : "",
182 (ci->state & DATA_OUT_URB_INFLIGHT) ? " OUT" : "",
183 (ci->state & COMMAND_COMPLETED) ? " done" : "",
184 (ci->state & COMMAND_ABORTED) ? " abort" : "");
187 static int uas_try_complete(struct scsi_cmnd *cmnd, const char *caller)
189 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
190 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
192 WARN_ON(!spin_is_locked(&devinfo->lock));
193 if (cmdinfo->state & (COMMAND_INFLIGHT |
194 DATA_IN_URB_INFLIGHT |
195 DATA_OUT_URB_INFLIGHT))
197 BUG_ON(cmdinfo->state & COMMAND_COMPLETED);
198 cmdinfo->state |= COMMAND_COMPLETED;
199 usb_free_urb(cmdinfo->data_in_urb);
200 usb_free_urb(cmdinfo->data_out_urb);
201 if (cmdinfo->state & COMMAND_ABORTED) {
202 scmd_printk(KERN_INFO, cmnd, "abort completed\n");
203 cmnd->result = DID_ABORT << 16;
205 cmnd->scsi_done(cmnd);
209 static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
212 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
215 cmdinfo->state |= direction | SUBMIT_STATUS_URB;
216 err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
218 spin_lock(&uas_work_lock);
219 list_add_tail(&cmdinfo->list, &uas_work_list);
220 spin_unlock(&uas_work_lock);
221 schedule_work(&uas_work);
225 static void uas_stat_cmplt(struct urb *urb)
227 struct iu *iu = urb->transfer_buffer;
228 struct Scsi_Host *shost = urb->context;
229 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
230 struct scsi_cmnd *cmnd;
231 struct uas_cmd_info *cmdinfo;
236 dev_err(&urb->dev->dev, "URB BAD STATUS %d\n", urb->status);
241 if (devinfo->resetting) {
246 spin_lock_irqsave(&devinfo->lock, flags);
247 tag = be16_to_cpup(&iu->tag) - 1;
249 cmnd = devinfo->cmnd;
251 cmnd = scsi_host_find_tag(shost, tag - 1);
254 if (iu->iu_id == IU_ID_RESPONSE) {
255 /* store results for uas_eh_task_mgmt() */
256 memcpy(&devinfo->response, iu, sizeof(devinfo->response));
259 spin_unlock_irqrestore(&devinfo->lock, flags);
263 cmdinfo = (void *)&cmnd->SCp;
266 if (devinfo->cmnd == cmnd)
267 devinfo->cmnd = NULL;
269 if (urb->actual_length < 16)
270 devinfo->uas_sense_old = 1;
271 if (devinfo->uas_sense_old)
272 uas_sense_old(urb, cmnd);
274 uas_sense(urb, cmnd);
275 if (cmnd->result != 0) {
276 /* cancel data transfers on error */
277 if (cmdinfo->state & DATA_IN_URB_INFLIGHT) {
278 spin_unlock_irqrestore(&devinfo->lock, flags);
279 usb_unlink_urb(cmdinfo->data_in_urb);
280 spin_lock_irqsave(&devinfo->lock, flags);
282 if (cmdinfo->state & DATA_OUT_URB_INFLIGHT) {
283 spin_unlock_irqrestore(&devinfo->lock, flags);
284 usb_unlink_urb(cmdinfo->data_out_urb);
285 spin_lock_irqsave(&devinfo->lock, flags);
288 cmdinfo->state &= ~COMMAND_INFLIGHT;
289 uas_try_complete(cmnd, __func__);
291 case IU_ID_READ_READY:
292 uas_xfer_data(urb, cmnd, SUBMIT_DATA_IN_URB);
294 case IU_ID_WRITE_READY:
295 uas_xfer_data(urb, cmnd, SUBMIT_DATA_OUT_URB);
298 scmd_printk(KERN_ERR, cmnd,
299 "Bogus IU (%d) received on status pipe\n", iu->iu_id);
302 spin_unlock_irqrestore(&devinfo->lock, flags);
305 static void uas_data_cmplt(struct urb *urb)
307 struct scsi_cmnd *cmnd = urb->context;
308 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
309 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
310 struct scsi_data_buffer *sdb = NULL;
313 spin_lock_irqsave(&devinfo->lock, flags);
314 if (cmdinfo->data_in_urb == urb) {
316 cmdinfo->state &= ~DATA_IN_URB_INFLIGHT;
317 } else if (cmdinfo->data_out_urb == urb) {
318 sdb = scsi_out(cmnd);
319 cmdinfo->state &= ~DATA_OUT_URB_INFLIGHT;
323 /* error: no data transfered */
324 sdb->resid = sdb->length;
326 sdb->resid = sdb->length - urb->actual_length;
328 uas_try_complete(cmnd, __func__);
329 spin_unlock_irqrestore(&devinfo->lock, flags);
332 static struct urb *uas_alloc_data_urb(struct uas_dev_info *devinfo, gfp_t gfp,
333 unsigned int pipe, u16 stream_id,
334 struct scsi_cmnd *cmnd,
335 enum dma_data_direction dir)
337 struct usb_device *udev = devinfo->udev;
338 struct urb *urb = usb_alloc_urb(0, gfp);
339 struct scsi_data_buffer *sdb = (dir == DMA_FROM_DEVICE)
340 ? scsi_in(cmnd) : scsi_out(cmnd);
344 usb_fill_bulk_urb(urb, udev, pipe, NULL, sdb->length,
345 uas_data_cmplt, cmnd);
346 if (devinfo->use_streams)
347 urb->stream_id = stream_id;
348 urb->num_sgs = udev->bus->sg_tablesize ? sdb->table.nents : 0;
349 urb->sg = sdb->table.sgl;
354 static struct urb *uas_alloc_sense_urb(struct uas_dev_info *devinfo, gfp_t gfp,
355 struct Scsi_Host *shost, u16 stream_id)
357 struct usb_device *udev = devinfo->udev;
358 struct urb *urb = usb_alloc_urb(0, gfp);
364 iu = kzalloc(sizeof(*iu), gfp);
368 usb_fill_bulk_urb(urb, udev, devinfo->status_pipe, iu, sizeof(*iu),
369 uas_stat_cmplt, shost);
370 urb->stream_id = stream_id;
371 urb->transfer_flags |= URB_FREE_BUFFER;
379 static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
380 struct scsi_cmnd *cmnd, u16 stream_id)
382 struct usb_device *udev = devinfo->udev;
383 struct scsi_device *sdev = cmnd->device;
384 struct urb *urb = usb_alloc_urb(0, gfp);
385 struct command_iu *iu;
391 len = cmnd->cmd_len - 16;
395 iu = kzalloc(sizeof(*iu) + len, gfp);
399 iu->iu_id = IU_ID_COMMAND;
400 if (blk_rq_tagged(cmnd->request))
401 iu->tag = cpu_to_be16(cmnd->request->tag + 2);
403 iu->tag = cpu_to_be16(1);
404 iu->prio_attr = UAS_SIMPLE_TAG;
406 int_to_scsilun(sdev->lun, &iu->lun);
407 memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
409 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu) + len,
411 urb->transfer_flags |= URB_FREE_BUFFER;
419 static int uas_submit_task_urb(struct scsi_cmnd *cmnd, gfp_t gfp,
420 u8 function, u16 stream_id)
422 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
423 struct usb_device *udev = devinfo->udev;
424 struct urb *urb = usb_alloc_urb(0, gfp);
425 struct task_mgmt_iu *iu;
431 iu = kzalloc(sizeof(*iu), gfp);
435 iu->iu_id = IU_ID_TASK_MGMT;
436 iu->tag = cpu_to_be16(stream_id);
437 int_to_scsilun(cmnd->device->lun, &iu->lun);
439 iu->function = function;
442 if (blk_rq_tagged(cmnd->request))
443 iu->task_tag = cpu_to_be16(cmnd->request->tag + 2);
445 iu->task_tag = cpu_to_be16(1);
449 usb_fill_bulk_urb(urb, udev, devinfo->cmd_pipe, iu, sizeof(*iu),
451 urb->transfer_flags |= URB_FREE_BUFFER;
453 err = usb_submit_urb(urb, gfp);
456 usb_anchor_urb(urb, &devinfo->cmd_urbs);
466 * Why should I request the Status IU before sending the Command IU? Spec
467 * says to, but also says the device may receive them in any order. Seems
471 static int uas_submit_sense_urb(struct Scsi_Host *shost,
472 gfp_t gfp, unsigned int stream)
474 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
477 urb = uas_alloc_sense_urb(devinfo, gfp, shost, stream);
479 return SCSI_MLQUEUE_DEVICE_BUSY;
480 if (usb_submit_urb(urb, gfp)) {
481 shost_printk(KERN_INFO, shost,
482 "sense urb submission failure\n");
484 return SCSI_MLQUEUE_DEVICE_BUSY;
486 usb_anchor_urb(urb, &devinfo->sense_urbs);
490 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
491 struct uas_dev_info *devinfo, gfp_t gfp)
493 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
496 WARN_ON(!spin_is_locked(&devinfo->lock));
497 if (cmdinfo->state & SUBMIT_STATUS_URB) {
498 err = uas_submit_sense_urb(cmnd->device->host, gfp,
503 cmdinfo->state &= ~SUBMIT_STATUS_URB;
506 if (cmdinfo->state & ALLOC_DATA_IN_URB) {
507 cmdinfo->data_in_urb = uas_alloc_data_urb(devinfo, gfp,
508 devinfo->data_in_pipe, cmdinfo->stream,
509 cmnd, DMA_FROM_DEVICE);
510 if (!cmdinfo->data_in_urb)
511 return SCSI_MLQUEUE_DEVICE_BUSY;
512 cmdinfo->state &= ~ALLOC_DATA_IN_URB;
515 if (cmdinfo->state & SUBMIT_DATA_IN_URB) {
516 if (usb_submit_urb(cmdinfo->data_in_urb, gfp)) {
517 scmd_printk(KERN_INFO, cmnd,
518 "data in urb submission failure\n");
519 return SCSI_MLQUEUE_DEVICE_BUSY;
521 cmdinfo->state &= ~SUBMIT_DATA_IN_URB;
522 cmdinfo->state |= DATA_IN_URB_INFLIGHT;
523 usb_anchor_urb(cmdinfo->data_in_urb, &devinfo->data_urbs);
526 if (cmdinfo->state & ALLOC_DATA_OUT_URB) {
527 cmdinfo->data_out_urb = uas_alloc_data_urb(devinfo, gfp,
528 devinfo->data_out_pipe, cmdinfo->stream,
529 cmnd, DMA_TO_DEVICE);
530 if (!cmdinfo->data_out_urb)
531 return SCSI_MLQUEUE_DEVICE_BUSY;
532 cmdinfo->state &= ~ALLOC_DATA_OUT_URB;
535 if (cmdinfo->state & SUBMIT_DATA_OUT_URB) {
536 if (usb_submit_urb(cmdinfo->data_out_urb, gfp)) {
537 scmd_printk(KERN_INFO, cmnd,
538 "data out urb submission failure\n");
539 return SCSI_MLQUEUE_DEVICE_BUSY;
541 cmdinfo->state &= ~SUBMIT_DATA_OUT_URB;
542 cmdinfo->state |= DATA_OUT_URB_INFLIGHT;
543 usb_anchor_urb(cmdinfo->data_out_urb, &devinfo->data_urbs);
546 if (cmdinfo->state & ALLOC_CMD_URB) {
547 cmdinfo->cmd_urb = uas_alloc_cmd_urb(devinfo, gfp, cmnd,
549 if (!cmdinfo->cmd_urb)
550 return SCSI_MLQUEUE_DEVICE_BUSY;
551 cmdinfo->state &= ~ALLOC_CMD_URB;
554 if (cmdinfo->state & SUBMIT_CMD_URB) {
555 usb_get_urb(cmdinfo->cmd_urb);
556 if (usb_submit_urb(cmdinfo->cmd_urb, gfp)) {
557 scmd_printk(KERN_INFO, cmnd,
558 "cmd urb submission failure\n");
559 return SCSI_MLQUEUE_DEVICE_BUSY;
561 usb_anchor_urb(cmdinfo->cmd_urb, &devinfo->cmd_urbs);
562 usb_put_urb(cmdinfo->cmd_urb);
563 cmdinfo->cmd_urb = NULL;
564 cmdinfo->state &= ~SUBMIT_CMD_URB;
565 cmdinfo->state |= COMMAND_INFLIGHT;
571 static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
572 void (*done)(struct scsi_cmnd *))
574 struct scsi_device *sdev = cmnd->device;
575 struct uas_dev_info *devinfo = sdev->hostdata;
576 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
580 BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
582 spin_lock_irqsave(&devinfo->lock, flags);
584 spin_unlock_irqrestore(&devinfo->lock, flags);
585 return SCSI_MLQUEUE_DEVICE_BUSY;
588 if (blk_rq_tagged(cmnd->request)) {
589 cmdinfo->stream = cmnd->request->tag + 2;
591 devinfo->cmnd = cmnd;
595 cmnd->scsi_done = done;
597 cmdinfo->state = SUBMIT_STATUS_URB |
598 ALLOC_CMD_URB | SUBMIT_CMD_URB;
600 switch (cmnd->sc_data_direction) {
601 case DMA_FROM_DEVICE:
602 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
604 case DMA_BIDIRECTIONAL:
605 cmdinfo->state |= ALLOC_DATA_IN_URB | SUBMIT_DATA_IN_URB;
607 cmdinfo->state |= ALLOC_DATA_OUT_URB | SUBMIT_DATA_OUT_URB;
612 if (!devinfo->use_streams) {
613 cmdinfo->state &= ~(SUBMIT_DATA_IN_URB | SUBMIT_DATA_OUT_URB);
617 err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
619 /* If we did nothing, give up now */
620 if (cmdinfo->state & SUBMIT_STATUS_URB) {
621 spin_unlock_irqrestore(&devinfo->lock, flags);
622 return SCSI_MLQUEUE_DEVICE_BUSY;
624 spin_lock(&uas_work_lock);
625 list_add_tail(&cmdinfo->list, &uas_work_list);
626 spin_unlock(&uas_work_lock);
627 schedule_work(&uas_work);
630 spin_unlock_irqrestore(&devinfo->lock, flags);
634 static DEF_SCSI_QCMD(uas_queuecommand)
636 static int uas_eh_task_mgmt(struct scsi_cmnd *cmnd,
637 const char *fname, u8 function)
639 struct Scsi_Host *shost = cmnd->device->host;
640 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
641 u16 tag = devinfo->qdepth - 1;
644 spin_lock_irqsave(&devinfo->lock, flags);
645 memset(&devinfo->response, 0, sizeof(devinfo->response));
646 if (uas_submit_sense_urb(shost, GFP_ATOMIC, tag)) {
647 shost_printk(KERN_INFO, shost,
648 "%s: %s: submit sense urb failed\n",
650 spin_unlock_irqrestore(&devinfo->lock, flags);
653 if (uas_submit_task_urb(cmnd, GFP_ATOMIC, function, tag)) {
654 shost_printk(KERN_INFO, shost,
655 "%s: %s: submit task mgmt urb failed\n",
657 spin_unlock_irqrestore(&devinfo->lock, flags);
660 spin_unlock_irqrestore(&devinfo->lock, flags);
662 if (usb_wait_anchor_empty_timeout(&devinfo->sense_urbs, 3000) == 0) {
663 shost_printk(KERN_INFO, shost,
664 "%s: %s timed out\n", __func__, fname);
667 if (be16_to_cpu(devinfo->response.tag) != tag) {
668 shost_printk(KERN_INFO, shost,
669 "%s: %s failed (wrong tag %d/%d)\n", __func__,
670 fname, be16_to_cpu(devinfo->response.tag), tag);
673 if (devinfo->response.response_code != RC_TMF_COMPLETE) {
674 shost_printk(KERN_INFO, shost,
675 "%s: %s failed (rc 0x%x)\n", __func__,
676 fname, devinfo->response.response_code);
682 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
684 struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
685 struct uas_dev_info *devinfo = (void *)cmnd->device->hostdata;
689 uas_log_cmd_state(cmnd, __func__);
690 spin_lock_irqsave(&devinfo->lock, flags);
691 cmdinfo->state |= COMMAND_ABORTED;
692 spin_unlock_irqrestore(&devinfo->lock, flags);
693 ret = uas_eh_task_mgmt(cmnd, "ABORT TASK", TMF_ABORT_TASK);
697 static int uas_eh_device_reset_handler(struct scsi_cmnd *cmnd)
699 sdev_printk(KERN_INFO, cmnd->device, "%s\n", __func__);
700 return uas_eh_task_mgmt(cmnd, "LOGICAL UNIT RESET",
701 TMF_LOGICAL_UNIT_RESET);
704 static int uas_eh_bus_reset_handler(struct scsi_cmnd *cmnd)
706 struct scsi_device *sdev = cmnd->device;
707 struct uas_dev_info *devinfo = sdev->hostdata;
708 struct usb_device *udev = devinfo->udev;
711 devinfo->resetting = 1;
712 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
713 usb_kill_anchored_urbs(&devinfo->sense_urbs);
714 usb_kill_anchored_urbs(&devinfo->data_urbs);
715 err = usb_reset_device(udev);
716 devinfo->resetting = 0;
719 shost_printk(KERN_INFO, sdev->host, "%s FAILED\n", __func__);
723 shost_printk(KERN_INFO, sdev->host, "%s success\n", __func__);
727 static int uas_slave_alloc(struct scsi_device *sdev)
729 sdev->hostdata = (void *)sdev->host->hostdata[0];
733 static int uas_slave_configure(struct scsi_device *sdev)
735 struct uas_dev_info *devinfo = sdev->hostdata;
736 scsi_set_tag_type(sdev, MSG_ORDERED_TAG);
737 scsi_activate_tcq(sdev, devinfo->qdepth - 3);
741 static struct scsi_host_template uas_host_template = {
742 .module = THIS_MODULE,
744 .queuecommand = uas_queuecommand,
745 .slave_alloc = uas_slave_alloc,
746 .slave_configure = uas_slave_configure,
747 .eh_abort_handler = uas_eh_abort_handler,
748 .eh_device_reset_handler = uas_eh_device_reset_handler,
749 .eh_bus_reset_handler = uas_eh_bus_reset_handler,
750 .can_queue = 65536, /* Is there a limit on the _host_ ? */
752 .sg_tablesize = SG_NONE,
753 .cmd_per_lun = 1, /* until we override it */
754 .skip_settle_delay = 1,
758 static struct usb_device_id uas_usb_ids[] = {
759 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
760 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_UAS) },
761 /* 0xaa is a prototype device I happen to have access to */
762 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, 0xaa) },
765 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
767 static int uas_is_interface(struct usb_host_interface *intf)
769 return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
770 intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
771 intf->desc.bInterfaceProtocol == USB_PR_UAS);
774 static int uas_isnt_supported(struct usb_device *udev)
776 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
778 dev_warn(&udev->dev, "The driver for the USB controller %s does not "
779 "support scatter-gather which is\n",
780 hcd->driver->description);
781 dev_warn(&udev->dev, "required by the UAS driver. Please try an"
782 "alternative USB controller if you wish to use UAS.\n");
786 static int uas_switch_interface(struct usb_device *udev,
787 struct usb_interface *intf)
790 int sg_supported = udev->bus->sg_tablesize != 0;
792 for (i = 0; i < intf->num_altsetting; i++) {
793 struct usb_host_interface *alt = &intf->altsetting[i];
795 if (uas_is_interface(alt)) {
797 return uas_isnt_supported(udev);
798 return usb_set_interface(udev,
799 alt->desc.bInterfaceNumber,
800 alt->desc.bAlternateSetting);
807 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
809 struct usb_host_endpoint *eps[4] = { };
810 struct usb_interface *intf = devinfo->intf;
811 struct usb_device *udev = devinfo->udev;
812 struct usb_host_endpoint *endpoint = intf->cur_altsetting->endpoint;
813 unsigned i, n_endpoints = intf->cur_altsetting->desc.bNumEndpoints;
815 devinfo->uas_sense_old = 0;
816 devinfo->cmnd = NULL;
818 for (i = 0; i < n_endpoints; i++) {
819 unsigned char *extra = endpoint[i].extra;
820 int len = endpoint[i].extralen;
822 if (extra[1] == USB_DT_PIPE_USAGE) {
823 unsigned pipe_id = extra[2];
824 if (pipe_id > 0 && pipe_id < 5)
825 eps[pipe_id - 1] = &endpoint[i];
834 * Assume that if we didn't find a control pipe descriptor, we're
835 * using a device with old firmware that happens to be set up like
839 devinfo->cmd_pipe = usb_sndbulkpipe(udev, 1);
840 devinfo->status_pipe = usb_rcvbulkpipe(udev, 1);
841 devinfo->data_in_pipe = usb_rcvbulkpipe(udev, 2);
842 devinfo->data_out_pipe = usb_sndbulkpipe(udev, 2);
844 eps[1] = usb_pipe_endpoint(udev, devinfo->status_pipe);
845 eps[2] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
846 eps[3] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
848 devinfo->cmd_pipe = usb_sndbulkpipe(udev,
849 eps[0]->desc.bEndpointAddress);
850 devinfo->status_pipe = usb_rcvbulkpipe(udev,
851 eps[1]->desc.bEndpointAddress);
852 devinfo->data_in_pipe = usb_rcvbulkpipe(udev,
853 eps[2]->desc.bEndpointAddress);
854 devinfo->data_out_pipe = usb_sndbulkpipe(udev,
855 eps[3]->desc.bEndpointAddress);
858 devinfo->qdepth = usb_alloc_streams(devinfo->intf, eps + 1, 3, 256,
860 if (devinfo->qdepth < 0) {
861 devinfo->qdepth = 256;
862 devinfo->use_streams = 0;
864 devinfo->use_streams = 1;
868 static void uas_free_streams(struct uas_dev_info *devinfo)
870 struct usb_device *udev = devinfo->udev;
871 struct usb_host_endpoint *eps[3];
873 eps[0] = usb_pipe_endpoint(udev, devinfo->status_pipe);
874 eps[1] = usb_pipe_endpoint(udev, devinfo->data_in_pipe);
875 eps[2] = usb_pipe_endpoint(udev, devinfo->data_out_pipe);
876 usb_free_streams(devinfo->intf, eps, 3, GFP_KERNEL);
880 * XXX: What I'd like to do here is register a SCSI host for each USB host in
881 * the system. Follow usb-storage's design of registering a SCSI host for
882 * each USB device for the moment. Can implement this by walking up the
883 * USB hierarchy until we find a USB host.
885 static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
888 struct Scsi_Host *shost;
889 struct uas_dev_info *devinfo;
890 struct usb_device *udev = interface_to_usbdev(intf);
892 if (uas_switch_interface(udev, intf))
895 devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
900 shost = scsi_host_alloc(&uas_host_template, sizeof(void *));
904 shost->max_cmd_len = 16 + 252;
906 shost->sg_tablesize = udev->bus->sg_tablesize;
908 devinfo->intf = intf;
909 devinfo->udev = udev;
910 devinfo->resetting = 0;
911 init_usb_anchor(&devinfo->cmd_urbs);
912 init_usb_anchor(&devinfo->sense_urbs);
913 init_usb_anchor(&devinfo->data_urbs);
914 spin_lock_init(&devinfo->lock);
915 uas_configure_endpoints(devinfo);
917 result = scsi_init_shared_tag_map(shost, devinfo->qdepth - 3);
921 result = scsi_add_host(shost, &intf->dev);
925 shost->hostdata[0] = (unsigned long)devinfo;
927 scsi_scan_host(shost);
928 usb_set_intfdata(intf, shost);
932 uas_free_streams(devinfo);
936 scsi_host_put(shost);
940 static int uas_pre_reset(struct usb_interface *intf)
942 /* XXX: Need to return 1 if it's not our device in error handling */
946 static int uas_post_reset(struct usb_interface *intf)
948 /* XXX: Need to return 1 if it's not our device in error handling */
952 static void uas_disconnect(struct usb_interface *intf)
954 struct Scsi_Host *shost = usb_get_intfdata(intf);
955 struct uas_dev_info *devinfo = (void *)shost->hostdata[0];
957 scsi_remove_host(shost);
958 usb_kill_anchored_urbs(&devinfo->cmd_urbs);
959 usb_kill_anchored_urbs(&devinfo->sense_urbs);
960 usb_kill_anchored_urbs(&devinfo->data_urbs);
961 uas_free_streams(devinfo);
966 * XXX: Should this plug into libusual so we can auto-upgrade devices from
969 static struct usb_driver uas_driver = {
972 .disconnect = uas_disconnect,
973 .pre_reset = uas_pre_reset,
974 .post_reset = uas_post_reset,
975 .id_table = uas_usb_ids,
978 module_usb_driver(uas_driver);
980 MODULE_LICENSE("GPL");
981 MODULE_AUTHOR("Matthew Wilcox and Sarah Sharp");