rtasd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/poll.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/init.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/cpu.h>
  21. #include <linux/delay.h>
  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. #include <asm/machdep.h>
  29. #if 0
  30. #define DEBUG(A...) printk(KERN_ERR A)
  31. #else
  32. #define DEBUG(A...)
  33. #endif
  34. static DEFINE_SPINLOCK(rtasd_log_lock);
  35. DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
  36. static char *rtas_log_buf;
  37. static unsigned long rtas_log_start;
  38. static unsigned long rtas_log_size;
  39. static int surveillance_timeout = -1;
  40. static unsigned int rtas_error_log_max;
  41. static unsigned int rtas_error_log_buffer_max;
  42. /* RTAS service tokens */
  43. static unsigned int event_scan;
  44. static unsigned int rtas_event_scan_rate;
  45. static int full_rtas_msgs = 0;
  46. extern int no_logging;
  47. volatile int error_log_cnt = 0;
  48. /*
  49. * Since we use 32 bit RTAS, the physical address of this must be below
  50. * 4G or else bad things happen. Allocate this in the kernel data and
  51. * make it big enough.
  52. */
  53. static unsigned char logdata[RTAS_ERROR_LOG_MAX];
  54. static int get_eventscan_parms(void);
  55. static char *rtas_type[] = {
  56. "Unknown", "Retry", "TCE Error", "Internal Device Failure",
  57. "Timeout", "Data Parity", "Address Parity", "Cache Parity",
  58. "Address Invalid", "ECC Uncorrected", "ECC Corrupted",
  59. };
  60. static char *rtas_event_type(int type)
  61. {
  62. if ((type > 0) && (type < 11))
  63. return rtas_type[type];
  64. switch (type) {
  65. case RTAS_TYPE_EPOW:
  66. return "EPOW";
  67. case RTAS_TYPE_PLATFORM:
  68. return "Platform Error";
  69. case RTAS_TYPE_IO:
  70. return "I/O Event";
  71. case RTAS_TYPE_INFO:
  72. return "Platform Information Event";
  73. case RTAS_TYPE_DEALLOC:
  74. return "Resource Deallocation Event";
  75. case RTAS_TYPE_DUMP:
  76. return "Dump Notification Event";
  77. }
  78. return rtas_type[0];
  79. }
  80. /* To see this info, grep RTAS /var/log/messages and each entry
  81. * will be collected together with obvious begin/end.
  82. * There will be a unique identifier on the begin and end lines.
  83. * This will persist across reboots.
  84. *
  85. * format of error logs returned from RTAS:
  86. * bytes (size) : contents
  87. * --------------------------------------------------------
  88. * 0-7 (8) : rtas_error_log
  89. * 8-47 (40) : extended info
  90. * 48-51 (4) : vendor id
  91. * 52-1023 (vendor specific) : location code and debug data
  92. */
  93. static void printk_log_rtas(char *buf, int len)
  94. {
  95. int i,j,n = 0;
  96. int perline = 16;
  97. char buffer[64];
  98. char * str = "RTAS event";
  99. if (full_rtas_msgs) {
  100. printk(RTAS_DEBUG "%d -------- %s begin --------\n",
  101. error_log_cnt, str);
  102. /*
  103. * Print perline bytes on each line, each line will start
  104. * with RTAS and a changing number, so syslogd will
  105. * print lines that are otherwise the same. Separate every
  106. * 4 bytes with a space.
  107. */
  108. for (i = 0; i < len; i++) {
  109. j = i % perline;
  110. if (j == 0) {
  111. memset(buffer, 0, sizeof(buffer));
  112. n = sprintf(buffer, "RTAS %d:", i/perline);
  113. }
  114. if ((i % 4) == 0)
  115. n += sprintf(buffer+n, " ");
  116. n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
  117. if (j == (perline-1))
  118. printk(KERN_DEBUG "%s\n", buffer);
  119. }
  120. if ((i % perline) != 0)
  121. printk(KERN_DEBUG "%s\n", buffer);
  122. printk(RTAS_DEBUG "%d -------- %s end ----------\n",
  123. error_log_cnt, str);
  124. } else {
  125. struct rtas_error_log *errlog = (struct rtas_error_log *)buf;
  126. printk(RTAS_DEBUG "event: %d, Type: %s, Severity: %d\n",
  127. error_log_cnt, rtas_event_type(errlog->type),
  128. errlog->severity);
  129. }
  130. }
  131. static int log_rtas_len(char * buf)
  132. {
  133. int len;
  134. struct rtas_error_log *err;
  135. /* rtas fixed header */
  136. len = 8;
  137. err = (struct rtas_error_log *)buf;
  138. if (err->extended_log_length) {
  139. /* extended header */
  140. len += err->extended_log_length;
  141. }
  142. if (rtas_error_log_max == 0) {
  143. get_eventscan_parms();
  144. }
  145. if (len > rtas_error_log_max)
  146. len = rtas_error_log_max;
  147. return len;
  148. }
  149. /*
  150. * First write to nvram, if fatal error, that is the only
  151. * place we log the info. The error will be picked up
  152. * on the next reboot by rtasd. If not fatal, run the
  153. * method for the type of error. Currently, only RTAS
  154. * errors have methods implemented, but in the future
  155. * there might be a need to store data in nvram before a
  156. * call to panic().
  157. *
  158. * XXX We write to nvram periodically, to indicate error has
  159. * been written and sync'd, but there is a possibility
  160. * that if we don't shutdown correctly, a duplicate error
  161. * record will be created on next reboot.
  162. */
  163. void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
  164. {
  165. unsigned long offset;
  166. unsigned long s;
  167. int len = 0;
  168. DEBUG("logging event\n");
  169. if (buf == NULL)
  170. return;
  171. spin_lock_irqsave(&rtasd_log_lock, s);
  172. /* get length and increase count */
  173. switch (err_type & ERR_TYPE_MASK) {
  174. case ERR_TYPE_RTAS_LOG:
  175. len = log_rtas_len(buf);
  176. if (!(err_type & ERR_FLAG_BOOT))
  177. error_log_cnt++;
  178. break;
  179. case ERR_TYPE_KERNEL_PANIC:
  180. default:
  181. spin_unlock_irqrestore(&rtasd_log_lock, s);
  182. return;
  183. }
  184. /* Write error to NVRAM */
  185. if (!no_logging && !(err_type & ERR_FLAG_BOOT))
  186. nvram_write_error_log(buf, len, err_type);
  187. /*
  188. * rtas errors can occur during boot, and we do want to capture
  189. * those somewhere, even if nvram isn't ready (why not?), and even
  190. * if rtasd isn't ready. Put them into the boot log, at least.
  191. */
  192. if ((err_type & ERR_TYPE_MASK) == ERR_TYPE_RTAS_LOG)
  193. printk_log_rtas(buf, len);
  194. /* Check to see if we need to or have stopped logging */
  195. if (fatal || no_logging) {
  196. no_logging = 1;
  197. spin_unlock_irqrestore(&rtasd_log_lock, s);
  198. return;
  199. }
  200. /* call type specific method for error */
  201. switch (err_type & ERR_TYPE_MASK) {
  202. case ERR_TYPE_RTAS_LOG:
  203. offset = rtas_error_log_buffer_max *
  204. ((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
  205. /* First copy over sequence number */
  206. memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));
  207. /* Second copy over error log data */
  208. offset += sizeof(int);
  209. memcpy(&rtas_log_buf[offset], buf, len);
  210. if (rtas_log_size < LOG_NUMBER)
  211. rtas_log_size += 1;
  212. else
  213. rtas_log_start += 1;
  214. spin_unlock_irqrestore(&rtasd_log_lock, s);
  215. wake_up_interruptible(&rtas_log_wait);
  216. break;
  217. case ERR_TYPE_KERNEL_PANIC:
  218. default:
  219. spin_unlock_irqrestore(&rtasd_log_lock, s);
  220. return;
  221. }
  222. }
  223. static int rtas_log_open(struct inode * inode, struct file * file)
  224. {
  225. return 0;
  226. }
  227. static int rtas_log_release(struct inode * inode, struct file * file)
  228. {
  229. return 0;
  230. }
  231. /* This will check if all events are logged, if they are then, we
  232. * know that we can safely clear the events in NVRAM.
  233. * Next we'll sit and wait for something else to log.
  234. */
  235. static ssize_t rtas_log_read(struct file * file, char __user * buf,
  236. size_t count, loff_t *ppos)
  237. {
  238. int error;
  239. char *tmp;
  240. unsigned long s;
  241. unsigned long offset;
  242. if (!buf || count < rtas_error_log_buffer_max)
  243. return -EINVAL;
  244. count = rtas_error_log_buffer_max;
  245. if (!access_ok(VERIFY_WRITE, buf, count))
  246. return -EFAULT;
  247. tmp = kmalloc(count, GFP_KERNEL);
  248. if (!tmp)
  249. return -ENOMEM;
  250. spin_lock_irqsave(&rtasd_log_lock, s);
  251. /* if it's 0, then we know we got the last one (the one in NVRAM) */
  252. if (rtas_log_size == 0 && !no_logging)
  253. nvram_clear_error_log();
  254. spin_unlock_irqrestore(&rtasd_log_lock, s);
  255. error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
  256. if (error)
  257. goto out;
  258. spin_lock_irqsave(&rtasd_log_lock, s);
  259. offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
  260. memcpy(tmp, &rtas_log_buf[offset], count);
  261. rtas_log_start += 1;
  262. rtas_log_size -= 1;
  263. spin_unlock_irqrestore(&rtasd_log_lock, s);
  264. error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
  265. out:
  266. kfree(tmp);
  267. return error;
  268. }
  269. static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
  270. {
  271. poll_wait(file, &rtas_log_wait, wait);
  272. if (rtas_log_size)
  273. return POLLIN | POLLRDNORM;
  274. return 0;
  275. }
  276. const struct file_operations proc_rtas_log_operations = {
  277. .read = rtas_log_read,
  278. .poll = rtas_log_poll,
  279. .open = rtas_log_open,
  280. .release = rtas_log_release,
  281. };
  282. static int enable_surveillance(int timeout)
  283. {
  284. int error;
  285. error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
  286. if (error == 0)
  287. return 0;
  288. if (error == -EINVAL) {
  289. printk(KERN_DEBUG "rtasd: surveillance not supported\n");
  290. return 0;
  291. }
  292. printk(KERN_ERR "rtasd: could not update surveillance\n");
  293. return -1;
  294. }
  295. static int get_eventscan_parms(void)
  296. {
  297. struct device_node *node;
  298. const int *ip;
  299. node = of_find_node_by_path("/rtas");
  300. ip = of_get_property(node, "rtas-event-scan-rate", NULL);
  301. if (ip == NULL) {
  302. printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
  303. of_node_put(node);
  304. return -1;
  305. }
  306. rtas_event_scan_rate = *ip;
  307. DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);
  308. /* Make room for the sequence number */
  309. rtas_error_log_max = rtas_get_error_log_max();
  310. rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
  311. of_node_put(node);
  312. return 0;
  313. }
  314. static void do_event_scan(void)
  315. {
  316. int error;
  317. do {
  318. memset(logdata, 0, rtas_error_log_max);
  319. error = rtas_call(event_scan, 4, 1, NULL,
  320. RTAS_EVENT_SCAN_ALL_EVENTS, 0,
  321. __pa(logdata), rtas_error_log_max);
  322. if (error == -1) {
  323. printk(KERN_ERR "event-scan failed\n");
  324. break;
  325. }
  326. if (error == 0)
  327. pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
  328. } while(error == 0);
  329. }
  330. static void do_event_scan_all_cpus(long delay)
  331. {
  332. int cpu;
  333. lock_cpu_hotplug();
  334. cpu = first_cpu(cpu_online_map);
  335. for (;;) {
  336. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  337. do_event_scan();
  338. set_cpus_allowed(current, CPU_MASK_ALL);
  339. /* Drop hotplug lock, and sleep for the specified delay */
  340. unlock_cpu_hotplug();
  341. msleep_interruptible(delay);
  342. lock_cpu_hotplug();
  343. cpu = next_cpu(cpu, cpu_online_map);
  344. if (cpu == NR_CPUS)
  345. break;
  346. }
  347. unlock_cpu_hotplug();
  348. }
  349. static int rtasd(void *unused)
  350. {
  351. unsigned int err_type;
  352. int rc;
  353. daemonize("rtasd");
  354. if (get_eventscan_parms() == -1)
  355. goto error;
  356. rtas_log_buf = vmalloc(rtas_error_log_buffer_max*LOG_NUMBER);
  357. if (!rtas_log_buf) {
  358. printk(KERN_ERR "rtasd: no memory\n");
  359. goto error;
  360. }
  361. printk(KERN_DEBUG "RTAS daemon started\n");
  362. DEBUG("will sleep for %d milliseconds\n", (30000/rtas_event_scan_rate));
  363. /* See if we have any error stored in NVRAM */
  364. memset(logdata, 0, rtas_error_log_max);
  365. rc = nvram_read_error_log(logdata, rtas_error_log_max, &err_type);
  366. /* We can use rtas_log_buf now */
  367. no_logging = 0;
  368. if (!rc) {
  369. if (err_type != ERR_FLAG_ALREADY_LOGGED) {
  370. pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
  371. }
  372. }
  373. /* First pass. */
  374. do_event_scan_all_cpus(1000);
  375. if (surveillance_timeout != -1) {
  376. DEBUG("enabling surveillance\n");
  377. enable_surveillance(surveillance_timeout);
  378. DEBUG("surveillance enabled\n");
  379. }
  380. /* Delay should be at least one second since some
  381. * machines have problems if we call event-scan too
  382. * quickly. */
  383. for (;;)
  384. do_event_scan_all_cpus(30000/rtas_event_scan_rate);
  385. error:
  386. /* Should delete proc entries */
  387. return -EINVAL;
  388. }
  389. static int __init rtas_init(void)
  390. {
  391. struct proc_dir_entry *entry;
  392. if (!machine_is(pseries))
  393. return 0;
  394. /* No RTAS */
  395. event_scan = rtas_token("event-scan");
  396. if (event_scan == RTAS_UNKNOWN_SERVICE) {
  397. printk(KERN_DEBUG "rtasd: no event-scan on system\n");
  398. return -ENODEV;
  399. }
  400. entry = create_proc_entry("ppc64/rtas/error_log", S_IRUSR, NULL);
  401. if (entry)
  402. entry->proc_fops = &proc_rtas_log_operations;
  403. else
  404. printk(KERN_ERR "Failed to create error_log proc entry\n");
  405. if (kernel_thread(rtasd, NULL, CLONE_FS) < 0)
  406. printk(KERN_ERR "Failed to start RTAS daemon\n");
  407. return 0;
  408. }
  409. static int __init surveillance_setup(char *str)
  410. {
  411. int i;
  412. if (get_option(&str,&i)) {
  413. if (i >= 0 && i <= 255)
  414. surveillance_timeout = i;
  415. }
  416. return 1;
  417. }
  418. static int __init rtasmsgs_setup(char *str)
  419. {
  420. if (strcmp(str, "on") == 0)
  421. full_rtas_msgs = 1;
  422. else if (strcmp(str, "off") == 0)
  423. full_rtas_msgs = 0;
  424. return 1;
  425. }
  426. __initcall(rtas_init);
  427. __setup("surveillance=", surveillance_setup);
  428. __setup("rtasmsgs=", rtasmsgs_setup);