How do I disable hard disk spin down or head parking in FreeBSD?

Solution 1:

On FreeBSD 12, camcontrol is used to control HDD power management, including spinning down and hard parking. Previously, ataidle or atacontrol was used, but their functionalities have already been merged into camcontrol, they are deprecated and no longer supported.

TL;DR

You should run these commands on every reboot. For example, via /etc/rc.local.

Disable automatic spin down

Set "Standby" timer to 0 seconds, set APM to a number equal or greater to 128, and disable EPC "standby_z" timer. Don't forget to change ada1 to your hard drive.

# ATA has a "Standby" timer, but there's no "Idle" timer. The time
# `-t` is for standby timer.
# XXX: This command put the disk to Idle immediately, even if we only
# want to program a timer, there is no way to do that otherwise. 
# But it's only a one-time event on boot, it shouldn't matter.
camcontrol idle ada1 -t 0

# Enable APM should automatically disable EPC. If "camcontrol apm"
# succeeds but "camcontrol epc" fails, it's safe to ignore. Vice versa,
# if "camcontrol epc" succeeds but "camcontrol apm" fails, it's also
# safe to ignore.
camcontrol apm ada1 -l 128
camcontrol epc ada1 -c state -d -p standby_z

This should be good enough for most people.

Disable automatic spin down & head parking

Set "Standby" timer to 0 seconds, disable APM, and disable EPC.

This essentially disables all power management. If your hard drive supports EPC, it's recommended to use the EPC solution below (keep on reading), instead of blindly disabling all power management. Also, if you are not seeing excessive increase of Load/Unload Cycle Count in S.M.A.R.T., you probably don't need it, disabling spin down should be good enough.

# There is a "Standby" timer, but there is no
# "Idle" timer. Actually both "standby -t" and
# "idle -t" program the same "Standby" timer.
camcontrol idle ada1 -t 0
# not specify a number = disable apm
camcontrol apm ada1
# EPC is not necessaily supported, it can fail
camcontrol epc ada1 -c disable

Finally, it should be noted that there is no guaranteed way to disable head parking, ultimately it's a vendor and model specific behavior. The best thing we can try is to disable as much power management as possible. It usually works, but has no effect on some especially problematic hard drive models (such as WD Green). The only way to save them is using vendor-specific tools, which is out of the scope of this answer.

Disable automatic spin down or head parking via EPC

If your hard drive supports EPC, it's recommended to use the EPC solution below (keep on reading) to disable spin down or head parking. To prevent HDDs from spining down, disable standby_z. To prevent HDDs from parking, disable Idle_b, Idle_c and standby_y.

First, check whether your HDD supports EPC.

# check whether EPC is supported
camcontrol identify ada1 | grep "Feature\|extended power conditions"

# see the current EPC power state
camcontrol epc ada1 -c status

To enable and disable spin-down via EPC...

# disable APM first, and enable EPC.
# APM can fail if the HDD only support EPC, which is okay.
camcontrol apm ada1
camcontrol epc ada1 -c enable

# disable spin-down (Standby_z) via EPC
camcontrol epc ada1 -c state -d -p standby_z
# just in case, also disable the Standby timer
camcontrol idle ada1 -t 0

### Optional: disable head parking ###

# disable head parking (Idle_b, Idle_c, Standby_y)
camcontrol epc ada1 -c state -d -p idle_b
camcontrol epc ada1 -c state -d -p idle_c
camcontrol epc ada1 -c state -d -p standby_y

Troubleshooting

If your HDD does not support APM, you'll get the following error. If your HDD only support EPC, you can use EPC and ignore this error.

camcontrol: ATA SETFEATURES ENABLE APM failed

If your HDD does not support EPC (or if EPC is already completely disabled, e.g. also, enable APM will automatically disable EPC), you can use APM and ignore this error.

SETFEATURES EXTENDED POWER CONDITIONS. ACB: ef 4a 03 00 00 40 00 00 00 00 00 00
CAM status: ATA Status Error
ATA status: 51 (DRDY SERV ERR), error: 04 (ABRT )
RES: 51 04 00 00 00 00 00 00 00 00 00

If you can not disable hard disk spin down or head parking cannot be disable regardless of Standby/Idle Timer, APM, or EPC settings, unfortunately, only vendor-specific tools may help you. It's outside the scope of this answer.


Explanation

To understand the meanings of these commands, we must pause for a moment to learn their technical background. In fact, HDD power management is a mess. It's controlled by three independent mechanisms, namely, the basic ATA Command Set 2 (ATA8-ACS2), Advanced Power Management (APM), and Extended Power Conditions (EPC). Also, if we count vendor proprietary features found in some Seagate and WD Green drives, there are four mechanisms in total, but it's out of the scope of this answer.

In order to get the expected result, each of these mechanisms must be configured correctly. For example, if the disk Standby timer is disabled via the ATA Command Set, but APM is still set to a value lower than 128, the disk will still spin down. And to complicate the matter, not all hard drives support all of these mechanisms. For example, some enterprise-grade HDDs don't support APM and use EPC exclusively, we also have HDDs that support EPC but disables it, defaulting to APM, in addition, some earlier-generations of HDDs and many consumer-grade HDDs don't support EPC at all.

To get the desirable settings, each of the power management mechanism must be correctly configured.

ATA Command Set 2 (ATA8-ACS2)

The ATA Command Set 2 specification supports basic power management. It defines four power states in ATA drives and a Standby timer.

  1. Active–The device is fully powered up and ready to send/receive data.

  2. Idle–The device is capable of responding to commands but the device may take longer to complete commands than when in the Activemode. Power consumption of the device in this state is lower than that of Active mode. If a hard drive is present, it is spun up.

  3. Standby–The device is capable of responding to commands but the device may take longer (up to 30 seconds) to complete commands than in the Idle mode. Power consumption is reduced from that of Idle mode. If a hard drive is present, it is spun down.

  4. Sleep–This is the lowest power mode. The device interface is typically inactive and, if a hard drive is present, the drive is spun down. The device will exit the Sleep mode only after receiving a reset. Wakeup time can be as long as 30 seconds.

Source: SATA Power Management: It’s Good to Be Green

In other words, Standby causes the hard drive to spin down, and Idle is a low-power mode without no spin down. Since the standard doesn't say what a hard drive should do in Idle mode, it's vendor-specific behavior. For an educated guess, it could allow the HDD to spin at a reduced speed, or to also park its head. Finally, Sleep should never be used, unless you're going to remove the HDD or shut down the system.

Only the Standby timer can be disabled by setting it to 0 seconds. The Idle state is not controlled by a timer, and cannot be disabled.

Alternatively, you can also set the Standby timer to the number you want, however, the encoding of the timer is really strange!

Table 63 —  Standby timer periods Source: ATA/ATAPI Command Set - 2 (ACS-2)

A concise summary can be found from hdparm's man page:

Values from 1 to 240 specify multiples of 5 seconds, yielding timeouts from 5 seconds to 20 minutes. Values from 241 to 251 specify from 1 to 11 units of 30 minutes, yielding timeouts from 30 minutes to 5.5 hours. A value of 252 signifies a timeout of 21 minutes. A value of 253 sets a vendor-defined timeout period between 8 and 12 hours, and the value 254 is reserved. 255 is interpreted as 21 minutes plus 15 seconds. Note that some older drives may have very different interpretations of these values.

On FreeBSD,

# check whether ATA power management is supported
# don't omit "^", or you'll get "advanced power management"
camcontrol identify ada1 | grep "Feature\|^power management"

# set standby timer, 0 to disable
camcontrol idle ada1 -t 0

# "idle -t 0" is equivalent to "standby -t 0", it set the same
# "Standby" timer, but without forcing the device to standby,
# only to "idle", which allows a quicker recovery. There is
# no way to set the timer without actually send an "idle"
# or "standby" command.
camcontrol idle ada1 -t 0

However, the devil is in the detail. There is no guaranteed way to disable either "Standby" or "Idle" states. First, as previously stated, there is no timer for "Idle", only a timer for "Standby". Thus, if the HDD vendor decides to enter Idle aggressively, and if Idle mode implies a head parking, you are out of luck without EPC. For example, some WD Green hard drives are known to perform head parking aggressively.

Also, HDDs are allowed to enter "Idle" or "Standby" by itself without host intervention.

PM1: Idle: This state shall be entered when the device receives an IDLE command or IDLE IMMEDIATE command. Some devices may perform vendor specific internal power management and transition to the Idle mode without host intervention. [...]

PM2: Standby: This state shall be entered when:

  • h) a device performs a vendor specific power management function;

Source: ATA/ATAPI Command Set - 2 (ACS-2)

Thus, if the HDD vendor decides to enter Standby aggressively, You're also out of luck.

Advanced Power Management (APM)

Advanced Power Management (APM) is a power management mechanism for personal computers standardized in the late 90s. It has long been superseded by ACPI, however, it's still used in HDD power management and is supported by almost all hard drives. It's unsupported only in some enterprise-grade hard drives.

The APM level is a scale from the lowest power consumption setting of 01h to the maximum performance level of FEh. Table 120 shows these values.Device performance may increase with increasing APM levels. Device power consumption may increase with increasing power management levels. The APM levels may contain discrete bands (e.g., a device may implement one APM method from 80h to A0h and a higher performance, higher power consumption method from level A1h to FEh). APM levels 80h and higher do not permit the device to spin down to save power.Subcommand code 85h disables APM. Subcommand 85h may not be implemented on all devices that implement SET FEATURES subcommand 05h.

Table 120: APM levels

  • 00h (0): Reserved
  • 01h (1): Minimum power consumption with Standby
  • 02h-7Fh (2-127): Intermediate power management levels with Standby
  • 80h (128): Minimum power consumption without Standby
  • 81h-FDh (129-254): Intermediate power management levels without Standby
  • FEh (254): Maximum performance
  • FFh (255): Reserved

APM is independent of the Standby timer. If both APM and the Standby timer are set, then the device shall go to the Standby state when the timer expires or the device’s APM algorithm indicates that the Standby state should be entered.

Source: ATA/ATAPI Command Set - 2 (ACS-2)

The implementation of APM is not specified by the standard, thus it depends on the vendor and model of your hard drive. The only things we can be sure are that

  1. "128" enables all power saving features without spinning down. Presumably, the HDD can spin at a reduced speed, or to park its head.

  2. Usually, APM can be disabled. It's possible that speed reduction or head parking can be disabled if APM is disabled, but we cannot know for sure.

  3. On some hard drives, APM cannot be disabled, alternatively, we can use "254", the maximum performance mode defined by APM.

In FreeBSD,

# check whether APM is supported
camcontrol identify ada1 | grep "Feature\|advanced power management"

# set APM level
camcontrol apm ada1 -l 128

# disable APM
camcontrol apm ada1

Extended Power Conditions (EPC)

This is the latest power management standard in hard drives, it's usually supported on enterprise-grade hard drives (some newer hard drives don't support APM, EPC is used exclusively). Seagate markets EPC as PowerChoice™ Technology.

It offers the more fine-tuned power management levels, more specific than the basic Idle, Standby states in the standard ATA command set. It defines 2 power states and 5 power conditions.

  1. PM1: Idle state
  • Idle_a

    • Disables some electronics.
    • Discs rotating at full speed (7,200 RPM).
  • Idle_b

    • Disables some electronics.
    • Heads are unloaded to drive ramp.
    • Discs rotating at full speed (7,200 RPM)
  • Idle_c

    • Disables some electronics.
    • Heads are unloaded to drive ramp.
    • Drive speed reduced to a lower RPM (reduced RPM)
  1. PM2: Standby state
  • Standby_y

    • Same as Idle_c in Seagate and HGST (now WD).
  • Standby_z

    • Heads are unloaded to drive ramp.
    • Drive motor is spun down.

Source: * ATA/ATAPI Command Set - 2 (ACS-2)* Hard Drive for Low PowerEnergy Efficiency in Disk Storage by Hitachi* Seagate® PowerChoice™ Technology Provides Unprecedented Hard Drive Power Savings and Flexibility by Seagate* Ultrastar® DC HC620 Hard disk drive specifications by HGST/Western Digital.

Each power condition can be controlled by a timer, and they can also be enabled or disabled individually.

Note that the precise meaning of Idle_a, Idle_b, Idle_c, Standby_y, or Standby_z is not specific in the standard at all! Again, it's up to HDD device manufecterer to implement them. I used the definition from the HGST/WD Ultrastar and Seagate datasheets. Make sure to check the full datasheet of your hard drive model!

It's clear to see that for our selected hard drives, disabling Standby_z will prevent the HDD from spinning down, and disabling Idle_b, Idle_c and Standby_y will prevent the head from being unloaded. Thus, if your HDD support EPC, it's recommended to use them.

Also, APM must be disabled first before using EPC.

The Extended Power Conditions feature set and the Advanced Power Management feature set are mutually exclusive. All EPC subcommands, except Enable the EPC feature set (see 7.49.18.6), shall return command aborted if the EPC feature set is disabled. If the device processes a SET FEATURES Enable APM subcommand without error and IDENTIFY DEVICE data word 120 bit 7 (see 7.17.7.41) is set to one, then the device shall disable the EPC feature set.

In FreeBSD,

# check whether EPC is supported
camcontrol identify ada1 | grep "Feature\|extended power conditions"

# see the current EPC power state
camcontrol epc ada1 -c status

# disable APM first
camcontrol apm ada1

# enable EPC
camcontrol epc ada1 -c enable

# disable spin-down (Standby_z)
camcontrol epc ada1 -c state -d -p standby_z

# disable head parking (Idle_b, Idle_c, Standby_y)
camcontrol epc ada1 -c state -d -p idle_b
camcontrol epc ada1 -c state -d -p idle_c
camcontrol epc ada1 -c state -d -p standby_y