blob: 15464a20fb388ee56fb4c7f05de9e8ed34803be6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- mode: c; c-basic-offset: 8 -*- */
2
3/* Copyright (C) 2001
4 *
5 * Author: J.E.J.Bottomley@HansenPartnership.com
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This module provides the machine status monitor thread for the
8 * voyager architecture. This allows us to monitor the machine
9 * environment (temp, voltage, fan function) and the front panel and
10 * internal UPS. If a fault is detected, this thread takes corrective
11 * action (usually just informing init)
12 * */
13
14#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
16#include <linux/kernel_stat.h>
17#include <linux/delay.h>
18#include <linux/mc146818rtc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/bootmem.h>
21#include <linux/kmod.h>
22#include <linux/completion.h>
23#include <linux/sched.h>
Christoph Hellwigf3402a42007-04-22 20:30:43 +010024#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/desc.h>
26#include <asm/voyager.h>
27#include <asm/vic.h>
28#include <asm/mtrr.h>
29#include <asm/msr.h>
30
Christoph Hellwigf3402a42007-04-22 20:30:43 +010031struct task_struct *voyager_thread;
32static __u8 set_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010034static int execute(const char *string)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 int ret;
37
38 char *envp[] = {
39 "HOME=/",
40 "TERM=linux",
41 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
42 NULL,
43 };
44 char *argv[] = {
45 "/bin/bash",
46 "-c",
47 (char *)string,
48 NULL,
49 };
50
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010051 if ((ret =
52 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC)) != 0) {
53 printk(KERN_ERR "Voyager failed to run \"%s\": %i\n", string,
54 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56 return ret;
57}
58
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010059static void check_from_kernel(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010061 if (voyager_status.switch_off) {
62
Simon Arlott27b46d72007-10-20 01:13:56 +020063 /* FIXME: This should be configurable via proc */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010065 } else if (voyager_status.power_fail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 VDEBUG(("Voyager daemon detected AC power failure\n"));
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 /* FIXME: This should be configureable via proc */
69 execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
70 set_timeout = 1;
71 }
72}
73
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010074static void check_continuing_condition(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010076 if (voyager_status.power_fail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 __u8 data;
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010078 voyager_cat_psi(VOYAGER_PSI_SUBREAD,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 VOYAGER_PSI_AC_FAIL_REG, &data);
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010080 if ((data & 0x1f) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* all power restored */
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010082 printk(KERN_NOTICE
83 "VOYAGER AC power restored, cancelling shutdown\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* FIXME: should be user configureable */
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010085 execute
86 ("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 set_timeout = 0;
88 }
89 }
90}
91
Ingo Molnara4ec1ef2008-01-30 13:30:10 +010092static int thread(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 printk(KERN_NOTICE "Voyager starting monitor thread\n");
95
Christoph Hellwigf3402a42007-04-22 20:30:43 +010096 for (;;) {
97 set_current_state(TASK_INTERRUPTIBLE);
98 schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 VDEBUG(("Voyager Daemon awoken\n"));
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100101 if (voyager_status.request_from_kernel == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 /* probably awoken from timeout */
103 check_continuing_condition();
104 } else {
105 check_from_kernel();
106 voyager_status.request_from_kernel = 0;
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109}
110
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100111static int __init voyager_thread_start(void)
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100112{
113 voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
114 if (IS_ERR(voyager_thread)) {
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100115 printk(KERN_ERR
116 "Voyager: Failed to create system monitor thread.\n");
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100117 return PTR_ERR(voyager_thread);
118 }
119 return 0;
120}
121
Ingo Molnara4ec1ef2008-01-30 13:30:10 +0100122static void __exit voyager_thread_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100124 kthread_stop(voyager_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127module_init(voyager_thread_start);
Christoph Hellwigf3402a42007-04-22 20:30:43 +0100128module_exit(voyager_thread_stop);