Skip to contentSkip to conversation

patch to put IDE drives in sleep-mode after an halt

PATCH Rodrigo Ventura 3 participants 4 messages May 24, 2001

lore
Share & tools
lore
#0 lore raw

I am submitting a patch to kernel/sys.c that walks through all
IDE drives (#ifdef CONFIG_BLK_DEV_IDE, of course), and issues a "sleep" command (as code in hdparam) to each one of them right before the kernel halts. Here goes the diff:

sys.c renamed from sys.c.ORIG +46 -0
@@ -15,6 +15,11 @@
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
+#ifdef CONFIG_BLK_DEV_IDE
+#include "../drivers/block/ide.h"
+#endif
+
+
 /*
  * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  */
@@ -146,6 +151,45 @@
 
 
 /*
+ * Puts IDE disks in sleep mode
+ */
+void disks_sleep(void) {
+#ifdef CONFIG_BLK_DEV_IDE
+#define WIN_SLEEPNOW1 0xE6
+#define WIN_SLEEPNOW2 0x99
+	int i, d, ret;
+	unsigned char args[4];
+	ide_drive_t *drv;
+
+	for ( i=0 ; i<MAX_HWIFS ; i++ ) {
+		if ( !ide_hwifs[i].present )
+			continue;
+		/*printk(KERN_DEBUG "HD iface %s\n", ide_hwifs[i].name);*/
+		for ( d=0 ; d<MAX_DRIVES ; d++ ) {
+			if ( !ide_hwifs[i].drives[d].present )
+				continue;
+			/*printk(KERN_DEBUG "  - drive %s\n", ide_hwifs[i].drives[d].name);*/
+			drv = &ide_hwifs[i].drives[d];
+			args[0] = WIN_SLEEPNOW1;
+			args[1] = args[2] = args[3] = 0;
+			ret = ide_wait_cmd(drv, args[0], args[1], args[2], args[3], args);
+			if (ret) {
+				args[0] = WIN_SLEEPNOW2;
+				args[1] = args[2] = args[3] = 0;
+				ret = ide_wait_cmd(drv, args[0], args[1], args[2], args[3], args);
+				if (ret)
+					printk(KERN_NOTICE "Putting %s to sleep failed.\n", drv->name);
+				else
+					printk(KERN_NOTICE "Putting %s to sleep at second attempt.\n", drv->name);
+			} else
+				printk(KERN_NOTICE "Putting %s to sleep.\n", drv->name);
+		}
+	}
+#endif
+}
+
+
+/*
  * Reboot system call: for obvious reasons only root may call it,
  * and even root needs to set up some magic numbers in the registers
  * so that some mistake won't make this reboot the whole machine.
@@ -186,6 +230,7 @@
 	case LINUX_REBOOT_CMD_HALT:
 		notifier_call_chain(&reboot_notifier_list, SYS_HALT, NULL);
 		printk(KERN_EMERG "System halted.\n");
+		disks_sleep();
 		machine_halt();
 		do_exit(0);
 		break;
@@ -193,6 +238,7 @@
 	case LINUX_REBOOT_CMD_POWER_OFF:
 		notifier_call_chain(&reboot_notifier_list, SYS_POWER_OFF, NULL);
 		printk(KERN_EMERG "Power down.\n");
+		disks_sleep();
 		machine_power_off();
 		do_exit(0);
 		break;

Please comment!

Cheers,

Signature · 6 lines
*** Rodrigo Martins de Matos Ventura <[email protected]>
***  Web page: http://www.isr.ist.utl.pt/~yoda
***   Teaching Assistant and PhD Student at ISR:
***    Instituto de Sistemas e Robotica, Polo de Lisboa
***     Instituto Superior Tecnico, Lisboa, PORTUGAL
*** PGP fingerprint = 0119 AD13 9EEE 264A 3F10  31D3 89B3 C6C4 60C6 4585
#1 lore raw
Quoted 4 lines · Rodrigo Ventura

I am submitting a patch to kernel/sys.c that walks through all
IDE drives (#ifdef CONFIG_BLK_DEV_IDE, of course), and issues a "sleep" command (as code in hdparam) to each one of them right before the kernel halts. Here goes the diff:

I'm not going to comment on the idea, just the implementation. Eww.

First point is that this has no business being in kernel/sys.c - it belongs in the ide layer, not the generic kernel.

Secondly, you have this wonderous reboot notifier list which you can arbitarily register functions with throughout the kernel, and they will get called prior to halt/reboot. You should be using this, via the register_reboot_notifier() hooks.

--
Russell King ([email protected]) The developer of ARM Linux
http://www.arm.linux.org.uk/personal/aboutme.html

#2 lore raw
In reply to Rodrigo Ventura
Quoted 4 lines · Rodrigo Ventura

I am submitting a patch to kernel/sys.c that walks through all
IDE drives (#ifdef CONFIG_BLK_DEV_IDE, of course), and issues a "sleep" command (as code in hdparam) to each one of them right before the kernel halts. Here goes the diff:

What was wrong with "hdparm -Y /dev/hd*" in the halt/reboot script that you need to do it in kernel?

Erik

Signature · 5 lines
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2783635  Fax: +31-15-2781843  Email: [email protected]
WWW: http://www-ict.its.tudelft.nl/~erik/
#3 lore raw
In reply to Rodrigo Ventura
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Erik" == Erik Mouw <[email protected]> writes:

Erik> What was wrong with "hdparm -Y /dev/hd*" in the halt/reboot Erik> script that you need to do it in kernel?

Must be sure there is no disk access after the hdparm is
run. It makes sense to me that IDE drivers could shut the drives off after their last access.

Anyway, you've made a good point -- I'll try that tonight.

Cheers,

Signature · 6 lines
*** Rodrigo Martins de Matos Ventura <[email protected]>
***  Web page: http://www.isr.ist.utl.pt/~yoda
***   Teaching Assistant and PhD Student at ISR:
***    Instituto de Sistemas e Robotica, Polo de Lisboa
***     Instituto Superior Tecnico, Lisboa, PORTUGAL
*** PGP fingerprint = 0119 AD13 9EEE 264A 3F10  31D3 89B3 C6C4 60C6 4585