rtasd.c 13 KB

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