[PATCH] scsi: sd: add runtime pm to open / release

From: Martin Kepplinger
Date: Tue Jun 23 2020 - 07:10:29 EST


This add a very conservative but simple implementation for runtime PM
to the sd scsi driver:
Resume when opened (mounted) and suspend when released (unmounted).

Improvements that allow suspending while a device is "open" can
be added later, but now we save power when no filesystem is mounted
and runtime PM is enabled.

Signed-off-by: Martin Kepplinger <martin.kepplinger@xxxxxxx>
---
drivers/scsi/sd.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index d90fefffe31b..fe4cb7c50ec1 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1372,6 +1372,7 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));

sdev = sdkp->device;
+ scsi_autopm_get_device(sdev);

/*
* If the device is in error recovery, wait until it is done.
@@ -1418,6 +1419,9 @@ static int sd_open(struct block_device *bdev, fmode_t mode)

error_out:
scsi_disk_put(sdkp);
+
+ scsi_autopm_put_device(sdev);
+
return retval;
}

@@ -1441,6 +1445,8 @@ static void sd_release(struct gendisk *disk, fmode_t mode)

SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));

+ scsi_autopm_put_device(sdev);
+
if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
if (scsi_block_when_processing_errors(sdev))
scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
--
2.20.1