1 /*
2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Communication to userspace based on kernel/printk.c
10 */
11
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/init.h>
19 #include <linux/vmalloc.h>
20 #include <linux/spinlock.h>
21
22 #include <asm/uaccess.h>
23 #include <asm/io.h>
24 #include <asm/rtas.h>
25 #include <asm/prom.h>
26 #include <asm/nvram.h>
27 #include <asm/atomic.h>
28
29 #if 0
30 #define DEBUG(A...) printk(KERN_ERR A)
31 #else
32 #define DEBUG(A...)
33 #endif
34
35 static spinlock_t log_lock = SPIN_LOCK_UNLOCKED;
36
37 DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
38
39 static char *rtas_log_buf;
40 static unsigned long rtas_log_start;
41 static unsigned long rtas_log_size;
42
43 static int surveillance_timeout = -1;
44 static unsigned int rtas_event_scan_rate;
45 static unsigned int rtas_error_log_max;
46
47 static unsigned int rtas_error_log_buffer_max;
48
49 extern struct proc_dir_entry *proc_ppc64_root;
50 extern struct proc_dir_entry *rtas_proc_dir;
51 extern spinlock_t proc_ppc64_lock;
52 extern volatile int no_more_logging;
53
54 volatile int error_log_cnt = 0;
55
56 /*
57 * Since we use 32 bit RTAS, the physical address of this must be below
58 * 4G or else bad things happen. Allocate this in the kernel data and
59 * make it big enough.
60 */
61 static unsigned char logdata[RTAS_ERROR_LOG_MAX];
62
63 /* To see this info, grep RTAS /var/log/messages and each entry
64 * will be collected together with obvious begin/end.
65 * There will be a unique identifier on the begin and end lines.
66 * This will persist across reboots.
67 *
68 * format of error logs returned from RTAS:
69 * bytes (size) : contents
70 * --------------------------------------------------------
71 * 0-7 (8) : rtas_error_log
72 * 8-47 (40) : extended info
73 * 48-51 (4) : vendor id
74 * 52-1023 (vendor specific) : location code and debug data
75 */
printk_log_rtas(char * buf,int len)76 static void printk_log_rtas(char *buf, int len)
77 {
78
79 int i,j,n;
80 int perline = 16;
81 char buffer[64];
82 char * str = "RTAS event";
83
84 printk(RTAS_ERR "%d -------- %s begin --------\n", error_log_cnt, str);
85
86 /*
87 * Print perline bytes on each line, each line will start
88 * with RTAS and a changing number, so syslogd will
89 * print lines that are otherwise the same. Separate every
90 * 4 bytes with a space.
91 */
92 for (i=0; i < len; i++) {
93 j = i % perline;
94 if (j == 0) {
95 memset(buffer, 0, sizeof(buffer));
96 n = sprintf(buffer, "RTAS %d:", i/perline);
97 }
98
99 if ((i % 4) == 0)
100 n += sprintf(buffer+n, " ");
101
102 n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
103
104 if (j == (perline-1))
105 printk(KERN_ERR "%s\n", buffer);
106 }
107 if ((i % perline) != 0)
108 printk(KERN_ERR "%s\n", buffer);
109
110 printk(RTAS_ERR "%d -------- %s end ----------\n", error_log_cnt, str);
111 }
112
log_rtas_len(char * buf)113 static int log_rtas_len(char * buf)
114 {
115 int len;
116 struct rtas_error_log *err;
117
118 /* rtas fixed header */
119 len = 8;
120 err = (struct rtas_error_log *)buf;
121 if (err->extended_log_length) {
122
123 /* extended header */
124 len += err->extended_log_length;
125
126 if (len > RTAS_ERROR_LOG_MAX)
127 len = RTAS_ERROR_LOG_MAX;
128 }
129 return len;
130 }
131
132 /*
133 * First write to nvram, if fatal error, that is the only
134 * place we log the info. The error will be picked up
135 * on the next reboot by rtasd. If not fatal, run the
136 * method for the type of error. Currently, only RTAS
137 * errors have methods implemented, but in the future
138 * there might be a need to store data in nvram before a
139 * call to panic().
140 */
pSeries_log_error(char * buf,unsigned int err_type,int fatal)141 void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
142 {
143 unsigned long offset;
144 unsigned long s;
145 int len = 0;
146
147 DEBUG("logging event\n");
148
149 if (buf == NULL)
150 return;
151
152 spin_lock_irqsave(&log_lock, s);
153
154 /* get length and increase count */
155 switch (err_type & ERR_TYPE_MASK) {
156 case ERR_TYPE_RTAS_LOG:
157 len = log_rtas_len(buf);
158 if (!(err_type & ERR_FLAG_BOOT))
159 error_log_cnt++;
160 break;
161 case ERR_TYPE_KERNEL_PANIC:
162 default:
163 spin_unlock_irqrestore(&log_lock, s);
164 return;
165 }
166
167 /* Write error to NVRAM */
168 if (!no_more_logging && !(err_type & ERR_FLAG_BOOT)) {
169 write_error_log_nvram(buf, len, err_type);
170 }
171
172 /* Check to see if we need to or have stopped logging */
173 if (fatal || no_more_logging) {
174 no_more_logging = 1;
175 spin_unlock_irqrestore(&log_lock, s);
176 return;
177 }
178
179 /* call type specific method for error */
180 switch (err_type & ERR_TYPE_MASK) {
181 case ERR_TYPE_RTAS_LOG:
182 /* put into syslog and error_log file */
183 printk_log_rtas(buf, len);
184
185 offset = rtas_error_log_buffer_max *
186 ((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
187
188 /* First copy over sequence number */
189 memcpy(&rtas_log_buf[offset], &error_log_cnt, sizeof(int));
190
191 /* Second copy over error log data */
192 offset += sizeof(int);
193 memcpy(&rtas_log_buf[offset], buf, len);
194
195 if (rtas_log_size < LOG_NUMBER)
196 rtas_log_size += 1;
197 else
198 rtas_log_start += 1;
199
200 spin_unlock_irqrestore(&log_lock, s);
201 wake_up_interruptible(&rtas_log_wait);
202 break;
203 case ERR_TYPE_KERNEL_PANIC:
204 default:
205 spin_unlock_irqrestore(&log_lock, s);
206 return;
207 }
208
209 }
rtas_log_open(struct inode * inode,struct file * file)210 static int rtas_log_open(struct inode * inode, struct file * file)
211 {
212 return 0;
213 }
214
rtas_log_release(struct inode * inode,struct file * file)215 static int rtas_log_release(struct inode * inode, struct file * file)
216 {
217 return 0;
218 }
219
220 /* This will check if all events are logged, if they are then, we
221 * know that we can safely clear the events in NVRAM.
222 * Next we'll sit and wait for something else to log.
223 */
rtas_log_read(struct file * file,char * buf,size_t count,loff_t * ppos)224 static ssize_t rtas_log_read(struct file * file, char * buf,
225 size_t count, loff_t *ppos)
226 {
227 int error;
228 char *tmp;
229 unsigned long s;
230 unsigned long offset;
231
232 if (!buf || count < rtas_error_log_buffer_max)
233 return -EINVAL;
234
235 count = rtas_error_log_buffer_max;
236
237 error = verify_area(VERIFY_WRITE, buf, count);
238 if (error)
239 return -EFAULT;
240
241 tmp = kmalloc(count, GFP_KERNEL);
242 if (!tmp)
243 return -ENOMEM;
244
245
246 spin_lock_irqsave(&log_lock, s);
247 /* if it's 0, then we know we got the last one (the one in NVRAM) */
248 if (rtas_log_size == 0 && !no_more_logging)
249 clear_error_log_nvram();
250 spin_unlock_irqrestore(&log_lock, s);
251
252
253 error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
254 if (error)
255 goto out;
256
257 spin_lock_irqsave(&log_lock, s);
258 offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
259 memcpy(tmp, &rtas_log_buf[offset], count);
260
261 rtas_log_start += 1;
262 rtas_log_size -= 1;
263 spin_unlock_irqrestore(&log_lock, s);
264
265 error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
266 out:
267 kfree(tmp);
268 return error;
269 }
270
rtas_log_poll(struct file * file,poll_table * wait)271 static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
272 {
273 poll_wait(file, &rtas_log_wait, wait);
274 if (rtas_log_size)
275 return POLLIN | POLLRDNORM;
276 return 0;
277 }
278
279 struct file_operations proc_rtas_log_operations = {
280 .read = rtas_log_read,
281 .poll = rtas_log_poll,
282 .open = rtas_log_open,
283 .release = rtas_log_release,
284 };
285
enable_surveillance(int timeout)286 static int enable_surveillance(int timeout)
287 {
288 int error;
289
290 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL, SURVEILLANCE_TOKEN,
291 0, timeout);
292
293 if (error) {
294 printk(KERN_ERR "rtasd: could not enable surveillance\n");
295 return -1;
296 }
297
298 return 0;
299 }
300
get_eventscan_parms(void)301 static int get_eventscan_parms(void)
302 {
303 struct device_node *node;
304 int *ip;
305
306 node = find_path_device("/rtas");
307
308 ip = (int *)get_property(node, "rtas-event-scan-rate", NULL);
309 if (ip == NULL) {
310 printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
311 return -1;
312 }
313 rtas_event_scan_rate = *ip;
314 DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);
315
316 ip = (int *)get_property(node, "rtas-error-log-max", NULL);
317 if (ip == NULL) {
318 printk(KERN_ERR "rtasd: no rtas-error-log-max\n");
319 return -1;
320 }
321 rtas_error_log_max = *ip;
322 DEBUG("rtas-error-log-max %d\n", rtas_error_log_max);
323
324 if (rtas_error_log_max > RTAS_ERROR_LOG_MAX) {
325 printk(KERN_ERR "rtasd: truncated error log from %d to %d bytes\n", rtas_error_log_max, RTAS_ERROR_LOG_MAX);
326 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
327 }
328
329 /* Make room for the sequence number */
330 rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
331
332 return 0;
333 }
334
335 extern long sys_sched_get_priority_max(int policy);
336
rtasd(void * unused)337 static int rtasd(void *unused)
338 {
339 unsigned int err_type;
340 unsigned long s;
341 int cpu = 0;
342 int error;
343 int first_pass = 1;
344 int event_scan = rtas_token("event-scan");
345 int rc;
346
347 if (event_scan == RTAS_UNKNOWN_SERVICE || get_eventscan_parms() == -1)
348 goto error;
349
350 rtas_log_buf = vmalloc(rtas_error_log_buffer_max*LOG_NUMBER);
351 if (!rtas_log_buf) {
352 printk(KERN_ERR "rtasd: no memory\n");
353 goto error;
354 }
355
356 no_more_logging = 0;
357
358 DEBUG("will sleep for %d jiffies\n", (HZ*60/rtas_event_scan_rate) / 2);
359
360 daemonize();
361 sigfillset(¤t->blocked);
362 sprintf(current->comm, "rtasd");
363
364 /* Rusty unreal time task */
365 current->policy = SCHED_FIFO;
366 current->nice = sys_sched_get_priority_max(SCHED_FIFO) + 1;
367
368 cpu = 0;
369 current->cpus_allowed = 1UL << cpu_logical_map(cpu);
370 schedule();
371
372 /* See if we have any error stored in NVRAM */
373 memset(logdata, 0, rtas_error_log_max);
374
375 rc = read_error_log_nvram(logdata, rtas_error_log_max, &err_type);
376 if (!rc) {
377 if (err_type != ERR_FLAG_ALREADY_LOGGED) {
378 pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
379 }
380 }
381
382 while(1) {
383
384
385 do {
386 memset(logdata, 0, rtas_error_log_max);
387 error = rtas_call(event_scan, 4, 1, NULL,
388 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
389 __pa(logdata), rtas_error_log_max);
390 if (error == -1) {
391 printk(KERN_ERR "event-scan failed\n");
392 break;
393 }
394
395 if (error == 0)
396 pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
397
398 } while(error == 0);
399
400 DEBUG("watchdog scheduled on cpu %d\n", smp_processor_id());
401
402 cpu++;
403 if (cpu >= smp_num_cpus) {
404
405 if (first_pass && (surveillance_timeout != -1)) {
406 DEBUG("enabling surveillance\n");
407 if (enable_surveillance(surveillance_timeout))
408 goto error_vfree;
409 DEBUG("surveillance enabled\n");
410 }
411
412 first_pass = 0;
413 cpu = 0;
414 }
415
416 current->cpus_allowed = 1UL << cpu_logical_map(cpu);
417
418 /* Check all cpus for pending events before sleeping*/
419 set_current_state(TASK_INTERRUPTIBLE);
420 schedule_timeout(first_pass ? HZ : (HZ*60/rtas_event_scan_rate) / 2);
421 }
422
423 error_vfree:
424 vfree(rtas_log_buf);
425 error:
426 /* Should delete proc entries */
427 return -EINVAL;
428 }
429
rtas_init(void)430 static int __init rtas_init(void)
431 {
432 int ret = 0;
433 struct proc_dir_entry *entry;
434
435 spin_lock(&proc_ppc64_lock);
436 if (proc_ppc64_root == NULL) {
437 proc_ppc64_root = proc_mkdir("ppc64", 0);
438 if (!proc_ppc64_root) {
439 spin_unlock(&proc_ppc64_lock);
440 return -EINVAL;
441 }
442 }
443 spin_unlock(&proc_ppc64_lock);
444
445 if (rtas_proc_dir == NULL) {
446 rtas_proc_dir = proc_mkdir("rtas", proc_ppc64_root);
447 }
448
449 if (rtas_proc_dir == NULL) {
450 printk(KERN_ERR "Failed to create /proc/ppc64/rtas in rtas_init\n");
451 ret = -EINVAL;
452 } else {
453 entry = create_proc_entry("error_log", S_IRUSR, rtas_proc_dir);
454 if (entry)
455 entry->proc_fops = &proc_rtas_log_operations;
456 else {
457 printk(KERN_ERR "Failed to create rtas/error_log proc entry\n");
458 ret = -EINVAL;
459 }
460 }
461
462 if (kernel_thread(rtasd, 0, CLONE_FS) < 0) {
463 printk(KERN_ERR "Failed to start RTAS daemon\n");
464 ret = -EINVAL;
465 }
466
467 printk(KERN_ERR "RTAS daemon started\n");
468 return ret;
469 }
470
surveillance_setup(char * str)471 static int __init surveillance_setup(char *str)
472 {
473 int i;
474
475 if (get_option(&str,&i)) {
476 if (i >= 0 && i <= 255)
477 surveillance_timeout = i;
478 }
479
480 return 1;
481 }
482
483 __initcall(rtas_init);
484 __setup("surveillance=", surveillance_setup);
485