1*4882a593Smuzhiyun========================== 2*4882a593SmuzhiyunHard disk shock protection 3*4882a593Smuzhiyun========================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunAuthor: Elias Oltmanns <eo@nebensachen.de> 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunLast modified: 2008-10-03 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun.. 0. Contents 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun 1. Intro 13*4882a593Smuzhiyun 2. The interface 14*4882a593Smuzhiyun 3. References 15*4882a593Smuzhiyun 4. CREDITS 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun1. Intro 19*4882a593Smuzhiyun-------- 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunATA/ATAPI-7 specifies the IDLE IMMEDIATE command with unload feature. 22*4882a593SmuzhiyunIssuing this command should cause the drive to switch to idle mode and 23*4882a593Smuzhiyununload disk heads. This feature is being used in modern laptops in 24*4882a593Smuzhiyunconjunction with accelerometers and appropriate software to implement 25*4882a593Smuzhiyuna shock protection facility. The idea is to stop all I/O operations on 26*4882a593Smuzhiyunthe internal hard drive and park its heads on the ramp when critical 27*4882a593Smuzhiyunsituations are anticipated. The desire to have such a feature 28*4882a593Smuzhiyunavailable on GNU/Linux systems has been the original motivation to 29*4882a593Smuzhiyunimplement a generic disk head parking interface in the Linux kernel. 30*4882a593SmuzhiyunPlease note, however, that other components have to be set up on your 31*4882a593Smuzhiyunsystem in order to get disk shock protection working (see 32*4882a593Smuzhiyunsection 3. References below for pointers to more information about 33*4882a593Smuzhiyunthat). 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun2. The interface 37*4882a593Smuzhiyun---------------- 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunFor each ATA device, the kernel exports the file 40*4882a593Smuzhiyun`block/*/device/unload_heads` in sysfs (here assumed to be mounted under 41*4882a593Smuzhiyun/sys). Access to `/sys/block/*/device/unload_heads` is denied with 42*4882a593Smuzhiyun-EOPNOTSUPP if the device does not support the unload feature. 43*4882a593SmuzhiyunOtherwise, writing an integer value to this file will take the heads 44*4882a593Smuzhiyunof the respective drive off the platter and block all I/O operations 45*4882a593Smuzhiyunfor the specified number of milliseconds. When the timeout expires and 46*4882a593Smuzhiyunno further disk head park request has been issued in the meantime, 47*4882a593Smuzhiyunnormal operation will be resumed. The maximal value accepted for a 48*4882a593Smuzhiyuntimeout is 30000 milliseconds. Exceeding this limit will return 49*4882a593Smuzhiyun-EOVERFLOW, but heads will be parked anyway and the timeout will be 50*4882a593Smuzhiyunset to 30 seconds. However, you can always change a timeout to any 51*4882a593Smuzhiyunvalue between 0 and 30000 by issuing a subsequent head park request 52*4882a593Smuzhiyunbefore the timeout of the previous one has expired. In particular, the 53*4882a593Smuzhiyuntotal timeout can exceed 30 seconds and, more importantly, you can 54*4882a593Smuzhiyuncancel a previously set timeout and resume normal operation 55*4882a593Smuzhiyunimmediately by specifying a timeout of 0. Values below -2 are rejected 56*4882a593Smuzhiyunwith -EINVAL (see below for the special meaning of -1 and -2). If the 57*4882a593Smuzhiyuntimeout specified for a recent head park request has not yet expired, 58*4882a593Smuzhiyunreading from `/sys/block/*/device/unload_heads` will report the number 59*4882a593Smuzhiyunof milliseconds remaining until normal operation will be resumed; 60*4882a593Smuzhiyunotherwise, reading the unload_heads attribute will return 0. 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunFor example, do the following in order to park the heads of drive 63*4882a593Smuzhiyun/dev/sda and stop all I/O operations for five seconds:: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun # echo 5000 > /sys/block/sda/device/unload_heads 66*4882a593Smuzhiyun 67*4882a593SmuzhiyunA simple:: 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun # cat /sys/block/sda/device/unload_heads 70*4882a593Smuzhiyun 71*4882a593Smuzhiyunwill show you how many milliseconds are left before normal operation 72*4882a593Smuzhiyunwill be resumed. 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunA word of caution: The fact that the interface operates on a basis of 75*4882a593Smuzhiyunmilliseconds may raise expectations that cannot be satisfied in 76*4882a593Smuzhiyunreality. In fact, the ATA specs clearly state that the time for an 77*4882a593Smuzhiyununload operation to complete is vendor specific. The hint in ATA-7 78*4882a593Smuzhiyunthat this will typically be within 500 milliseconds apparently has 79*4882a593Smuzhiyunbeen dropped in ATA-8. 80*4882a593Smuzhiyun 81*4882a593SmuzhiyunThere is a technical detail of this implementation that may cause some 82*4882a593Smuzhiyunconfusion and should be discussed here. When a head park request has 83*4882a593Smuzhiyunbeen issued to a device successfully, all I/O operations on the 84*4882a593Smuzhiyuncontroller port this device is attached to will be deferred. That is 85*4882a593Smuzhiyunto say, any other device that may be connected to the same port will 86*4882a593Smuzhiyunbe affected too. The only exception is that a subsequent head unload 87*4882a593Smuzhiyunrequest to that other device will be executed immediately. Further 88*4882a593Smuzhiyunoperations on that port will be deferred until the timeout specified 89*4882a593Smuzhiyunfor either device on the port has expired. As far as PATA (old style 90*4882a593SmuzhiyunIDE) configurations are concerned, there can only be two devices 91*4882a593Smuzhiyunattached to any single port. In SATA world we have port multipliers 92*4882a593Smuzhiyunwhich means that a user-issued head parking request to one device may 93*4882a593Smuzhiyunactually result in stopping I/O to a whole bunch of devices. However, 94*4882a593Smuzhiyunsince this feature is supposed to be used on laptops and does not seem 95*4882a593Smuzhiyunto be very useful in any other environment, there will be mostly one 96*4882a593Smuzhiyundevice per port. Even if the CD/DVD writer happens to be connected to 97*4882a593Smuzhiyunthe same port as the hard drive, it generally *should* recover just 98*4882a593Smuzhiyunfine from the occasional buffer under-run incurred by a head park 99*4882a593Smuzhiyunrequest to the HD. Actually, when you are using an ide driver rather 100*4882a593Smuzhiyunthan its libata counterpart (i.e. your disk is called /dev/hda 101*4882a593Smuzhiyuninstead of /dev/sda), then parking the heads of one drive (drive X) 102*4882a593Smuzhiyunwill generally not affect the mode of operation of another drive 103*4882a593Smuzhiyun(drive Y) on the same port as described above. It is only when a port 104*4882a593Smuzhiyunreset is required to recover from an exception on drive Y that further 105*4882a593SmuzhiyunI/O operations on that drive (and the reset itself) will be delayed 106*4882a593Smuzhiyununtil drive X is no longer in the parked state. 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunFinally, there are some hard drives that only comply with an earlier 109*4882a593Smuzhiyunversion of the ATA standard than ATA-7, but do support the unload 110*4882a593Smuzhiyunfeature nonetheless. Unfortunately, there is no safe way Linux can 111*4882a593Smuzhiyundetect these devices, so you won't be able to write to the 112*4882a593Smuzhiyununload_heads attribute. If you know that your device really does 113*4882a593Smuzhiyunsupport the unload feature (for instance, because the vendor of your 114*4882a593Smuzhiyunlaptop or the hard drive itself told you so), then you can tell the 115*4882a593Smuzhiyunkernel to enable the usage of this feature for that drive by writing 116*4882a593Smuzhiyunthe special value -1 to the unload_heads attribute:: 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun # echo -1 > /sys/block/sda/device/unload_heads 119*4882a593Smuzhiyun 120*4882a593Smuzhiyunwill enable the feature for /dev/sda, and giving -2 instead of -1 will 121*4882a593Smuzhiyundisable it again. 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun3. References 125*4882a593Smuzhiyun------------- 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunThere are several laptops from different vendors featuring shock 128*4882a593Smuzhiyunprotection capabilities. As manufacturers have refused to support open 129*4882a593Smuzhiyunsource development of the required software components so far, Linux 130*4882a593Smuzhiyunsupport for shock protection varies considerably between different 131*4882a593Smuzhiyunhardware implementations. Ideally, this section should contain a list 132*4882a593Smuzhiyunof pointers at different projects aiming at an implementation of shock 133*4882a593Smuzhiyunprotection on different systems. Unfortunately, I only know of a 134*4882a593Smuzhiyunsingle project which, although still considered experimental, is fit 135*4882a593Smuzhiyunfor use. Please feel free to add projects that have been the victims 136*4882a593Smuzhiyunof my ignorance. 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun- https://www.thinkwiki.org/wiki/HDAPS 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun See this page for information about Linux support of the hard disk 141*4882a593Smuzhiyun active protection system as implemented in IBM/Lenovo Thinkpads. 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun4. CREDITS 145*4882a593Smuzhiyun---------- 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunThis implementation of disk head parking has been inspired by a patch 148*4882a593Smuzhiyunoriginally published by Jon Escombe <lists@dresco.co.uk>. My efforts 149*4882a593Smuzhiyunto develop an implementation of this feature that is fit to be merged 150*4882a593Smuzhiyuninto mainline have been aided by various kernel developers, in 151*4882a593Smuzhiyunparticular by Tejun Heo and Bartlomiej Zolnierkiewicz. 152