rtasd.c 12 KB

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