rtasd.c 15 KB

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