#include <scsi/scsi_device.h>
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_transport.h>
+#include <scsi/scsi_cmnd.h>
 
 #include "scsi_priv.h"
 #include "scsi_logging.h"
 }
 EXPORT_SYMBOL(scsi_host_get);
 
+static bool scsi_host_check_in_flight(struct request *rq, void *data,
+                                     bool reserved)
+{
+       int *count = data;
+       struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
+
+       if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
+               (*count)++;
+
+       return true;
+}
+
 /**
  * scsi_host_busy - Return the host busy counter
  * @shost:     Pointer to Scsi_Host to inc.
  **/
 int scsi_host_busy(struct Scsi_Host *shost)
 {
-       return atomic_read(&shost->host_busy);
+       int cnt = 0;
+
+       blk_mq_tagset_busy_iter(&shost->tag_set,
+                               scsi_host_check_in_flight, &cnt);
+       return cnt;
 }
 EXPORT_SYMBOL(scsi_host_busy);
 
 
         * active on the host/device.
         */
        if (unbusy)
-               scsi_device_unbusy(device);
+               scsi_device_unbusy(device, cmd);
 
        /*
         * Requeue this command.  It will go before all other commands
 }
 
 /*
- * Decrement the host_busy counter and wake up the error handler if necessary.
- * Avoid as follows that the error handler is not woken up if shost->host_busy
- * == shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination
- * with an RCU read lock in this function to ensure that this function in its
- * entirety either finishes before scsi_eh_scmd_add() increases the
+ * Wake up the error handler if necessary. Avoid as follows that the error
+ * handler is not woken up if host in-flight requests number ==
+ * shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination
+ * with an RCU read lock in this function to ensure that this function in
+ * its entirety either finishes before scsi_eh_scmd_add() increases the
  * host_failed counter or that it notices the shost state change made by
  * scsi_eh_scmd_add().
  */
-static void scsi_dec_host_busy(struct Scsi_Host *shost)
+static void scsi_dec_host_busy(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
 {
        unsigned long flags;
 
        rcu_read_lock();
-       atomic_dec(&shost->host_busy);
+       __clear_bit(SCMD_STATE_INFLIGHT, &cmd->state);
        if (unlikely(scsi_host_in_recovery(shost))) {
                spin_lock_irqsave(shost->host_lock, flags);
                if (shost->host_failed || shost->host_eh_scheduled)
        rcu_read_unlock();
 }
 
-void scsi_device_unbusy(struct scsi_device *sdev)
+void scsi_device_unbusy(struct scsi_device *sdev, struct scsi_cmnd *cmd)
 {
        struct Scsi_Host *shost = sdev->host;
        struct scsi_target *starget = scsi_target(sdev);
 
-       scsi_dec_host_busy(shost);
+       scsi_dec_host_busy(shost, cmd);
 
        if (starget->can_queue > 0)
                atomic_dec(&starget->target_busy);
 
 static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
 {
-       if (shost->can_queue > 0 &&
-           atomic_read(&shost->host_busy) >= shost->can_queue)
-               return true;
        if (atomic_read(&shost->host_blocked) > 0)
                return true;
        if (shost->host_self_blocked)
        unsigned int flags = cmd->flags & SCMD_PRESERVED_FLAGS;
        unsigned long jiffies_at_alloc;
        int retries;
+       bool in_flight;
 
        if (!blk_rq_is_scsi(rq) && !(flags & SCMD_INITIALIZED)) {
                flags |= SCMD_INITIALIZED;
 
        jiffies_at_alloc = cmd->jiffies_at_alloc;
        retries = cmd->retries;
+       in_flight = test_bit(SCMD_STATE_INFLIGHT, &cmd->state);
        /* zero out the cmd, except for the embedded scsi_request */
        memset((char *)cmd + sizeof(cmd->req), 0,
                sizeof(*cmd) - sizeof(cmd->req) + dev->host->hostt->cmd_size);
        INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
        cmd->jiffies_at_alloc = jiffies_at_alloc;
        cmd->retries = retries;
+       if (in_flight)
+               __set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
 
        scsi_add_cmd_to_list(cmd);
 }
  */
 static inline int scsi_host_queue_ready(struct request_queue *q,
                                   struct Scsi_Host *shost,
-                                  struct scsi_device *sdev)
+                                  struct scsi_device *sdev,
+                                  struct scsi_cmnd *cmd)
 {
-       unsigned int busy;
-
        if (scsi_host_in_recovery(shost))
                return 0;
 
-       busy = atomic_inc_return(&shost->host_busy) - 1;
        if (atomic_read(&shost->host_blocked) > 0) {
-               if (busy)
+               if (scsi_host_busy(shost) > 0)
                        goto starved;
 
                /*
                                     "unblocking host at zero depth\n"));
        }
 
-       if (shost->can_queue > 0 && busy >= shost->can_queue)
-               goto starved;
        if (shost->host_self_blocked)
                goto starved;
 
                spin_unlock_irq(shost->host_lock);
        }
 
+       __set_bit(SCMD_STATE_INFLIGHT, &cmd->state);
+
        return 1;
 
 starved:
                list_add_tail(&sdev->starved_entry, &shost->starved_list);
        spin_unlock_irq(shost->host_lock);
 out_dec:
-       scsi_dec_host_busy(shost);
+       scsi_dec_host_busy(shost, cmd);
        return 0;
 }
 
        ret = BLK_STS_RESOURCE;
        if (!scsi_target_queue_ready(shost, sdev))
                goto out_put_budget;
-       if (!scsi_host_queue_ready(q, shost, sdev))
+       if (!scsi_host_queue_ready(q, shost, sdev, cmd))
                goto out_dec_target_busy;
 
        if (!(req->rq_flags & RQF_DONTPREP)) {
        return BLK_STS_OK;
 
 out_dec_host_busy:
-       scsi_dec_host_busy(shost);
+       scsi_dec_host_busy(shost, cmd);
 out_dec_target_busy:
        if (scsi_target(sdev)->can_queue > 0)
                atomic_dec(&scsi_target(sdev)->target_busy);