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