fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / ia64 / kernel / salinfo.c
index 696b3ba..e375a2f 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Creates entries in /proc/sal for various system features.
  *
- * Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
+ * Copyright (c) 2003, 2006 Silicon Graphics, Inc.  All rights reserved.
  * Copyright (c) 2003 Hewlett-Packard Co
  *     Bjorn Helgaas <bjorn.helgaas@hp.com>
  *
  *
  * Jan 28 2004 kaos@sgi.com
  *   Periodically check for outstanding MCA or INIT records.
+ *
+ * Dec  5 2004 kaos@sgi.com
+ *   Standardize which records are cleared automatically.
+ *
+ * Aug 18 2005 kaos@sgi.com
+ *   mca.c may not pass a buffer, a NULL buffer just indicates that a new
+ *   record is available in SAL.
+ *   Replace some NR_CPUS by cpus_online, for hotplug cpu.
+ *
+ * Jan  5 2006        kaos@sgi.com
+ *   Handle hotplug cpus coming online.
+ *   Handle hotplug cpus going offline while they still have outstanding records.
+ *   Use the cpu_* macros consistently.
+ *   Replace the counting semaphore with a mutex and a test if the cpumask is non-empty.
+ *   Modify the locking to make the test for "work to do" an atomic operation.
  */
 
+#include <linux/capability.h>
+#include <linux/cpu.h>
 #include <linux/types.h>
 #include <linux/proc_fs.h>
 #include <linux/module.h>
@@ -123,8 +140,8 @@ enum salinfo_state {
 };
 
 struct salinfo_data {
-       volatile cpumask_t      cpu_event;      /* which cpus have outstanding events */
-       struct semaphore        sem;            /* count of cpus with outstanding events (bits set in cpu_event) */
+       cpumask_t               cpu_event;      /* which cpus have outstanding events */
+       struct semaphore        mutex;
        u8                      *log_buffer;
        u64                     log_size;
        u8                      *oemdata;       /* decoded oem data */
@@ -140,7 +157,8 @@ struct salinfo_data {
 
 static struct salinfo_data salinfo_data[ARRAY_SIZE(salinfo_log_name)];
 
-static spinlock_t data_lock, data_saved_lock;
+static DEFINE_SPINLOCK(data_lock);
+static DEFINE_SPINLOCK(data_saved_lock);
 
 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
  * record.
@@ -164,6 +182,21 @@ struct salinfo_platform_oemdata_parms {
        int ret;
 };
 
+/* Kick the mutex that tells user space that there is work to do.  Instead of
+ * trying to track the state of the mutex across multiple cpus, in user
+ * context, interrupt context, non-maskable interrupt context and hotplug cpu,
+ * it is far easier just to grab the mutex if it is free then release it.
+ *
+ * This routine must be called with data_saved_lock held, to make the down/up
+ * operation atomic.
+ */
+static void
+salinfo_work_to_do(struct salinfo_data *data)
+{
+       down_trylock(&data->mutex);
+       up(&data->mutex);
+}
+
 static void
 salinfo_platform_oemdata_cpu(void *context)
 {
@@ -189,7 +222,7 @@ shift1_data_saved (struct salinfo_data *data, int shift)
  * The buffer passed from mca.c points to the output from ia64_log_get. This is
  * a persistent buffer but its contents can change between the interrupt and
  * when user space processes the record.  Save the record id to identify
- * changes.
+ * changes.  If the buffer is NULL then just update the bitmap.
  */
 void
 salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
@@ -204,55 +237,54 @@ salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
 
        if (irqsafe)
                spin_lock_irqsave(&data_saved_lock, flags);
-       for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
-               if (!data_saved->buffer)
-                       break;
-       }
-       if (i == saved_size) {
-               if (!data->saved_num) {
-                       shift1_data_saved(data, 0);
-                       data_saved = data->data_saved + saved_size - 1;
-               } else
-                       data_saved = NULL;
-       }
-       if (data_saved) {
-               data_saved->cpu = smp_processor_id();
-               data_saved->id = ((sal_log_record_header_t *)buffer)->id;
-               data_saved->size = size;
-               data_saved->buffer = buffer;
+       if (buffer) {
+               for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
+                       if (!data_saved->buffer)
+                               break;
+               }
+               if (i == saved_size) {
+                       if (!data->saved_num) {
+                               shift1_data_saved(data, 0);
+                               data_saved = data->data_saved + saved_size - 1;
+                       } else
+                               data_saved = NULL;
+               }
+               if (data_saved) {
+                       data_saved->cpu = smp_processor_id();
+                       data_saved->id = ((sal_log_record_header_t *)buffer)->id;
+                       data_saved->size = size;
+                       data_saved->buffer = buffer;
+               }
        }
-       if (irqsafe)
+       cpu_set(smp_processor_id(), data->cpu_event);
+       if (irqsafe) {
+               salinfo_work_to_do(data);
                spin_unlock_irqrestore(&data_saved_lock, flags);
-
-       if (!test_and_set_bit(smp_processor_id(), &data->cpu_event)) {
-               if (irqsafe)
-                       up(&data->sem);
        }
 }
 
-/* Check for outstanding MCA/INIT records every 5 minutes (arbitrary) */
-#define SALINFO_TIMER_DELAY (5*60*HZ)
+/* Check for outstanding MCA/INIT records every minute (arbitrary) */
+#define SALINFO_TIMER_DELAY (60*HZ)
 static struct timer_list salinfo_timer;
+extern void ia64_mlogbuf_dump(void);
 
 static void
 salinfo_timeout_check(struct salinfo_data *data)
 {
-       int i;
+       unsigned long flags;
        if (!data->open)
                return;
-       for (i = 0; i < NR_CPUS; ++i) {
-               if (test_bit(i, &data->cpu_event)) {
-                       /* double up() is not a problem, user space will see no
-                        * records for the additional "events".
-                        */
-                       up(&data->sem);
-               }
+       if (!cpus_empty(data->cpu_event)) {
+               spin_lock_irqsave(&data_saved_lock, flags);
+               salinfo_work_to_do(data);
+               spin_unlock_irqrestore(&data_saved_lock, flags);
        }
 }
 
-static void 
+static void
 salinfo_timeout (unsigned long arg)
 {
+       ia64_mlogbuf_dump();
        salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_MCA);
        salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_INIT);
        salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
@@ -268,9 +300,9 @@ salinfo_event_open(struct inode *inode, struct file *file)
 }
 
 static ssize_t
-salinfo_event_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+salinfo_event_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
 {
-       struct inode *inode = file->f_dentry->d_inode;
+       struct inode *inode = file->f_path.dentry->d_inode;
        struct proc_dir_entry *entry = PDE(inode);
        struct salinfo_data *data = entry->data;
        char cmd[32];
@@ -278,16 +310,20 @@ salinfo_event_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
        int i, n, cpu = -1;
 
 retry:
-       if (down_trylock(&data->sem)) {
+       if (cpus_empty(data->cpu_event) && down_trylock(&data->mutex)) {
                if (file->f_flags & O_NONBLOCK)
                        return -EAGAIN;
-               if (down_interruptible(&data->sem))
-                       return -ERESTARTSYS;
+               if (down_interruptible(&data->mutex))
+                       return -EINTR;
        }
 
        n = data->cpu_check;
        for (i = 0; i < NR_CPUS; i++) {
-               if (test_bit(n, &data->cpu_event)) {
+               if (cpu_isset(n, data->cpu_event)) {
+                       if (!cpu_online(n)) {
+                               cpu_clear(n, data->cpu_event);
+                               continue;
+                       }
                        cpu = n;
                        break;
                }
@@ -298,8 +334,7 @@ retry:
        if (cpu == -1)
                goto retry;
 
-       /* events are sticky until the user says "clear" */
-       up(&data->sem);
+       ia64_mlogbuf_dump();
 
        /* for next read, start checking at next CPU */
        data->cpu_check = cpu;
@@ -369,10 +404,8 @@ salinfo_log_release(struct inode *inode, struct file *file)
 static void
 call_on_cpu(int cpu, void (*fn)(void *), void *arg)
 {
-       cpumask_t save_cpus_allowed, new_cpus_allowed;
-       memcpy(&save_cpus_allowed, &current->cpus_allowed, sizeof(save_cpus_allowed));
-       memset(&new_cpus_allowed, 0, sizeof(new_cpus_allowed));
-       set_bit(cpu, &new_cpus_allowed);
+       cpumask_t save_cpus_allowed = current->cpus_allowed;
+       cpumask_t new_cpus_allowed = cpumask_of_cpu(cpu);
        set_cpus_allowed(current, new_cpus_allowed);
        (*fn)(arg);
        set_cpus_allowed(current, save_cpus_allowed);
@@ -382,8 +415,11 @@ static void
 salinfo_log_read_cpu(void *context)
 {
        struct salinfo_data *data = context;
+       sal_log_record_header_t *rh;
        data->log_size = ia64_sal_get_state_info(data->type, (u64 *) data->log_buffer);
-       if (data->type == SAL_INFO_TYPE_CPE || data->type == SAL_INFO_TYPE_CMC)
+       rh = (sal_log_record_header_t *)(data->log_buffer);
+       /* Clear corrected errors as they are read from SAL */
+       if (rh->severity == sal_log_severity_corrected)
                ia64_sal_clear_state_info(data->type);
 }
 
@@ -417,17 +453,20 @@ retry:
 
        if (!data->saved_num)
                call_on_cpu(cpu, salinfo_log_read_cpu, data);
-       data->state = data->log_size ? STATE_LOG_RECORD : STATE_NO_DATA;
+       if (!data->log_size) {
+               data->state = STATE_NO_DATA;
+               cpu_clear(cpu, data->cpu_event);
+       } else {
+               data->state = STATE_LOG_RECORD;
+       }
 }
 
 static ssize_t
-salinfo_log_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
+salinfo_log_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
 {
-       struct inode *inode = file->f_dentry->d_inode;
+       struct inode *inode = file->f_path.dentry->d_inode;
        struct proc_dir_entry *entry = PDE(inode);
        struct salinfo_data *data = entry->data;
-       void *saldata;
-       size_t size;
        u8 *buf;
        u64 bufsize;
 
@@ -441,18 +480,7 @@ salinfo_log_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
                buf = NULL;
                bufsize = 0;
        }
-       if (*ppos >= bufsize)
-               return 0;
-
-       saldata = buf + file->f_pos;
-       size = bufsize - file->f_pos;
-       if (size > count)
-               size = count;
-       if (copy_to_user(buffer, saldata, size))
-               return -EFAULT;
-
-       *ppos += size;
-       return size;
+       return simple_read_from_buffer(buffer, count, ppos, buf, bufsize);
 }
 
 static void
@@ -465,35 +493,39 @@ salinfo_log_clear_cpu(void *context)
 static int
 salinfo_log_clear(struct salinfo_data *data, int cpu)
 {
+       sal_log_record_header_t *rh;
+       unsigned long flags;
+       spin_lock_irqsave(&data_saved_lock, flags);
        data->state = STATE_NO_DATA;
-       if (!test_bit(cpu, &data->cpu_event))
+       if (!cpu_isset(cpu, data->cpu_event)) {
+               spin_unlock_irqrestore(&data_saved_lock, flags);
                return 0;
-       down(&data->sem);
-       clear_bit(cpu, &data->cpu_event);
+       }
+       cpu_clear(cpu, data->cpu_event);
        if (data->saved_num) {
-               unsigned long flags;
-               spin_lock_irqsave(&data_saved_lock, flags);
-               shift1_data_saved(data, data->saved_num - 1 );
+               shift1_data_saved(data, data->saved_num - 1);
                data->saved_num = 0;
-               spin_unlock_irqrestore(&data_saved_lock, flags);
        }
-       /* ia64_mca_log_sal_error_record or salinfo_log_read_cpu already cleared
-        * CPE and CMC errors
-        */
-       if (data->type != SAL_INFO_TYPE_CPE && data->type != SAL_INFO_TYPE_CMC)
+       spin_unlock_irqrestore(&data_saved_lock, flags);
+       rh = (sal_log_record_header_t *)(data->log_buffer);
+       /* Corrected errors have already been cleared from SAL */
+       if (rh->severity != sal_log_severity_corrected)
                call_on_cpu(cpu, salinfo_log_clear_cpu, data);
        /* clearing a record may make a new record visible */
        salinfo_log_new_read(cpu, data);
-       if (data->state == STATE_LOG_RECORD &&
-           !test_and_set_bit(cpu,  &data->cpu_event))
-               up(&data->sem);
+       if (data->state == STATE_LOG_RECORD) {
+               spin_lock_irqsave(&data_saved_lock, flags);
+               cpu_set(cpu, data->cpu_event);
+               salinfo_work_to_do(data);
+               spin_unlock_irqrestore(&data_saved_lock, flags);
+       }
        return 0;
 }
 
 static ssize_t
-salinfo_log_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
+salinfo_log_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
 {
-       struct inode *inode = file->f_dentry->d_inode;
+       struct inode *inode = file->f_path.dentry->d_inode;
        struct proc_dir_entry *entry = PDE(inode);
        struct salinfo_data *data = entry->data;
        char cmd[32];
@@ -543,6 +575,51 @@ static struct file_operations salinfo_data_fops = {
        .write   = salinfo_log_write,
 };
 
+static int __devinit
+salinfo_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
+{
+       unsigned int i, cpu = (unsigned long)hcpu;
+       unsigned long flags;
+       struct salinfo_data *data;
+       switch (action) {
+       case CPU_ONLINE:
+               spin_lock_irqsave(&data_saved_lock, flags);
+               for (i = 0, data = salinfo_data;
+                    i < ARRAY_SIZE(salinfo_data);
+                    ++i, ++data) {
+                       cpu_set(cpu, data->cpu_event);
+                       salinfo_work_to_do(data);
+               }
+               spin_unlock_irqrestore(&data_saved_lock, flags);
+               break;
+       case CPU_DEAD:
+               spin_lock_irqsave(&data_saved_lock, flags);
+               for (i = 0, data = salinfo_data;
+                    i < ARRAY_SIZE(salinfo_data);
+                    ++i, ++data) {
+                       struct salinfo_data_saved *data_saved;
+                       int j;
+                       for (j = ARRAY_SIZE(data->data_saved) - 1, data_saved = data->data_saved + j;
+                            j >= 0;
+                            --j, --data_saved) {
+                               if (data_saved->buffer && data_saved->cpu == cpu) {
+                                       shift1_data_saved(data, j);
+                               }
+                       }
+                       cpu_clear(cpu, data->cpu_event);
+               }
+               spin_unlock_irqrestore(&data_saved_lock, flags);
+               break;
+       }
+       return NOTIFY_OK;
+}
+
+static struct notifier_block salinfo_cpu_notifier =
+{
+       .notifier_call = salinfo_cpu_callback,
+       .priority = 0,
+};
+
 static int __init
 salinfo_init(void)
 {
@@ -550,7 +627,7 @@ salinfo_init(void)
        struct proc_dir_entry **sdir = salinfo_proc_entries; /* keeps track of every entry */
        struct proc_dir_entry *dir, *entry;
        struct salinfo_data *data;
-       int i, j, online;
+       int i, j;
 
        salinfo_dir = proc_mkdir("sal", NULL);
        if (!salinfo_dir)
@@ -565,7 +642,7 @@ salinfo_init(void)
        for (i = 0; i < ARRAY_SIZE(salinfo_log_name); i++) {
                data = salinfo_data + i;
                data->type = i;
-               sema_init(&data->sem, 0);
+               init_MUTEX(&data->mutex);
                dir = proc_mkdir(salinfo_log_name[i], salinfo_dir);
                if (!dir)
                        continue;
@@ -585,13 +662,8 @@ salinfo_init(void)
                *sdir++ = entry;
 
                /* we missed any events before now */
-               online = 0;
-               for (j = 0; j < NR_CPUS; j++)
-                       if (cpu_online(j)) {
-                               set_bit(j, &data->cpu_event);
-                               ++online;
-                       }
-               sema_init(&data->sem, online);
+               for_each_online_cpu(j)
+                       cpu_set(j, data->cpu_event);
 
                *sdir++ = dir;
        }
@@ -603,6 +675,8 @@ salinfo_init(void)
        salinfo_timer.function = &salinfo_timeout;
        add_timer(&salinfo_timer);
 
+       register_hotcpu_notifier(&salinfo_cpu_notifier);
+
        return 0;
 }