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