blob: 7053c55b7649337c19f5495c205a0d1584f33caa [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 *
Keith Owense026cca2006-01-06 10:36:06 +11006 * Copyright (c) 2003, 2006 Silicon Graphics, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
Keith Owens289d7732005-09-11 17:21:46 +100025 *
26 * Aug 18 2005 kaos@sgi.com
27 * mca.c may not pass a buffer, a NULL buffer just indicates that a new
28 * record is available in SAL.
29 * Replace some NR_CPUS by cpus_online, for hotplug cpu.
Keith Owense026cca2006-01-06 10:36:06 +110030 *
31 * Jan 5 2006 kaos@sgi.com
32 * Handle hotplug cpus coming online.
33 * Handle hotplug cpus going offline while they still have outstanding records.
34 * Use the cpu_* macros consistently.
35 * Replace the counting semaphore with a mutex and a test if the cpumask is non-empty.
36 * Modify the locking to make the test for "work to do" an atomic operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
Randy Dunlapa9415642006-01-11 12:17:48 -080039#include <linux/capability.h>
Keith Owense026cca2006-01-06 10:36:06 +110040#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
42#include <linux/proc_fs.h>
43#include <linux/module.h>
44#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/timer.h>
46#include <linux/vmalloc.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040047#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/sal.h>
50#include <asm/uaccess.h>
51
52MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>");
53MODULE_DESCRIPTION("/proc interface to IA-64 SAL features");
54MODULE_LICENSE("GPL");
55
56static int salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data);
57
58typedef struct {
59 const char *name; /* name of the proc entry */
60 unsigned long feature; /* feature bit */
61 struct proc_dir_entry *entry; /* registered entry (removal) */
62} salinfo_entry_t;
63
64/*
65 * List {name,feature} pairs for every entry in /proc/sal/<feature>
66 * that this module exports
67 */
68static salinfo_entry_t salinfo_entries[]={
69 { "bus_lock", IA64_SAL_PLATFORM_FEATURE_BUS_LOCK, },
70 { "irq_redirection", IA64_SAL_PLATFORM_FEATURE_IRQ_REDIR_HINT, },
71 { "ipi_redirection", IA64_SAL_PLATFORM_FEATURE_IPI_REDIR_HINT, },
72 { "itc_drift", IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT, },
73};
74
75#define NR_SALINFO_ENTRIES ARRAY_SIZE(salinfo_entries)
76
77static char *salinfo_log_name[] = {
78 "mca",
79 "init",
80 "cmc",
81 "cpe",
82};
83
84static struct proc_dir_entry *salinfo_proc_entries[
85 ARRAY_SIZE(salinfo_entries) + /* /proc/sal/bus_lock */
86 ARRAY_SIZE(salinfo_log_name) + /* /proc/sal/{mca,...} */
87 (2 * ARRAY_SIZE(salinfo_log_name)) + /* /proc/sal/mca/{event,data} */
88 1]; /* /proc/sal */
89
90/* Some records we get ourselves, some are accessed as saved data in buffers
91 * that are owned by mca.c.
92 */
93struct salinfo_data_saved {
94 u8* buffer;
95 u64 size;
96 u64 id;
97 int cpu;
98};
99
100/* State transitions. Actions are :-
101 * Write "read <cpunum>" to the data file.
102 * Write "clear <cpunum>" to the data file.
103 * Write "oemdata <cpunum> <offset> to the data file.
104 * Read from the data file.
105 * Close the data file.
106 *
107 * Start state is NO_DATA.
108 *
109 * NO_DATA
110 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
111 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
112 * write "oemdata <cpunum> <offset> -> return -EINVAL.
113 * read data -> return EOF.
114 * close -> unchanged. Free record areas.
115 *
116 * LOG_RECORD
117 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
118 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
119 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
120 * read data -> return the INIT/MCA/CMC/CPE record.
121 * close -> unchanged. Keep record areas.
122 *
123 * OEMDATA
124 * write "read <cpunum>" -> NO_DATA or LOG_RECORD.
125 * write "clear <cpunum>" -> NO_DATA or LOG_RECORD.
126 * write "oemdata <cpunum> <offset> -> format the oem data, goto OEMDATA.
127 * read data -> return the formatted oemdata.
128 * close -> unchanged. Keep record areas.
129 *
130 * Closing the data file does not change the state. This allows shell scripts
131 * to manipulate salinfo data, each shell redirection opens the file, does one
132 * action then closes it again. The record areas are only freed at close when
133 * the state is NO_DATA.
134 */
135enum salinfo_state {
136 STATE_NO_DATA,
137 STATE_LOG_RECORD,
138 STATE_OEMDATA,
139};
140
141struct salinfo_data {
Keith Owense026cca2006-01-06 10:36:06 +1100142 cpumask_t cpu_event; /* which cpus have outstanding events */
143 struct semaphore mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 u8 *log_buffer;
145 u64 log_size;
146 u8 *oemdata; /* decoded oem data */
147 u64 oemdata_size;
148 int open; /* single-open to prevent races */
149 u8 type;
150 u8 saved_num; /* using a saved record? */
151 enum salinfo_state state :8; /* processing state */
152 u8 padding;
153 int cpu_check; /* next CPU to check */
154 struct salinfo_data_saved data_saved[5];/* save last 5 records from mca.c, must be < 255 */
155};
156
157static struct salinfo_data salinfo_data[ARRAY_SIZE(salinfo_log_name)];
158
Keith Owens71841b82005-07-30 17:52:00 -0700159static DEFINE_SPINLOCK(data_lock);
160static DEFINE_SPINLOCK(data_saved_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/** salinfo_platform_oemdata - optional callback to decode oemdata from an error
163 * record.
164 * @sect_header: pointer to the start of the section to decode.
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700165 * @oemdata: returns vmalloc area containing the decoded output.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * @oemdata_size: returns length of decoded output (strlen).
167 *
168 * Description: If user space asks for oem data to be decoded by the kernel
169 * and/or prom and the platform has set salinfo_platform_oemdata to the address
170 * of a platform specific routine then call that routine. salinfo_platform_oemdata
171 * vmalloc's and formats its output area, returning the address of the text
172 * and its strlen. Returns 0 for success, -ve for error. The callback is
173 * invoked on the cpu that generated the error record.
174 */
175int (*salinfo_platform_oemdata)(const u8 *sect_header, u8 **oemdata, u64 *oemdata_size);
176
177struct salinfo_platform_oemdata_parms {
178 const u8 *efi_guid;
179 u8 **oemdata;
180 u64 *oemdata_size;
181 int ret;
182};
183
Keith Owense026cca2006-01-06 10:36:06 +1100184/* Kick the mutex that tells user space that there is work to do. Instead of
185 * trying to track the state of the mutex across multiple cpus, in user
186 * context, interrupt context, non-maskable interrupt context and hotplug cpu,
187 * it is far easier just to grab the mutex if it is free then release it.
188 *
189 * This routine must be called with data_saved_lock held, to make the down/up
190 * operation atomic.
191 */
192static void
193salinfo_work_to_do(struct salinfo_data *data)
194{
195 down_trylock(&data->mutex);
196 up(&data->mutex);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void
200salinfo_platform_oemdata_cpu(void *context)
201{
202 struct salinfo_platform_oemdata_parms *parms = context;
203 parms->ret = salinfo_platform_oemdata(parms->efi_guid, parms->oemdata, parms->oemdata_size);
204}
205
206static void
207shift1_data_saved (struct salinfo_data *data, int shift)
208{
209 memcpy(data->data_saved+shift, data->data_saved+shift+1,
210 (ARRAY_SIZE(data->data_saved) - (shift+1)) * sizeof(data->data_saved[0]));
211 memset(data->data_saved + ARRAY_SIZE(data->data_saved) - 1, 0,
212 sizeof(data->data_saved[0]));
213}
214
215/* This routine is invoked in interrupt context. Note: mca.c enables
216 * interrupts before calling this code for CMC/CPE. MCA and INIT events are
217 * not irq safe, do not call any routines that use spinlocks, they may deadlock.
218 * MCA and INIT records are recorded, a timer event will look for any
219 * outstanding events and wake up the user space code.
220 *
221 * The buffer passed from mca.c points to the output from ia64_log_get. This is
222 * a persistent buffer but its contents can change between the interrupt and
223 * when user space processes the record. Save the record id to identify
Keith Owens289d7732005-09-11 17:21:46 +1000224 * changes. If the buffer is NULL then just update the bitmap.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
226void
227salinfo_log_wakeup(int type, u8 *buffer, u64 size, int irqsafe)
228{
229 struct salinfo_data *data = salinfo_data + type;
230 struct salinfo_data_saved *data_saved;
231 unsigned long flags = 0;
232 int i;
233 int saved_size = ARRAY_SIZE(data->data_saved);
234
235 BUG_ON(type >= ARRAY_SIZE(salinfo_log_name));
236
Keith Owense026cca2006-01-06 10:36:06 +1100237 if (irqsafe)
238 spin_lock_irqsave(&data_saved_lock, flags);
Keith Owens289d7732005-09-11 17:21:46 +1000239 if (buffer) {
Keith Owens289d7732005-09-11 17:21:46 +1000240 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
241 if (!data_saved->buffer)
242 break;
243 }
244 if (i == saved_size) {
245 if (!data->saved_num) {
246 shift1_data_saved(data, 0);
247 data_saved = data->data_saved + saved_size - 1;
248 } else
249 data_saved = NULL;
250 }
251 if (data_saved) {
252 data_saved->cpu = smp_processor_id();
253 data_saved->id = ((sal_log_record_header_t *)buffer)->id;
254 data_saved->size = size;
255 data_saved->buffer = buffer;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Keith Owense026cca2006-01-06 10:36:06 +1100258 cpu_set(smp_processor_id(), data->cpu_event);
259 if (irqsafe) {
260 salinfo_work_to_do(data);
261 spin_unlock_irqrestore(&data_saved_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263}
264
265/* Check for outstanding MCA/INIT records every minute (arbitrary) */
266#define SALINFO_TIMER_DELAY (60*HZ)
267static struct timer_list salinfo_timer;
Hidetoshi Seto43ed3ba2006-09-26 14:44:37 -0700268extern void ia64_mlogbuf_dump(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270static void
271salinfo_timeout_check(struct salinfo_data *data)
272{
Keith Owense026cca2006-01-06 10:36:06 +1100273 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (!data->open)
275 return;
Keith Owense026cca2006-01-06 10:36:06 +1100276 if (!cpus_empty(data->cpu_event)) {
277 spin_lock_irqsave(&data_saved_lock, flags);
278 salinfo_work_to_do(data);
279 spin_unlock_irqrestore(&data_saved_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281}
282
Keith Owense026cca2006-01-06 10:36:06 +1100283static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284salinfo_timeout (unsigned long arg)
285{
Hidetoshi Seto43ed3ba2006-09-26 14:44:37 -0700286 ia64_mlogbuf_dump();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_MCA);
288 salinfo_timeout_check(salinfo_data + SAL_INFO_TYPE_INIT);
289 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
290 add_timer(&salinfo_timer);
291}
292
293static int
294salinfo_event_open(struct inode *inode, struct file *file)
295{
296 if (!capable(CAP_SYS_ADMIN))
297 return -EPERM;
298 return 0;
299}
300
301static ssize_t
302salinfo_event_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
303{
Josef Sipekb66ffad2006-12-08 02:37:09 -0800304 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct proc_dir_entry *entry = PDE(inode);
306 struct salinfo_data *data = entry->data;
307 char cmd[32];
308 size_t size;
309 int i, n, cpu = -1;
310
311retry:
Keith Owense026cca2006-01-06 10:36:06 +1100312 if (cpus_empty(data->cpu_event) && down_trylock(&data->mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (file->f_flags & O_NONBLOCK)
314 return -EAGAIN;
Keith Owense026cca2006-01-06 10:36:06 +1100315 if (down_interruptible(&data->mutex))
Keith Owens05f70392005-12-02 13:40:15 +1100316 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 n = data->cpu_check;
Rusty Russell5dd3c992009-03-16 14:12:42 +1030320 for (i = 0; i < nr_cpu_ids; i++) {
Keith Owense026cca2006-01-06 10:36:06 +1100321 if (cpu_isset(n, data->cpu_event)) {
322 if (!cpu_online(n)) {
323 cpu_clear(n, data->cpu_event);
324 continue;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 cpu = n;
327 break;
328 }
Rusty Russell5dd3c992009-03-16 14:12:42 +1030329 if (++n == nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 n = 0;
331 }
332
333 if (cpu == -1)
334 goto retry;
335
Hidetoshi Seto43ed3ba2006-09-26 14:44:37 -0700336 ia64_mlogbuf_dump();
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* for next read, start checking at next CPU */
339 data->cpu_check = cpu;
Rusty Russell5dd3c992009-03-16 14:12:42 +1030340 if (++data->cpu_check == nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 data->cpu_check = 0;
342
343 snprintf(cmd, sizeof(cmd), "read %d\n", cpu);
344
345 size = strlen(cmd);
346 if (size > count)
347 size = count;
348 if (copy_to_user(buffer, cmd, size))
349 return -EFAULT;
350
351 return size;
352}
353
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800354static const struct file_operations salinfo_event_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .open = salinfo_event_open,
356 .read = salinfo_event_read,
357};
358
359static int
360salinfo_log_open(struct inode *inode, struct file *file)
361{
362 struct proc_dir_entry *entry = PDE(inode);
363 struct salinfo_data *data = entry->data;
364
365 if (!capable(CAP_SYS_ADMIN))
366 return -EPERM;
367
368 spin_lock(&data_lock);
369 if (data->open) {
370 spin_unlock(&data_lock);
371 return -EBUSY;
372 }
373 data->open = 1;
374 spin_unlock(&data_lock);
375
376 if (data->state == STATE_NO_DATA &&
377 !(data->log_buffer = vmalloc(ia64_sal_get_state_info_size(data->type)))) {
378 data->open = 0;
379 return -ENOMEM;
380 }
381
382 return 0;
383}
384
385static int
386salinfo_log_release(struct inode *inode, struct file *file)
387{
388 struct proc_dir_entry *entry = PDE(inode);
389 struct salinfo_data *data = entry->data;
390
391 if (data->state == STATE_NO_DATA) {
392 vfree(data->log_buffer);
393 vfree(data->oemdata);
394 data->log_buffer = NULL;
395 data->oemdata = NULL;
396 }
397 spin_lock(&data_lock);
398 data->open = 0;
399 spin_unlock(&data_lock);
400 return 0;
401}
402
403static void
404call_on_cpu(int cpu, void (*fn)(void *), void *arg)
405{
Keith Owense026cca2006-01-06 10:36:06 +1100406 cpumask_t save_cpus_allowed = current->cpus_allowed;
407 cpumask_t new_cpus_allowed = cpumask_of_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 set_cpus_allowed(current, new_cpus_allowed);
409 (*fn)(arg);
410 set_cpus_allowed(current, save_cpus_allowed);
411}
412
413static void
414salinfo_log_read_cpu(void *context)
415{
416 struct salinfo_data *data = context;
417 sal_log_record_header_t *rh;
418 data->log_size = ia64_sal_get_state_info(data->type, (u64 *) data->log_buffer);
419 rh = (sal_log_record_header_t *)(data->log_buffer);
420 /* Clear corrected errors as they are read from SAL */
421 if (rh->severity == sal_log_severity_corrected)
422 ia64_sal_clear_state_info(data->type);
423}
424
425static void
426salinfo_log_new_read(int cpu, struct salinfo_data *data)
427{
428 struct salinfo_data_saved *data_saved;
429 unsigned long flags;
430 int i;
431 int saved_size = ARRAY_SIZE(data->data_saved);
432
433 data->saved_num = 0;
434 spin_lock_irqsave(&data_saved_lock, flags);
435retry:
436 for (i = 0, data_saved = data->data_saved; i < saved_size; ++i, ++data_saved) {
437 if (data_saved->buffer && data_saved->cpu == cpu) {
438 sal_log_record_header_t *rh = (sal_log_record_header_t *)(data_saved->buffer);
439 data->log_size = data_saved->size;
440 memcpy(data->log_buffer, rh, data->log_size);
441 barrier(); /* id check must not be moved */
442 if (rh->id == data_saved->id) {
443 data->saved_num = i+1;
444 break;
445 }
446 /* saved record changed by mca.c since interrupt, discard it */
447 shift1_data_saved(data, i);
448 goto retry;
449 }
450 }
451 spin_unlock_irqrestore(&data_saved_lock, flags);
452
453 if (!data->saved_num)
454 call_on_cpu(cpu, salinfo_log_read_cpu, data);
455 if (!data->log_size) {
Keith Owense026cca2006-01-06 10:36:06 +1100456 data->state = STATE_NO_DATA;
457 cpu_clear(cpu, data->cpu_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else {
Keith Owense026cca2006-01-06 10:36:06 +1100459 data->state = STATE_LOG_RECORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461}
462
463static ssize_t
464salinfo_log_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
465{
Josef Sipekb66ffad2006-12-08 02:37:09 -0800466 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct proc_dir_entry *entry = PDE(inode);
468 struct salinfo_data *data = entry->data;
469 u8 *buf;
470 u64 bufsize;
471
472 if (data->state == STATE_LOG_RECORD) {
473 buf = data->log_buffer;
474 bufsize = data->log_size;
475 } else if (data->state == STATE_OEMDATA) {
476 buf = data->oemdata;
477 bufsize = data->oemdata_size;
478 } else {
479 buf = NULL;
480 bufsize = 0;
481 }
482 return simple_read_from_buffer(buffer, count, ppos, buf, bufsize);
483}
484
485static void
486salinfo_log_clear_cpu(void *context)
487{
488 struct salinfo_data *data = context;
489 ia64_sal_clear_state_info(data->type);
490}
491
492static int
493salinfo_log_clear(struct salinfo_data *data, int cpu)
494{
495 sal_log_record_header_t *rh;
Keith Owense026cca2006-01-06 10:36:06 +1100496 unsigned long flags;
497 spin_lock_irqsave(&data_saved_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 data->state = STATE_NO_DATA;
Keith Owense026cca2006-01-06 10:36:06 +1100499 if (!cpu_isset(cpu, data->cpu_event)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spin_unlock_irqrestore(&data_saved_lock, flags);
Keith Owense026cca2006-01-06 10:36:06 +1100501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Keith Owense026cca2006-01-06 10:36:06 +1100503 cpu_clear(cpu, data->cpu_event);
504 if (data->saved_num) {
505 shift1_data_saved(data, data->saved_num - 1);
506 data->saved_num = 0;
507 }
508 spin_unlock_irqrestore(&data_saved_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 rh = (sal_log_record_header_t *)(data->log_buffer);
510 /* Corrected errors have already been cleared from SAL */
511 if (rh->severity != sal_log_severity_corrected)
512 call_on_cpu(cpu, salinfo_log_clear_cpu, data);
513 /* clearing a record may make a new record visible */
514 salinfo_log_new_read(cpu, data);
Keith Owense026cca2006-01-06 10:36:06 +1100515 if (data->state == STATE_LOG_RECORD) {
516 spin_lock_irqsave(&data_saved_lock, flags);
517 cpu_set(cpu, data->cpu_event);
518 salinfo_work_to_do(data);
519 spin_unlock_irqrestore(&data_saved_lock, flags);
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522}
523
524static ssize_t
525salinfo_log_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
526{
Josef Sipekb66ffad2006-12-08 02:37:09 -0800527 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct proc_dir_entry *entry = PDE(inode);
529 struct salinfo_data *data = entry->data;
530 char cmd[32];
531 size_t size;
532 u32 offset;
533 int cpu;
534
535 size = sizeof(cmd);
536 if (count < size)
537 size = count;
538 if (copy_from_user(cmd, buffer, size))
539 return -EFAULT;
540
541 if (sscanf(cmd, "read %d", &cpu) == 1) {
542 salinfo_log_new_read(cpu, data);
543 } else if (sscanf(cmd, "clear %d", &cpu) == 1) {
544 int ret;
545 if ((ret = salinfo_log_clear(data, cpu)))
546 count = ret;
547 } else if (sscanf(cmd, "oemdata %d %d", &cpu, &offset) == 2) {
548 if (data->state != STATE_LOG_RECORD && data->state != STATE_OEMDATA)
549 return -EINVAL;
550 if (offset > data->log_size - sizeof(efi_guid_t))
551 return -EINVAL;
552 data->state = STATE_OEMDATA;
553 if (salinfo_platform_oemdata) {
554 struct salinfo_platform_oemdata_parms parms = {
555 .efi_guid = data->log_buffer + offset,
556 .oemdata = &data->oemdata,
557 .oemdata_size = &data->oemdata_size
558 };
559 call_on_cpu(cpu, salinfo_platform_oemdata_cpu, &parms);
560 if (parms.ret)
561 count = parms.ret;
562 } else
563 data->oemdata_size = 0;
564 } else
565 return -EINVAL;
566
567 return count;
568}
569
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800570static const struct file_operations salinfo_data_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 .open = salinfo_log_open,
572 .release = salinfo_log_release,
573 .read = salinfo_log_read,
574 .write = salinfo_log_write,
575};
576
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700577static int __cpuinit
Keith Owense026cca2006-01-06 10:36:06 +1100578salinfo_cpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu)
579{
580 unsigned int i, cpu = (unsigned long)hcpu;
581 unsigned long flags;
582 struct salinfo_data *data;
583 switch (action) {
584 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700585 case CPU_ONLINE_FROZEN:
Keith Owense026cca2006-01-06 10:36:06 +1100586 spin_lock_irqsave(&data_saved_lock, flags);
587 for (i = 0, data = salinfo_data;
588 i < ARRAY_SIZE(salinfo_data);
589 ++i, ++data) {
590 cpu_set(cpu, data->cpu_event);
591 salinfo_work_to_do(data);
592 }
593 spin_unlock_irqrestore(&data_saved_lock, flags);
594 break;
595 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700596 case CPU_DEAD_FROZEN:
Keith Owense026cca2006-01-06 10:36:06 +1100597 spin_lock_irqsave(&data_saved_lock, flags);
598 for (i = 0, data = salinfo_data;
599 i < ARRAY_SIZE(salinfo_data);
600 ++i, ++data) {
601 struct salinfo_data_saved *data_saved;
602 int j;
603 for (j = ARRAY_SIZE(data->data_saved) - 1, data_saved = data->data_saved + j;
604 j >= 0;
605 --j, --data_saved) {
606 if (data_saved->buffer && data_saved->cpu == cpu) {
607 shift1_data_saved(data, j);
608 }
609 }
610 cpu_clear(cpu, data->cpu_event);
611 }
612 spin_unlock_irqrestore(&data_saved_lock, flags);
613 break;
614 }
615 return NOTIFY_OK;
616}
617
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700618static struct notifier_block salinfo_cpu_notifier __cpuinitdata =
Keith Owense026cca2006-01-06 10:36:06 +1100619{
620 .notifier_call = salinfo_cpu_callback,
621 .priority = 0,
622};
Keith Owense026cca2006-01-06 10:36:06 +1100623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static int __init
625salinfo_init(void)
626{
627 struct proc_dir_entry *salinfo_dir; /* /proc/sal dir entry */
628 struct proc_dir_entry **sdir = salinfo_proc_entries; /* keeps track of every entry */
629 struct proc_dir_entry *dir, *entry;
630 struct salinfo_data *data;
Keith Owense026cca2006-01-06 10:36:06 +1100631 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 salinfo_dir = proc_mkdir("sal", NULL);
634 if (!salinfo_dir)
635 return 0;
636
637 for (i=0; i < NR_SALINFO_ENTRIES; i++) {
638 /* pass the feature bit in question as misc data */
639 *sdir++ = create_proc_read_entry (salinfo_entries[i].name, 0, salinfo_dir,
640 salinfo_read, (void *)salinfo_entries[i].feature);
641 }
642
643 for (i = 0; i < ARRAY_SIZE(salinfo_log_name); i++) {
644 data = salinfo_data + i;
645 data->type = i;
Keith Owense026cca2006-01-06 10:36:06 +1100646 init_MUTEX(&data->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 dir = proc_mkdir(salinfo_log_name[i], salinfo_dir);
648 if (!dir)
649 continue;
650
Denis V. Luneve2363762008-04-29 01:02:25 -0700651 entry = proc_create_data("event", S_IRUSR, dir,
652 &salinfo_event_fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (!entry)
654 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *sdir++ = entry;
656
Denis V. Luneve2363762008-04-29 01:02:25 -0700657 entry = proc_create_data("data", S_IRUSR | S_IWUSR, dir,
658 &salinfo_data_fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!entry)
660 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 *sdir++ = entry;
662
663 /* we missed any events before now */
Keith Owense026cca2006-01-06 10:36:06 +1100664 for_each_online_cpu(j)
665 cpu_set(j, data->cpu_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 *sdir++ = dir;
668 }
669
670 *sdir++ = salinfo_dir;
671
672 init_timer(&salinfo_timer);
673 salinfo_timer.expires = jiffies + SALINFO_TIMER_DELAY;
674 salinfo_timer.function = &salinfo_timeout;
675 add_timer(&salinfo_timer);
676
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -0700677 register_hotcpu_notifier(&salinfo_cpu_notifier);
Keith Owense026cca2006-01-06 10:36:06 +1100678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 0;
680}
681
682/*
683 * 'data' contains an integer that corresponds to the feature we're
684 * testing
685 */
686static int
687salinfo_read(char *page, char **start, off_t off, int count, int *eof, void *data)
688{
689 int len = 0;
690
691 len = sprintf(page, (sal_platform_features & (unsigned long)data) ? "1\n" : "0\n");
692
693 if (len <= off+count) *eof = 1;
694
695 *start = page + off;
696 len -= off;
697
698 if (len>count) len = count;
699 if (len<0) len = 0;
700
701 return len;
702}
703
704module_init(salinfo_init);