scsi: sd: Introduce manage_shutdown device flag
Damien Le Moal
maintainerPATCH v2 1/1 scsi: sd: Introduce manage_shutdown device flag
Commit aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop") change setting the manage_system_start_stop flag to false for libata managed disks to enable libata internal management of disk suspend/resume. However, a side effect of this change is that on system shutdown, disks are no longer being stopped (set to standby mode with the heads unloaded). While this is not a critical issue, this unclean shutdown is not recommended and shows up with increased smart counters (e.g. the unexpected power loss counter "Unexpect_Power_Loss_Ct").
Instead of defining a shutdown driver method for all ATA adapter drivers (not all of them define that operation), this patch resolves this issue by further refining the sd driver start/stop control of disks using the new flag manage_shutdown. If this new flag is set to true by a low level driver, the function sd_shutdown() will issue a START STOP UNIT command with the start argument set to 0 when a disk needs to be powered off (suspended) on system power off, that is, when system_state is equal to SYSTEM_POWER_OFF.
Similarly to the other manage_xxx flags, the new manage_shutdown flag is exposed through sysfs as a read-write device attribute.
To avoid any confusion between manage_shutdown and manage_system_start_stop, the comments describing these flags in include/scsi/scsi.h are also improved.
--- drivers/ata/libata-scsi.c | 5 +++-- drivers/firewire/sbp2.c | 1 + drivers/scsi/sd.c | 39 +++++++++++++++++++++++++++++++++++--- include/scsi/scsi_device.h | 20 +++++++++++++++++-- 4 files changed, 58 insertions(+), 7 deletions(-)
drivers/ata/libata-scsi.c +3 -2
@@ -1053,10 +1053,11 @@ int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)/* * Ask the sd driver to issue START STOP UNIT on runtime suspend -* and resume only. For system level suspend/resume, devices -* power state is handled directly by libata EH. +* and resume and shutdown only. For system level suspend/resume, +* devices power state is handled directly by libata EH. */ sdev->manage_runtime_start_stop = true; + sdev->manage_shutdown = true; }/*
drivers/firewire/sbp2.c +1 -0
@@ -1521,6 +1521,7 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)if (sbp2_param_exclusive_login) { sdev->manage_system_start_stop = true; sdev->manage_runtime_start_stop = true; + sdev->manage_shutdown = true; }if (sdev->type ==TYPE_ROM )
drivers/scsi/sd.c +36 -3
@@ -209,7 +209,8 @@ manage_start_stop_show(struct device *dev,return sysfs_emit (buf,"%u\n" , sdp->manage_system_start_stop && - sdp->manage_runtime_start_stop); + sdp->manage_runtime_start_stop && + sdp->manage_shutdown); }static DEVICE_ATTR_RO (manage_start_stop);
@@ -275,6 +276,35 @@ manage_runtime_start_stop_store(struct device *dev, }static DEVICE_ATTR_RW (manage_runtime_start_stop); +static ssize_t manage_shutdown_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct scsi_disk *sdkp =to_scsi_disk (dev); +struct scsi_device *sdp = sdkp->device; + +return sysfs_emit (buf,"%u\n" , sdp->manage_shutdown); +} + +static ssize_t manage_shutdown_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct scsi_disk *sdkp =to_scsi_disk (dev); +struct scsi_device *sdp = sdkp->device; +bool v; + +if (!capable (CAP_SYS_ADMIN )) +return -EACCES ; + +if (kstrtobool (buf, &v)) +return -EINVAL ; + + sdp->manage_shutdown = v; + +return count; +} +static DEVICE_ATTR_RW (manage_shutdown); +static ssize_t allow_restart_show (struct device *dev,struct device_attribute *attr,char *buf) {
@@ -607,6 +637,7 @@ static struct attribute *sd_disk_attrs[] = { &dev_attr_manage_start_stop.attr, &dev_attr_manage_system_start_stop.attr, &dev_attr_manage_runtime_start_stop.attr, + &dev_attr_manage_shutdown.attr, &dev_attr_protection_type.attr, &dev_attr_protection_mode.attr, &dev_attr_app_tag_own.attr,
@@ -3819,8 +3850,10 @@ static void sd_shutdown(struct device *dev)sd_sync_cache (sdkp,NULL ); } -if (system_state !=SYSTEM_RESTART && - sdkp->device->manage_system_start_stop) { +if ((system_state !=SYSTEM_RESTART && + sdkp->device->manage_system_start_stop) || + (system_state ==SYSTEM_POWER_OFF && + sdkp->device->manage_shutdown)) {sd_printk (KERN_NOTICE , sdkp,"Stopping disk\n" );sd_start_stop_device (sdkp,0 ); }
include/scsi/scsi_device.h +18 -2
@@ -162,8 +162,24 @@ struct scsi_device {* core. */ unsigned int eh_timeout;/* Error handling timeout */ -bool manage_system_start_stop;/* Let HLD (sd) manage system start/stop */ -bool manage_runtime_start_stop;/* Let HLD (sd) manage runtime start/stop */ +/* +* If true, let the high-level device driver (sd) manage the device +* power state for system suspend/resume (suspend to RAM and +* hybernation) operations. +*/ +bool manage_system_start_stop; + +/* +* If true, let the high-level device driver (sd) manage the device +* power state for runtime device suspand and resume operations. +*/ +bool manage_runtime_start_stop; + +/* +* If true, let the high-level device driver (sd) manage the device +* power state for system shutdown (power off) operations. +*/ +bool manage_shutdown; unsigned removable:1; unsigned changed:1; /* Data invalid due to media change */
Signature · 1 line
2.41.0
Damien Le Moal
maintainerQuoted 26 lines · Damien Le Moal
Commit
aa3998dbeb3a("ata: libata-scsi: Disable scsi devicemanage_system_start_stop") change setting themanage_system_start_stopflag to false for libata managed disks to enable libata internal management of disk suspend/resume. However, a side effect of this change is that on system shutdown, disks are no longer being stopped (set to standby mode with the heads unloaded). While this is not a critical issue, this unclean shutdown is not recommended and shows up with increased smart counters (e.g. the unexpected power loss counter "Unexpect_Power_Loss_Ct").Instead of defining a shutdown driver method for all ATA adapter drivers (not all of them define that operation), this patch resolves this issue by further refining the sd driver start/stop control of disks using the new flag
manage_shutdown. If this new flag is set to true by a low level driver, the functionsd_shutdown()will issue a START STOP UNIT command with the start argument set to 0 when a disk needs to be powered off (suspended) on system power off, that is, whensystem_stateis equal toSYSTEM_POWER_OFF.Similarly to the other
manage_xxxflags, the newmanage_shutdownflag is exposed through sysfs as a read-write device attribute.To avoid any confusion between
manage_shutdownandmanage_system_start_stop, the comments describing these flags ininclude/scsi/scsi.hare also improved.
Quoted 6 lines
Fixes aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop")Signed-off-by Damien Le Moal <[email protected]>---
Oops... Forgot to add the changelog:
Changes from v1:
- Improved flags description in include/scsi/scsi_device.h
- Added missing sysfs export of manage_shutdown
Quoted 138 lines
drivers/ata/libata-scsi.c | 5 +++-- drivers/firewire/sbp2.c | 1 + drivers/scsi/sd.c | 39 +++++++++++++++++++++++++++++++++++--- include/scsi/scsi_device.h | 20 +++++++++++++++++-- 4 files changed, 58 insertions(+), 7 deletions(-)drivers/ata/libata-scsi.c +3
-2@@ -1053,10 +1053,11 @@ int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)/* * Ask the sd driver to issue START STOP UNIT on runtime suspend -* and resume only. For system level suspend/resume, devices -* power state is handled directly by libata EH. +* and resume and shutdown only. For system level suspend/resume, +* devices power state is handled directly by libata EH. */ sdev->manage_runtime_start_stop = true; + sdev->manage_shutdown = true; }/* drivers/firewire/sbp2.c +1
-0@@ -1521,6 +1521,7 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)if (sbp2_param_exclusive_login) { sdev->manage_system_start_stop = true; sdev->manage_runtime_start_stop = true; + sdev->manage_shutdown = true; }if (sdev->type ==TYPE_ROM )drivers/scsi/sd.c +36
-3@@ -209,7 +209,8 @@ manage_start_stop_show(struct device *dev,return sysfs_emit (buf,"%u\n" , sdp->manage_system_start_stop && - sdp->manage_runtime_start_stop); + sdp->manage_runtime_start_stop && + sdp->manage_shutdown); }static DEVICE_ATTR_RO (manage_start_stop);@@ -275,6 +276,35 @@ manage_runtime_start_stop_store(struct device *dev, }static DEVICE_ATTR_RW (manage_runtime_start_stop); +static ssize_t manage_shutdown_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct scsi_disk *sdkp =to_scsi_disk (dev); +struct scsi_device *sdp = sdkp->device; + +return sysfs_emit (buf,"%u\n" , sdp->manage_shutdown); +} + +static ssize_t manage_shutdown_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct scsi_disk *sdkp =to_scsi_disk (dev); +struct scsi_device *sdp = sdkp->device; +bool v; + +if (!capable (CAP_SYS_ADMIN )) +return -EACCES ; + +if (kstrtobool (buf, &v)) +return -EINVAL ; + + sdp->manage_shutdown = v; + +return count; +} +static DEVICE_ATTR_RW (manage_shutdown); +static ssize_t allow_restart_show (struct device *dev,struct device_attribute *attr,char *buf) {@@ -607,6 +637,7 @@ static struct attribute *sd_disk_attrs[] = { &dev_attr_manage_start_stop.attr, &dev_attr_manage_system_start_stop.attr, &dev_attr_manage_runtime_start_stop.attr, + &dev_attr_manage_shutdown.attr, &dev_attr_protection_type.attr, &dev_attr_protection_mode.attr, &dev_attr_app_tag_own.attr,@@ -3819,8 +3850,10 @@ static void sd_shutdown(struct device *dev)sd_sync_cache (sdkp,NULL ); } -if (system_state !=SYSTEM_RESTART && - sdkp->device->manage_system_start_stop) { +if ((system_state !=SYSTEM_RESTART && + sdkp->device->manage_system_start_stop) || + (system_state ==SYSTEM_POWER_OFF && + sdkp->device->manage_shutdown)) {sd_printk (KERN_NOTICE , sdkp,"Stopping disk\n" );sd_start_stop_device (sdkp,0 ); }include/scsi/scsi_device.h +18
-2@@ -162,8 +162,24 @@ struct scsi_device {* core. */ unsigned int eh_timeout;/* Error handling timeout */ -bool manage_system_start_stop;/* Let HLD (sd) manage system start/stop */ -bool manage_runtime_start_stop;/* Let HLD (sd) manage runtime start/stop */ +/* +* If true, let the high-level device driver (sd) manage the device +* power state for system suspend/resume (suspend to RAM and +* hybernation) operations. +*/ +bool manage_system_start_stop; + +/* +* If true, let the high-level device driver (sd) manage the device +* power state for runtime device suspand and resume operations. +*/ +bool manage_runtime_start_stop; + +/* +* If true, let the high-level device driver (sd) manage the device +* power state for system shutdown (power off) operations. +*/ +bool manage_shutdown; unsigned removable:1; unsigned changed:1; /* Data invalid due to media change */
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 144 lines · Damien Le Moal
Commit
aa3998dbeb3a("ata: libata-scsi: Disable scsi devicemanage_system_start_stop") change setting themanage_system_start_stopflag to false for libata managed disks to enable libata internal management of disk suspend/resume. However, a side effect of this change is that on system shutdown, disks are no longer being stopped (set to standby mode with the heads unloaded). While this is not a critical issue, this unclean shutdown is not recommended and shows up with increased smart counters (e.g. the unexpected power loss counter "Unexpect_Power_Loss_Ct").Instead of defining a shutdown driver method for all ATA adapter drivers (not all of them define that operation), this patch resolves this issue by further refining the sd driver start/stop control of disks using the new flag
manage_shutdown. If this new flag is set to true by a low level driver, the functionsd_shutdown()will issue a START STOP UNIT command with the start argument set to 0 when a disk needs to be powered off (suspended) on system power off, that is, whensystem_stateis equal toSYSTEM_POWER_OFF.Similarly to the other
manage_xxxflags, the newmanage_shutdownflag is exposed through sysfs as a read-write device attribute.To avoid any confusion between
manage_shutdownandmanage_system_start_stop, the comments describing these flags ininclude/scsi/scsi.hare also improved.Fixes aa3998dbeb3a ("ata: libata-scsi: Disable scsi device manage_system_start_stop")Signed-off-by Damien Le Moal <[email protected]>--- drivers/ata/libata-scsi.c | 5 +++-- drivers/firewire/sbp2.c | 1 + drivers/scsi/sd.c | 39 +++++++++++++++++++++++++++++++++++--- include/scsi/scsi_device.h | 20 +++++++++++++++++-- 4 files changed, 58 insertions(+), 7 deletions(-)drivers/ata/libata-scsi.c +3
-2@@ -1053,10 +1053,11 @@ int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)/* * Ask the sd driver to issue START STOP UNIT on runtime suspend -* and resume only. For system level suspend/resume, devices -* power state is handled directly by libata EH. +* and resume and shutdown only. For system level suspend/resume, +* devices power state is handled directly by libata EH. */ sdev->manage_runtime_start_stop = true; + sdev->manage_shutdown = true; }/* drivers/firewire/sbp2.c +1
-0@@ -1521,6 +1521,7 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)if (sbp2_param_exclusive_login) { sdev->manage_system_start_stop = true; sdev->manage_runtime_start_stop = true; + sdev->manage_shutdown = true; }if (sdev->type ==TYPE_ROM )drivers/scsi/sd.c +36
-3@@ -209,7 +209,8 @@ manage_start_stop_show(struct device *dev,return sysfs_emit (buf,"%u\n" , sdp->manage_system_start_stop && - sdp->manage_runtime_start_stop); + sdp->manage_runtime_start_stop && + sdp->manage_shutdown); }static DEVICE_ATTR_RO (manage_start_stop);@@ -275,6 +276,35 @@ manage_runtime_start_stop_store(struct device *dev, }static DEVICE_ATTR_RW (manage_runtime_start_stop); +static ssize_t manage_shutdown_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct scsi_disk *sdkp =to_scsi_disk (dev); +struct scsi_device *sdp = sdkp->device; + +return sysfs_emit (buf,"%u\n" , sdp->manage_shutdown); +} + +static ssize_t manage_shutdown_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t count) +{ +struct scsi_disk *sdkp =to_scsi_disk (dev); +struct scsi_device *sdp = sdkp->device; +bool v; + +if (!capable (CAP_SYS_ADMIN )) +return -EACCES ; + +if (kstrtobool (buf, &v)) +return -EINVAL ; + + sdp->manage_shutdown = v; + +return count; +} +static DEVICE_ATTR_RW (manage_shutdown); +static ssize_t allow_restart_show (struct device *dev,struct device_attribute *attr,char *buf) {@@ -607,6 +637,7 @@ static struct attribute *sd_disk_attrs[] = { &dev_attr_manage_start_stop.attr, &dev_attr_manage_system_start_stop.attr, &dev_attr_manage_runtime_start_stop.attr, + &dev_attr_manage_shutdown.attr, &dev_attr_protection_type.attr, &dev_attr_protection_mode.attr, &dev_attr_app_tag_own.attr,@@ -3819,8 +3850,10 @@ static void sd_shutdown(struct device *dev)sd_sync_cache (sdkp,NULL ); } -if (system_state !=SYSTEM_RESTART && - sdkp->device->manage_system_start_stop) { +if ((system_state !=SYSTEM_RESTART && + sdkp->device->manage_system_start_stop) || + (system_state ==SYSTEM_POWER_OFF && + sdkp->device->manage_shutdown)) {sd_printk (KERN_NOTICE , sdkp,"Stopping disk\n" );sd_start_stop_device (sdkp,0 ); }include/scsi/scsi_device.h +0
-0
include/scsi/scsi_device.h struct scsi_device { · line 168
Quoted 9 lines
include/scsi/scsi_device.h +4
-2@@ -162,8 +162,24 @@ struct scsi_device {* core. */ unsigned int eh_timeout;/* Error handling timeout */ -bool manage_system_start_stop;/* Let HLD (sd) manage system start/stop */ -bool manage_runtime_start_stop;/* Let HLD (sd) manage runtime start/stop */ +/* +* If true, let the high-level device driver (sd) manage the device +* power state for system suspend/resume (suspend to RAM and +* hybernation) operations.
Nit: s/hybernation/hibernation/
Quoted 20 lines
include/scsi/scsi_device.h +14
-0+*/ +bool manage_system_start_stop; + +/* +* If true, let the high-level device driver (sd) manage the device +* power state for runtime device suspand and resume operations. +*/ +bool manage_runtime_start_stop; + +/* +* If true, let the high-level device driver (sd) manage the device +* power state for system shutdown (power off) operations. +*/ +bool manage_shutdown; unsigned removable:1; unsigned changed:1; /* Data invalid due to media change */Signature · 1 line
2.41.0