Skip to contentSkip to conversation

Parking hard disk head from drivers

DISCUSSION Michael Hanselmann 2 participants 3 messages Jun 22, 2006 – Jul 1, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hello

I'm working on a driver for the Apple Motion Sensor found in PowerBooks from 2005 and MacBooks. The chip doing the real work gives an interrupt when it detects a "freefall" or "shock". When that happens, Mac OS X parks the internal hard disk's head to minimize the possible damage.

What would be the best and accepted way to implement the parking in the Linux kernel? IMHO this has to be done in kernel space, because it has to be instant.

My idea was to extend the genhd layer by a function to receive the gendisk structure by the HD's name (foo("hda"), foo("sda")). This might work, because the internal HD is always the first one. Another idea was to do it in the ide-disk (PowerBooks, later iBooks) and libata code (MacBooks), depending on which type is available.

Thanks,
Michael

#1 lore raw
Quoted 11 lines · Michael Hanselmann

Hello

I'm working on a driver for the Apple Motion Sensor found in PowerBooks from 2005 and MacBooks. The chip doing the real work gives an interrupt when it detects a "freefall" or "shock". When that happens, Mac OS X parks the internal hard disk's head to minimize the possible damage.

What would be the best and accepted way to implement the parking in the Linux kernel? IMHO this has to be done in kernel space, because it has to be instant.

Quoted 5 lines

My idea was to extend the genhd layer by a function to receive the gendisk structure by the HD's name (foo("hda"), foo("sda")). This might work, because the internal HD is always the first one. Another idea was to do it in the ide-disk (PowerBooks, later iBooks) and libata code (MacBooks), depending on which type is available.

If I had to guess, I would say use a notifier...

Jeff

#2 lore raw
Quoted 1 line · Jeff Garzik

If I had to guess, I would say use a notifier...

Thanks, that way I finally came up with something.

Below you find the HD parking part of my driver. I'll be really thankful for any comments, suggestions and (constructive) critics on it. It's not like I fully understood what the IDE layer does, but I looked at the power management code and implemented something like it.

It does what it should on my PowerBook, but maybe it's totally wrong.

---

block/hdpark.c +60 -0
diff -Nrup --exclude-from linux-exclude-from linux-2.6.17.orig/block/hdpark.c linux-2.6.17/block/hdpark.c
@@ -0,0 +1,60 @@
+/*
+ * Generic code for hard disk head parking
+ *
+ * Copyright (C) 2006 Michael Hanselmann ([email protected])
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/hdpark.h>
+#include <asm/atomic.h>
+
+static BLOCKING_NOTIFIER_HEAD(hdpark_notifier_list);
+static atomic_t hdpark_nested = ATOMIC_INIT(0);
+
+int register_hdpark_notifier(struct notifier_block *nb)
+{
+	return blocking_notifier_chain_register(&hdpark_notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(register_hdpark_notifier);
+
+int unregister_hdpark_notifier(struct notifier_block *nb)
+{
+	return blocking_notifier_chain_unregister(&hdpark_notifier_list, nb);
+}
+EXPORT_SYMBOL_GPL(unregister_hdpark_notifier);
+
+/* Parks the head of all registered hard disks */
+int hdpark_start()
+{
+	if (atomic_inc_return(&hdpark_nested) == 1) {
+		int err = blocking_notifier_call_chain(&hdpark_notifier_list,
+				HDPARK_ACTION_START, NULL);
+		if (err == NOTIFY_BAD)
+			return -EIO;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hdpark_start);
+
+/* Reactivates the registered hard disks */
+int hdpark_end()
+{
+	if (atomic_dec_and_test(&hdpark_nested)) {
+		int err = blocking_notifier_call_chain(&hdpark_notifier_list,
+				HDPARK_ACTION_END, NULL);
+		if (err == NOTIFY_BAD)
+			return -EIO;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(hdpark_end);
block/Kconfig +3 -0
diff -Nrup --exclude-from linux-exclude-from linux-2.6.17.orig/block/Kconfig linux-2.6.17/block/Kconfig
@@ -33,4 +33,7 @@ config LSF
 
 	  If unsure, say Y.
 
+config HDPARK
+	bool ""
+
 source block/Kconfig.iosched
block/Makefile +1 -0
diff -Nrup --exclude-from linux-exclude-from linux-2.6.17.orig/block/Makefile linux-2.6.17/block/Makefile
@@ -10,3 +10,4 @@ obj-$(CONFIG_IOSCHED_DEADLINE)	+= deadli
 obj-$(CONFIG_IOSCHED_CFQ)	+= cfq-iosched.o
 
 obj-$(CONFIG_BLK_DEV_IO_TRACE)	+= blktrace.o
+obj-$(CONFIG_HDPARK)		+= hdpark.o
drivers/ide/ide-disk.c +85 -0

109 lines not on this page · raw message

include/linux/hdpark.h +22 -0
diff -Nrup --exclude-from linux-exclude-from linux-2.6.17.orig/include/linux/hdpark.h linux-2.6.17/include/linux/hdpark.h
@@ -0,0 +1,22 @@
+/*
+ * Header for block/hdpark.c -- generic code for hard disk head parking
+ *
+ * Copyright (C) 2006 Michael Hanselmann ([email protected])
+ */
+
+#ifndef _LINUX_HDPARK_H
+#define _LINUX_HDPAKR_H
+
+#include <linux/notifier.h>
+
+enum hdpark_action {
+	HDPARK_ACTION_START = 1,
+	HDPARK_ACTION_END = 2,
+};
+
+extern int register_hdpark_notifier(struct notifier_block *nb);
+extern int unregister_hdpark_notifier(struct notifier_block *nb);
+extern int hdpark_start(void);
+extern int hdpark_end(void);
+
+#endif
include/linux/ide.h +4 -0
diff -Nrup --exclude-from linux-exclude-from linux-2.6.17.orig/include/linux/ide.h linux-2.6.17/include/linux/ide.h
@@ -639,6 +639,10 @@ typedef struct ide_drive_s {
 	struct list_head list;
 	struct device	gendev;
 	struct completion gendev_rel_comp;	/* to deal with device release() */
+
+#ifdef CONFIG_HDPARK
+	void *park_notifier;
+#endif
 } ide_drive_t;
 
 #define to_ide_device(dev)container_of(dev, ide_drive_t, gendev)
include/linux/notifier.h +1 -0
diff -Nrup --exclude-from linux-exclude-from linux-2.6.17.orig/include/linux/notifier.h linux-2.6.17/include/linux/notifier.h
@@ -36,6 +36,7 @@ struct notifier_block {
 	int (*notifier_call)(struct notifier_block *, unsigned long, void *);
 	struct notifier_block *next;
 	int priority;
+	void *private_data;
 };
 
 struct atomic_notifier_head {