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