[PATCH] capable/capability.h (arch/)
[pandora-kernel.git] / arch / ia64 / kernel / salinfo.c
1 /*
2  * salinfo.c
3  *
4  * Creates entries in /proc/sal for various system features.
5  *
6  * Copyright (c) 2003 Silicon Graphics, Inc.  All rights reserved.
7  * Copyright (c) 2003 Hewlett-Packard Co
8  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
9  *
10  * 10/30/2001   jbarnes@sgi.com         copied much of Stephane's palinfo
11  *                                      code to create this file
12  * Oct 23 2003  kaos@sgi.com
13  *   Replace IPI with set_cpus_allowed() to read a record from the required cpu.
14  *   Redesign salinfo log processing to separate interrupt and user space
15  *   contexts.
16  *   Cache the record across multi-block reads from user space.
17  *   Support > 64 cpus.
18  *   Delete module_exit and MOD_INC/DEC_COUNT, salinfo cannot be a module.
19  *
20  * Jan 28 2004  kaos@sgi.com
21  *   Periodically check for outstanding MCA or INIT records.
22  *
23  * Dec  5 2004  kaos@sgi.com
24  *   Standardize which records are cleared automatically.
25  *
26  * Aug 18 2005  kaos@sgi.com
27  *   mca.c may not pass a buffer, a NULL buffer just indicates that a new
28  *   record is available in SAL.
29  *   Replace some NR_CPUS by cpus_online, for hotplug cpu.
30  */
31
32 #include <linux/capability.h>
33 #include <linux/types.h>
34 #include <linux/proc_fs.h>
35 #include <linux/module.h>
36 #include <linux/smp.h>
37 #include <linux/smp_lock.h>
38 #include <linux/timer.h>
39 #include <linux/vmalloc.h>
40
41 #include <asm/semaphore.h>
42 #include <asm/sal.h>
43 #include <asm/uaccess.h>
44
45 MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
46 MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
47 MODULE_LICENSE("GPL");
48
49 static int salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data);
50
51 typedef struct {
52         const char              *name;          /* name of the proc entry */
53         unsigned long           feature;        /* feature bit */
54         struct proc_dir_entry   *entry;         /* registered entry (removal) */
55 } salinfo_entry_t;
56
57 /*
58  * List {name,feature} pairs for every entry in /proc/sal/<feature>
59  * that this module exports
60  */
61 static salinfo_entry_t salinfo_entries[]={
62         { "bus_lock",           IA64_SAL_PLATFORM_FEATURE_BUS_LOCK, },
63         { "irq_redirection",    IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT, },
64         { "ipi_redirection",    IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT, },
65         { "itc_drift",          IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT, },
66 };
67
68 #define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
69
70 static char *salinfo_log_name[] = {
71         "mca",
72         "init",
73         "cmc",
74         "cpe",
75 };
76
77 static struct proc_dir_entry *salinfo_proc_entries[
78         ARRAY_SIZE(salinfo_entries) +                   /* /proc/sal/bus_lock */
79         ARRAY_SIZE(salinfo_log_name) +                  /* /proc/sal/{mca,...} */
80         (2 * ARRAY_SIZE(salinfo_log_name)) +            /* /proc/sal/mca/{event,data} */
81         1];                                             /* /proc/sal */
82
83 /* Some records we get ourselves, some are accessed as saved data in buffers
84  * that are owned by mca.c.
85  */
86 struct salinfo_data_saved {
87         u8*                     buffer;
88         u64                     size;
89         u64                     id;
90         int                     cpu;
91 };
92
93 /* State transitions.  Actions are :-
94  *   Write "read <cpunum>" to the data file.
95  *   Write "clear <cpunum>" to the data file.
96  *   Write "oemdata <cpunum> <offset> to the data file.
97  *   Read from the data file.
98  *   Close the data file.
99  *
100  * Start state is NO_DATA.
101  *
102  * NO_DATA
103  *    write "read <cpunum>" -> NO_DATA or LOG_RECORD.
104  *    write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
105  *    write "oemdata <cpunum> <offset> -> return -EINVAL.
106  *    read data -> return EOF.
107  *    close -> unchanged.  Free record areas.
108  *
109  * LOG_RECORD
110  *    write "read <cpunum>" -> NO_DATA or LOG_RECORD.
111  *    write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
112  *    write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
113  *    read data -> return the INIT/MCA/CMC/CPE record.
114  *    close -> unchanged.  Keep record areas.
115  *
116  * OEMDATA
117  *    write "read <cpunum>" -> NO_DATA or LOG_RECORD.
118  *    write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
119  *    write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
120  *    read data -> return the formatted oemdata.
121  *    close -> unchanged.  Keep record areas.
122  *
123  * Closing the data file does not change the state.  This allows shell scripts
124  * to manipulate salinfo data, each shell redirection opens the file, does one
125  * action then closes it again.  The record areas are only freed at close when
126  * the state is NO_DATA.
127  */
128 enum salinfo_state {
129         STATE_NO_DATA,
130         STATE_LOG_RECORD,
131         STATE_OEMDATA,
132 };
133
134 struct salinfo_data {
135         volatile cpumask_t      cpu_event;      /* which cpus have outstanding events */
136         struct semaphore        sem;            /* count of cpus with outstanding events (bits set in cpu_event) */
137         u8                      *log_buffer;
138         u64                     log_size;
139         u8                      *oemdata;       /* decoded oem data */
140         u64                     oemdata_size;
141         int                     open;           /* single-open to prevent races */
142         u8                      type;
143         u8                      saved_num;      /* using a saved record? */
144         enum salinfo_state      state :8;       /* processing state */
145         u8                      padding;
146         int                     cpu_check;      /* next CPU to check */
147         struct salinfo_data_saved data_saved[5];/* save last 5 records from mca.c, must be < 255 */
148 };
149
150 static struct salinfo_data salinfo_data[ARRAY_SIZE(salinfo_log_name)];
151
152 static DEFINE_SPINLOCK(data_lock);
153 static DEFINE_SPINLOCK(data_saved_lock);
154
155 /** salinfo_platform_oemdata - optional callback to decode oemdata from an error
156  * record.
157  * @sect_header: pointer to the start of the section to decode.
158  * @oemdata: returns vmalloc area containing the decded output.
159  * @oemdata_size: returns length of decoded output (strlen).
160  *
161  * Description: If user space asks for oem data to be decoded by the kernel
162  * and/or prom and the platform has set salinfo_platform_oemdata to the address
163  * of a platform specific routine then call that routine.  salinfo_platform_oemdata
164  * vmalloc's and formats its output area, returning the address of the text
165  * and its strlen.  Returns 0 for success, -ve for error.  The callback is
166  * invoked on the cpu that generated the error record.
167  */
168 int (*salinfo_platform_oemdata)(const u8 *sect_header, u8 **oemdata, u64 *oemdata_size);
169
170 struct salinfo_platform_oemdata_parms {
171         const u8 *efi_guid;
172         u8 **oemdata;
173         u64 *oemdata_size;
174         int ret;
175 };
176
177 static void
178 salinfo_platform_oemdata_cpu(void *context)
179 {
180         struct salinfo_platform_oemdata_parms *parms = context;
181         parms->ret = salinfo_platform_oemdata(parms->efi_guid, parms->oemdata, parms->oemdata_size);
182 }
183
184 static void
185 shift1_data_saved (struct salinfo_data *data, int shift)
186 {
187         memcpy(data->data_saved+shift, data->data_saved+shift+1,
188                (ARRAY_SIZE(data->data_saved) - (shift+1)) * sizeof(data->data_saved[0]));
189         memset(data->data_saved + ARRAY_SIZE(data->data_saved) - 1, 0,
190                sizeof(data->data_saved[0]));
191 }
192
193 /* This routine is invoked in interrupt context.  Note: mca.c enables
194  * interrupts before calling this code for CMC/CPE.  MCA and INIT events are
195  * not irq safe, do not call any routines that use spinlocks, they may deadlock.
196  * MCA and INIT records are recorded, a timer event will look for any
197  * outstanding events and wake up the user space code.
198  *
199  * The buffer passed from mca.c points to the output from ia64_log_get. This is
200  * a persistent buffer but its contents can change between the interrupt and
201  * when user space processes the record.  Save the record id to identify
202  * changes.  If the buffer is NULL then just update the bitmap.
203  */
204 void
205 salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
206 {
207         struct salinfo_data *data = salinfo_data + type;
208         struct salinfo_data_saved *data_saved;
209         unsigned long flags = 0;
210         int i;
211         int saved_size = ARRAY_SIZE(data->data_saved);
212
213         BUG_ON(type >= ARRAY_SIZE(salinfo_log_name));
214
215         if (buffer) {
216                 if (irqsafe)
217                         spin_lock_irqsave(&data_saved_lock, flags);
218                 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
219                         if (!data_saved->buffer)
220                                 break;
221                 }
222                 if (i == saved_size) {
223                         if (!data->saved_num) {
224                                 shift1_data_saved(data, 0);
225                                 data_saved = data->data_saved + saved_size - 1;
226                         } else
227                                 data_saved = NULL;
228                 }
229                 if (data_saved) {
230                         data_saved->cpu = smp_processor_id();
231                         data_saved->id = ((sal_log_record_header_t *)buffer)->id;
232                         data_saved->size = size;
233                         data_saved->buffer = buffer;
234                 }
235                 if (irqsafe)
236                         spin_unlock_irqrestore(&data_saved_lock, flags);
237         }
238
239         if (!test_and_set_bit(smp_processor_id(), &data->cpu_event)) {
240                 if (irqsafe)
241                         up(&data->sem);
242         }
243 }
244
245 /* Check for outstanding MCA/INIT records every minute (arbitrary) */
246 #define SALINFO_TIMER_DELAY (60*HZ)
247 static struct timer_list salinfo_timer;
248
249 static void
250 salinfo_timeout_check(struct salinfo_data *data)
251 {
252         int i;
253         if (!data->open)
254                 return;
255         for_each_online_cpu(i) {
256                 if (test_bit(i, &data->cpu_event)) {
257                         /* double up() is not a problem, user space will see no
258                          * records for the additional "events".
259                          */
260                         up(&data->sem);
261                 }
262         }
263 }
264
265 static void 
266 salinfo_timeout (unsigned long arg)
267 {
268         salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_MCA);
269         salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_INIT);
270         salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
271         add_timer(&salinfo_timer);
272 }
273
274 static int
275 salinfo_event_open(struct inode *inode, struct file *file)
276 {
277         if (!capable(CAP_SYS_ADMIN))
278                 return -EPERM;
279         return 0;
280 }
281
282 static ssize_t
283 salinfo_event_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
284 {
285         struct inode *inode = file->f_dentry->d_inode;
286         struct proc_dir_entry *entry = PDE(inode);
287         struct salinfo_data *data = entry->data;
288         char cmd[32];
289         size_t size;
290         int i, n, cpu = -1;
291
292 retry:
293         if (down_trylock(&data->sem)) {
294                 if (file->f_flags & O_NONBLOCK)
295                         return -EAGAIN;
296                 if (down_interruptible(&data->sem))
297                         return -EINTR;
298         }
299
300         n = data->cpu_check;
301         for (i = 0; i < NR_CPUS; i++) {
302                 if (test_bit(n, &data->cpu_event) && cpu_online(n)) {
303                         cpu = n;
304                         break;
305                 }
306                 if (++n == NR_CPUS)
307                         n = 0;
308         }
309
310         if (cpu == -1)
311                 goto retry;
312
313         /* events are sticky until the user says "clear" */
314         up(&data->sem);
315
316         /* for next read, start checking at next CPU */
317         data->cpu_check = cpu;
318         if (++data->cpu_check == NR_CPUS)
319                 data->cpu_check = 0;
320
321         snprintf(cmd, sizeof(cmd), "read %d\n", cpu);
322
323         size = strlen(cmd);
324         if (size > count)
325                 size = count;
326         if (copy_to_user(buffer, cmd, size))
327                 return -EFAULT;
328
329         return size;
330 }
331
332 static struct file_operations salinfo_event_fops = {
333         .open  = salinfo_event_open,
334         .read  = salinfo_event_read,
335 };
336
337 static int
338 salinfo_log_open(struct inode *inode, struct file *file)
339 {
340         struct proc_dir_entry *entry = PDE(inode);
341         struct salinfo_data *data = entry->data;
342
343         if (!capable(CAP_SYS_ADMIN))
344                 return -EPERM;
345
346         spin_lock(&data_lock);
347         if (data->open) {
348                 spin_unlock(&data_lock);
349                 return -EBUSY;
350         }
351         data->open = 1;
352         spin_unlock(&data_lock);
353
354         if (data->state == STATE_NO_DATA &&
355             !(data->log_buffer = vmalloc(ia64_sal_get_state_info_size(data->type)))) {
356                 data->open = 0;
357                 return -ENOMEM;
358         }
359
360         return 0;
361 }
362
363 static int
364 salinfo_log_release(struct inode *inode, struct file *file)
365 {
366         struct proc_dir_entry *entry = PDE(inode);
367         struct salinfo_data *data = entry->data;
368
369         if (data->state == STATE_NO_DATA) {
370                 vfree(data->log_buffer);
371                 vfree(data->oemdata);
372                 data->log_buffer = NULL;
373                 data->oemdata = NULL;
374         }
375         spin_lock(&data_lock);
376         data->open = 0;
377         spin_unlock(&data_lock);
378         return 0;
379 }
380
381 static void
382 call_on_cpu(int cpu, void (*fn)(void *), void *arg)
383 {
384         cpumask_t save_cpus_allowed, new_cpus_allowed;
385         memcpy(&save_cpus_allowed, &current->cpus_allowed, sizeof(save_cpus_allowed));
386         memset(&new_cpus_allowed, 0, sizeof(new_cpus_allowed));
387         set_bit(cpu, &new_cpus_allowed);
388         set_cpus_allowed(current, new_cpus_allowed);
389         (*fn)(arg);
390         set_cpus_allowed(current, save_cpus_allowed);
391 }
392
393 static void
394 salinfo_log_read_cpu(void *context)
395 {
396         struct salinfo_data *data = context;
397         sal_log_record_header_t *rh;
398         data->log_size = ia64_sal_get_state_info(data->type, (u64 *) data->log_buffer);
399         rh = (sal_log_record_header_t *)(data->log_buffer);
400         /* Clear corrected errors as they are read from SAL */
401         if (rh->severity == sal_log_severity_corrected)
402                 ia64_sal_clear_state_info(data->type);
403 }
404
405 static void
406 salinfo_log_new_read(int cpu, struct salinfo_data *data)
407 {
408         struct salinfo_data_saved *data_saved;
409         unsigned long flags;
410         int i;
411         int saved_size = ARRAY_SIZE(data->data_saved);
412
413         data->saved_num = 0;
414         spin_lock_irqsave(&data_saved_lock, flags);
415 retry:
416         for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
417                 if (data_saved->buffer && data_saved->cpu == cpu) {
418                         sal_log_record_header_t *rh = (sal_log_record_header_t *)(data_saved->buffer);
419                         data->log_size = data_saved->size;
420                         memcpy(data->log_buffer, rh, data->log_size);
421                         barrier();      /* id check must not be moved */
422                         if (rh->id == data_saved->id) {
423                                 data->saved_num = i+1;
424                                 break;
425                         }
426                         /* saved record changed by mca.c since interrupt, discard it */
427                         shift1_data_saved(data, i);
428                         goto retry;
429                 }
430         }
431         spin_unlock_irqrestore(&data_saved_lock, flags);
432
433         if (!data->saved_num)
434                 call_on_cpu(cpu, salinfo_log_read_cpu, data);
435         if (!data->log_size) {
436                 data->state = STATE_NO_DATA;
437                 clear_bit(cpu, &data->cpu_event);
438         } else {
439                 data->state = STATE_LOG_RECORD;
440         }
441 }
442
443 static ssize_t
444 salinfo_log_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
445 {
446         struct inode *inode = file->f_dentry->d_inode;
447         struct proc_dir_entry *entry = PDE(inode);
448         struct salinfo_data *data = entry->data;
449         u8 *buf;
450         u64 bufsize;
451
452         if (data->state == STATE_LOG_RECORD) {
453                 buf = data->log_buffer;
454                 bufsize = data->log_size;
455         } else if (data->state == STATE_OEMDATA) {
456                 buf = data->oemdata;
457                 bufsize = data->oemdata_size;
458         } else {
459                 buf = NULL;
460                 bufsize = 0;
461         }
462         return simple_read_from_buffer(buffer, count, ppos, buf, bufsize);
463 }
464
465 static void
466 salinfo_log_clear_cpu(void *context)
467 {
468         struct salinfo_data *data = context;
469         ia64_sal_clear_state_info(data->type);
470 }
471
472 static int
473 salinfo_log_clear(struct salinfo_data *data, int cpu)
474 {
475         sal_log_record_header_t *rh;
476         data->state = STATE_NO_DATA;
477         if (!test_bit(cpu, &data->cpu_event))
478                 return 0;
479         down(&data->sem);
480         clear_bit(cpu, &data->cpu_event);
481         if (data->saved_num) {
482                 unsigned long flags;
483                 spin_lock_irqsave(&data_saved_lock, flags);
484                 shift1_data_saved(data, data->saved_num - 1 );
485                 data->saved_num = 0;
486                 spin_unlock_irqrestore(&data_saved_lock, flags);
487         }
488         rh = (sal_log_record_header_t *)(data->log_buffer);
489         /* Corrected errors have already been cleared from SAL */
490         if (rh->severity != sal_log_severity_corrected)
491                 call_on_cpu(cpu, salinfo_log_clear_cpu, data);
492         /* clearing a record may make a new record visible */
493         salinfo_log_new_read(cpu, data);
494         if (data->state == STATE_LOG_RECORD &&
495             !test_and_set_bit(cpu,  &data->cpu_event))
496                 up(&data->sem);
497         return 0;
498 }
499
500 static ssize_t
501 salinfo_log_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
502 {
503         struct inode *inode = file->f_dentry->d_inode;
504         struct proc_dir_entry *entry = PDE(inode);
505         struct salinfo_data *data = entry->data;
506         char cmd[32];
507         size_t size;
508         u32 offset;
509         int cpu;
510
511         size = sizeof(cmd);
512         if (count < size)
513                 size = count;
514         if (copy_from_user(cmd, buffer, size))
515                 return -EFAULT;
516
517         if (sscanf(cmd, "read %d", &cpu) == 1) {
518                 salinfo_log_new_read(cpu, data);
519         } else if (sscanf(cmd, "clear %d", &cpu) == 1) {
520                 int ret;
521                 if ((ret = salinfo_log_clear(data, cpu)))
522                         count = ret;
523         } else if (sscanf(cmd, "oemdata %d %d", &cpu, &offset) == 2) {
524                 if (data->state != STATE_LOG_RECORD && data->state != STATE_OEMDATA)
525                         return -EINVAL;
526                 if (offset > data->log_size - sizeof(efi_guid_t))
527                         return -EINVAL;
528                 data->state = STATE_OEMDATA;
529                 if (salinfo_platform_oemdata) {
530                         struct salinfo_platform_oemdata_parms parms = {
531                                 .efi_guid = data->log_buffer + offset,
532                                 .oemdata = &data->oemdata,
533                                 .oemdata_size = &data->oemdata_size
534                         };
535                         call_on_cpu(cpu, salinfo_platform_oemdata_cpu, &parms);
536                         if (parms.ret)
537                                 count = parms.ret;
538                 } else
539                         data->oemdata_size = 0;
540         } else
541                 return -EINVAL;
542
543         return count;
544 }
545
546 static struct file_operations salinfo_data_fops = {
547         .open    = salinfo_log_open,
548         .release = salinfo_log_release,
549         .read    = salinfo_log_read,
550         .write   = salinfo_log_write,
551 };
552
553 static int __init
554 salinfo_init(void)
555 {
556         struct proc_dir_entry *salinfo_dir; /* /proc/sal dir entry */
557         struct proc_dir_entry **sdir = salinfo_proc_entries; /* keeps track of every entry */
558         struct proc_dir_entry *dir, *entry;
559         struct salinfo_data *data;
560         int i, j, online;
561
562         salinfo_dir = proc_mkdir("sal", NULL);
563         if (!salinfo_dir)
564                 return 0;
565
566         for (i=0; i < NR_SALINFO_ENTRIES; i++) {
567                 /* pass the feature bit in question as misc data */
568                 *sdir++ = create_proc_read_entry (salinfo_entries[i].name, 0, salinfo_dir,
569                                                   salinfo_read, (void *)salinfo_entries[i].feature);
570         }
571
572         for (i = 0; i < ARRAY_SIZE(salinfo_log_name); i++) {
573                 data = salinfo_data + i;
574                 data->type = i;
575                 sema_init(&data->sem, 0);
576                 dir = proc_mkdir(salinfo_log_name[i], salinfo_dir);
577                 if (!dir)
578                         continue;
579
580                 entry = create_proc_entry("event", S_IRUSR, dir);
581                 if (!entry)
582                         continue;
583                 entry->data = data;
584                 entry->proc_fops = &salinfo_event_fops;
585                 *sdir++ = entry;
586
587                 entry = create_proc_entry("data", S_IRUSR | S_IWUSR, dir);
588                 if (!entry)
589                         continue;
590                 entry->data = data;
591                 entry->proc_fops = &salinfo_data_fops;
592                 *sdir++ = entry;
593
594                 /* we missed any events before now */
595                 online = 0;
596                 for_each_online_cpu(j) {
597                         set_bit(j, &data->cpu_event);
598                         ++online;
599                 }
600                 sema_init(&data->sem, online);
601
602                 *sdir++ = dir;
603         }
604
605         *sdir++ = salinfo_dir;
606
607         init_timer(&salinfo_timer);
608         salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
609         salinfo_timer.function = &salinfo_timeout;
610         add_timer(&salinfo_timer);
611
612         return 0;
613 }
614
615 /*
616  * 'data' contains an integer that corresponds to the feature we're
617  * testing
618  */
619 static int
620 salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data)
621 {
622         int len = 0;
623
624         len = sprintf(page, (sal_platform_features & (unsigned long)data) ? "1\n" : "0\n");
625
626         if (len <= off+count) *eof = 1;
627
628         *start = page + off;
629         len   -= off;
630
631         if (len>count) len = count;
632         if (len<0) len = 0;
633
634         return len;
635 }
636
637 module_init(salinfo_init);