blob: d227fabecd023d741880d4fed93dcec60c012a3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
146static spinlock_t data_lock, data_saved_lock;
147
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).
153 *
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.
160 */
161int (*salinfo_platform_oemdata)(const u8 *sect_header, u8 **oemdata, u64 *oemdata_size);
162
163struct salinfo_platform_oemdata_parms {
164 const u8 *efi_guid;
165 u8 **oemdata;
166 u64 *oemdata_size;
167 int ret;
168};
169
170static void
171salinfo_platform_oemdata_cpu(void *context)
172{
173 struct salinfo_platform_oemdata_parms *parms = context;
174 parms->ret = salinfo_platform_oemdata(parms->efi_guid, parms->oemdata, parms->oemdata_size);
175}
176
177static void
178shift1_data_saved (struct salinfo_data *data, int shift)
179{
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]));
184}
185
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.
191 *
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.
196 */
197void
198salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
199{
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);
205
206 BUG_ON(type >= ARRAY_SIZE(salinfo_log_name));
207
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;
213 }
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;
220 }
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;
226 }
227 if (irqsafe)
228 spin_unlock_irqrestore(&data_saved_lock, flags);
229
230 if (!test_and_set_bit(smp_processor_id(), &data->cpu_event)) {
231 if (irqsafe)
232 up(&data->sem);
233 }
234}
235
236/* Check for outstanding MCA/INIT records every minute (arbitrary) */
237#define SALINFO_TIMER_DELAY (60*HZ)
238static struct timer_list salinfo_timer;
239
240static void
241salinfo_timeout_check(struct salinfo_data *data)
242{
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".
250 */
251 up(&data->sem);
252 }
253 }
254}
255
256static void
257salinfo_timeout (unsigned long arg)
258{
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);
263}
264
265static int
266salinfo_event_open(struct inode *inode, struct file *file)
267{
268 if (!capable(CAP_SYS_ADMIN))
269 return -EPERM;
270 return 0;
271}
272
273static ssize_t
274salinfo_event_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
275{
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;
282
283retry:
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;
289 }
290
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;
296 }
297 if (++n == NR_CPUS)
298 n = 0;
299 }
300
301 if (cpu == -1)
302 goto retry;
303
304 /* events are sticky until the user says "clear" */
305 up(&data->sem);
306
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;
311
312 snprintf(cmd, sizeof(cmd), "read %d\n", cpu);
313
314 size = strlen(cmd);
315 if (size > count)
316 size = count;
317 if (copy_to_user(buffer, cmd, size))
318 return -EFAULT;
319
320 return size;
321}
322
323static struct file_operations salinfo_event_fops = {
324 .open = salinfo_event_open,
325 .read = salinfo_event_read,
326};
327
328static int
329salinfo_log_open(struct inode *inode, struct file *file)
330{
331 struct proc_dir_entry *entry = PDE(inode);
332 struct salinfo_data *data = entry->data;
333
334 if (!capable(CAP_SYS_ADMIN))
335 return -EPERM;
336
337 spin_lock(&data_lock);
338 if (data->open) {
339 spin_unlock(&data_lock);
340 return -EBUSY;
341 }
342 data->open = 1;
343 spin_unlock(&data_lock);
344
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;
349 }
350
351 return 0;
352}
353
354static int
355salinfo_log_release(struct inode *inode, struct file *file)
356{
357 struct proc_dir_entry *entry = PDE(inode);
358 struct salinfo_data *data = entry->data;
359
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;
365 }
366 spin_lock(&data_lock);
367 data->open = 0;
368 spin_unlock(&data_lock);
369 return 0;
370}
371
372static void
373call_on_cpu(int cpu, void (*fn)(void *), void *arg)
374{
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);
382}
383
384static void
385salinfo_log_read_cpu(void *context)
386{
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);
394}
395
396static void
397salinfo_log_new_read(int cpu, struct salinfo_data *data)
398{
399 struct salinfo_data_saved *data_saved;
400 unsigned long flags;
401 int i;
402 int saved_size = ARRAY_SIZE(data->data_saved);
403
404 data->saved_num = 0;
405 spin_lock_irqsave(&data_saved_lock, flags);
406retry:
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;
416 }
417 /* saved record changed by mca.c since interrupt, discard it */
418 shift1_data_saved(data, i);
419 goto retry;
420 }
421 }
422 spin_unlock_irqrestore(&data_saved_lock, flags);
423
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;
431 }
432}
433
434static ssize_t
435salinfo_log_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
436{
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;
442
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;
452 }
453 return simple_read_from_buffer(buffer, count, ppos, buf, bufsize);
454}
455
456static void
457salinfo_log_clear_cpu(void *context)
458{
459 struct salinfo_data *data = context;
460 ia64_sal_clear_state_info(data->type);
461}
462
463static int
464salinfo_log_clear(struct salinfo_data *data, int cpu)
465{
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);
478 }
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;
489}
490
491static ssize_t
492salinfo_log_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
493{
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;
501
502 size = sizeof(cmd);
503 if (count < size)
504 size = count;
505 if (copy_from_user(cmd, buffer, size))
506 return -EFAULT;
507
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
525 };
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;
533
534 return count;
535}
536
537static 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,
542};
543
544static int __init
545salinfo_init(void)
546{
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;
552
553 salinfo_dir = proc_mkdir("sal", NULL);
554 if (!salinfo_dir)
555 return 0;
556
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);
561 }
562
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;
570
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;
577
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;
584
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;
591 }
592 sema_init(&data->sem, online);
593
594 *sdir++ = dir;
595 }
596
597 *sdir++ = salinfo_dir;
598
599 init_timer(&salinfo_timer);
600 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
601 salinfo_timer.function = &salinfo_timeout;
602 add_timer(&salinfo_timer);
603
604 return 0;
605}
606
607/*
608 * 'data' contains an integer that corresponds to the feature we're
609 * testing
610 */
611static int
612salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data)
613{
614 int len = 0;
615
616 len = sprintf(page, (sal_platform_features & (unsigned long)data) ? "1\n" : "0\n");
617
618 if (len <= off+count) *eof = 1;
619
620 *start = page + off;
621 len -= off;
622
623 if (len>count) len = count;
624 if (len<0) len = 0;
625
626 return len;
627}
628
629module_init(salinfo_init);