vserver 1.9.5.x5
[linux-2.6.git] / drivers / block / ub.c
index f605535..acea9f0 100644 (file)
@@ -8,13 +8,11 @@
  * and is not licensed separately. See file COPYING for details.
  *
  * TODO (sorted by decreasing priority)
- *  -- ZIP does "ub: resid 18 len 0 act 0" and whole transport quits (toggles?)
+ *  -- Do resets with usb_device_reset (needs a thread context, use khubd)
  *  -- set readonly flag for CDs, set removable flag for CF readers
  *  -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
  *  -- support pphaneuf's SDDR-75 with two LUNs (also broken capacity...)
  *  -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
- *  -- do something about spin-down devices, they are extremely dangerous
- *     (ZIP is one. Needs spin-up command as well.)
  *  -- verify the 13 conditions and do bulk resets
  *  -- normal pool of commands instead of cmdv[]?
  *  -- kill last_pipe and simply do two-state clearing on both pipes
@@ -25,6 +23,7 @@
  *  -- prune comments, they are too volumnous
  *  -- Exterminate P3 printks
  *  -- Resove XXX's
+ *  -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
  */
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -37,7 +36,7 @@
 #define DRV_NAME "ub"
 #define DEVFS_NAME DRV_NAME
 
-#define UB_MAJOR 125   /* Stolen from Experimental range for a week - XXX */
+#define UB_MAJOR 180
 
 /*
  * Definitions which have to be scattered once we understand the layout better.
@@ -62,9 +61,9 @@
 
 /* command block wrapper */
 struct bulk_cb_wrap {
-       u32     Signature;              /* contains 'USBC' */
+       __le32  Signature;              /* contains 'USBC' */
        u32     Tag;                    /* unique per command id */
-       u32     DataTransferLength;     /* size of data */
+       __le32  DataTransferLength;     /* size of data */
        u8      Flags;                  /* direction in bit 0 */
        u8      Lun;                    /* LUN normally 0 */
        u8      Length;                 /* of of the CDB */
@@ -78,9 +77,9 @@ struct bulk_cb_wrap {
 
 /* command status wrapper */
 struct bulk_cs_wrap {
-       u32     Signature;              /* should = 'USBS' */
+       __le32  Signature;              /* should = 'USBS' */
        u32     Tag;                    /* same as original command */
-       u32     Residue;                /* amount not transferred */
+       __le32  Residue;                /* amount not transferred */
        u8      Status;                 /* see below */
 };
 
@@ -104,14 +103,13 @@ struct ub_dev;
 #define UB_MAX_SECTORS 64
 
 /*
- * A second ought to be enough for a 32K transfer (UB_MAX_SECTORS)
- * even if a webcam hogs the bus (famous last words).
- * Some CDs need a second to spin up though.
- * ZIP drive rejects commands when it's not spinning,
- * so it does not need long timeouts either.
+ * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
+ * even if a webcam hogs the bus, but some devices need time to spin up.
  */
 #define UB_URB_TIMEOUT (HZ*2)
-#define UB_CTRL_TIMEOUT        (HZ/2) /* 500ms ought to be enough to clear a stall */
+#define UB_DATA_TIMEOUT        (HZ*5)  /* ZIP does spin-ups in the data phase */
+#define UB_STAT_TIMEOUT        (HZ*5)  /* Same spinups and eject for a dataless cmd. */
+#define UB_CTRL_TIMEOUT        (HZ/2)  /* 500ms ought to be enough to clear a stall */
 
 /*
  * An instance of a SCSI command in transit.
@@ -157,7 +155,8 @@ struct ub_scsi_cmd {
        struct ub_scsi_cmd *next;
 
        int error;                      /* Return code - valid upon done */
-       int act_len;                    /* Return size */
+       unsigned int act_len;           /* Return size */
+       unsigned char key, asc, ascq;   /* May be valid if error==-EIO */
 
        int stat_count;                 /* Retries getting status. */
 
@@ -186,7 +185,7 @@ struct ub_capacity {
  */
 
 #define SCMD_ST_HIST_SZ   8
-#define SCMD_TRACE_SZ    15    /* No more than 256 (trace_index) */
+#define SCMD_TRACE_SZ    63            /* Less than 4KB of 61-byte lines */
 
 struct ub_scsi_cmd_trace {
        int hcur;
@@ -265,6 +264,7 @@ struct ub_dev {
        int changed;                    /* Media was changed */
        int removable;
        int readonly;
+       int first_open;                 /* Kludge. See ub_bd_open. */
        char name[8];
        struct usb_device *dev;
        struct usb_interface *intf;
@@ -300,6 +300,11 @@ struct ub_dev {
 
 /*
  */
+static int ub_bd_rq_fn_1(struct ub_dev *sc, struct request *rq);
+static int ub_cmd_build_block(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
+    struct request *rq);
+static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
+    struct request *rq);
 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
 static void ub_end_rq(struct request *rq, int uptodate);
 static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
@@ -308,6 +313,7 @@ static void ub_scsi_action(unsigned long _dev);
 static void ub_scsi_dispatch(struct ub_dev *sc);
 static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
 static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
+static void __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
 static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
 static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
 static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
@@ -335,7 +341,7 @@ MODULE_DEVICE_TABLE(usb, ub_usb_ids);
  */
 #define UB_MAX_HOSTS  26
 static char ub_hostv[UB_MAX_HOSTS];
-static spinlock_t ub_lock = SPIN_LOCK_UNLOCKED;        /* Locks globals and ->openc */
+static DEFINE_SPINLOCK(ub_lock);       /* Locks globals and ->openc */
 
 /*
  * The SCSI command tracing procedures.
@@ -490,6 +496,18 @@ static void ub_id_put(int id)
  */
 static void ub_cleanup(struct ub_dev *sc)
 {
+
+       /*
+        * If we zero disk->private_data BEFORE put_disk, we have to check
+        * for NULL all over the place in open, release, check_media and
+        * revalidate, because the block level semaphore is well inside the
+        * put_disk. But we cannot zero after the call, because *disk is gone.
+        * The sd.c is blatantly racy in this area.
+        */
+       /* disk->private_data = NULL; */
+       put_disk(sc->disk);
+       sc->disk = NULL;
+
        ub_id_put(sc->id);
        kfree(sc);
 }
@@ -578,40 +596,73 @@ static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
  * The request function is our main entry point
  */
 
-static inline int ub_bd_rq_fn_1(request_queue_t *q)
+static void ub_bd_rq_fn(request_queue_t *q)
 {
-#if 0
-       int writing = 0, pci_dir, i, n_elem;
-       u32 tmp;
-       unsigned int msg_size;
-#endif
        struct ub_dev *sc = q->queuedata;
        struct request *rq;
-#if 0 /* We use rq->buffer for now */
-       struct scatterlist *sg;
-       int n_elem;
-#endif
+
+       while ((rq = elv_next_request(q)) != NULL) {
+               if (ub_bd_rq_fn_1(sc, rq) != 0) {
+                       blk_stop_queue(q);
+                       break;
+               }
+       }
+}
+
+static int ub_bd_rq_fn_1(struct ub_dev *sc, struct request *rq)
+{
        struct ub_scsi_cmd *cmd;
-       int ub_dir;
-       unsigned int block, nblks;
        int rc;
 
-       if ((rq = elv_next_request(q)) == NULL)
-               return 1;
-
        if (atomic_read(&sc->poison) || sc->changed) {
                blkdev_dequeue_request(rq);
                ub_end_rq(rq, 0);
                return 0;
        }
 
-       if ((cmd = ub_get_cmd(sc)) == NULL) {
-               blk_stop_queue(q);
-               return 1;
-       }
+       if ((cmd = ub_get_cmd(sc)) == NULL)
+               return -1;
+       memset(cmd, 0, sizeof(struct ub_scsi_cmd));
 
        blkdev_dequeue_request(rq);
 
+       if (blk_pc_request(rq)) {
+               rc = ub_cmd_build_packet(sc, cmd, rq);
+       } else {
+               rc = ub_cmd_build_block(sc, cmd, rq);
+       }
+       if (rc != 0) {
+               ub_put_cmd(sc, cmd);
+               ub_end_rq(rq, 0);
+               blk_start_queue(sc->disk->queue);
+               return 0;
+       }
+
+       cmd->state = UB_CMDST_INIT;
+       cmd->done = ub_rw_cmd_done;
+       cmd->back = rq;
+
+       cmd->tag = sc->tagcnt++;
+       if ((rc = ub_submit_scsi(sc, cmd)) != 0) {
+               ub_put_cmd(sc, cmd);
+               ub_end_rq(rq, 0);
+               blk_start_queue(sc->disk->queue);
+               return 0;
+       }
+
+       return 0;
+}
+
+static int ub_cmd_build_block(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
+    struct request *rq)
+{
+       int ub_dir;
+#if 0 /* We use rq->buffer for now */
+       struct scatterlist *sg;
+       int n_elem;
+#endif
+       unsigned int block, nblks;
+
        if (rq_data_dir(rq) == WRITE)
                ub_dir = UB_DIR_WRITE;
        else
@@ -639,6 +690,7 @@ static inline int ub_bd_rq_fn_1(request_queue_t *q)
                return 0;
        }
 #endif
+
        /*
         * XXX Unfortunately, this check does not work. It is quite possible
         * to get bogus non-null rq->buffer if you allow sg by mistake.
@@ -650,22 +702,23 @@ static inline int ub_bd_rq_fn_1(request_queue_t *q)
                 */
                static int do_print = 1;
                if (do_print) {
-                       printk(KERN_WARNING "%s: unmapped request\n", sc->name);
+                       printk(KERN_WARNING "%s: unmapped block request"
+                           " flags 0x%lx sectors %lu\n",
+                           sc->name, rq->flags, rq->nr_sectors);
                        do_print = 0;
                }
-               ub_put_cmd(sc, cmd);
-               ub_end_rq(rq, 0);
-               blk_start_queue(q);
-               return 0;
+               return -1;
        }
 
        /*
         * build the command
+        *
+        * The call to blk_queue_hardsect_size() guarantees that request
+        * is aligned, but it is given in terms of 512 byte units, always.
         */
-       block = rq->sector;
-       nblks = rq->nr_sectors;
+       block = rq->sector >> sc->capacity.bshift;
+       nblks = rq->nr_sectors >> sc->capacity.bshift;
 
-       memset(cmd, 0, sizeof(struct ub_scsi_cmd));
        cmd->cdb[0] = (ub_dir == UB_DIR_READ)? READ_10: WRITE_10;
        /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
        cmd->cdb[2] = block >> 24;
@@ -675,27 +728,44 @@ static inline int ub_bd_rq_fn_1(request_queue_t *q)
        cmd->cdb[7] = nblks >> 8;
        cmd->cdb[8] = nblks;
        cmd->cdb_len = 10;
+
        cmd->dir = ub_dir;
-       cmd->state = UB_CMDST_INIT;
        cmd->data = rq->buffer;
-       cmd->len = nblks * 512;
-       cmd->done = ub_rw_cmd_done;
-       cmd->back = rq;
-
-       cmd->tag = sc->tagcnt++;
-       if ((rc = ub_submit_scsi(sc, cmd)) != 0) {
-               ub_put_cmd(sc, cmd);
-               ub_end_rq(rq, 0);
-               blk_start_queue(q);
-               return 0;
-       }
+       cmd->len = rq->nr_sectors * 512;
 
        return 0;
 }
 
-static void ub_bd_rq_fn(request_queue_t *q)
+static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
+    struct request *rq)
 {
-       do { } while (ub_bd_rq_fn_1(q) == 0);
+
+       if (rq->data_len != 0 && rq->data == NULL) {
+               static int do_print = 1;
+               if (do_print) {
+                       printk(KERN_WARNING "%s: unmapped packet request"
+                           " flags 0x%lx length %d\n",
+                           sc->name, rq->flags, rq->data_len);
+                       do_print = 0;
+               }
+               return -1;
+       }
+
+       memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
+       cmd->cdb_len = rq->cmd_len;
+
+       if (rq->data_len == 0) {
+               cmd->dir = UB_DIR_NONE;
+       } else {
+               if (rq_data_dir(rq) == WRITE)
+                       cmd->dir = UB_DIR_WRITE;
+               else
+                       cmd->dir = UB_DIR_READ;
+       }
+       cmd->data = rq->data;
+       cmd->len = rq->data_len;
+
+       return 0;
 }
 
 static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
@@ -705,6 +775,12 @@ static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        request_queue_t *q = disk->queue;
        int uptodate;
 
+       if (blk_pc_request(rq)) {
+               /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
+               memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
+               rq->sense_len = UB_SENSE_SIZE;
+       }
+
        if (cmd->error == 0)
                uptodate = 1;
        else
@@ -763,6 +839,17 @@ static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
 
        bcb = &sc->work_bcb;
 
+       /*
+        * ``If the allocation length is eighteen or greater, and a device
+        * server returns less than eithteen bytes of data, the application
+        * client should assume that the bytes not transferred would have been
+        * zeroes had the device server returned those bytes.''
+        *
+        * We zero sense for all commands so that when a packet request
+        * fails it does not return a stale sense.
+        */
+       memset(&sc->top_sense, 0, UB_SENSE_SIZE);
+
        /* set up the command wrapper */
        bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
        bcb->Tag = cmd->tag;            /* Endianness is not important */
@@ -786,17 +873,16 @@ static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        sc->work_urb.error_count = 0;
        sc->work_urb.status = 0;
 
-       sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
-       add_timer(&sc->work_timer);
-
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                /* XXX Clear stalls */
                printk("ub: cmd #%d start failed (%d)\n", cmd->tag, rc); /* P3 */
-               del_timer(&sc->work_timer);
                ub_complete(&sc->work_done);
                return rc;
        }
 
+       sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
+       add_timer(&sc->work_timer);
+
        cmd->state = UB_CMDST_CMD;
        ub_cmdtr_state(sc, cmd);
        return 0;
@@ -836,6 +922,7 @@ static void ub_scsi_action(unsigned long _dev)
        unsigned long flags;
 
        spin_lock_irqsave(&sc->lock, flags);
+       del_timer(&sc->work_timer);
        ub_scsi_dispatch(sc);
        spin_unlock_irqrestore(&sc->lock, flags);
 }
@@ -871,9 +958,6 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        int pipe;
        int rc;
 
-/* P3 */ /** printk("ub: urb status %d pipe 0x%08x len %d act %d\n",
- urb->status, urb->pipe, urb->transfer_buffer_length, urb->actual_length); **/
-
        if (atomic_read(&sc->poison)) {
                /* A little too simplistic, I feel... */
                goto Bad_End;
@@ -883,7 +967,7 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                if (urb->status == -EPIPE) {
                        /*
                         * STALL while clearning STALL.
-                        * A STALL is illegal on a control pipe!
+                        * The control pipe clears itself - nothing to do.
                         * XXX Might try to reset the device here and retry.
                         */
                        printk(KERN_NOTICE "%s: "
@@ -906,7 +990,7 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                if (urb->status == -EPIPE) {
                        /*
                         * STALL while clearning STALL.
-                        * A STALL is illegal on a control pipe!
+                        * The control pipe clears itself - nothing to do.
                         * XXX Might try to reset the device here and retry.
                         */
                        printk(KERN_NOTICE "%s: "
@@ -930,7 +1014,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                        rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
                        if (rc != 0) {
                                printk(KERN_NOTICE "%s: "
-                                   "unable to submit clear for device %u (%d)\n",
+                                   "unable to submit clear for device %u"
+                                   " (code %d)\n",
                                    sc->name, sc->dev->devnum, rc);
                                /*
                                 * This is typically ENOMEM or some other such shit.
@@ -942,9 +1027,12 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                        ub_cmdtr_state(sc, cmd);
                        return;
                }
-               if (urb->status != 0)
+               if (urb->status != 0) {
+                       printk("ub: cmd #%d cmd status (%d)\n", cmd->tag, urb->status); /* P3 */
                        goto Bad_End;
+               }
                if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
+                       printk("ub: cmd #%d xferred %d\n", cmd->tag, urb->actual_length); /* P3 */
                        /* XXX Must do reset here to unconfuse the device */
                        goto Bad_End;
                }
@@ -968,18 +1056,17 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                sc->work_urb.error_count = 0;
                sc->work_urb.status = 0;
 
-               sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
-               add_timer(&sc->work_timer);
-
                if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                        /* XXX Clear stalls */
                        printk("ub: data #%d submit failed (%d)\n", cmd->tag, rc); /* P3 */
-                       del_timer(&sc->work_timer);
                        ub_complete(&sc->work_done);
                        ub_state_done(sc, cmd, rc);
                        return;
                }
 
+               sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
+               add_timer(&sc->work_timer);
+
                cmd->state = UB_CMDST_DATA;
                ub_cmdtr_state(sc, cmd);
 
@@ -988,7 +1075,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                        rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
                        if (rc != 0) {
                                printk(KERN_NOTICE "%s: "
-                                   "unable to submit clear for device %u (%d)\n",
+                                   "unable to submit clear for device %u"
+                                   " (code %d)\n",
                                    sc->name, sc->dev->devnum, rc);
                                /*
                                 * This is typically ENOMEM or some other such shit.
@@ -1020,7 +1108,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                        rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
                        if (rc != 0) {
                                printk(KERN_NOTICE "%s: "
-                                   "unable to submit clear for device %u (%d)\n",
+                                   "unable to submit clear for device %u"
+                                   " (code %d)\n",
                                    sc->name, sc->dev->devnum, rc);
                                /*
                                 * This is typically ENOMEM or some other such shit.
@@ -1048,34 +1137,7 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                                    sc->name, sc->dev->devnum);
                                goto Bad_End;
                        }
-
-                       /*
-                        * ub_state_stat only not dropping the count...
-                        */
-                       UB_INIT_COMPLETION(sc->work_done);
-
-                       sc->last_pipe = sc->recv_bulk_pipe;
-                       usb_fill_bulk_urb(&sc->work_urb, sc->dev,
-                           sc->recv_bulk_pipe, &sc->work_bcs,
-                           US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
-                       sc->work_urb.transfer_flags = URB_ASYNC_UNLINK;
-                       sc->work_urb.actual_length = 0;
-                       sc->work_urb.error_count = 0;
-                       sc->work_urb.status = 0;
-
-                       sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
-                       add_timer(&sc->work_timer);
-
-                       rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC);
-                       if (rc != 0) {
-                               /* XXX Clear stalls */
-                               printk("%s: CSW #%d submit failed (%d)\n",
-                                  sc->name, cmd->tag, rc); /* P3 */
-                               del_timer(&sc->work_timer);
-                               ub_complete(&sc->work_done);
-                               ub_state_done(sc, cmd, rc);
-                               return;
-                       }
+                       __ub_state_stat(sc, cmd);
                        return;
                }
 
@@ -1096,17 +1158,31 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                        goto Bad_End;
                }
 
+#if 0
                if (bcs->Signature != cpu_to_le32(US_BULK_CS_SIGN) &&
                    bcs->Signature != cpu_to_le32(US_BULK_CS_OLYMPUS_SIGN)) {
-                       /* XXX Rate-limit, even for P3 tagged */
-                       /* P3 */ printk("ub: signature 0x%x\n", bcs->Signature);
                        /* Windows ignores signatures, so do we. */
                }
+#endif
 
                if (bcs->Tag != cmd->tag) {
-                       /* P3 */ printk("%s: tag orig 0x%x reply 0x%x\n",
-                           sc->name, cmd->tag, bcs->Tag);
-                       goto Bad_End;
+                       /*
+                        * This usually happens when we disagree with the
+                        * device's microcode about something. For instance,
+                        * a few of them throw this after timeouts. They buffer
+                        * commands and reply at commands we timed out before.
+                        * Without flushing these replies we loop forever.
+                        */
+                       if (++cmd->stat_count >= 4) {
+                               printk(KERN_NOTICE "%s: "
+                                   "tag mismatch orig 0x%x reply 0x%x "
+                                   "on device %u\n",
+                                   sc->name, cmd->tag, bcs->Tag,
+                                   sc->dev->devnum);
+                               goto Bad_End;
+                       }
+                       __ub_state_stat(sc, cmd);
+                       return;
                }
 
                switch (bcs->Status) {
@@ -1132,16 +1208,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                (*cmd->done)(sc, cmd);
 
        } else if (cmd->state == UB_CMDST_SENSE) {
-               /* 
-                * We do not look at sense, because even if there was no sense,
-                * we get into UB_CMDST_SENSE from a STALL or CSW FAIL only.
-                * We request sense because we want to clear CHECK CONDITION
-                * on devices with delusions of SCSI, and not because we
-                * are curious in any way about the sense itself.
-                */
-               /* if ((cmd->top_sense[2] & 0x0F) == NO_SENSE) { foo } */
-
                ub_state_done(sc, cmd, -EIO);
+
        } else {
                printk(KERN_WARNING "%s: "
                    "wrong command state %d on device %u\n",
@@ -1170,9 +1238,9 @@ static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
 
 /*
  * Factorization helper for the command state machine:
- * Submit a CSW read and go to STAT state.
+ * Submit a CSW read.
  */
-static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
+static void __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
 {
        int rc;
 
@@ -1186,18 +1254,26 @@ static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
        sc->work_urb.error_count = 0;
        sc->work_urb.status = 0;
 
-       sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
-       add_timer(&sc->work_timer);
-
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
                /* XXX Clear stalls */
-               printk("ub: CSW #%d submit failed (%d)\n", cmd->tag, rc); /* P3 */
-               del_timer(&sc->work_timer);
+               printk("%s: CSW #%d submit failed (%d)\n", sc->name, cmd->tag, rc); /* P3 */
                ub_complete(&sc->work_done);
                ub_state_done(sc, cmd, rc);
                return;
        }
 
+       sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
+       add_timer(&sc->work_timer);
+}
+
+/*
+ * Factorization helper for the command state machine:
+ * Submit a CSW read and go to STAT state.
+ */
+static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
+{
+       __ub_state_stat(sc, cmd);
+
        cmd->stat_count = 0;
        cmd->state = UB_CMDST_STAT;
        ub_cmdtr_state(sc, cmd);
@@ -1217,9 +1293,9 @@ static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
                goto error;
        }
 
-       memset(&sc->top_sense, 0, UB_SENSE_SIZE);
        scmd = &sc->top_rqs_cmd;
        scmd->cdb[0] = REQUEST_SENSE;
+       scmd->cdb[4] = UB_SENSE_SIZE;
        scmd->cdb_len = 6;
        scmd->dir = UB_DIR_READ;
        scmd->state = UB_CMDST_INIT;
@@ -1271,14 +1347,13 @@ static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
        sc->work_urb.error_count = 0;
        sc->work_urb.status = 0;
 
-       sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
-       add_timer(&sc->work_timer);
-
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
-               del_timer(&sc->work_timer);
                ub_complete(&sc->work_done);
                return rc;
        }
+
+       sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
+       add_timer(&sc->work_timer);
        return 0;
 }
 
@@ -1289,8 +1364,15 @@ static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
        unsigned char *sense = scmd->data;
        struct ub_scsi_cmd *cmd;
 
+       /*
+        * Ignoring scmd->act_len, because the buffer was pre-zeroed.
+        */
        ub_cmdtr_sense(sc, scmd, sense);
 
+       /*
+        * Find the command which triggered the unit attention or a check,
+        * save the sense into it, and advance its state machine.
+        */
        if ((cmd = ub_cmdq_peek(sc)) == NULL) {
                printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
                return;
@@ -1308,6 +1390,10 @@ static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
                return;
        }
 
+       cmd->key = sense[2] & 0x0F;
+       cmd->asc = sense[12];
+       cmd->ascq = sense[13];
+
        ub_scsi_urb_compl(sc, cmd);
 }
 
@@ -1355,12 +1441,7 @@ static void ub_revalidate(struct ub_dev *sc)
 
        sc->readonly = 0;       /* XXX Query this from the device */
 
-       /*
-        * XXX sd.c sets capacity to zero in such case. However, it doesn't
-        * work for us. In case of zero capacity, block layer refuses to
-        * have the /dev/uba opened (why?) Set capacity to some random value.
-        */
-       sc->capacity.nsec = 50;
+       sc->capacity.nsec = 0;
        sc->capacity.bsize = 512;
        sc->capacity.bshift = 0;
 
@@ -1375,7 +1456,7 @@ static void ub_revalidate(struct ub_dev *sc)
                 * We keep this because sd.c has retries for capacity.
                 */
                if (ub_sync_read_cap(sc, &sc->capacity) != 0) {
-                       sc->capacity.nsec = 100;
+                       sc->capacity.nsec = 0;
                        sc->capacity.bsize = 512;
                        sc->capacity.bshift = 0;
                }
@@ -1404,10 +1485,38 @@ static int ub_bd_open(struct inode *inode, struct file *filp)
        sc->openc++;
        spin_unlock_irqrestore(&ub_lock, flags);
 
+       /*
+        * This is a workaround for a specific problem in our block layer.
+        * In 2.6.9, register_disk duplicates the code from rescan_partitions.
+        * However, if we do add_disk with a device which persistently reports
+        * a changed media, add_disk calls register_disk, which does do_open,
+        * which will call rescan_paritions for changed media. After that,
+        * register_disk attempts to do it all again and causes double kobject
+        * registration and a eventually an oops on module removal.
+        *
+        * The bottom line is, Al Viro says that we should not allow
+        * bdev->bd_invalidated to be set when doing add_disk no matter what.
+        */
+       if (sc->first_open) {
+               if (sc->changed) {
+                       sc->first_open = 0;
+                       rc = -ENOMEDIUM;
+                       goto err_open;
+               }
+       }
+
        if (sc->removable || sc->readonly)
                check_disk_change(inode->i_bdev);
 
-       /* XXX sd.c and floppy.c bail on open if media is not present. */
+       /*
+        * The sd.c considers ->media_present and ->changed not equivalent,
+        * under some pretty murky conditions (a failure of READ CAPACITY).
+        * We may need it one day.
+        */
+       if (sc->removable && sc->changed && !(filp->f_flags & O_NDELAY)) {
+               rc = -ENOMEDIUM;
+               goto err_open;
+       }
 
        if (sc->readonly && (filp->f_mode & FMODE_WRITE)) {
                rc = -EROFS;
@@ -1435,6 +1544,8 @@ static int ub_bd_release(struct inode *inode, struct file *filp)
 
        spin_lock_irqsave(&ub_lock, flags);
        --sc->openc;
+       if (sc->openc == 0)
+               sc->first_open = 0;
        if (sc->openc == 0 && atomic_read(&sc->poison))
                ub_cleanup(sc);
        spin_unlock_irqrestore(&ub_lock, flags);
@@ -1447,30 +1558,10 @@ static int ub_bd_release(struct inode *inode, struct file *filp)
 static int ub_bd_ioctl(struct inode *inode, struct file *filp,
     unsigned int cmd, unsigned long arg)
 {
-// void __user *usermem = (void *) arg;
-// struct carm_port *port = ino->i_bdev->bd_disk->private_data;
-// struct hd_geometry geom;
-
-#if 0
-       switch (cmd) {
-       case HDIO_GETGEO:
-               if (usermem == NULL)            // XXX Bizzare. Why?
-                       return -EINVAL;
-
-               geom.heads = (u8) port->dev_geom_head;
-               geom.sectors = (u8) port->dev_geom_sect;
-               geom.cylinders = port->dev_geom_cyl;
-               geom.start = get_start_sect(ino->i_bdev);
-
-               if (copy_to_user(usermem, &geom, sizeof(geom)))
-                       return -EFAULT;
-               return 0;
-
-       default: ;
-       }
-#endif
+       struct gendisk *disk = inode->i_bdev->bd_disk;
+       void __user *usermem = (void __user *) arg;
 
-       return -ENOTTY;
+       return scsi_cmd_ioctl(filp, disk, cmd, usermem);
 }
 
 /*
@@ -1489,11 +1580,17 @@ static int ub_bd_revalidate(struct gendisk *disk)
 
        ub_revalidate(sc);
        /* This is pretty much a long term P3 */
-       printk(KERN_INFO "%s: device %u capacity nsec %ld bsize %u\n",
-           sc->name, sc->dev->devnum, sc->capacity.nsec, sc->capacity.bsize);
+       if (!atomic_read(&sc->poison)) {                /* Cover sc->dev */
+               printk(KERN_INFO "%s: device %u capacity nsec %ld bsize %u\n",
+                   sc->name, sc->dev->devnum,
+                   sc->capacity.nsec, sc->capacity.bsize);
+       }
 
+       /* XXX Support sector size switching like in sr.c */
+       blk_queue_hardsect_size(disk->queue, sc->capacity.bsize);
        set_capacity(disk, sc->capacity.nsec);
        // set_disk_ro(sdkp->disk, sc->readonly);
+
        return 0;
 }
 
@@ -1524,13 +1621,9 @@ static int ub_bd_media_changed(struct gendisk *disk)
         */
        if (ub_sync_tur(sc) != 0) {
                sc->changed = 1;
-               /* P3 */ printk("%s: made changed\n", sc->name);
                return 1;
        }
 
-       /* The sd.c clears this before returning (one-shot flag). Why? */
-       /* P3 */ printk("%s: %s changed\n", sc->name,
-           sc->changed? "is": "was not");
        return sc->changed;
 }
 
@@ -1592,6 +1685,9 @@ static int ub_sync_tur(struct ub_dev *sc)
 
        rc = cmd->error;
 
+       if (rc == -EIO && cmd->key != 0)        /* Retries for benh's key */
+               rc = cmd->key;
+
 err_submit:
        kfree(cmd);
 err_alloc:
@@ -1654,8 +1750,8 @@ static int ub_sync_read_cap(struct ub_dev *sc, struct ub_capacity *ret)
        }
 
        /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
-       nsec = be32_to_cpu(*(u32 *)p) + 1;
-       bsize = be32_to_cpu(*(u32 *)(p + 4));
+       nsec = be32_to_cpu(*(__be32 *)p) + 1;
+       bsize = be32_to_cpu(*(__be32 *)(p + 4));
        switch (bsize) {
        case 512:       shift = 0;      break;
        case 1024:      shift = 1;      break;
@@ -1725,28 +1821,22 @@ static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
        sc->work_urb.error_count = 0;
        sc->work_urb.status = 0;
 
-       init_timer(&timer);
-       timer.function = ub_probe_timeout;
-       timer.data = (unsigned long) &compl;
-       timer.expires = jiffies + UB_CTRL_TIMEOUT;
-       add_timer(&timer);
-
        if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
                printk(KERN_WARNING
                     "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
-               del_timer_sync(&timer);
                return rc;
        }
 
+       init_timer(&timer);
+       timer.function = ub_probe_timeout;
+       timer.data = (unsigned long) &compl;
+       timer.expires = jiffies + UB_CTRL_TIMEOUT;
+       add_timer(&timer);
+
        wait_for_completion(&compl);
 
        del_timer_sync(&timer);
-       /*
-        * Most of the time, URB was done and dev set to NULL, and so
-        * the unlink bounces out with ENODEV. We do not call usb_kill_urb
-        * because we still think about a backport to 2.4.
-        */
-       usb_unlink_urb(&sc->work_urb);
+       usb_kill_urb(&sc->work_urb);
 
        /* reset the endpoint toggle */
        usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
@@ -1813,6 +1903,7 @@ static int ub_probe(struct usb_interface *intf,
        request_queue_t *q;
        struct gendisk *disk;
        int rc;
+       int i;
 
        rc = -ENOMEM;
        if ((sc = kmalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
@@ -1879,9 +1970,15 @@ static int ub_probe(struct usb_interface *intf,
         * has to succeed, so we clear checks with an additional one here.
         * In any case it's not our business how revaliadation is implemented.
         */
-       ub_sync_tur(sc);
+       for (i = 0; i < 3; i++) {       /* Retries for benh's key */
+               if ((rc = ub_sync_tur(sc)) <= 0) break;
+               if (rc != 0x6) break;
+               msleep(10);
+       }
 
        sc->removable = 1;              /* XXX Query this from the device */
+       sc->changed = 1;                /* ub_revalidate clears only */
+       sc->first_open = 1;
 
        ub_revalidate(sc);
        /* This is pretty much a long term P3 */
@@ -1915,7 +2012,7 @@ static int ub_probe(struct usb_interface *intf,
        blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
        // blk_queue_segment_boundary(q, CARM_SG_BOUNDARY);
        blk_queue_max_sectors(q, UB_MAX_SECTORS);
-       // blk_queue_hardsect_size(q, xxxxx);
+       blk_queue_hardsect_size(q, sc->capacity.bsize);
 
        /*
         * This is a serious infraction, caused by a deficiency in the
@@ -2005,17 +2102,6 @@ static void ub_disconnect(struct usb_interface *intf)
        if (q)
                blk_cleanup_queue(q);
 
-       /*
-        * If we zero disk->private_data BEFORE put_disk, we have to check
-        * for NULL all over the place in open, release, check_media and
-        * revalidate, because the block level semaphore is well inside the
-        * put_disk. But we cannot zero after the call, because *disk is gone.
-        * The sd.c is blatantly racy in this area.
-        */
-       /* disk->private_data = NULL; */
-       put_disk(disk);
-       sc->disk = NULL;
-
        /*
         * We really expect blk_cleanup_queue() to wait, so no amount
         * of paranoya is too much.
@@ -2034,6 +2120,13 @@ static void ub_disconnect(struct usb_interface *intf)
        }
        spin_unlock_irqrestore(&sc->lock, flags);
 
+       /*
+        * There is virtually no chance that other CPU runs times so long
+        * after ub_urb_complete should have called del_timer, but only if HCD
+        * didn't forget to deliver a callback on unlink.
+        */
+       del_timer_sync(&sc->work_timer);
+
        /*
         * At this point there must be no commands coming from anyone
         * and no URBs left in transit.