UAS: Free status URB when we can't find the SCSI tag.
[linux-flexiantxendom0.git] / drivers / usb / storage / uas.c
index 721fe37..9dd4aae 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/blkdev.h>
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/module.h>
 #include <linux/usb.h>
 #include <linux/usb/storage.h>
 
@@ -100,8 +101,8 @@ struct uas_dev_info {
 };
 
 enum {
-       ALLOC_SENSE_URB         = (1 << 0),
-       SUBMIT_SENSE_URB        = (1 << 1),
+       ALLOC_STATUS_URB        = (1 << 0),
+       SUBMIT_STATUS_URB       = (1 << 1),
        ALLOC_DATA_IN_URB       = (1 << 2),
        SUBMIT_DATA_IN_URB      = (1 << 3),
        ALLOC_DATA_OUT_URB      = (1 << 4),
@@ -115,7 +116,7 @@ struct uas_cmd_info {
        unsigned int state;
        unsigned int stream;
        struct urb *cmd_urb;
-       struct urb *sense_urb;
+       struct urb *status_urb;
        struct urb *data_in_urb;
        struct urb *data_out_urb;
        struct list_head list;
@@ -124,29 +125,38 @@ struct uas_cmd_info {
 /* I hate forward declarations, but I actually have a loop */
 static int uas_submit_urbs(struct scsi_cmnd *cmnd,
                                struct uas_dev_info *devinfo, gfp_t gfp);
+static void uas_do_work(struct work_struct *work);
 
+static DECLARE_WORK(uas_work, uas_do_work);
 static DEFINE_SPINLOCK(uas_work_lock);
 static LIST_HEAD(uas_work_list);
 
 static void uas_do_work(struct work_struct *work)
 {
        struct uas_cmd_info *cmdinfo;
+       struct uas_cmd_info *temp;
        struct list_head list;
+       int err;
 
        spin_lock_irq(&uas_work_lock);
        list_replace_init(&uas_work_list, &list);
        spin_unlock_irq(&uas_work_lock);
 
-       list_for_each_entry(cmdinfo, &list, list) {
+       list_for_each_entry_safe(cmdinfo, temp, &list, list) {
                struct scsi_pointer *scp = (void *)cmdinfo;
                struct scsi_cmnd *cmnd = container_of(scp,
                                                        struct scsi_cmnd, SCp);
-               uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_KERNEL);
+               err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_NOIO);
+               if (err) {
+                       list_del(&cmdinfo->list);
+                       spin_lock_irq(&uas_work_lock);
+                       list_add_tail(&cmdinfo->list, &uas_work_list);
+                       spin_unlock_irq(&uas_work_lock);
+                       schedule_work(&uas_work);
+               }
        }
 }
 
-static DECLARE_WORK(uas_work, uas_do_work);
-
 static void uas_sense(struct urb *urb, struct scsi_cmnd *cmnd)
 {
        struct sense_iu *sense_iu = urb->transfer_buffer;
@@ -207,7 +217,7 @@ static void uas_xfer_data(struct urb *urb, struct scsi_cmnd *cmnd,
        struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
        int err;
 
-       cmdinfo->state = direction | SUBMIT_SENSE_URB;
+       cmdinfo->state = direction | SUBMIT_STATUS_URB;
        err = uas_submit_urbs(cmnd, cmnd->device->hostdata, GFP_ATOMIC);
        if (err) {
                spin_lock(&uas_work_lock);
@@ -236,8 +246,10 @@ static void uas_stat_cmplt(struct urb *urb)
                cmnd = sdev->current_cmnd;
        else
                cmnd = scsi_find_tag(sdev, tag);
-       if (!cmnd)
+       if (!cmnd) {
+               usb_free_urb(urb);
                return;
+       }
 
        switch (iu->iu_id) {
        case IU_ID_STATUS:
@@ -333,11 +345,11 @@ static struct urb *uas_alloc_cmd_urb(struct uas_dev_info *devinfo, gfp_t gfp,
                goto free;
 
        iu->iu_id = IU_ID_COMMAND;
-       iu->tag = cpu_to_be16(stream_id);
-       if (sdev->ordered_tags && (cmnd->request->cmd_flags & REQ_HARDBARRIER))
-               iu->prio_attr = UAS_ORDERED_TAG;
+       if (blk_rq_tagged(cmnd->request))
+               iu->tag = cpu_to_be16(cmnd->request->tag + 1);
        else
-               iu->prio_attr = UAS_SIMPLE_TAG;
+               iu->tag = cpu_to_be16(1);
+       iu->prio_attr = UAS_SIMPLE_TAG;
        iu->len = len;
        int_to_scsilun(sdev->lun, &iu->lun);
        memcpy(iu->cdb, cmnd->cmnd, cmnd->cmd_len);
@@ -363,21 +375,21 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
 {
        struct uas_cmd_info *cmdinfo = (void *)&cmnd->SCp;
 
-       if (cmdinfo->state & ALLOC_SENSE_URB) {
-               cmdinfo->sense_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd,
-                                                       cmdinfo->stream);
-               if (!cmdinfo->sense_urb)
+       if (cmdinfo->state & ALLOC_STATUS_URB) {
+               cmdinfo->status_urb = uas_alloc_sense_urb(devinfo, gfp, cmnd,
+                                                         cmdinfo->stream);
+               if (!cmdinfo->status_urb)
                        return SCSI_MLQUEUE_DEVICE_BUSY;
-               cmdinfo->state &= ~ALLOC_SENSE_URB;
+               cmdinfo->state &= ~ALLOC_STATUS_URB;
        }
 
-       if (cmdinfo->state & SUBMIT_SENSE_URB) {
-               if (usb_submit_urb(cmdinfo->sense_urb, gfp)) {
+       if (cmdinfo->state & SUBMIT_STATUS_URB) {
+               if (usb_submit_urb(cmdinfo->status_urb, gfp)) {
                        scmd_printk(KERN_INFO, cmnd,
                                        "sense urb submission failure\n");
                        return SCSI_MLQUEUE_DEVICE_BUSY;
                }
-               cmdinfo->state &= ~SUBMIT_SENSE_URB;
+               cmdinfo->state &= ~SUBMIT_STATUS_URB;
        }
 
        if (cmdinfo->state & ALLOC_DATA_IN_URB) {
@@ -436,7 +448,7 @@ static int uas_submit_urbs(struct scsi_cmnd *cmnd,
        return 0;
 }
 
-static int uas_queuecommand(struct scsi_cmnd *cmnd,
+static int uas_queuecommand_lck(struct scsi_cmnd *cmnd,
                                        void (*done)(struct scsi_cmnd *))
 {
        struct scsi_device *sdev = cmnd->device;
@@ -446,7 +458,7 @@ static int uas_queuecommand(struct scsi_cmnd *cmnd,
 
        BUILD_BUG_ON(sizeof(struct uas_cmd_info) > sizeof(struct scsi_pointer));
 
-       if (!cmdinfo->sense_urb && sdev->current_cmnd)
+       if (!cmdinfo->status_urb && sdev->current_cmnd)
                return SCSI_MLQUEUE_DEVICE_BUSY;
 
        if (blk_rq_tagged(cmnd->request)) {
@@ -458,7 +470,7 @@ static int uas_queuecommand(struct scsi_cmnd *cmnd,
 
        cmnd->scsi_done = done;
 
-       cmdinfo->state = ALLOC_SENSE_URB | SUBMIT_SENSE_URB |
+       cmdinfo->state = ALLOC_STATUS_URB | SUBMIT_STATUS_URB |
                        ALLOC_CMD_URB | SUBMIT_CMD_URB;
 
        switch (cmnd->sc_data_direction) {
@@ -481,8 +493,8 @@ static int uas_queuecommand(struct scsi_cmnd *cmnd,
        err = uas_submit_urbs(cmnd, devinfo, GFP_ATOMIC);
        if (err) {
                /* If we did nothing, give up now */
-               if (cmdinfo->state & SUBMIT_SENSE_URB) {
-                       usb_free_urb(cmdinfo->sense_urb);
+               if (cmdinfo->state & SUBMIT_STATUS_URB) {
+                       usb_free_urb(cmdinfo->status_urb);
                        return SCSI_MLQUEUE_DEVICE_BUSY;
                }
                spin_lock(&uas_work_lock);
@@ -494,6 +506,8 @@ static int uas_queuecommand(struct scsi_cmnd *cmnd,
        return 0;
 }
 
+static DEF_SCSI_QCMD(uas_queuecommand)
+
 static int uas_eh_abort_handler(struct scsi_cmnd *cmnd)
 {
        struct scsi_device *sdev = cmnd->device;
@@ -582,6 +596,34 @@ static struct usb_device_id uas_usb_ids[] = {
 };
 MODULE_DEVICE_TABLE(usb, uas_usb_ids);
 
+static int uas_is_interface(struct usb_host_interface *intf)
+{
+       return (intf->desc.bInterfaceClass == USB_CLASS_MASS_STORAGE &&
+               intf->desc.bInterfaceSubClass == USB_SC_SCSI &&
+               intf->desc.bInterfaceProtocol == USB_PR_UAS);
+}
+
+static int uas_switch_interface(struct usb_device *udev,
+                                               struct usb_interface *intf)
+{
+       int i;
+
+       if (uas_is_interface(intf->cur_altsetting))
+               return 0;
+
+       for (i = 0; i < intf->num_altsetting; i++) {
+               struct usb_host_interface *alt = &intf->altsetting[i];
+               if (alt == intf->cur_altsetting)
+                       continue;
+               if (uas_is_interface(alt))
+                       return usb_set_interface(udev,
+                                               alt->desc.bInterfaceNumber,
+                                               alt->desc.bAlternateSetting);
+       }
+
+       return -ENODEV;
+}
+
 static void uas_configure_endpoints(struct uas_dev_info *devinfo)
 {
        struct usb_host_endpoint *eps[4] = { };
@@ -655,13 +697,8 @@ static int uas_probe(struct usb_interface *intf, const struct usb_device_id *id)
        struct uas_dev_info *devinfo;
        struct usb_device *udev = interface_to_usbdev(intf);
 
-       if (id->bInterfaceProtocol == 0x50) {
-               int ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
-/* XXX: Shouldn't assume that 1 is the alternative we want */
-               int ret = usb_set_interface(udev, ifnum, 1);
-               if (ret)
-                       return -ENODEV;
-       }
+       if (uas_switch_interface(udev, intf))
+               return -ENODEV;
 
        devinfo = kmalloc(sizeof(struct uas_dev_info), GFP_KERNEL);
        if (!devinfo)