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