Instead of only handling dev->budget_map.map != NULL, also handle dev->budget_map.map == NULL. This patch prepares for supporting logical units without budget map (sdev->budget_map.map == NULL). Cc: Jens Axboe Cc: Christoph Hellwig Cc: Ming Lei Cc: John Garry Cc: Hannes Reinecke Signed-off-by: Bart Van Assche --- drivers/scsi/scsi_lib.c | 38 ++++++++++++++++++++++++++++++++++++++ include/scsi/scsi_device.h | 5 +---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index 51ad2ad07e43..ddc51472b5eb 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -446,6 +446,44 @@ static void scsi_single_lun_run(struct scsi_device *current_sdev) spin_unlock_irqrestore(shost->host_lock, flags); } +struct sdev_cmds_allocated_data { + const struct scsi_device *sdev; + int count; +}; + +static bool scsi_device_check_allocated(struct request *rq, void *data) +{ + struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); + struct sdev_cmds_allocated_data *sifd = data; + + if (cmd->device == sifd->sdev) + sifd->count++; + + return true; +} + +/** + * scsi_device_busy() - Number of commands allocated for a SCSI device + * @sdev: SCSI device. + * + * Note: There is a subtle difference between this function and + * scsi_host_busy(). scsi_host_busy() counts the number of commands that have + * been started. This function counts the number of commands that have been + * allocated. At least the UFS driver depends on this function counting commands + * that have already been allocated but that have not yet been started. + */ +int scsi_device_busy(const struct scsi_device *sdev) +{ + struct sdev_cmds_allocated_data sifd = { .sdev = sdev }; + struct blk_mq_tag_set *set = &sdev->host->tag_set; + + if (sdev->budget_map.map) + return sbitmap_weight(&sdev->budget_map); + blk_mq_tagset_iter(set, scsi_device_check_allocated, &sifd); + return sifd.count; +} +EXPORT_SYMBOL(scsi_device_busy); + static inline bool scsi_device_is_busy(struct scsi_device *sdev) { if (scsi_device_busy(sdev) >= sdev->queue_depth) diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h index d32f5841f4f8..0dd078ac9b89 100644 --- a/include/scsi/scsi_device.h +++ b/include/scsi/scsi_device.h @@ -713,10 +713,7 @@ static inline int scsi_device_supports_vpd(struct scsi_device *sdev) return 0; } -static inline int scsi_device_busy(struct scsi_device *sdev) -{ - return sbitmap_weight(&sdev->budget_map); -} +int scsi_device_busy(const struct scsi_device *sdev); /* Macros to access the UNIT ATTENTION counters */ #define scsi_get_ua_new_media_ctr(sdev) atomic_read(&sdev->ua_new_media_ctr)