Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-microblaze.git] / drivers / scsi / scsi_lib.c
index 41e9ac9..0adfb3b 100644 (file)
@@ -238,7 +238,7 @@ void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
 
 
 /**
- * scsi_execute - insert request and wait for the result
+ * __scsi_execute - insert request and wait for the result
  * @sdev:      scsi device
  * @cmd:       scsi command
  * @data_direction: data direction
@@ -255,7 +255,7 @@ void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  * Returns the scsi_cmnd result field if a command was executed, or a negative
  * Linux error code if we didn't get that far.
  */
-int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
+int __scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
                 int data_direction, void *buffer, unsigned bufflen,
                 unsigned char *sense, struct scsi_sense_hdr *sshdr,
                 int timeout, int retries, u64 flags, req_flags_t rq_flags,
@@ -309,7 +309,7 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
 
        return ret;
 }
-EXPORT_SYMBOL(scsi_execute);
+EXPORT_SYMBOL(__scsi_execute);
 
 /*
  * Function:    scsi_init_cmd_errh()
@@ -345,7 +345,8 @@ static void scsi_dec_host_busy(struct Scsi_Host *shost)
        unsigned long flags;
 
        rcu_read_lock();
-       atomic_dec(&shost->host_busy);
+       if (!shost->use_blk_mq)
+               atomic_dec(&shost->host_busy);
        if (unlikely(scsi_host_in_recovery(shost))) {
                spin_lock_irqsave(shost->host_lock, flags);
                if (shost->host_failed || shost->host_eh_scheduled)
@@ -371,7 +372,7 @@ void scsi_device_unbusy(struct scsi_device *sdev)
 static void scsi_kick_queue(struct request_queue *q)
 {
        if (q->mq_ops)
-               blk_mq_start_hw_queues(q);
+               blk_mq_run_hw_queues(q, false);
        else
                blk_run_queue(q);
 }
@@ -444,7 +445,12 @@ static inline bool scsi_target_is_busy(struct scsi_target *starget)
 
 static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
 {
-       if (shost->can_queue > 0 &&
+       /*
+        * blk-mq can handle host queue busy efficiently via host-wide driver
+        * tag allocation
+        */
+
+       if (!shost->use_blk_mq && shost->can_queue > 0 &&
            atomic_read(&shost->host_busy) >= shost->can_queue)
                return true;
        if (atomic_read(&shost->host_blocked) > 0)
@@ -662,6 +668,7 @@ static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
        cmd->request->next_rq->special = NULL;
 }
 
+/* Returns false when no more bytes to process, true if there are more */
 static bool scsi_end_request(struct request *req, blk_status_t error,
                unsigned int bytes, unsigned int bidi_bytes)
 {
@@ -760,161 +767,39 @@ static blk_status_t scsi_result_to_blk_status(struct scsi_cmnd *cmd, int result)
        }
 }
 
-/*
- * Function:    scsi_io_completion()
- *
- * Purpose:     Completion processing for block device I/O requests.
- *
- * Arguments:   cmd   - command that is finished.
- *
- * Lock status: Assumed that no lock is held upon entry.
- *
- * Returns:     Nothing
- *
- * Notes:       We will finish off the specified number of sectors.  If we
- *             are done, the command block will be released and the queue
- *             function will be goosed.  If we are not done then we have to
- *             figure out what to do next:
- *
- *             a) We can call scsi_requeue_command().  The request
- *                will be unprepared and put back on the queue.  Then
- *                a new command will be created for it.  This should
- *                be used if we made forward progress, or if we want
- *                to switch from READ(10) to READ(6) for example.
- *
- *             b) We can call __scsi_queue_insert().  The request will
- *                be put back on the queue and retried using the same
- *                command as before, possibly after a delay.
- *
- *             c) We can call scsi_end_request() with -EIO to fail
- *                the remainder of the request.
- */
-void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
+/* Helper for scsi_io_completion() when "reprep" action required. */
+static void scsi_io_completion_reprep(struct scsi_cmnd *cmd,
+                                     struct request_queue *q)
+{
+       /* A new command will be prepared and issued. */
+       if (q->mq_ops) {
+               scsi_mq_requeue_cmd(cmd);
+       } else {
+               /* Unprep request and put it back at head of the queue. */
+               scsi_release_buffers(cmd);
+               scsi_requeue_command(q, cmd);
+       }
+}
+
+/* Helper for scsi_io_completion() when special action required. */
+static void scsi_io_completion_action(struct scsi_cmnd *cmd, int result)
 {
-       int result = cmd->result;
        struct request_queue *q = cmd->device->request_queue;
        struct request *req = cmd->request;
-       blk_status_t error = BLK_STS_OK;
-       struct scsi_sense_hdr sshdr;
-       bool sense_valid = false;
-       int sense_deferred = 0, level = 0;
+       int level = 0;
        enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
              ACTION_DELAYED_RETRY} action;
        unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
+       struct scsi_sense_hdr sshdr;
+       bool sense_valid;
+       bool sense_current = true;      /* false implies "deferred sense" */
+       blk_status_t blk_stat;
 
-       if (result) {
-               sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
-               if (sense_valid)
-                       sense_deferred = scsi_sense_is_deferred(&sshdr);
-       }
-
-       if (blk_rq_is_passthrough(req)) {
-               if (result) {
-                       if (sense_valid) {
-                               /*
-                                * SG_IO wants current and deferred errors
-                                */
-                               scsi_req(req)->sense_len =
-                                       min(8 + cmd->sense_buffer[7],
-                                           SCSI_SENSE_BUFFERSIZE);
-                       }
-                       if (!sense_deferred)
-                               error = scsi_result_to_blk_status(cmd, result);
-               }
-               /*
-                * scsi_result_to_blk_status may have reset the host_byte
-                */
-               scsi_req(req)->result = cmd->result;
-               scsi_req(req)->resid_len = scsi_get_resid(cmd);
-
-               if (scsi_bidi_cmnd(cmd)) {
-                       /*
-                        * Bidi commands Must be complete as a whole,
-                        * both sides at once.
-                        */
-                       scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
-                       if (scsi_end_request(req, BLK_STS_OK, blk_rq_bytes(req),
-                                       blk_rq_bytes(req->next_rq)))
-                               BUG();
-                       return;
-               }
-       } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
-               /*
-                * Flush commands do not transfers any data, and thus cannot use
-                * good_bytes != blk_rq_bytes(req) as the signal for an error.
-                * This sets the error explicitly for the problem case.
-                */
-               error = scsi_result_to_blk_status(cmd, result);
-       }
-
-       /* no bidi support for !blk_rq_is_passthrough yet */
-       BUG_ON(blk_bidi_rq(req));
-
-       /*
-        * Next deal with any sectors which we were able to correctly
-        * handle.
-        */
-       SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
-               "%u sectors total, %d bytes done.\n",
-               blk_rq_sectors(req), good_bytes));
-
-       /*
-        * Recovered errors need reporting, but they're always treated as
-        * success, so fiddle the result code here.  For passthrough requests
-        * we already took a copy of the original into sreq->result which
-        * is what gets returned to the user
-        */
-       if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
-               /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
-                * print since caller wants ATA registers. Only occurs on
-                * SCSI ATA PASS_THROUGH commands when CK_COND=1
-                */
-               if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
-                       ;
-               else if (!(req->rq_flags & RQF_QUIET))
-                       scsi_print_sense(cmd);
-               result = 0;
-               /* 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
-        * drop down into the retry code. Otherwise, if we finished
-        * all bytes in the request we are done now.
-        */
-       if (!(blk_rq_bytes(req) == 0 && error) &&
-           !scsi_end_request(req, error, good_bytes, 0))
-               return;
-
-       /*
-        * Kill remainder if no retrys.
-        */
-       if (error && scsi_noretry_cmd(cmd)) {
-               if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
-                       BUG();
-               return;
-       }
-
-       /*
-        * If there had been no error, but we have leftover bytes in the
-        * requeues just queue the command up again.
-        */
-       if (result == 0)
-               goto requeue;
+       sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
+       if (sense_valid)
+               sense_current = !scsi_sense_is_deferred(&sshdr);
 
-       error = scsi_result_to_blk_status(cmd, result);
+       blk_stat = scsi_result_to_blk_status(cmd, result);
 
        if (host_byte(result) == DID_RESET) {
                /* Third party bus reset or reset for error recovery
@@ -922,7 +807,7 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
                 * happens.
                 */
                action = ACTION_RETRY;
-       } else if (sense_valid && !sense_deferred) {
+       } else if (sense_valid && sense_current) {
                switch (sshdr.sense_key) {
                case UNIT_ATTENTION:
                        if (cmd->device->removable) {
@@ -958,18 +843,18 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
                                action = ACTION_REPREP;
                        } else if (sshdr.asc == 0x10) /* DIX */ {
                                action = ACTION_FAIL;
-                               error = BLK_STS_PROTECTION;
+                               blk_stat = BLK_STS_PROTECTION;
                        /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
                        } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
                                action = ACTION_FAIL;
-                               error = BLK_STS_TARGET;
+                               blk_stat = BLK_STS_TARGET;
                        } else
                                action = ACTION_FAIL;
                        break;
                case ABORTED_COMMAND:
                        action = ACTION_FAIL;
                        if (sshdr.asc == 0x10) /* DIF */
-                               error = BLK_STS_PROTECTION;
+                               blk_stat = BLK_STS_PROTECTION;
                        break;
                case NOT_READY:
                        /* If the device is in the process of becoming
@@ -1022,8 +907,9 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
                                        DEFAULT_RATELIMIT_BURST);
 
                        if (unlikely(scsi_logging_level))
-                               level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
-                                                      SCSI_LOG_MLCOMPLETE_BITS);
+                               level =
+                                    SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
+                                                   SCSI_LOG_MLCOMPLETE_BITS);
 
                        /*
                         * if logging is enabled the failure will be printed
@@ -1031,25 +917,16 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
                         */
                        if (!level && __ratelimit(&_rs)) {
                                scsi_print_result(cmd, NULL, FAILED);
-                               if (driver_byte(result) & DRIVER_SENSE)
+                               if (driver_byte(result) == DRIVER_SENSE)
                                        scsi_print_sense(cmd);
                                scsi_print_command(cmd);
                        }
                }
-               if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
+               if (!scsi_end_request(req, blk_stat, blk_rq_err_bytes(req), 0))
                        return;
                /*FALLTHRU*/
        case ACTION_REPREP:
-       requeue:
-               /* Unprep the request and put it back at the head of the queue.
-                * A new command will be prepared and issued.
-                */
-               if (q->mq_ops) {
-                       scsi_mq_requeue_cmd(cmd);
-               } else {
-                       scsi_release_buffers(cmd);
-                       scsi_requeue_command(q, cmd);
-               }
+               scsi_io_completion_reprep(cmd, q);
                break;
        case ACTION_RETRY:
                /* Retry the same command immediately */
@@ -1062,6 +939,185 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
        }
 }
 
+/*
+ * Helper for scsi_io_completion() when cmd->result is non-zero. Returns a
+ * new result that may suppress further error checking. Also modifies
+ * *blk_statp in some cases.
+ */
+static int scsi_io_completion_nz_result(struct scsi_cmnd *cmd, int result,
+                                       blk_status_t *blk_statp)
+{
+       bool sense_valid;
+       bool sense_current = true;      /* false implies "deferred sense" */
+       struct request *req = cmd->request;
+       struct scsi_sense_hdr sshdr;
+
+       sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
+       if (sense_valid)
+               sense_current = !scsi_sense_is_deferred(&sshdr);
+
+       if (blk_rq_is_passthrough(req)) {
+               if (sense_valid) {
+                       /*
+                        * SG_IO wants current and deferred errors
+                        */
+                       scsi_req(req)->sense_len =
+                               min(8 + cmd->sense_buffer[7],
+                                   SCSI_SENSE_BUFFERSIZE);
+               }
+               if (sense_current)
+                       *blk_statp = scsi_result_to_blk_status(cmd, result);
+       } else if (blk_rq_bytes(req) == 0 && sense_current) {
+               /*
+                * Flush commands do not transfers any data, and thus cannot use
+                * good_bytes != blk_rq_bytes(req) as the signal for an error.
+                * This sets *blk_statp explicitly for the problem case.
+                */
+               *blk_statp = scsi_result_to_blk_status(cmd, result);
+       }
+       /*
+        * Recovered errors need reporting, but they're always treated as
+        * success, so fiddle the result code here.  For passthrough requests
+        * we already took a copy of the original into sreq->result which
+        * is what gets returned to the user
+        */
+       if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
+               bool do_print = true;
+               /*
+                * if ATA PASS-THROUGH INFORMATION AVAILABLE [0x0, 0x1d]
+                * skip print since caller wants ATA registers. Only occurs
+                * on SCSI ATA PASS_THROUGH commands when CK_COND=1
+                */
+               if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
+                       do_print = false;
+               else if (req->rq_flags & RQF_QUIET)
+                       do_print = false;
+               if (do_print)
+                       scsi_print_sense(cmd);
+               result = 0;
+               /* for passthrough, *blk_statp may be set */
+               *blk_statp = 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;
+               *blk_statp = BLK_STS_OK;
+       }
+       return result;
+}
+
+/*
+ * Function:    scsi_io_completion()
+ *
+ * Purpose:     Completion processing for block device I/O requests.
+ *
+ * Arguments:   cmd   - command that is finished.
+ *
+ * Lock status: Assumed that no lock is held upon entry.
+ *
+ * Returns:     Nothing
+ *
+ * Notes:       We will finish off the specified number of sectors.  If we
+ *             are done, the command block will be released and the queue
+ *             function will be goosed.  If we are not done then we have to
+ *             figure out what to do next:
+ *
+ *             a) We can call scsi_requeue_command().  The request
+ *                will be unprepared and put back on the queue.  Then
+ *                a new command will be created for it.  This should
+ *                be used if we made forward progress, or if we want
+ *                to switch from READ(10) to READ(6) for example.
+ *
+ *             b) We can call __scsi_queue_insert().  The request will
+ *                be put back on the queue and retried using the same
+ *                command as before, possibly after a delay.
+ *
+ *             c) We can call scsi_end_request() with blk_stat other than
+ *                BLK_STS_OK, to fail the remainder of the request.
+ */
+void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
+{
+       int result = cmd->result;
+       struct request_queue *q = cmd->device->request_queue;
+       struct request *req = cmd->request;
+       blk_status_t blk_stat = BLK_STS_OK;
+
+       if (unlikely(result))   /* a nz result may or may not be an error */
+               result = scsi_io_completion_nz_result(cmd, result, &blk_stat);
+
+       if (unlikely(blk_rq_is_passthrough(req))) {
+               /*
+                * scsi_result_to_blk_status may have reset the host_byte
+                */
+               scsi_req(req)->result = cmd->result;
+               scsi_req(req)->resid_len = scsi_get_resid(cmd);
+
+               if (unlikely(scsi_bidi_cmnd(cmd))) {
+                       /*
+                        * Bidi commands Must be complete as a whole,
+                        * both sides at once.
+                        */
+                       scsi_req(req->next_rq)->resid_len = scsi_in(cmd)->resid;
+                       if (scsi_end_request(req, BLK_STS_OK, blk_rq_bytes(req),
+                                       blk_rq_bytes(req->next_rq)))
+                               WARN_ONCE(true,
+                                         "Bidi command with remaining bytes");
+                       return;
+               }
+       }
+
+       /* no bidi support yet, other than in pass-through */
+       if (unlikely(blk_bidi_rq(req))) {
+               WARN_ONCE(true, "Only support bidi command in passthrough");
+               scmd_printk(KERN_ERR, cmd, "Killing bidi command\n");
+               if (scsi_end_request(req, BLK_STS_IOERR, blk_rq_bytes(req),
+                                    blk_rq_bytes(req->next_rq)))
+                       WARN_ONCE(true, "Bidi command with remaining bytes");
+               return;
+       }
+
+       /*
+        * Next deal with any sectors which we were able to correctly
+        * handle.
+        */
+       SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
+               "%u sectors total, %d bytes done.\n",
+               blk_rq_sectors(req), good_bytes));
+
+       /*
+        * Next deal with any sectors which we were able to correctly
+        * handle. Failed, zero length commands always need to drop down
+        * to retry code. Fast path should return in this block.
+        */
+       if (likely(blk_rq_bytes(req) > 0 || blk_stat == BLK_STS_OK)) {
+               if (likely(!scsi_end_request(req, blk_stat, good_bytes, 0)))
+                       return; /* no bytes remaining */
+       }
+
+       /* Kill remainder if no retries. */
+       if (unlikely(blk_stat && scsi_noretry_cmd(cmd))) {
+               if (scsi_end_request(req, blk_stat, blk_rq_bytes(req), 0))
+                       WARN_ONCE(true,
+                           "Bytes remaining after failed, no-retry command");
+               return;
+       }
+
+       /*
+        * If there had been no error, but we have leftover bytes in the
+        * requeues just queue the command up again.
+        */
+       if (likely(result == 0))
+               scsi_io_completion_reprep(cmd, q);
+       else
+               scsi_io_completion_action(cmd, result);
+}
+
 static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb)
 {
        int count;
@@ -1550,7 +1606,10 @@ static inline int scsi_host_queue_ready(struct request_queue *q,
        if (scsi_host_in_recovery(shost))
                return 0;
 
-       busy = atomic_inc_return(&shost->host_busy) - 1;
+       if (!shost->use_blk_mq)
+               busy = atomic_inc_return(&shost->host_busy) - 1;
+       else
+               busy = 0;
        if (atomic_read(&shost->host_blocked) > 0) {
                if (busy)
                        goto starved;
@@ -1566,7 +1625,7 @@ static inline int scsi_host_queue_ready(struct request_queue *q,
                                     "unblocking host at zero depth\n"));
        }
 
-       if (shost->can_queue > 0 && busy >= shost->can_queue)
+       if (!shost->use_blk_mq && shost->can_queue > 0 && busy >= shost->can_queue)
                goto starved;
        if (shost->host_self_blocked)
                goto starved;
@@ -1652,7 +1711,9 @@ static void scsi_kill_request(struct request *req, struct request_queue *q)
         * with the locks as normal issue path does.
         */
        atomic_inc(&sdev->device_busy);
-       atomic_inc(&shost->host_busy);
+
+       if (!shost->use_blk_mq)
+               atomic_inc(&shost->host_busy);
        if (starget->can_queue > 0)
                atomic_inc(&starget->target_busy);
 
@@ -2555,7 +2616,7 @@ scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
         * ILLEGAL REQUEST if the code page isn't supported */
 
        if (use_10_for_ms && !scsi_status_is_good(result) &&
-           (driver_byte(result) & DRIVER_SENSE)) {
+           driver_byte(result) == DRIVER_SENSE) {
                if (scsi_sense_valid(sshdr)) {
                        if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
                            (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {