Looking for a way to safely power down an ATA IDE drive? - Page 1
Forum section: electronics / projects

author: homemadecpu1986
author_rank: Contributor
timestamp: October 08, 2020, 10:26:55 am
content: Hello friends.I am using an ATA IDE in a bare bones way. I am controlling it directly with a CPU via an IDE cable.However I am not sure if it's good to simply power the drive down without first sending it a command for it to power itself down in some way? I am scared of a possible head crash or something...I only see a SLEEP command in the ATA specifications.Is this the only possibility? Or is this all that's required? Whenever I power the drive down, it's actually not being used, but the motor can be running.Is the head parked safely when the drive is not actually being used and so can it just be powered down without any commands?Thanks a lot!

author: magic
author_rank: Super Contributor
timestamp: October 08, 2020, 10:35:36 am
content: There is surely a way to spin down disks by software, Linux can do it, although I have only used it on SATA disks myself.That being said, this site says there is a standby command in ATA.https://wiki.osdev.org/ATA/ATAPI_Power_Management

author: homemadecpu1986
author_rank: Contributor
timestamp: October 08, 2020, 10:42:40 am
content: Yes indeed.......... Standby and Sleep commands........... There doesn't seem to be more than that......... I thought maybe there is/was a command to just power it down. But perhaps it's not needed really. Sleep should be enough I guess........Thanks

author: David Hess
author_rank: Super Contributor
timestamp: October 08, 2020, 11:05:46 am
content: There are no special requirements to power down an IDE hard drive safely; they are designed to handle sudden loss of power gracefully.

author: Gyro
author_rank: Super Contributor
timestamp: October 08, 2020, 11:42:28 am
content: Sleep or Standby first if possible, that should park the heads. The spec for the number of emergency head park events (sudden power loss) on HDDs is a lot lower than the normal park spec (it uses back emf from the motor to park the heads and is a lot less gentle).

author: magic
author_rank: Super Contributor
timestamp: October 08, 2020, 05:06:39 pm
content: Yeah, disks tend to make weird noise when power is cut from them suddenly. And then there are the SSDs, which don't appreciate sudden power loss at all. If you think standby isn't it, go download the spec and look for other commands. Standby probably is better than nothing, anyway.Another annoying thing is disks which automatically park their heads when idle for a few seconds. Besides adding surprise latency, it tends to scarily increase their "load cycle count", often beyond the guaranteed spec. I have never had a drive fail from that, but I have one where the normalized SMART attribute it close to zero. There are commands to disable this crap too, most of the time.

author: amyk
author_rank: Super Contributor
timestamp: October 10, 2020, 11:35:28 pm
content: As others have explained above, there are definitely commands that will cause a spindown, but they aren't used by most if not all PC OSs on a real powering off; only for power saving. OSs that turn off via ACPI (i.e. all the regular ones) just toggle the PS_ON signal from the PSU which is basically equivalent to flipping the switch off from the point of view of the drives.

author: NiHaoMike
author_rank: Super Contributor
timestamp: October 10, 2020, 11:45:53 pm
content: Quote from: amyk on October 10, 2020, 11:35:28 pmThe disks are told to power down shortly after unmounting the filesystems.

author: David Hess
author_rank: Super Contributor
timestamp: October 11, 2020, 12:13:38 am
content: Quote from: magic on October 08, 2020, 05:06:39 pmThe noise should be the heads automatically parking. I do not know if it is still the case but in the past the back EMF off of the spindle motor was used to supply power for a safe shutdown and head parking.QuoteSome are suppose to be designed to handle power loss without damage, and some without even losing buffered data.

author: coromonadalix
author_rank: Super Contributor
timestamp: October 11, 2020, 02:00:47 am
content: you even had disk controllers with a battery to provide buffering in case of a sudden power drop, and the ancestor of the ssd, a ram drive had a battery backup for at least 8 hoursIn dos days you had a "park" software to park the heads and spindown the drive ... now on modern controllers, i have one who will park the drive and send a spin down command ... and windows will eject the drive from its hardware "list point of view / enumeration" but they are hot swap drive

author: magic
author_rank: Super Contributor
timestamp: October 11, 2020, 05:34:55 am
content: Quote from: amyk on October 10, 2020, 11:35:28 pmOSs that turn off by ACPI execute a blob of code provided by the BIOS and hell only knows what this blob does or doesn't do.Disks are spun down during normal shutdown, either by the OS or by ACPI, possibly by both in case the former doesn't.If in doubt, check SMART data of your HDD and compare "power cycle count" with "power-off retract count"Also many SSDs, particularly the early ones, would be losing data every week if they weren't shut down orderly before cutting power from them.

author: Nominal Animal
author_rank: Super Contributor
timestamp: October 11, 2020, 07:08:59 am
content: Quote from: homemadecpu1986 on October 08, 2020, 10:26:55 amYes,SLEEP(non-data commandE6) causes the drive to move the heads to a safe location, and spin down the disk. The drive will not respond to anything except a hardware reset, software reset, or DEVICE RESET command.See e.g.ATA/ATAPI-4section 8.40 SLEEP, section 6.8.1 POWER MODES, or the corresponding sections inlater standards.Personally, I would first issueSTANDBY IMMEDIATE(E0), and wait for the output. If that outputs an ABRT, wait a second, then retry.Then, I would issueSLEEP(E6). If that outputs an ABRT, the drive does not support SLEEP, and you should wait 3 seconds, then power off.If the output is successful, wait for the INTRQ line to be asserted. Then, read the ATA status register (to clear the INTRQ). When the INTRQ clears, it is safe to power off.

author: Gyro
author_rank: Super Contributor
timestamp: October 11, 2020, 05:30:20 pm
content: Quote from: David Hess on October 11, 2020, 12:13:38 amI'msurethere's an echo in here!Quote from: Gyro on October 08, 2020, 11:42:28 am

author: Nominal Animal
author_rank: Super Contributor
timestamp: October 11, 2020, 05:55:19 pm
content: Quote from: Gyro on October 11, 2020, 05:30:20 pmAn echo that adds specific details, yes.

author: Gyro
author_rank: Super Contributor
timestamp: October 11, 2020, 09:34:56 pm
content: Quote from: Nominal Animal on October 11, 2020, 05:55:19 pmOther way round.

author: amyk
author_rank: Super Contributor
timestamp: October 11, 2020, 11:19:08 pm
content: Quote from: magic on October 11, 2020, 05:34:55 amOn the disks I have, those values are almost exactly the same. Nothing additional happens on shutdown.Quote from: magic on October 11, 2020, 05:34:55 amYou meanexceptthe early ones...It seems no one here remembers that ATA/IDE predates ATX and ACPI, and you simply turned off the computer with the Big Red Switch on the AT PSU once you saw a DOS prompt or "It is now safe to shut down your computer", and absolutely nothing bad happens because of that; otherwise there would've been plenty of evidence to the contrary.I repeat my position: there is precisely zero benefit to doing anything else before turning off the power on any hard disk made within the past 2 decades or so (those which use a voice-coil driver --- stepper-based ones that need explicit parking have been obsolete for a long time). The heads automatically park when power is off....I wish free_electron would chime in here, I believe he worked in the HDD industry for a while.

author: Nominal Animal
author_rank: Super Contributor
timestamp: October 12, 2020, 12:21:38 am
content: Quote from: amyk on October 11, 2020, 11:19:08 pmThe thing about the STANDBY IMMEDIATE (E0) followed by SLEEP (E6) is not that it somehow protects the hard disk; it makes thesystemmore robust. This is why I claim there is a benefit to using this sequence.The STANDBY IMMEDIATE (E0) ensures the disk is idle, and not still for example writing the last blocks. (I guess one should issue a FLUSH CACHE (E7) first, though.)The SLEEP (E6) ensures that any following attempt to do additional reads or writes will fail. This should catch bugs in the host implementation. (Looking at how suspend-to-disk is implemented in the Linux kernel, and what it involves wrt. memory, swap, and the storage containing the hibernation image, shows that this stuff is not trivial, and is usually buggy at least initially: there are too many moving parts for an ordinary human to get it right from the get go.)Even if current spinny disks are not adversely affected by sudden power loss, I think the robustnessof the systemwarrants the command sequence. It's not like it is a lot of code to implement, after all.

author: David Hess
author_rank: Super Contributor
timestamp: October 12, 2020, 01:43:01 am
content: Quote from: coromonadalix on October 11, 2020, 02:00:47 amI have a few but the battery only preserves data buffered by the HBA (host bus adapter). I assume the HBA issues synchronous writes before marking buffered data as written but I do not know if this is the case.QuoteEarly drives landed the heads anywhere, which is what head disk parking programs were for. Later drives required a textured area of the disk to land the heads because the data area of the disk was too smooth. Modern drives retract the head assemblies onto ramps. Using ramps is not as straightforward as you might think because they must be designed to not abrade contaminates.

author: amyk
author_rank: Super Contributor
timestamp: October 12, 2020, 01:55:51 am
content: Quote from: Nominal Animal on October 12, 2020, 12:21:38 amMore code = more complexity = more chance of something going wrong. When there's a simple solution that's been working fordecades(wait for all read/write activity to stop; wait perhaps a few seconds more; then cut the power) there's really no need to do anything else.

author: magic
author_rank: Super Contributor
timestamp: October 12, 2020, 06:52:01 am
content: Quote from: amyk on October 11, 2020, 11:19:08 pmNow I am mildly curious how you are managing to do itI have a dozen disks and to the best of my knowledge none has this ratio at more than 10%.No special effort towards that end, just standard PC hardware running Linux.I'm pretty sure the same was the case with disks pulled out of machines running Windows (newer than 98).

author: Gyro
author_rank: Super Contributor
timestamp: October 12, 2020, 09:55:02 am
content: Quote from: amyk on October 11, 2020, 11:19:08 pmJust to try to inject some actually manufacturer information rather than opinion...1).Note that the Datasheets for all current WD Black, Red and Green Sata have a rider on their Load/Unload cycle specs:*4, Where*4says: "Controlled unload at ambient conditions". Given that it is impossible to poke your finger in and unload the heads manually, then the alternative to "controlled" can only refer to uncontrolled Power-off unload. Always read the small print!2).From the datasheet for thecurrent(2020) WD Ultrastar DC HC530 SATA drive (https://documents.westerndigital.com/content/dam/doc-library/en_us/assets/public/western-digital/product/data-center-drives/ultrastar-dc-hc500-series/product-manual-ultrastar-dc-hc530-sata-oem-spec.pdf):Quote3).Ok, this one is direct observation rather than from a datasheet. The sound level associated with an uncontrolled power-removal head unload is far higher than a controlled idling head unload. This can only indicate a much higher level of mechanical force being employed (the motor back-EMF powered mode).

author: Twoflower
author_rank: Frequent Contributor
timestamp: October 12, 2020, 10:33:37 am
content: That reminds me of an accident I had: In the old ages of 240MByte drives (auto-parking was already in place long before that) I actually killed a HDD by using an outlet with a timer-switch. You know fixing the computer issue doesn't take that long before the timer triggers...*klick*I heard the relay in the time-switch klick and very little later some bad noise from the drive. I assume the heads were immediately before the sudden power loss directed to the center of the spindle by a disk operation, power went off while accelerated and since no controlling was possible anymore the heads hit the center of the spindle stack and on the way back they hit the sectors on the platers that maintain the bad-block realocation table (figured that out by hours of fiddling with low level format that thing to rescue the drive). So the drive was not usable anymore.Again, that was long time ago. And things most likely improved since then. But if your SW knows that the drive will lose power: Tell the drive about that.

author: thinkfat
author_rank: Unknown
timestamp: October 12, 2020, 11:54:55 am
content: When I still worked for Archos and making portable media players, powering down ATA drives safely was one of the key power-saving aspects. I remember that ATA drives specified a rather small number of "forced unloads" they would be able to take. Since these players basically only spun up the drives for a couple of seconds for filling the memory with audio or video data, we needed to survive thousands of power cycles over the lifetime of a player. The sequence involved a "standby immediate" but I think we didn't do a "sleep" because it would take quite long to complete with no benefit, and getting the drive shut down _quickly_ was good for autonomy on battery.

author: amyk
author_rank: Super Contributor
timestamp: October 12, 2020, 11:04:46 pm
content: Quote from: Gyro on October 12, 2020, 09:55:02 amThat is interesting, and I have also discovered other information that states "minimum 20000 emergency unload cycles" for WD disks, and 600000 "controlled unloads", so there is a 30x difference... but 20000 cycles is still a lot! If you were turning your computer on and off once a day, that's still almost55 years. If you were doing it 10x a day, that's a little over 5 years, or roughly the warranty period they used to have."controlled unloads" rating has to be much higher, since they like to turn them on/off when idle for power saving.Also worth noting that SATA (and USB) disks can behotplugged, which basically means no chance of even trying to spin down before power loss.Here is a datasheet for a much older drive which is rated at 50k start/stop cycles:https://www.seagate.com/staticfiles/maxtor/en_us/documentation/manuals/fireball_lct20_manual.pdfQuoteIn other words, zero practical benefit. My experience and the numbers line up.

author: thinkfat
author_rank: Unknown
timestamp: October 12, 2020, 11:18:23 pm
content: Quote from: amyk on October 12, 2020, 11:04:46 pmIt depends on the use case. For your typical HDD based media player, the disk would spin up roughly every 3 minutes (at the end of a song) for a couple of seconds, buffer the next song, then shut down again. That amounts to a lot of power cycling, depending on your music consumption. During a typical video playback, it would power cycle every 20 seconds or so. That was before Flash memory became cheap and ubiquitous, of course.What you quoted in "4.5.1 Power Sequencing" has nothing to do with unloading the heads. It just means it doesn't matter if your power switch is in the power supply or in the power return and you need not observe any sequencing when applying or cutting power. You're still required to standby or sleep the drive before cutting power.

author: magic
author_rank: Super Contributor
timestamp: October 13, 2020, 06:06:45 am
content: The quoted paragraph certainly does refer to powering the disk down from operational state. Here's more of it:QuoteSo they don't care how you turn it off.But this is a disk from 20 years ago.

author: thinkfat
author_rank: Unknown
timestamp: October 13, 2020, 08:00:13 am
content: Quote from: magic on October 13, 2020, 06:06:45 amYou need to read the whole chapter 4.5. The drive has two supply voltages, +12V and +5V. "You may apply power in any order" means it doesn't matter which of the two power rails you switch on first. "open either the power or return line" means you can disconnect GND before +12V and +5V and it will not damage the drive electronics or cause otherwise undefined behavior resulting in data loss.

author: magic
author_rank: Super Contributor
timestamp: October 13, 2020, 08:37:19 am
content: And the second sentence in 4.5.1 plus absolute lack of any other shutdown instructions strongly implies they expect you to just cut the power from the disk while it's spinning.As amyk says, this was a common practice in PCs of the '90s and before. My only disagreement with amyk is that the '90s is not exactly same thing as "the last 20 years" anymoreHeck, I have powered up my old box with Pentium II and Windows 98 to see what happens. Hard shutdown is exactly what it does and the disk doesn't even have a power-off retract counter.