Merge branch 'fixes' into misc
[sfrench/cifs-2.6.git] / drivers / scsi / scsi_lib.c
index c9844043504e18de606b07e87e0fc7ccabf65920..1d83f29aee740a4c971017d388e3233bdd2112f5 100644 (file)
@@ -79,14 +79,15 @@ int scsi_init_sense_cache(struct Scsi_Host *shost)
        if (shost->unchecked_isa_dma) {
                scsi_sense_isadma_cache =
                        kmem_cache_create("scsi_sense_cache(DMA)",
-                       SCSI_SENSE_BUFFERSIZE, 0,
-                       SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
+                               SCSI_SENSE_BUFFERSIZE, 0,
+                               SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA, NULL);
                if (!scsi_sense_isadma_cache)
                        ret = -ENOMEM;
        } else {
                scsi_sense_cache =
-                       kmem_cache_create("scsi_sense_cache",
-                       SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN, NULL);
+                       kmem_cache_create_usercopy("scsi_sense_cache",
+                               SCSI_SENSE_BUFFERSIZE, 0, SLAB_HWCACHE_ALIGN,
+                               0, SCSI_SENSE_BUFFERSIZE, NULL);
                if (!scsi_sense_cache)
                        ret = -ENOMEM;
        }
@@ -190,7 +191,19 @@ static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, bool unbusy)
         */
        cmd->result = 0;
        if (q->mq_ops) {
-               scsi_mq_requeue_cmd(cmd);
+               /*
+                * Before a SCSI command is dispatched,
+                * get_device(&sdev->sdev_gendev) is called and the host,
+                * target and device busy counters are increased. Since
+                * requeuing a request causes these actions to be repeated and
+                * since scsi_device_unbusy() has already been called,
+                * put_device(&device->sdev_gendev) must still be called. Call
+                * put_device() after blk_mq_requeue_request() to avoid that
+                * removal of the SCSI device can start before requeueing has
+                * happened.
+                */
+               blk_mq_requeue_request(cmd->request, true);
+               put_device(&device->sdev_gendev);
                return;
        }
        spin_lock_irqsave(q->queue_lock, flags);
@@ -857,6 +870,17 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
                /* for passthrough error may be set */
                error = BLK_STS_OK;
        }
+       /*
+        * Another corner case: the SCSI status byte is non-zero but 'good'.
+        * Example: PRE-FETCH command returns SAM_STAT_CONDITION_MET when
+        * it is able to fit nominated LBs in its cache (and SAM_STAT_GOOD
+        * if it can't fit). Treat SAM_STAT_CONDITION_MET and the related
+        * intermediate statuses (both obsolete in SAM-4) as good.
+        */
+       if (status_byte(result) && scsi_status_is_good(result)) {
+               result = 0;
+               error = BLK_STS_OK;
+       }
 
        /*
         * special case: failed zero length commands always need to
@@ -1987,6 +2011,8 @@ static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx)
 out_put_device:
        put_device(&sdev->sdev_gendev);
 out:
+       if (atomic_read(&sdev->device_busy) == 0 && !scsi_device_blocked(sdev))
+               blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
        return false;
 }
 
@@ -2048,9 +2074,9 @@ out_put_budget:
        case BLK_STS_OK:
                break;
        case BLK_STS_RESOURCE:
-               if (atomic_read(&sdev->device_busy) == 0 &&
-                   !scsi_device_blocked(sdev))
-                       blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY);
+               if (atomic_read(&sdev->device_busy) ||
+                   scsi_device_blocked(sdev))
+                       ret = BLK_STS_DEV_RESOURCE;
                break;
        default:
                /*
@@ -2168,11 +2194,13 @@ void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
                q->limits.cluster = 0;
 
        /*
-        * set a reasonable default alignment on word boundaries: the
-        * host and device may alter it using
-        * blk_queue_update_dma_alignment() later.
+        * Set a reasonable default alignment:  The larger of 32-byte (dword),
+        * which is a common minimum for HBAs, and the minimum DMA alignment,
+        * which is set by the platform.
+        *
+        * Devices that require a bigger alignment can increase it later.
         */
-       blk_queue_dma_alignment(q, 0x03);
+       blk_queue_dma_alignment(q, max(4, dma_get_cache_alignment()) - 1);
 }
 EXPORT_SYMBOL_GPL(__scsi_init_queue);
 
@@ -2606,7 +2634,7 @@ scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
        /* try to eat the UNIT_ATTENTION if there are enough retries */
        do {
                result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
-                                         timeout, retries, NULL);
+                                         timeout, 1, NULL);
                if (sdev->removable && scsi_sense_valid(sshdr) &&
                    sshdr->sense_key == UNIT_ATTENTION)
                        sdev->changed = 1;